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 | |
| 82 | public abstract function writeByte($out, $byte); |
| 83 | |
Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame^] | 84 | public abstract function writeI16($out, $i16); |
| 85 | |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 86 | public abstract function writeI32($out, $i32); |
| 87 | |
| 88 | public abstract function writeI64($out, $i64); |
| 89 | |
| 90 | public abstract function writeString($out, $str); |
| 91 | |
| 92 | |
Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame^] | 93 | /** |
| 94 | * Reads the message header |
| 95 | * |
| 96 | * @param TTransport $out Output transport |
| 97 | * @param string $name Function name |
| 98 | * @param int $type message type TMessageType::CALL or TMessageType::REPLY |
| 99 | * @parem int $seqid The sequence id of this message |
| 100 | */ |
Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 101 | public abstract function readMessageBegin($out, &$name, &$type, &$seqid); |
| 102 | |
Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame^] | 103 | /** |
| 104 | * Read the close of message |
| 105 | * |
| 106 | * @param TTransport $out Output transport |
| 107 | */ |
Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 108 | public abstract function readMessageEnd($out); |
| 109 | |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 110 | public abstract function readStructBegin($in, &$name); |
| 111 | |
| 112 | public abstract function readStructEnd($in); |
| 113 | |
| 114 | public abstract function readFieldBegin($in, &$name, &$fieldType, &$fieldId); |
| 115 | |
| 116 | public abstract function readFieldEnd($in); |
| 117 | |
| 118 | public abstract function readMapBegin($in, &$keyType, &$valType, &$size); |
| 119 | |
| 120 | public abstract function readMapEnd($in); |
| 121 | |
| 122 | public abstract function readListBegin($in, &$elemType, &$size); |
| 123 | |
| 124 | public abstract function readListEnd($in); |
| 125 | |
| 126 | public abstract function readSetBegin($in, &$elemType, &$size); |
| 127 | |
| 128 | public abstract function readSetEnd($in); |
| 129 | |
| 130 | public abstract function readByte($in, &$byte); |
| 131 | |
Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame^] | 132 | public abstract function readI16($in, &$i16); |
| 133 | |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 134 | public abstract function readI32($in, &$i32); |
| 135 | |
| 136 | public abstract function readI64($in, &$i64); |
| 137 | |
| 138 | public abstract function readString($in, &$str); |
| 139 | |
Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame^] | 140 | /** |
| 141 | * The skip function is a utility to parse over unrecognized date without |
| 142 | * causing corruption. |
| 143 | * |
| 144 | * @param TTransport $in Input transport |
| 145 | * @param TType $type What type is it |
| 146 | */ |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 147 | public function skip($in, $type) { |
| 148 | switch ($type) { |
| 149 | case TType::BYTE: |
| 150 | return $this->readByte($in, $byte); |
Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame^] | 151 | case TType::I16; |
| 152 | return $this->readI16($in, $i16); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 153 | case TType::I32: |
| 154 | return $this->readI32($in, $i32); |
| 155 | case TType::I64: |
| 156 | return $this->readI64($in, $i64); |
| 157 | case TType::STRING: |
| 158 | return $this->readString($in, $str); |
| 159 | case TType::STRUCT: |
| 160 | { |
| 161 | $result = $this->readStructBegin($in, $name); |
| 162 | while (true) { |
| 163 | $result += $this->readFieldBegin($in, $name, $ftype, $fid); |
| 164 | if ($ftype == TType::STOP) { |
| 165 | break; |
| 166 | } |
| 167 | $result += $this->skip($in, $ftype); |
| 168 | $result += $this->readFieldEnd($in); |
| 169 | } |
| 170 | $result += $this->readStructEnd($in); |
| 171 | return $result; |
| 172 | } |
| 173 | case TType::MAP: |
| 174 | { |
| 175 | $result = $this->readMapBegin($in, $keyType, $valType, $size); |
| 176 | for ($i = 0; $i < $size; $i++) { |
| 177 | $result += $this->skip($in, $keyType); |
| 178 | $result += $this->skip($in, $valType); |
| 179 | } |
| 180 | $result += $this->readMapEnd($in); |
| 181 | return $result; |
| 182 | } |
| 183 | case TType::SET: |
| 184 | { |
| 185 | $result = $this->readSetBegin($in, $elemType, $size); |
| 186 | for ($i = 0; $i < $size; $i++) { |
| 187 | $result += $this->skip($in, $elemType); |
| 188 | } |
| 189 | $result += $this->readSetEnd($in); |
| 190 | return $result; |
| 191 | } |
| 192 | case TType::LST: |
| 193 | { |
| 194 | $result = $this->readListBegin($in, $elemType, $size); |
| 195 | for ($i = 0; $i < $size; $i++) { |
| 196 | $result += $this->skip($in, $elemType); |
| 197 | } |
| 198 | $result += $this->readListEnd($in); |
| 199 | return $result; |
| 200 | } |
| 201 | default: |
| 202 | return 0; |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | ?> |