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