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