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