blob: 2b3666fadd3c9824799851caa3285d2fa59ae94d [file] [log] [blame]
Mark Sleee8540632006-05-30 09:24:40 +00001#ifndef T_PROTOCOL_H
2#define T_PROTOCOL_H
3
Marc Slemko16698852006-08-04 03:16:10 +00004#include <transport/TTransport.h>
5
6#include <boost/shared_ptr.hpp>
7
Mark Slee8d7e1f62006-06-07 06:48:56 +00008#include <netinet/in.h>
Mark Sleee8540632006-05-30 09:24:40 +00009#include <sys/types.h>
10#include <string>
11#include <map>
12
Marc Slemko6f038a72006-08-03 18:58:09 +000013namespace facebook { namespace thrift { namespace protocol {
14
Marc Slemko16698852006-08-04 03:16:10 +000015using namespace boost;
16
Marc Slemko6f038a72006-08-03 18:58:09 +000017using namespace facebook::thrift::transport;
18
Mark Slee8d7e1f62006-06-07 06:48:56 +000019#define ntohll(x) (((uint64_t)(ntohl((int)((x << 32) >> 32))) << 32) | (uint32_t)ntohl(((int)(x >> 32))))
20
21#define htonll(x) ntohll(x)
22
Mark Sleee8540632006-05-30 09:24:40 +000023/** Forward declaration for TProtocol */
24struct TBuf;
25
26/**
Mark Slee8d7e1f62006-06-07 06:48:56 +000027 * Enumerated definition of the types that the Thrift protocol supports.
28 * Take special note of the T_END type which is used specifically to mark
29 * the end of a sequence of fields.
30 */
31enum TType {
Marc Slemkod42a2c22006-08-10 03:30:18 +000032 T_STOP = 0,
Marc Slemko5b126d62006-08-11 23:03:42 +000033 T_VOID = 1,
34 T_BOOL = 2,
35 T_BYTE = 3,
Mark Sleecfc01932006-09-01 22:18:16 +000036 T_I08 = 3,
Marc Slemko5b126d62006-08-11 23:03:42 +000037 T_I16 = 6,
Marc Slemko5b126d62006-08-11 23:03:42 +000038 T_I32 = 8,
39 T_U64 = 9,
40 T_I64 = 10,
41 T_STRING = 11,
Marc Slemkod97eb612006-08-24 23:37:36 +000042 T_UTF7 = 11,
43 T_STRUCT = 12,
44 T_MAP = 13,
45 T_SET = 14,
46 T_LIST = 15,
47 T_UTF8 = 16,
48 T_UTF16 = 17
Mark Slee8d7e1f62006-06-07 06:48:56 +000049};
50
51/**
Marc Slemko16698852006-08-04 03:16:10 +000052 * Enumerated definition of the message types that the Thrift protocol supports.
53 */
54enum TMessageType {
55 T_CALL = 1,
56 T_REPLY = 2
57};
58
59/**
Mark Sleee8540632006-05-30 09:24:40 +000060 * Abstract class for a thrift protocol driver. These are all the methods that
61 * a protocol must implement. Essentially, there must be some way of reading
62 * and writing all the base types, plus a mechanism for writing out structs
63 * with indexed fields. Also notice that all methods are strictly const. This
64 * is by design. Protcol impelementations may NOT keep state, because the
65 * same TProtocol object may be used simultaneously by multiple threads. This
66 * theoretically introduces some limititations into the possible protocol
67 * formats, but with the benefit of performance, clarity, and simplicity.
68 *
69 * @author Mark Slee <mcslee@facebook.com>
70 */
71class TProtocol {
72 public:
73 virtual ~TProtocol() {}
74
75 /**
Mark Slee8d7e1f62006-06-07 06:48:56 +000076 * Writing functions.
Mark Sleee8540632006-05-30 09:24:40 +000077 */
78
Marc Slemko0b4ffa92006-08-11 02:49:29 +000079 virtual uint32_t writeMessageBegin(shared_ptr<TTransport> out,
Marc Slemko91f67482006-08-11 23:58:57 +000080 const std::string name,
Marc Slemko0b4ffa92006-08-11 02:49:29 +000081 const TMessageType messageType,
Mark Sleecfc01932006-09-01 22:18:16 +000082 const int32_t seqid) const = 0;
Marc Slemko16698852006-08-04 03:16:10 +000083
Marc Slemko0b4ffa92006-08-11 02:49:29 +000084 virtual uint32_t writeMessageEnd(shared_ptr<TTransport> out) const = 0;
Marc Slemko16698852006-08-04 03:16:10 +000085
86
Marc Slemko0b4ffa92006-08-11 02:49:29 +000087 virtual uint32_t writeStructBegin(shared_ptr<TTransport> out,
88 const std::string& name) const = 0;
89
90 virtual uint32_t writeStructEnd(shared_ptr<TTransport> out) const = 0;
91
92 virtual uint32_t writeFieldBegin(shared_ptr<TTransport> out,
93 const std::string& name,
94 const TType fieldType,
95 const int16_t fieldId) const = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +000096
Marc Slemko0b4ffa92006-08-11 02:49:29 +000097 virtual uint32_t writeFieldEnd(shared_ptr<TTransport> out) const = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +000098
Marc Slemko0b4ffa92006-08-11 02:49:29 +000099 virtual uint32_t writeFieldStop(shared_ptr<TTransport> out) const = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000100
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000101 virtual uint32_t writeMapBegin(shared_ptr<TTransport> out,
102 const TType keyType,
103 const TType valType,
Marc Slemkob09f5882006-08-23 22:03:34 +0000104 const uint32_t size) const = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000105
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000106 virtual uint32_t writeMapEnd(shared_ptr<TTransport> out) const = 0;
107
108 virtual uint32_t writeListBegin(shared_ptr<TTransport> out,
109 const TType elemType,
Marc Slemkob09f5882006-08-23 22:03:34 +0000110 const uint32_t size) const = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000111
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000112 virtual uint32_t writeListEnd(shared_ptr<TTransport> out) const = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000113
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000114 virtual uint32_t writeSetBegin(shared_ptr<TTransport> out,
115 const TType elemType,
Marc Slemkob09f5882006-08-23 22:03:34 +0000116 const uint32_t size) const = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000117
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000118 virtual uint32_t writeSetEnd(shared_ptr<TTransport> out) const = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000119
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000120 virtual uint32_t writeBool(shared_ptr<TTransport> out,
121 const bool value) const = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000122
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000123 virtual uint32_t writeByte(shared_ptr<TTransport> out,
Mark Sleecfc01932006-09-01 22:18:16 +0000124 const int8_t byte) const = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000125
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000126 virtual uint32_t writeI16(shared_ptr<TTransport> out,
127 const int16_t i16) const = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000128
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000129 virtual uint32_t writeI32(shared_ptr<TTransport> out,
130 const int32_t i32) const = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000131
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000132 virtual uint32_t writeI64(shared_ptr<TTransport> out,
133 const int64_t i64) const = 0;
134
135 virtual uint32_t writeString(shared_ptr<TTransport> out,
136 const std::string& str) const = 0;
Mark Sleee8540632006-05-30 09:24:40 +0000137
138 /**
Mark Slee8d7e1f62006-06-07 06:48:56 +0000139 * Reading functions
Mark Sleee8540632006-05-30 09:24:40 +0000140 */
141
Marc Slemko5b126d62006-08-11 23:03:42 +0000142 virtual uint32_t readMessageBegin(shared_ptr<TTransport> in,
Marc Slemko91f67482006-08-11 23:58:57 +0000143 std::string& name,
144 TMessageType& messageType,
Mark Sleecfc01932006-09-01 22:18:16 +0000145 int32_t& seqid) const = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000146
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000147 virtual uint32_t readMessageEnd(shared_ptr<TTransport> in) const = 0;
148
149 virtual uint32_t readStructBegin(shared_ptr<TTransport> in,
150 std::string& name) const = 0;
151
152 virtual uint32_t readStructEnd(shared_ptr<TTransport> in) const = 0;
153
154 virtual uint32_t readFieldBegin(shared_ptr<TTransport> in,
155 std::string& name,
156 TType& fieldType,
157 int16_t& fieldId) const = 0;
158
159 virtual uint32_t readFieldEnd(shared_ptr<TTransport> in) const = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000160
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000161 virtual uint32_t readMapBegin(shared_ptr<TTransport> in,
162 TType& keyType,
163 TType& valType,
Marc Slemkob09f5882006-08-23 22:03:34 +0000164 uint32_t& size) const = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000165
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000166 virtual uint32_t readMapEnd(shared_ptr<TTransport> in) const = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000167
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000168 virtual uint32_t readListBegin(shared_ptr<TTransport> in,
169 TType& elemType,
Marc Slemkob09f5882006-08-23 22:03:34 +0000170 uint32_t& size) const = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000171
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000172 virtual uint32_t readListEnd(shared_ptr<TTransport> in) const = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000173
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000174 virtual uint32_t readSetBegin(shared_ptr<TTransport> in,
175 TType& elemType,
Marc Slemkob09f5882006-08-23 22:03:34 +0000176 uint32_t& size) const = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000177
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000178 virtual uint32_t readSetEnd(shared_ptr<TTransport> in) const = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000179
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000180 virtual uint32_t readBool(shared_ptr<TTransport> in,
181 bool& value) const = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000182
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000183 virtual uint32_t readByte(shared_ptr<TTransport> in,
Mark Sleecfc01932006-09-01 22:18:16 +0000184 int8_t& byte) const = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000185
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000186 virtual uint32_t readI16(shared_ptr<TTransport> in,
187 int16_t& i16) const = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000188
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000189 virtual uint32_t readI32(shared_ptr<TTransport> in,
190 int32_t& i32) const = 0;
191
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000192 virtual uint32_t readI64(shared_ptr<TTransport> in,
193 int64_t& i64) const = 0;
194
195 virtual uint32_t readString(shared_ptr<TTransport> in,
196 std::string& str) const = 0;
Mark Sleee8540632006-05-30 09:24:40 +0000197
198 /**
Mark Slee8d7e1f62006-06-07 06:48:56 +0000199 * Method to arbitrarily skip over data.
Mark Sleee8540632006-05-30 09:24:40 +0000200 */
Marc Slemko16698852006-08-04 03:16:10 +0000201 uint32_t skip(shared_ptr<TTransport> in, TType type) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000202 switch (type) {
203 case T_BYTE:
204 {
Mark Sleecfc01932006-09-01 22:18:16 +0000205 int8_t byte;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000206 return readByte(in, byte);
207 }
Mark Sleecfc01932006-09-01 22:18:16 +0000208 case T_I16:
Mark Slee8d7e1f62006-06-07 06:48:56 +0000209 {
Mark Sleecfc01932006-09-01 22:18:16 +0000210 int16_t i16;
211 return readI16(in, i16);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000212 }
213 case T_I32:
214 {
215 int32_t i32;
216 return readI32(in, i32);
217 }
Mark Slee8d7e1f62006-06-07 06:48:56 +0000218 case T_I64:
219 {
220 int64_t i64;
221 return readI64(in, i64);
222 }
223 case T_STRING:
224 {
225 std::string str;
226 return readString(in, str);
227 }
228 case T_STRUCT:
229 {
230 uint32_t result = 0;
231 std::string name;
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000232 int16_t fid;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000233 TType ftype;
234 result += readStructBegin(in, name);
235 while (true) {
236 result += readFieldBegin(in, name, ftype, fid);
237 if (ftype == T_STOP) {
238 break;
239 }
240 result += skip(in, ftype);
241 result += readFieldEnd(in);
242 }
243 result += readStructEnd(in);
244 return result;
245 }
246 case T_MAP:
247 {
248 uint32_t result = 0;
249 TType keyType;
250 TType valType;
Marc Slemkob09f5882006-08-23 22:03:34 +0000251 uint32_t i, size;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000252 result += readMapBegin(in, keyType, valType, size);
253 for (i = 0; i < size; i++) {
254 result += skip(in, keyType);
255 result += skip(in, valType);
256 }
257 result += readMapEnd(in);
258 return result;
259 }
260 case T_SET:
261 {
262 uint32_t result = 0;
263 TType elemType;
Marc Slemkob09f5882006-08-23 22:03:34 +0000264 uint32_t i, size;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000265 result += readSetBegin(in, elemType, size);
266 for (i = 0; i < size; i++) {
267 result += skip(in, elemType);
268 }
269 result += readSetEnd(in);
270 return result;
271 }
272 case T_LIST:
273 {
274 uint32_t result = 0;
275 TType elemType;
Marc Slemkob09f5882006-08-23 22:03:34 +0000276 uint32_t i, size;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000277 result += readListBegin(in, elemType, size);
278 for (i = 0; i < size; i++) {
279 result += skip(in, elemType);
280 }
281 result += readListEnd(in);
282 return result;
283 }
284 default:
285 return 0;
286 }
287 }
Mark Sleee8540632006-05-30 09:24:40 +0000288
289 protected:
290 TProtocol() {}
291};
292
Marc Slemko6f038a72006-08-03 18:58:09 +0000293}}} // facebook::thrift::protocol
294
Mark Sleee8540632006-05-30 09:24:40 +0000295#endif
Marc Slemko6f038a72006-08-03 18:58:09 +0000296