blob: d703bd738b67c284e747517274447dc77a390caa [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 */
6require_once $GLOBALS['THRIFT_ROOT'].'/protocol/TType.php';
7
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
92 public abstract function writeString($out, $str);
93
94
Mark Sleecfc01932006-09-01 22:18:16 +000095 /**
96 * Reads the message header
97 *
98 * @param TTransport $out Output transport
99 * @param string $name Function name
100 * @param int $type message type TMessageType::CALL or TMessageType::REPLY
101 * @parem int $seqid The sequence id of this message
102 */
Marc Slemkod97eb612006-08-24 23:37:36 +0000103 public abstract function readMessageBegin($out, &$name, &$type, &$seqid);
104
Mark Sleecfc01932006-09-01 22:18:16 +0000105 /**
106 * Read the close of message
107 *
108 * @param TTransport $out Output transport
109 */
Marc Slemkod97eb612006-08-24 23:37:36 +0000110 public abstract function readMessageEnd($out);
111
Mark Slee6e536442006-06-30 18:28:50 +0000112 public abstract function readStructBegin($in, &$name);
113
114 public abstract function readStructEnd($in);
115
116 public abstract function readFieldBegin($in, &$name, &$fieldType, &$fieldId);
117
118 public abstract function readFieldEnd($in);
119
120 public abstract function readMapBegin($in, &$keyType, &$valType, &$size);
121
122 public abstract function readMapEnd($in);
123
124 public abstract function readListBegin($in, &$elemType, &$size);
125
126 public abstract function readListEnd($in);
127
128 public abstract function readSetBegin($in, &$elemType, &$size);
129
130 public abstract function readSetEnd($in);
131
Mark Slee78f58e22006-09-02 04:17:07 +0000132 public abstract function readBool($in, &$bool);
133
Mark Slee6e536442006-06-30 18:28:50 +0000134 public abstract function readByte($in, &$byte);
135
Mark Sleecfc01932006-09-01 22:18:16 +0000136 public abstract function readI16($in, &$i16);
137
Mark Slee6e536442006-06-30 18:28:50 +0000138 public abstract function readI32($in, &$i32);
139
140 public abstract function readI64($in, &$i64);
141
142 public abstract function readString($in, &$str);
143
Mark Sleecfc01932006-09-01 22:18:16 +0000144 /**
145 * The skip function is a utility to parse over unrecognized date without
146 * causing corruption.
147 *
148 * @param TTransport $in Input transport
149 * @param TType $type What type is it
150 */
Mark Slee6e536442006-06-30 18:28:50 +0000151 public function skip($in, $type) {
152 switch ($type) {
Mark Slee78f58e22006-09-02 04:17:07 +0000153 case TType::BOOL:
154 return $this->readBool($in, $bool);
Mark Slee6e536442006-06-30 18:28:50 +0000155 case TType::BYTE:
156 return $this->readByte($in, $byte);
Mark Sleecfc01932006-09-01 22:18:16 +0000157 case TType::I16;
158 return $this->readI16($in, $i16);
Mark Slee6e536442006-06-30 18:28:50 +0000159 case TType::I32:
160 return $this->readI32($in, $i32);
161 case TType::I64:
162 return $this->readI64($in, $i64);
163 case TType::STRING:
164 return $this->readString($in, $str);
165 case TType::STRUCT:
166 {
167 $result = $this->readStructBegin($in, $name);
168 while (true) {
169 $result += $this->readFieldBegin($in, $name, $ftype, $fid);
170 if ($ftype == TType::STOP) {
171 break;
172 }
173 $result += $this->skip($in, $ftype);
174 $result += $this->readFieldEnd($in);
175 }
176 $result += $this->readStructEnd($in);
177 return $result;
178 }
179 case TType::MAP:
180 {
181 $result = $this->readMapBegin($in, $keyType, $valType, $size);
182 for ($i = 0; $i < $size; $i++) {
183 $result += $this->skip($in, $keyType);
184 $result += $this->skip($in, $valType);
185 }
186 $result += $this->readMapEnd($in);
187 return $result;
188 }
189 case TType::SET:
190 {
191 $result = $this->readSetBegin($in, $elemType, $size);
192 for ($i = 0; $i < $size; $i++) {
193 $result += $this->skip($in, $elemType);
194 }
195 $result += $this->readSetEnd($in);
196 return $result;
197 }
198 case TType::LST:
199 {
200 $result = $this->readListBegin($in, $elemType, $size);
201 for ($i = 0; $i < $size; $i++) {
202 $result += $this->skip($in, $elemType);
203 }
204 $result += $this->readListEnd($in);
205 return $result;
206 }
207 default:
208 return 0;
209 }
210 }
211}
212
213?>