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