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