Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame^] | 1 | <?php |
| 2 | |
| 3 | /** Types */ |
| 4 | require_once THRIFT_ROOT.'/protocol/TType.php'; |
| 5 | |
| 6 | /** |
| 7 | * Protocol module. |
| 8 | * |
| 9 | * @package thrift.protocol |
| 10 | * @author Mark Slee <mcslee@facebook.com> |
| 11 | */ |
| 12 | abstract class TProtocol { |
| 13 | |
| 14 | /** |
| 15 | * Writes a struct header. |
| 16 | * |
| 17 | * @param TTransport $out Output transport |
| 18 | * @param string $name Struct name |
| 19 | * @throws TException on write error |
| 20 | * @return int How many bytes written |
| 21 | */ |
| 22 | public abstract function writeStructBegin($out, $name); |
| 23 | |
| 24 | |
| 25 | /** |
| 26 | * Close a struct. |
| 27 | * |
| 28 | * @param TTransport $out Output transport |
| 29 | * @throws TException on write error |
| 30 | * @return int How many bytes written |
| 31 | */ |
| 32 | public abstract function writeStructEnd($out); |
| 33 | |
| 34 | /* |
| 35 | * Starts a field. |
| 36 | * |
| 37 | * @param TTransport $out Output transport |
| 38 | * @param string $name Field name |
| 39 | * @param int $type Field type |
| 40 | * @param int $fid Field id |
| 41 | * @throws TException on write error |
| 42 | * @return int How many bytes written |
| 43 | */ |
| 44 | public abstract function writeFieldBegin($out, $fieldName, $fieldType, $fieldId); |
| 45 | |
| 46 | public abstract function writeFieldEnd($out); |
| 47 | |
| 48 | public abstract function writeFieldStop($out); |
| 49 | |
| 50 | public abstract function writeMapBegin($out, $keyType, $valType, $size); |
| 51 | |
| 52 | public abstract function writeMapEnd($out); |
| 53 | |
| 54 | public abstract function writeListBegin($out, $elemType, $size); |
| 55 | |
| 56 | public abstract function writeListEnd($out); |
| 57 | |
| 58 | public abstract function writeSetBegin($out, $elemType, $size); |
| 59 | |
| 60 | public abstract function writeSetEnd($out); |
| 61 | |
| 62 | public abstract function writeByte($out, $byte); |
| 63 | |
| 64 | public abstract function writeI32($out, $i32); |
| 65 | |
| 66 | public abstract function writeI64($out, $i64); |
| 67 | |
| 68 | public abstract function writeString($out, $str); |
| 69 | |
| 70 | |
| 71 | public abstract function readStructBegin($in, &$name); |
| 72 | |
| 73 | public abstract function readStructEnd($in); |
| 74 | |
| 75 | public abstract function readFieldBegin($in, &$name, &$fieldType, &$fieldId); |
| 76 | |
| 77 | public abstract function readFieldEnd($in); |
| 78 | |
| 79 | public abstract function readMapBegin($in, &$keyType, &$valType, &$size); |
| 80 | |
| 81 | public abstract function readMapEnd($in); |
| 82 | |
| 83 | public abstract function readListBegin($in, &$elemType, &$size); |
| 84 | |
| 85 | public abstract function readListEnd($in); |
| 86 | |
| 87 | public abstract function readSetBegin($in, &$elemType, &$size); |
| 88 | |
| 89 | public abstract function readSetEnd($in); |
| 90 | |
| 91 | public abstract function readByte($in, &$byte); |
| 92 | |
| 93 | public abstract function readI32($in, &$i32); |
| 94 | |
| 95 | public abstract function readI64($in, &$i64); |
| 96 | |
| 97 | public abstract function readString($in, &$str); |
| 98 | |
| 99 | public function skip($in, $type) { |
| 100 | switch ($type) { |
| 101 | case TType::BYTE: |
| 102 | return $this->readByte($in, $byte); |
| 103 | case TType::I32: |
| 104 | return $this->readI32($in, $i32); |
| 105 | case TType::I64: |
| 106 | return $this->readI64($in, $i64); |
| 107 | case TType::STRING: |
| 108 | return $this->readString($in, $str); |
| 109 | case TType::STRUCT: |
| 110 | { |
| 111 | $result = $this->readStructBegin($in, $name); |
| 112 | while (true) { |
| 113 | $result += $this->readFieldBegin($in, $name, $ftype, $fid); |
| 114 | if ($ftype == TType::STOP) { |
| 115 | break; |
| 116 | } |
| 117 | $result += $this->skip($in, $ftype); |
| 118 | $result += $this->readFieldEnd($in); |
| 119 | } |
| 120 | $result += $this->readStructEnd($in); |
| 121 | return $result; |
| 122 | } |
| 123 | case TType::MAP: |
| 124 | { |
| 125 | $result = $this->readMapBegin($in, $keyType, $valType, $size); |
| 126 | for ($i = 0; $i < $size; $i++) { |
| 127 | $result += $this->skip($in, $keyType); |
| 128 | $result += $this->skip($in, $valType); |
| 129 | } |
| 130 | $result += $this->readMapEnd($in); |
| 131 | return $result; |
| 132 | } |
| 133 | case TType::SET: |
| 134 | { |
| 135 | $result = $this->readSetBegin($in, $elemType, $size); |
| 136 | for ($i = 0; $i < $size; $i++) { |
| 137 | $result += $this->skip($in, $elemType); |
| 138 | } |
| 139 | $result += $this->readSetEnd($in); |
| 140 | return $result; |
| 141 | } |
| 142 | case TType::LST: |
| 143 | { |
| 144 | $result = $this->readListBegin($in, $elemType, $size); |
| 145 | for ($i = 0; $i < $size; $i++) { |
| 146 | $result += $this->skip($in, $elemType); |
| 147 | } |
| 148 | $result += $this->readListEnd($in); |
| 149 | return $result; |
| 150 | } |
| 151 | default: |
| 152 | return 0; |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | ?> |