blob: 3d9c46694fdb08738d384bbab506551ed1ca9a19 [file] [log] [blame]
Mark Slee6e536442006-06-30 18:28:50 +00001<?php
2
Mark Sleecfc01932006-09-01 22:18:16 +00003/**
4 * For Type Constants
5 */
Mark Slee1c4a5592006-09-25 21:32:05 +00006include_once $GLOBALS['THRIFT_ROOT'].'/protocol/TType.php';
Mark Sleecfc01932006-09-01 22:18:16 +00007
Mark Slee6e536442006-06-30 18:28:50 +00008
9/**
10 * Protocol module.
11 *
12 * @package thrift.protocol
13 * @author Mark Slee <mcslee@facebook.com>
14 */
15abstract class TProtocol {
16
Mark Sleecfc01932006-09-01 22:18:16 +000017 /**
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 Slemkod97eb612006-08-24 23:37:36 +000025 public abstract function writeMessageBegin($out, $name, $type, $seqid);
26
Mark Sleecfc01932006-09-01 22:18:16 +000027 /**
28 * Close the message
29 *
30 * @param TTransport $out Output transport
31 */
Marc Slemkod97eb612006-08-24 23:37:36 +000032 public abstract function writeMessageEnd($out);
33
Mark Slee6e536442006-06-30 18:28:50 +000034 /**
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 Slee78f58e22006-09-02 04:17:07 +000082 public abstract function writeBool($out, $bool);
83
Mark Slee6e536442006-06-30 18:28:50 +000084 public abstract function writeByte($out, $byte);
85
Mark Sleecfc01932006-09-01 22:18:16 +000086 public abstract function writeI16($out, $i16);
87
Mark Slee6e536442006-06-30 18:28:50 +000088 public abstract function writeI32($out, $i32);
89
90 public abstract function writeI64($out, $i64);
91
Mark Sleec98d0502006-09-06 02:42:25 +000092 public abstract function writeDouble($out, $dub);
93
Mark Slee6e536442006-06-30 18:28:50 +000094 public abstract function writeString($out, $str);
95
96
Mark Sleecfc01932006-09-01 22:18:16 +000097 /**
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 Sleefc89d392006-09-04 00:04:39 +0000105 public abstract function readMessageBegin($in, &$name, &$type, &$seqid);
Marc Slemkod97eb612006-08-24 23:37:36 +0000106
Mark Sleecfc01932006-09-01 22:18:16 +0000107 /**
108 * Read the close of message
109 *
110 * @param TTransport $out Output transport
111 */
Mark Sleefc89d392006-09-04 00:04:39 +0000112 public abstract function readMessageEnd($in);
Marc Slemkod97eb612006-08-24 23:37:36 +0000113
Mark Slee6e536442006-06-30 18:28:50 +0000114 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 Slee78f58e22006-09-02 04:17:07 +0000134 public abstract function readBool($in, &$bool);
135
Mark Slee6e536442006-06-30 18:28:50 +0000136 public abstract function readByte($in, &$byte);
137
Mark Sleecfc01932006-09-01 22:18:16 +0000138 public abstract function readI16($in, &$i16);
139
Mark Slee6e536442006-06-30 18:28:50 +0000140 public abstract function readI32($in, &$i32);
141
142 public abstract function readI64($in, &$i64);
143
Mark Sleec98d0502006-09-06 02:42:25 +0000144 public abstract function readDouble($in, &$dub);
145
Mark Slee6e536442006-06-30 18:28:50 +0000146 public abstract function readString($in, &$str);
147
Mark Sleecfc01932006-09-01 22:18:16 +0000148 /**
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 Slee6e536442006-06-30 18:28:50 +0000155 public function skip($in, $type) {
156 switch ($type) {
Mark Slee78f58e22006-09-02 04:17:07 +0000157 case TType::BOOL:
158 return $this->readBool($in, $bool);
Mark Slee6e536442006-06-30 18:28:50 +0000159 case TType::BYTE:
160 return $this->readByte($in, $byte);
Mark Sleecfc01932006-09-01 22:18:16 +0000161 case TType::I16;
162 return $this->readI16($in, $i16);
Mark Slee6e536442006-06-30 18:28:50 +0000163 case TType::I32:
164 return $this->readI32($in, $i32);
165 case TType::I64:
166 return $this->readI64($in, $i64);
Mark Sleec98d0502006-09-06 02:42:25 +0000167 case TType::DOUBLE:
168 return $this->readDouble($in, $dub);
Mark Slee6e536442006-06-30 18:28:50 +0000169 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?>