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