blob: 15938dfb365c52753906ea4aa251227fb3672c31 [file] [log] [blame]
Mark Slee6e536442006-06-30 18:28:50 +00001<?php
2
Mark Sleecfc01932006-09-01 22:18:16 +00003/**
4 * For Type Constants
5 */
6require_once $GLOBALS['THRIFT_ROOT'].'/protocol/TType.php';
7
Mark Slee6e536442006-06-30 18:28:50 +00008
9/**
10 * Protocol module.
11 *
12 * @package thrift.protocol
13 * @author Mark Slee <mcslee@facebook.com>
14 */
15abstract class TProtocol {
16
Mark Sleecfc01932006-09-01 22:18:16 +000017 /**
18 * Writes the message header
19 *
20 * @param TTransport $out Output transport
21 * @param string $name Function name
22 * @param int $type message type TMessageType::CALL or TMessageType::REPLY
23 * @param int $seqid The sequence id of this message
24 */
Marc Slemkod97eb612006-08-24 23:37:36 +000025 public abstract function writeMessageBegin($out, $name, $type, $seqid);
26
Mark Sleecfc01932006-09-01 22:18:16 +000027 /**
28 * Close the message
29 *
30 * @param TTransport $out Output transport
31 */
Marc Slemkod97eb612006-08-24 23:37:36 +000032 public abstract function writeMessageEnd($out);
33
Mark Slee6e536442006-06-30 18:28:50 +000034 /**
35 * Writes a struct header.
36 *
37 * @param TTransport $out Output transport
38 * @param string $name Struct name
39 * @throws TException on write error
40 * @return int How many bytes written
41 */
42 public abstract function writeStructBegin($out, $name);
43
44
45 /**
46 * Close a struct.
47 *
48 * @param TTransport $out Output transport
49 * @throws TException on write error
50 * @return int How many bytes written
51 */
52 public abstract function writeStructEnd($out);
53
54 /*
55 * Starts a field.
56 *
57 * @param TTransport $out Output transport
58 * @param string $name Field name
59 * @param int $type Field type
60 * @param int $fid Field id
61 * @throws TException on write error
62 * @return int How many bytes written
63 */
64 public abstract function writeFieldBegin($out, $fieldName, $fieldType, $fieldId);
65
66 public abstract function writeFieldEnd($out);
67
68 public abstract function writeFieldStop($out);
69
70 public abstract function writeMapBegin($out, $keyType, $valType, $size);
71
72 public abstract function writeMapEnd($out);
73
74 public abstract function writeListBegin($out, $elemType, $size);
75
76 public abstract function writeListEnd($out);
77
78 public abstract function writeSetBegin($out, $elemType, $size);
79
80 public abstract function writeSetEnd($out);
81
82 public abstract function writeByte($out, $byte);
83
Mark Sleecfc01932006-09-01 22:18:16 +000084 public abstract function writeI16($out, $i16);
85
Mark Slee6e536442006-06-30 18:28:50 +000086 public abstract function writeI32($out, $i32);
87
88 public abstract function writeI64($out, $i64);
89
90 public abstract function writeString($out, $str);
91
92
Mark Sleecfc01932006-09-01 22:18:16 +000093 /**
94 * Reads the message header
95 *
96 * @param TTransport $out Output transport
97 * @param string $name Function name
98 * @param int $type message type TMessageType::CALL or TMessageType::REPLY
99 * @parem int $seqid The sequence id of this message
100 */
Marc Slemkod97eb612006-08-24 23:37:36 +0000101 public abstract function readMessageBegin($out, &$name, &$type, &$seqid);
102
Mark Sleecfc01932006-09-01 22:18:16 +0000103 /**
104 * Read the close of message
105 *
106 * @param TTransport $out Output transport
107 */
Marc Slemkod97eb612006-08-24 23:37:36 +0000108 public abstract function readMessageEnd($out);
109
Mark Slee6e536442006-06-30 18:28:50 +0000110 public abstract function readStructBegin($in, &$name);
111
112 public abstract function readStructEnd($in);
113
114 public abstract function readFieldBegin($in, &$name, &$fieldType, &$fieldId);
115
116 public abstract function readFieldEnd($in);
117
118 public abstract function readMapBegin($in, &$keyType, &$valType, &$size);
119
120 public abstract function readMapEnd($in);
121
122 public abstract function readListBegin($in, &$elemType, &$size);
123
124 public abstract function readListEnd($in);
125
126 public abstract function readSetBegin($in, &$elemType, &$size);
127
128 public abstract function readSetEnd($in);
129
130 public abstract function readByte($in, &$byte);
131
Mark Sleecfc01932006-09-01 22:18:16 +0000132 public abstract function readI16($in, &$i16);
133
Mark Slee6e536442006-06-30 18:28:50 +0000134 public abstract function readI32($in, &$i32);
135
136 public abstract function readI64($in, &$i64);
137
138 public abstract function readString($in, &$str);
139
Mark Sleecfc01932006-09-01 22:18:16 +0000140 /**
141 * The skip function is a utility to parse over unrecognized date without
142 * causing corruption.
143 *
144 * @param TTransport $in Input transport
145 * @param TType $type What type is it
146 */
Mark Slee6e536442006-06-30 18:28:50 +0000147 public function skip($in, $type) {
148 switch ($type) {
149 case TType::BYTE:
150 return $this->readByte($in, $byte);
Mark Sleecfc01932006-09-01 22:18:16 +0000151 case TType::I16;
152 return $this->readI16($in, $i16);
Mark Slee6e536442006-06-30 18:28:50 +0000153 case TType::I32:
154 return $this->readI32($in, $i32);
155 case TType::I64:
156 return $this->readI64($in, $i64);
157 case TType::STRING:
158 return $this->readString($in, $str);
159 case TType::STRUCT:
160 {
161 $result = $this->readStructBegin($in, $name);
162 while (true) {
163 $result += $this->readFieldBegin($in, $name, $ftype, $fid);
164 if ($ftype == TType::STOP) {
165 break;
166 }
167 $result += $this->skip($in, $ftype);
168 $result += $this->readFieldEnd($in);
169 }
170 $result += $this->readStructEnd($in);
171 return $result;
172 }
173 case TType::MAP:
174 {
175 $result = $this->readMapBegin($in, $keyType, $valType, $size);
176 for ($i = 0; $i < $size; $i++) {
177 $result += $this->skip($in, $keyType);
178 $result += $this->skip($in, $valType);
179 }
180 $result += $this->readMapEnd($in);
181 return $result;
182 }
183 case TType::SET:
184 {
185 $result = $this->readSetBegin($in, $elemType, $size);
186 for ($i = 0; $i < $size; $i++) {
187 $result += $this->skip($in, $elemType);
188 }
189 $result += $this->readSetEnd($in);
190 return $result;
191 }
192 case TType::LST:
193 {
194 $result = $this->readListBegin($in, $elemType, $size);
195 for ($i = 0; $i < $size; $i++) {
196 $result += $this->skip($in, $elemType);
197 }
198 $result += $this->readListEnd($in);
199 return $result;
200 }
201 default:
202 return 0;
203 }
204 }
205}
206
207?>