3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
20 * @package thrift.protocol
23 namespace Thrift\Protocol
;
25 use Thrift\Exception\TException
;
26 use Thrift\Transport\TTransport
;
27 use Thrift\Type\TType
;
28 use Thrift\Exception\TProtocolException
;
31 * Protocol base class module.
33 abstract class TProtocol
36 * Underlying transport
43 * @param TTransport $trans
45 protected function __construct($trans)
47 $this->trans_
= $trans;
51 * Accessor for transport
55 public function getTransport()
61 * Writes the message header
63 * @param string $name Function name
64 * @param int $type message type TMessageType::CALL or TMessageType::REPLY
65 * @param int $seqid The sequence id of this message
67 abstract public function writeMessageBegin($name, $type, $seqid);
72 abstract public function writeMessageEnd();
75 * Writes a struct header.
77 * @param string $name Struct name
78 * @throws TException on write error
79 * @return int How many bytes written
81 abstract public function writeStructBegin($name);
86 * @throws TException on write error
87 * @return int How many bytes written
89 abstract public function writeStructEnd();
94 * @param string $name Field name
95 * @param int $type Field type
96 * @param int $fid Field id
97 * @throws TException on write error
98 * @return int How many bytes written
100 abstract public function writeFieldBegin($fieldName, $fieldType, $fieldId);
102 abstract public function writeFieldEnd();
104 abstract public function writeFieldStop();
106 abstract public function writeMapBegin($keyType, $valType, $size);
108 abstract public function writeMapEnd();
110 abstract public function writeListBegin($elemType, $size);
112 abstract public function writeListEnd();
114 abstract public function writeSetBegin($elemType, $size);
116 abstract public function writeSetEnd();
118 abstract public function writeBool($bool);
120 abstract public function writeByte($byte);
122 abstract public function writeI16($i16);
124 abstract public function writeI32($i32);
126 abstract public function writeI64($i64);
128 abstract public function writeDouble($dub);
130 abstract public function writeString($str);
133 * Reads the message header
135 * @param string $name Function name
136 * @param int $type message type TMessageType::CALL or TMessageType::REPLY
137 * @parem int $seqid The sequence id of this message
139 abstract public function readMessageBegin(&$name, &$type, &$seqid);
142 * Read the close of message
144 abstract public function readMessageEnd();
146 abstract public function readStructBegin(&$name);
148 abstract public function readStructEnd();
150 abstract public function readFieldBegin(&$name, &$fieldType, &$fieldId);
152 abstract public function readFieldEnd();
154 abstract public function readMapBegin(&$keyType, &$valType, &$size);
156 abstract public function readMapEnd();
158 abstract public function readListBegin(&$elemType, &$size);
160 abstract public function readListEnd();
162 abstract public function readSetBegin(&$elemType, &$size);
164 abstract public function readSetEnd();
166 abstract public function readBool(&$bool);
168 abstract public function readByte(&$byte);
170 abstract public function readI16(&$i16);
172 abstract public function readI32(&$i32);
174 abstract public function readI64(&$i64);
176 abstract public function readDouble(&$dub);
178 abstract public function readString(&$str);
181 * The skip function is a utility to parse over unrecognized date without
182 * causing corruption.
184 * @param TType $type What type is it
186 public function skip($type)
190 return $this->readBool($bool);
192 return $this->readByte($byte);
194 return $this->readI16($i16);
196 return $this->readI32($i32);
198 return $this->readI64($i64);
200 return $this->readDouble($dub);
202 return $this->readString($str);
204 $result = $this->readStructBegin($name);
206 $result +
= $this->readFieldBegin($name, $ftype, $fid);
207 if ($ftype == TType
::STOP
) {
210 $result +
= $this->skip($ftype);
211 $result +
= $this->readFieldEnd();
213 $result +
= $this->readStructEnd();
218 $result = $this->readMapBegin($keyType, $valType, $size);
219 for ($i = 0; $i < $size; $i++
) {
220 $result +
= $this->skip($keyType);
221 $result +
= $this->skip($valType);
223 $result +
= $this->readMapEnd();
228 $result = $this->readSetBegin($elemType, $size);
229 for ($i = 0; $i < $size; $i++
) {
230 $result +
= $this->skip($elemType);
232 $result +
= $this->readSetEnd();
237 $result = $this->readListBegin($elemType, $size);
238 for ($i = 0; $i < $size; $i++
) {
239 $result +
= $this->skip($elemType);
241 $result +
= $this->readListEnd();
246 throw new TProtocolException(
247 'Unknown field type: ' . $type,
248 TProtocolException
::INVALID_DATA
254 * Utility for skipping binary data
256 * @param TTransport $itrans TTransport object
257 * @param int $type Field type
259 public static function skipBinary($itrans, $type)
263 return $itrans->readAll(1);
265 return $itrans->readAll(1);
267 return $itrans->readAll(2);
269 return $itrans->readAll(4);
271 return $itrans->readAll(8);
273 return $itrans->readAll(8);
275 $len = unpack('N', $itrans->readAll(4));
277 if ($len > 0x7fffffff) {
278 $len = 0 - (($len - 1) ^
0xffffffff);
281 return 4 +
$itrans->readAll($len);
288 $data = $itrans->readAll(1);
289 $arr = unpack('c', $data);
291 if ($ftype == TType
::STOP
) {
295 $result +
= $itrans->readAll(2);
296 $result +
= self
::skipBinary($itrans, $ftype);
303 $data = $itrans->readAll(1);
304 $arr = unpack('c', $data);
307 $data = $itrans->readAll(1);
308 $arr = unpack('c', $data);
311 $data = $itrans->readAll(4);
312 $arr = unpack('N', $data);
314 if ($size > 0x7fffffff) {
315 $size = 0 - (($size - 1) ^
0xffffffff);
318 for ($i = 0; $i < $size; $i++
) {
319 $result +
= self
::skipBinary($itrans, $ktype);
320 $result +
= self
::skipBinary($itrans, $vtype);
328 $data = $itrans->readAll(1);
329 $arr = unpack('c', $data);
332 $data = $itrans->readAll(4);
333 $arr = unpack('N', $data);
335 if ($size > 0x7fffffff) {
336 $size = 0 - (($size - 1) ^
0xffffffff);
339 for ($i = 0; $i < $size; $i++
) {
340 $result +
= self
::skipBinary($itrans, $vtype);
346 throw new TProtocolException(
347 'Unknown field type: ' . $type,
348 TProtocolException
::INVALID_DATA