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