| 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 | /** | 
|  | 4 | * For Type Constants | 
|  | 5 | */ | 
|  | 6 | require_once $GLOBALS['THRIFT_ROOT'].'/protocol/TType.php'; | 
|  | 7 |  | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 8 |  | 
|  | 9 | /** | 
|  | 10 | * Protocol module. | 
|  | 11 | * | 
|  | 12 | * @package thrift.protocol | 
|  | 13 | * @author Mark Slee <mcslee@facebook.com> | 
|  | 14 | */ | 
|  | 15 | abstract class TProtocol { | 
|  | 16 |  | 
| Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 17 | /** | 
|  | 18 | * Writes the message header | 
|  | 19 | * | 
|  | 20 | * @param TTransport $out Output transport | 
|  | 21 | * @param string $name Function name | 
|  | 22 | * @param int $type message type TMessageType::CALL or TMessageType::REPLY | 
|  | 23 | * @param int $seqid The sequence id of this message | 
|  | 24 | */ | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 25 | public abstract function writeMessageBegin($out, $name, $type, $seqid); | 
|  | 26 |  | 
| Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 27 | /** | 
|  | 28 | * Close the message | 
|  | 29 | * | 
|  | 30 | * @param TTransport $out Output transport | 
|  | 31 | */ | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 32 | public abstract function writeMessageEnd($out); | 
|  | 33 |  | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 34 | /** | 
|  | 35 | * Writes a struct header. | 
|  | 36 | * | 
|  | 37 | * @param TTransport $out  Output transport | 
|  | 38 | * @param string     $name Struct name | 
|  | 39 | * @throws TException on write error | 
|  | 40 | * @return int How many bytes written | 
|  | 41 | */ | 
|  | 42 | public abstract function writeStructBegin($out, $name); | 
|  | 43 |  | 
|  | 44 |  | 
|  | 45 | /** | 
|  | 46 | * Close a struct. | 
|  | 47 | * | 
|  | 48 | * @param TTransport $out Output transport | 
|  | 49 | * @throws TException on write error | 
|  | 50 | * @return int How many bytes written | 
|  | 51 | */ | 
|  | 52 | public abstract function writeStructEnd($out); | 
|  | 53 |  | 
|  | 54 | /* | 
|  | 55 | * Starts a field. | 
|  | 56 | * | 
|  | 57 | * @param TTransport $out  Output transport | 
|  | 58 | * @param string     $name Field name | 
|  | 59 | * @param int        $type Field type | 
|  | 60 | * @param int        $fid  Field id | 
|  | 61 | * @throws TException on write error | 
|  | 62 | * @return int How many bytes written | 
|  | 63 | */ | 
|  | 64 | public abstract function writeFieldBegin($out, $fieldName, $fieldType, $fieldId); | 
|  | 65 |  | 
|  | 66 | public abstract function writeFieldEnd($out); | 
|  | 67 |  | 
|  | 68 | public abstract function writeFieldStop($out); | 
|  | 69 |  | 
|  | 70 | public abstract function writeMapBegin($out, $keyType, $valType, $size); | 
|  | 71 |  | 
|  | 72 | public abstract function writeMapEnd($out); | 
|  | 73 |  | 
|  | 74 | public abstract function writeListBegin($out, $elemType, $size); | 
|  | 75 |  | 
|  | 76 | public abstract function writeListEnd($out); | 
|  | 77 |  | 
|  | 78 | public abstract function writeSetBegin($out, $elemType, $size); | 
|  | 79 |  | 
|  | 80 | public abstract function writeSetEnd($out); | 
|  | 81 |  | 
| Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 82 | public abstract function writeBool($out, $bool); | 
|  | 83 |  | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 84 | public abstract function writeByte($out, $byte); | 
|  | 85 |  | 
| Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 86 | public abstract function writeI16($out, $i16); | 
|  | 87 |  | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 88 | public abstract function writeI32($out, $i32); | 
|  | 89 |  | 
|  | 90 | public abstract function writeI64($out, $i64); | 
|  | 91 |  | 
| Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 92 | public abstract function writeDouble($out, $dub); | 
|  | 93 |  | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 94 | public abstract function writeString($out, $str); | 
|  | 95 |  | 
|  | 96 |  | 
| Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 97 | /** | 
|  | 98 | * Reads the message header | 
|  | 99 | * | 
|  | 100 | * @param TTransport $out Output transport | 
|  | 101 | * @param string $name Function name | 
|  | 102 | * @param int $type message type TMessageType::CALL or TMessageType::REPLY | 
|  | 103 | * @parem int $seqid The sequence id of this message | 
|  | 104 | */ | 
| Mark Slee | fc89d39 | 2006-09-04 00:04:39 +0000 | [diff] [blame] | 105 | public abstract function readMessageBegin($in, &$name, &$type, &$seqid); | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 106 |  | 
| Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 107 | /** | 
|  | 108 | * Read the close of message | 
|  | 109 | * | 
|  | 110 | * @param TTransport $out Output transport | 
|  | 111 | */ | 
| Mark Slee | fc89d39 | 2006-09-04 00:04:39 +0000 | [diff] [blame] | 112 | public abstract function readMessageEnd($in); | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 113 |  | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 114 | public abstract function readStructBegin($in, &$name); | 
|  | 115 |  | 
|  | 116 | public abstract function readStructEnd($in); | 
|  | 117 |  | 
|  | 118 | public abstract function readFieldBegin($in, &$name, &$fieldType, &$fieldId); | 
|  | 119 |  | 
|  | 120 | public abstract function readFieldEnd($in); | 
|  | 121 |  | 
|  | 122 | public abstract function readMapBegin($in, &$keyType, &$valType, &$size); | 
|  | 123 |  | 
|  | 124 | public abstract function readMapEnd($in); | 
|  | 125 |  | 
|  | 126 | public abstract function readListBegin($in, &$elemType, &$size); | 
|  | 127 |  | 
|  | 128 | public abstract function readListEnd($in); | 
|  | 129 |  | 
|  | 130 | public abstract function readSetBegin($in, &$elemType, &$size); | 
|  | 131 |  | 
|  | 132 | public abstract function readSetEnd($in); | 
|  | 133 |  | 
| Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 134 | public abstract function readBool($in, &$bool); | 
|  | 135 |  | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 136 | public abstract function readByte($in, &$byte); | 
|  | 137 |  | 
| Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 138 | public abstract function readI16($in, &$i16); | 
|  | 139 |  | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 140 | public abstract function readI32($in, &$i32); | 
|  | 141 |  | 
|  | 142 | public abstract function readI64($in, &$i64); | 
|  | 143 |  | 
| Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 144 | public abstract function readDouble($in, &$dub); | 
|  | 145 |  | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 146 | public abstract function readString($in, &$str); | 
|  | 147 |  | 
| Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 148 | /** | 
|  | 149 | * The skip function is a utility to parse over unrecognized date without | 
|  | 150 | * causing corruption. | 
|  | 151 | * | 
|  | 152 | * @param TTransport $in Input transport | 
|  | 153 | * @param TType $type What type is it | 
|  | 154 | */ | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 155 | public function skip($in, $type) { | 
|  | 156 | switch ($type) { | 
| Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 157 | case TType::BOOL: | 
|  | 158 | return $this->readBool($in, $bool); | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 159 | case TType::BYTE: | 
|  | 160 | return $this->readByte($in, $byte); | 
| Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 161 | case TType::I16; | 
|  | 162 | return $this->readI16($in, $i16); | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 163 | case TType::I32: | 
|  | 164 | return $this->readI32($in, $i32); | 
|  | 165 | case TType::I64: | 
|  | 166 | return $this->readI64($in, $i64); | 
| Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 167 | case TType::DOUBLE: | 
|  | 168 | return $this->readDouble($in, $dub); | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 169 | case TType::STRING: | 
|  | 170 | return $this->readString($in, $str); | 
|  | 171 | case TType::STRUCT: | 
|  | 172 | { | 
|  | 173 | $result = $this->readStructBegin($in, $name); | 
|  | 174 | while (true) { | 
|  | 175 | $result += $this->readFieldBegin($in, $name, $ftype, $fid); | 
|  | 176 | if ($ftype == TType::STOP) { | 
|  | 177 | break; | 
|  | 178 | } | 
|  | 179 | $result += $this->skip($in, $ftype); | 
|  | 180 | $result += $this->readFieldEnd($in); | 
|  | 181 | } | 
|  | 182 | $result += $this->readStructEnd($in); | 
|  | 183 | return $result; | 
|  | 184 | } | 
|  | 185 | case TType::MAP: | 
|  | 186 | { | 
|  | 187 | $result = $this->readMapBegin($in, $keyType, $valType, $size); | 
|  | 188 | for ($i = 0; $i < $size; $i++) { | 
|  | 189 | $result += $this->skip($in, $keyType); | 
|  | 190 | $result += $this->skip($in, $valType); | 
|  | 191 | } | 
|  | 192 | $result += $this->readMapEnd($in); | 
|  | 193 | return $result; | 
|  | 194 | } | 
|  | 195 | case TType::SET: | 
|  | 196 | { | 
|  | 197 | $result = $this->readSetBegin($in, $elemType, $size); | 
|  | 198 | for ($i = 0; $i < $size; $i++) { | 
|  | 199 | $result += $this->skip($in, $elemType); | 
|  | 200 | } | 
|  | 201 | $result += $this->readSetEnd($in); | 
|  | 202 | return $result; | 
|  | 203 | } | 
|  | 204 | case TType::LST: | 
|  | 205 | { | 
|  | 206 | $result = $this->readListBegin($in, $elemType, $size); | 
|  | 207 | for ($i = 0; $i < $size; $i++) { | 
|  | 208 | $result += $this->skip($in, $elemType); | 
|  | 209 | } | 
|  | 210 | $result += $this->readListEnd($in); | 
|  | 211 | return $result; | 
|  | 212 | } | 
|  | 213 | default: | 
|  | 214 | return 0; | 
|  | 215 | } | 
|  | 216 | } | 
|  | 217 | } | 
|  | 218 |  | 
|  | 219 | ?> |