| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 1 | <?php | 
|  | 2 |  | 
| Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 3 | /** | 
|  | 4 | * For Type Constants | 
|  | 5 | */ | 
| Mark Slee | 1c4a559 | 2006-09-25 21:32:05 +0000 | [diff] [blame] | 6 | include_once $GLOBALS['THRIFT_ROOT'].'/protocol/TType.php'; | 
| Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 7 |  | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 8 | /** | 
|  | 9 | * Protocol module. | 
|  | 10 | * | 
|  | 11 | * @package thrift.protocol | 
|  | 12 | * @author Mark Slee <mcslee@facebook.com> | 
|  | 13 | */ | 
|  | 14 | abstract class TProtocol { | 
|  | 15 |  | 
| Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 16 | /** | 
|  | 17 | * Writes the message header | 
|  | 18 | * | 
|  | 19 | * @param TTransport $out Output transport | 
|  | 20 | * @param string $name Function name | 
|  | 21 | * @param int $type message type TMessageType::CALL or TMessageType::REPLY | 
|  | 22 | * @param int $seqid The sequence id of this message | 
|  | 23 | */ | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 24 | public abstract function writeMessageBegin($out, $name, $type, $seqid); | 
|  | 25 |  | 
| Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 26 | /** | 
|  | 27 | * Close the message | 
|  | 28 | * | 
|  | 29 | * @param TTransport $out Output transport | 
|  | 30 | */ | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 31 | public abstract function writeMessageEnd($out); | 
|  | 32 |  | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 33 | /** | 
|  | 34 | * Writes a struct header. | 
|  | 35 | * | 
|  | 36 | * @param TTransport $out  Output transport | 
|  | 37 | * @param string     $name Struct name | 
|  | 38 | * @throws TException on write error | 
|  | 39 | * @return int How many bytes written | 
|  | 40 | */ | 
|  | 41 | public abstract function writeStructBegin($out, $name); | 
|  | 42 |  | 
|  | 43 |  | 
|  | 44 | /** | 
|  | 45 | * Close a struct. | 
|  | 46 | * | 
|  | 47 | * @param TTransport $out Output transport | 
|  | 48 | * @throws TException on write error | 
|  | 49 | * @return int How many bytes written | 
|  | 50 | */ | 
|  | 51 | public abstract function writeStructEnd($out); | 
|  | 52 |  | 
|  | 53 | /* | 
|  | 54 | * Starts a field. | 
|  | 55 | * | 
|  | 56 | * @param TTransport $out  Output transport | 
|  | 57 | * @param string     $name Field name | 
|  | 58 | * @param int        $type Field type | 
|  | 59 | * @param int        $fid  Field id | 
|  | 60 | * @throws TException on write error | 
|  | 61 | * @return int How many bytes written | 
|  | 62 | */ | 
|  | 63 | public abstract function writeFieldBegin($out, $fieldName, $fieldType, $fieldId); | 
|  | 64 |  | 
|  | 65 | public abstract function writeFieldEnd($out); | 
|  | 66 |  | 
|  | 67 | public abstract function writeFieldStop($out); | 
|  | 68 |  | 
|  | 69 | public abstract function writeMapBegin($out, $keyType, $valType, $size); | 
|  | 70 |  | 
|  | 71 | public abstract function writeMapEnd($out); | 
|  | 72 |  | 
|  | 73 | public abstract function writeListBegin($out, $elemType, $size); | 
|  | 74 |  | 
|  | 75 | public abstract function writeListEnd($out); | 
|  | 76 |  | 
|  | 77 | public abstract function writeSetBegin($out, $elemType, $size); | 
|  | 78 |  | 
|  | 79 | public abstract function writeSetEnd($out); | 
|  | 80 |  | 
| Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 81 | public abstract function writeBool($out, $bool); | 
|  | 82 |  | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 83 | public abstract function writeByte($out, $byte); | 
|  | 84 |  | 
| Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 85 | public abstract function writeI16($out, $i16); | 
|  | 86 |  | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 87 | public abstract function writeI32($out, $i32); | 
|  | 88 |  | 
|  | 89 | public abstract function writeI64($out, $i64); | 
|  | 90 |  | 
| Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 91 | public abstract function writeDouble($out, $dub); | 
|  | 92 |  | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 93 | public abstract function writeString($out, $str); | 
|  | 94 |  | 
|  | 95 |  | 
| Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 96 | /** | 
|  | 97 | * Reads the message header | 
|  | 98 | * | 
|  | 99 | * @param TTransport $out Output transport | 
|  | 100 | * @param string $name Function name | 
|  | 101 | * @param int $type message type TMessageType::CALL or TMessageType::REPLY | 
|  | 102 | * @parem int $seqid The sequence id of this message | 
|  | 103 | */ | 
| Mark Slee | fc89d39 | 2006-09-04 00:04:39 +0000 | [diff] [blame] | 104 | public abstract function readMessageBegin($in, &$name, &$type, &$seqid); | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 105 |  | 
| Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 106 | /** | 
|  | 107 | * Read the close of message | 
|  | 108 | * | 
|  | 109 | * @param TTransport $out Output transport | 
|  | 110 | */ | 
| Mark Slee | fc89d39 | 2006-09-04 00:04:39 +0000 | [diff] [blame] | 111 | public abstract function readMessageEnd($in); | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 112 |  | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 113 | public abstract function readStructBegin($in, &$name); | 
|  | 114 |  | 
|  | 115 | public abstract function readStructEnd($in); | 
|  | 116 |  | 
|  | 117 | public abstract function readFieldBegin($in, &$name, &$fieldType, &$fieldId); | 
|  | 118 |  | 
|  | 119 | public abstract function readFieldEnd($in); | 
|  | 120 |  | 
|  | 121 | public abstract function readMapBegin($in, &$keyType, &$valType, &$size); | 
|  | 122 |  | 
|  | 123 | public abstract function readMapEnd($in); | 
|  | 124 |  | 
|  | 125 | public abstract function readListBegin($in, &$elemType, &$size); | 
|  | 126 |  | 
|  | 127 | public abstract function readListEnd($in); | 
|  | 128 |  | 
|  | 129 | public abstract function readSetBegin($in, &$elemType, &$size); | 
|  | 130 |  | 
|  | 131 | public abstract function readSetEnd($in); | 
|  | 132 |  | 
| Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 133 | public abstract function readBool($in, &$bool); | 
|  | 134 |  | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 135 | public abstract function readByte($in, &$byte); | 
|  | 136 |  | 
| Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 137 | public abstract function readI16($in, &$i16); | 
|  | 138 |  | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 139 | public abstract function readI32($in, &$i32); | 
|  | 140 |  | 
|  | 141 | public abstract function readI64($in, &$i64); | 
|  | 142 |  | 
| Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 143 | public abstract function readDouble($in, &$dub); | 
|  | 144 |  | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 145 | public abstract function readString($in, &$str); | 
|  | 146 |  | 
| Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 147 | /** | 
|  | 148 | * The skip function is a utility to parse over unrecognized date without | 
|  | 149 | * causing corruption. | 
|  | 150 | * | 
|  | 151 | * @param TTransport $in Input transport | 
|  | 152 | * @param TType $type What type is it | 
|  | 153 | */ | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 154 | public function skip($in, $type) { | 
|  | 155 | switch ($type) { | 
| Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 156 | case TType::BOOL: | 
|  | 157 | return $this->readBool($in, $bool); | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 158 | case TType::BYTE: | 
|  | 159 | return $this->readByte($in, $byte); | 
| Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 160 | case TType::I16; | 
|  | 161 | return $this->readI16($in, $i16); | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 162 | case TType::I32: | 
|  | 163 | return $this->readI32($in, $i32); | 
|  | 164 | case TType::I64: | 
|  | 165 | return $this->readI64($in, $i64); | 
| Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 166 | case TType::DOUBLE: | 
|  | 167 | return $this->readDouble($in, $dub); | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 168 | case TType::STRING: | 
|  | 169 | return $this->readString($in, $str); | 
|  | 170 | case TType::STRUCT: | 
|  | 171 | { | 
|  | 172 | $result = $this->readStructBegin($in, $name); | 
|  | 173 | while (true) { | 
|  | 174 | $result += $this->readFieldBegin($in, $name, $ftype, $fid); | 
|  | 175 | if ($ftype == TType::STOP) { | 
|  | 176 | break; | 
|  | 177 | } | 
|  | 178 | $result += $this->skip($in, $ftype); | 
|  | 179 | $result += $this->readFieldEnd($in); | 
|  | 180 | } | 
|  | 181 | $result += $this->readStructEnd($in); | 
|  | 182 | return $result; | 
|  | 183 | } | 
|  | 184 | case TType::MAP: | 
|  | 185 | { | 
|  | 186 | $result = $this->readMapBegin($in, $keyType, $valType, $size); | 
|  | 187 | for ($i = 0; $i < $size; $i++) { | 
|  | 188 | $result += $this->skip($in, $keyType); | 
|  | 189 | $result += $this->skip($in, $valType); | 
|  | 190 | } | 
|  | 191 | $result += $this->readMapEnd($in); | 
|  | 192 | return $result; | 
|  | 193 | } | 
|  | 194 | case TType::SET: | 
|  | 195 | { | 
|  | 196 | $result = $this->readSetBegin($in, $elemType, $size); | 
|  | 197 | for ($i = 0; $i < $size; $i++) { | 
|  | 198 | $result += $this->skip($in, $elemType); | 
|  | 199 | } | 
|  | 200 | $result += $this->readSetEnd($in); | 
|  | 201 | return $result; | 
|  | 202 | } | 
|  | 203 | case TType::LST: | 
|  | 204 | { | 
|  | 205 | $result = $this->readListBegin($in, $elemType, $size); | 
|  | 206 | for ($i = 0; $i < $size; $i++) { | 
|  | 207 | $result += $this->skip($in, $elemType); | 
|  | 208 | } | 
|  | 209 | $result += $this->readListEnd($in); | 
|  | 210 | return $result; | 
|  | 211 | } | 
|  | 212 | default: | 
|  | 213 | return 0; | 
|  | 214 | } | 
|  | 215 | } | 
| Mark Slee | 99e2b26 | 2006-10-10 01:42:29 +0000 | [diff] [blame] | 216 |  | 
|  | 217 | /** | 
|  | 218 | * Utility for skipping binary data | 
|  | 219 | * | 
|  | 220 | * @param TTransport $itrans TTransport object | 
|  | 221 | * @param int        $type   Field type | 
|  | 222 | */ | 
|  | 223 | public static function skipBinary($itrans, $type) { | 
|  | 224 | switch ($type) { | 
|  | 225 | case TType::BOOL: | 
|  | 226 | return $itrans->readAll(1); | 
|  | 227 | case TType::BYTE: | 
|  | 228 | return $itrans->readAll(1); | 
|  | 229 | case TType::I16; | 
|  | 230 | return $itrans->readAll(2); | 
|  | 231 | case TType::I32: | 
|  | 232 | return $itrans->readAll(4); | 
|  | 233 | case TType::I64: | 
|  | 234 | return $itrans->readAll(8); | 
|  | 235 | case TType::DOUBLE: | 
|  | 236 | return $itrans->readAll(8); | 
|  | 237 | case TType::STRING: | 
|  | 238 | $len = unpack('N', $itrans->readAll(4)); | 
|  | 239 | $len = $len[1]; | 
|  | 240 | if ($len > 0x7fffffff) { | 
|  | 241 | $len = 0 - (($len - 1) ^ 0xffffffff); | 
|  | 242 | } | 
|  | 243 | return 4 + $itrans->readAll($len); | 
|  | 244 | case TType::STRUCT: | 
|  | 245 | { | 
|  | 246 | $result = 0; | 
|  | 247 | while (true) { | 
|  | 248 | $ftype = 0; | 
|  | 249 | $fid = 0; | 
|  | 250 | $data = $in->readAll(1); | 
|  | 251 | $arr = unpack('c', $data); | 
|  | 252 | $ftype = $arr[1]; | 
|  | 253 | if ($ftype == TType::STOP) { | 
|  | 254 | break; | 
|  | 255 | } | 
|  | 256 | // I16 field id | 
|  | 257 | $result += $itrans->readAll(2); | 
|  | 258 | $result += self::skipBinary($itrans, $ftype); | 
|  | 259 | } | 
|  | 260 | return $result; | 
|  | 261 | } | 
|  | 262 | case TType::MAP: | 
|  | 263 | { | 
|  | 264 | // Ktype | 
|  | 265 | $data = $itrans->readAll(1); | 
|  | 266 | $arr = unpack('c', $data); | 
|  | 267 | $ktype = $arr[1]; | 
|  | 268 | // Vtype | 
|  | 269 | $data = $itrans->readAll(1); | 
|  | 270 | $arr = unpack('c', $data); | 
|  | 271 | $vtype = $arr[1]; | 
|  | 272 | // Size | 
|  | 273 | $data = $itrans->readAll(4); | 
|  | 274 | $arr = unpack('N', $data); | 
|  | 275 | $size = $arr[1]; | 
|  | 276 | if ($size > 0x7fffffff) { | 
|  | 277 | $size = 0 - (($size - 1) ^ 0xffffffff); | 
|  | 278 | } | 
|  | 279 | $result = 6; | 
|  | 280 | for ($i = 0; $i < $size; $i++) { | 
|  | 281 | $result += self::skipBinary($itrans, $ktype); | 
|  | 282 | $result += self::skipBinary($itrans, $vtype); | 
|  | 283 | } | 
|  | 284 | return $result; | 
|  | 285 | } | 
|  | 286 | case TType::SET: | 
|  | 287 | case TType::LST: | 
|  | 288 | { | 
|  | 289 | // Vtype | 
|  | 290 | $data = $itrans->readAll(1); | 
|  | 291 | $arr = unpack('c', $data); | 
|  | 292 | $vtype = $arr[1]; | 
|  | 293 | // Size | 
|  | 294 | $data = $itrans->readAll(4); | 
|  | 295 | $arr = unpack('N', $data); | 
|  | 296 | $size = $arr[1]; | 
|  | 297 | if ($size > 0x7fffffff) { | 
|  | 298 | $size = 0 - (($size - 1) ^ 0xffffffff); | 
|  | 299 | } | 
|  | 300 | $result = 5; | 
|  | 301 | for ($i = 0; $i < $size; $i++) { | 
|  | 302 | $result += self::skipBinary($itrans, $vtype); | 
|  | 303 | } | 
|  | 304 | return $result; | 
|  | 305 | } | 
|  | 306 | default: | 
|  | 307 | return 0; | 
|  | 308 | } | 
|  | 309 | } | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 310 | } | 
|  | 311 |  | 
|  | 312 | ?> |