Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 1 | <?php |
David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 2 | /* |
| 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 |
Mark Slee | 4902c05 | 2007-03-01 00:31:30 +0000 | [diff] [blame] | 10 | * |
David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 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 |
| 18 | * under the License. |
Mark Slee | 4902c05 | 2007-03-01 00:31:30 +0000 | [diff] [blame] | 19 | * |
| 20 | * @package thrift.protocol |
Mark Slee | 4902c05 | 2007-03-01 00:31:30 +0000 | [diff] [blame] | 21 | */ |
| 22 | |
David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 23 | |
Mark Slee | 4902c05 | 2007-03-01 00:31:30 +0000 | [diff] [blame] | 24 | /** |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 25 | * Protocol module. Contains all the types and definitions needed to implement |
| 26 | * a protocol encoder/decoder. |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 27 | * |
| 28 | * @package thrift.protocol |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 29 | */ |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 30 | |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 31 | /** |
Mark Slee | dac7856 | 2007-02-21 07:35:03 +0000 | [diff] [blame] | 32 | * Protocol exceptions |
| 33 | */ |
| 34 | class TProtocolException extends TException { |
| 35 | const UNKNOWN = 0; |
| 36 | const INVALID_DATA = 1; |
| 37 | const NEGATIVE_SIZE = 2; |
| 38 | const SIZE_LIMIT = 3; |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 39 | const BAD_VERSION = 4; |
Mark Slee | dac7856 | 2007-02-21 07:35:03 +0000 | [diff] [blame] | 40 | |
| 41 | function __construct($message=null, $code=0) { |
| 42 | parent::__construct($message, $code); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | /** |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 47 | * Protocol base class module. |
| 48 | */ |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 49 | abstract class TProtocol { |
Mark Slee | 9726486 | 2007-12-20 03:23:27 +0000 | [diff] [blame] | 50 | // The below may seem silly, but it is to get around the problem that the |
| 51 | // "instanceof" operator can only take in a T_VARIABLE and not a T_STRING |
| 52 | // or T_CONSTANT_ENCAPSED_STRING. Using "is_a()" instead of "instanceof" is |
| 53 | // a workaround but is deprecated in PHP5. This is used in the generated |
| 54 | // deserialization code. |
| 55 | static $TBINARYPROTOCOLACCELERATED = 'TBinaryProtocolAccelerated'; |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 56 | |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 57 | /** |
Aditya Agarwal | 6a5bcaa | 2007-02-06 02:50:56 +0000 | [diff] [blame] | 58 | * Underlying transport |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 59 | * |
| 60 | * @var TTransport |
| 61 | */ |
Aditya Agarwal | 6a5bcaa | 2007-02-06 02:50:56 +0000 | [diff] [blame] | 62 | protected $trans_; |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 63 | |
| 64 | /** |
| 65 | * Constructor |
| 66 | */ |
Aditya Agarwal | 6a5bcaa | 2007-02-06 02:50:56 +0000 | [diff] [blame] | 67 | protected function __construct($trans) { |
| 68 | $this->trans_ = $trans; |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /** |
Aditya Agarwal | 6a5bcaa | 2007-02-06 02:50:56 +0000 | [diff] [blame] | 72 | * Accessor for transport |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 73 | * |
| 74 | * @return TTransport |
| 75 | */ |
Aditya Agarwal | 6a5bcaa | 2007-02-06 02:50:56 +0000 | [diff] [blame] | 76 | public function getTransport() { |
| 77 | return $this->trans_; |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Mark Slee | 9726486 | 2007-12-20 03:23:27 +0000 | [diff] [blame] | 80 | /** |
Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 81 | * Writes the message header |
| 82 | * |
Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 83 | * @param string $name Function name |
| 84 | * @param int $type message type TMessageType::CALL or TMessageType::REPLY |
| 85 | * @param int $seqid The sequence id of this message |
| 86 | */ |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 87 | public abstract function writeMessageBegin($name, $type, $seqid); |
Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 88 | |
Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 89 | /** |
| 90 | * Close the message |
Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 91 | */ |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 92 | public abstract function writeMessageEnd(); |
Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 93 | |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 94 | /** |
| 95 | * Writes a struct header. |
| 96 | * |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 97 | * @param string $name Struct name |
| 98 | * @throws TException on write error |
| 99 | * @return int How many bytes written |
| 100 | */ |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 101 | public abstract function writeStructBegin($name); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 102 | |
| 103 | /** |
| 104 | * Close a struct. |
| 105 | * |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 106 | * @throws TException on write error |
| 107 | * @return int How many bytes written |
| 108 | */ |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 109 | public abstract function writeStructEnd(); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 110 | |
| 111 | /* |
| 112 | * Starts a field. |
| 113 | * |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 114 | * @param string $name Field name |
| 115 | * @param int $type Field type |
| 116 | * @param int $fid Field id |
| 117 | * @throws TException on write error |
| 118 | * @return int How many bytes written |
| 119 | */ |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 120 | public abstract function writeFieldBegin($fieldName, $fieldType, $fieldId); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 121 | |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 122 | public abstract function writeFieldEnd(); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 123 | |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 124 | public abstract function writeFieldStop(); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 125 | |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 126 | public abstract function writeMapBegin($keyType, $valType, $size); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 127 | |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 128 | public abstract function writeMapEnd(); |
Mark Slee | 9726486 | 2007-12-20 03:23:27 +0000 | [diff] [blame] | 129 | |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 130 | public abstract function writeListBegin($elemType, $size); |
Mark Slee | 9726486 | 2007-12-20 03:23:27 +0000 | [diff] [blame] | 131 | |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 132 | public abstract function writeListEnd(); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 133 | |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 134 | public abstract function writeSetBegin($elemType, $size); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 135 | |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 136 | public abstract function writeSetEnd(); |
Mark Slee | 9726486 | 2007-12-20 03:23:27 +0000 | [diff] [blame] | 137 | |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 138 | public abstract function writeBool($bool); |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 139 | |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 140 | public abstract function writeByte($byte); |
Mark Slee | 9726486 | 2007-12-20 03:23:27 +0000 | [diff] [blame] | 141 | |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 142 | public abstract function writeI16($i16); |
Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 143 | |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 144 | public abstract function writeI32($i32); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 145 | |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 146 | public abstract function writeI64($i64); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 147 | |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 148 | public abstract function writeDouble($dub); |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 149 | |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 150 | public abstract function writeString($str); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 151 | |
Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 152 | /** |
| 153 | * Reads the message header |
| 154 | * |
Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 155 | * @param string $name Function name |
| 156 | * @param int $type message type TMessageType::CALL or TMessageType::REPLY |
| 157 | * @parem int $seqid The sequence id of this message |
| 158 | */ |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 159 | public abstract function readMessageBegin(&$name, &$type, &$seqid); |
Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 160 | |
Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 161 | /** |
| 162 | * Read the close of message |
Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 163 | */ |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 164 | public abstract function readMessageEnd(); |
Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 165 | |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 166 | public abstract function readStructBegin(&$name); |
Mark Slee | 9726486 | 2007-12-20 03:23:27 +0000 | [diff] [blame] | 167 | |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 168 | public abstract function readStructEnd(); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 169 | |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 170 | public abstract function readFieldBegin(&$name, &$fieldType, &$fieldId); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 171 | |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 172 | public abstract function readFieldEnd(); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 173 | |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 174 | public abstract function readMapBegin(&$keyType, &$valType, &$size); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 175 | |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 176 | public abstract function readMapEnd(); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 177 | |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 178 | public abstract function readListBegin(&$elemType, &$size); |
Mark Slee | 9726486 | 2007-12-20 03:23:27 +0000 | [diff] [blame] | 179 | |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 180 | public abstract function readListEnd(); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 181 | |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 182 | public abstract function readSetBegin(&$elemType, &$size); |
Mark Slee | 9726486 | 2007-12-20 03:23:27 +0000 | [diff] [blame] | 183 | |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 184 | public abstract function readSetEnd(); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 185 | |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 186 | public abstract function readBool(&$bool); |
Mark Slee | 9726486 | 2007-12-20 03:23:27 +0000 | [diff] [blame] | 187 | |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 188 | public abstract function readByte(&$byte); |
Mark Slee | 9726486 | 2007-12-20 03:23:27 +0000 | [diff] [blame] | 189 | |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 190 | public abstract function readI16(&$i16); |
Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 191 | |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 192 | public abstract function readI32(&$i32); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 193 | |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 194 | public abstract function readI64(&$i64); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 195 | |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 196 | public abstract function readDouble(&$dub); |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 197 | |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 198 | public abstract function readString(&$str); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 199 | |
Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 200 | /** |
| 201 | * The skip function is a utility to parse over unrecognized date without |
| 202 | * causing corruption. |
| 203 | * |
Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 204 | * @param TType $type What type is it |
| 205 | */ |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 206 | public function skip($type) { |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 207 | switch ($type) { |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 208 | case TType::BOOL: |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 209 | return $this->readBool($bool); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 210 | case TType::BYTE: |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 211 | return $this->readByte($byte); |
Adam Nichols | 2f816f2 | 2007-01-11 21:25:29 +0000 | [diff] [blame] | 212 | case TType::I16: |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 213 | return $this->readI16($i16); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 214 | case TType::I32: |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 215 | return $this->readI32($i32); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 216 | case TType::I64: |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 217 | return $this->readI64($i64); |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 218 | case TType::DOUBLE: |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 219 | return $this->readDouble($dub); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 220 | case TType::STRING: |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 221 | return $this->readString($str); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 222 | case TType::STRUCT: |
| 223 | { |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 224 | $result = $this->readStructBegin($name); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 225 | while (true) { |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 226 | $result += $this->readFieldBegin($name, $ftype, $fid); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 227 | if ($ftype == TType::STOP) { |
| 228 | break; |
| 229 | } |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 230 | $result += $this->skip($ftype); |
| 231 | $result += $this->readFieldEnd(); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 232 | } |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 233 | $result += $this->readStructEnd(); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 234 | return $result; |
| 235 | } |
| 236 | case TType::MAP: |
| 237 | { |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 238 | $result = $this->readMapBegin($keyType, $valType, $size); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 239 | for ($i = 0; $i < $size; $i++) { |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 240 | $result += $this->skip($keyType); |
| 241 | $result += $this->skip($valType); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 242 | } |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 243 | $result += $this->readMapEnd(); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 244 | return $result; |
| 245 | } |
| 246 | case TType::SET: |
| 247 | { |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 248 | $result = $this->readSetBegin($elemType, $size); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 249 | for ($i = 0; $i < $size; $i++) { |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 250 | $result += $this->skip($elemType); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 251 | } |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 252 | $result += $this->readSetEnd(); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 253 | return $result; |
| 254 | } |
| 255 | case TType::LST: |
| 256 | { |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 257 | $result = $this->readListBegin($elemType, $size); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 258 | for ($i = 0; $i < $size; $i++) { |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 259 | $result += $this->skip($elemType); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 260 | } |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 261 | $result += $this->readListEnd(); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 262 | return $result; |
| 263 | } |
| 264 | default: |
| 265 | return 0; |
| 266 | } |
| 267 | } |
Mark Slee | 99e2b26 | 2006-10-10 01:42:29 +0000 | [diff] [blame] | 268 | |
| 269 | /** |
| 270 | * Utility for skipping binary data |
| 271 | * |
| 272 | * @param TTransport $itrans TTransport object |
| 273 | * @param int $type Field type |
| 274 | */ |
| 275 | public static function skipBinary($itrans, $type) { |
| 276 | switch ($type) { |
| 277 | case TType::BOOL: |
| 278 | return $itrans->readAll(1); |
| 279 | case TType::BYTE: |
| 280 | return $itrans->readAll(1); |
Adam Nichols | 2f816f2 | 2007-01-11 21:25:29 +0000 | [diff] [blame] | 281 | case TType::I16: |
Mark Slee | 99e2b26 | 2006-10-10 01:42:29 +0000 | [diff] [blame] | 282 | return $itrans->readAll(2); |
| 283 | case TType::I32: |
| 284 | return $itrans->readAll(4); |
| 285 | case TType::I64: |
| 286 | return $itrans->readAll(8); |
| 287 | case TType::DOUBLE: |
| 288 | return $itrans->readAll(8); |
| 289 | case TType::STRING: |
| 290 | $len = unpack('N', $itrans->readAll(4)); |
| 291 | $len = $len[1]; |
| 292 | if ($len > 0x7fffffff) { |
| 293 | $len = 0 - (($len - 1) ^ 0xffffffff); |
| 294 | } |
| 295 | return 4 + $itrans->readAll($len); |
| 296 | case TType::STRUCT: |
| 297 | { |
| 298 | $result = 0; |
| 299 | while (true) { |
| 300 | $ftype = 0; |
| 301 | $fid = 0; |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 302 | $data = $itrans->readAll(1); |
Mark Slee | 99e2b26 | 2006-10-10 01:42:29 +0000 | [diff] [blame] | 303 | $arr = unpack('c', $data); |
| 304 | $ftype = $arr[1]; |
| 305 | if ($ftype == TType::STOP) { |
| 306 | break; |
| 307 | } |
| 308 | // I16 field id |
| 309 | $result += $itrans->readAll(2); |
| 310 | $result += self::skipBinary($itrans, $ftype); |
| 311 | } |
| 312 | return $result; |
| 313 | } |
| 314 | case TType::MAP: |
| 315 | { |
| 316 | // Ktype |
| 317 | $data = $itrans->readAll(1); |
| 318 | $arr = unpack('c', $data); |
| 319 | $ktype = $arr[1]; |
| 320 | // Vtype |
| 321 | $data = $itrans->readAll(1); |
| 322 | $arr = unpack('c', $data); |
| 323 | $vtype = $arr[1]; |
| 324 | // Size |
| 325 | $data = $itrans->readAll(4); |
| 326 | $arr = unpack('N', $data); |
| 327 | $size = $arr[1]; |
| 328 | if ($size > 0x7fffffff) { |
| 329 | $size = 0 - (($size - 1) ^ 0xffffffff); |
| 330 | } |
| 331 | $result = 6; |
| 332 | for ($i = 0; $i < $size; $i++) { |
| 333 | $result += self::skipBinary($itrans, $ktype); |
| 334 | $result += self::skipBinary($itrans, $vtype); |
| 335 | } |
| 336 | return $result; |
| 337 | } |
| 338 | case TType::SET: |
| 339 | case TType::LST: |
| 340 | { |
| 341 | // Vtype |
| 342 | $data = $itrans->readAll(1); |
| 343 | $arr = unpack('c', $data); |
| 344 | $vtype = $arr[1]; |
| 345 | // Size |
| 346 | $data = $itrans->readAll(4); |
| 347 | $arr = unpack('N', $data); |
| 348 | $size = $arr[1]; |
| 349 | if ($size > 0x7fffffff) { |
| 350 | $size = 0 - (($size - 1) ^ 0xffffffff); |
| 351 | } |
| 352 | $result = 5; |
| 353 | for ($i = 0; $i < $size; $i++) { |
| 354 | $result += self::skipBinary($itrans, $vtype); |
| 355 | } |
| 356 | return $result; |
| 357 | } |
| 358 | default: |
| 359 | return 0; |
Mark Slee | 9726486 | 2007-12-20 03:23:27 +0000 | [diff] [blame] | 360 | } |
Mark Slee | 99e2b26 | 2006-10-10 01:42:29 +0000 | [diff] [blame] | 361 | } |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 364 | /** |
| 365 | * Protocol factory creates protocol objects from transports |
| 366 | */ |
| 367 | interface TProtocolFactory { |
| 368 | /** |
Aditya Agarwal | 6a5bcaa | 2007-02-06 02:50:56 +0000 | [diff] [blame] | 369 | * Build a protocol from the base transport |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 370 | * |
Bryan Duxbury | 17115d7 | 2010-08-12 16:59:19 +0000 | [diff] [blame] | 371 | * @return TProtocol protocol |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 372 | */ |
Aditya Agarwal | 6a5bcaa | 2007-02-06 02:50:56 +0000 | [diff] [blame] | 373 | public function getProtocol($trans); |
Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 374 | } |