blob: 735268e17452f90c2cea88897f01844cea743a31 [file] [log] [blame]
Mark Slee9f0c6512007-02-28 23:58:26 +00001// Copyright (c) 2006- Facebook
2// Distributed under the Thrift Software License
3//
4// See accompanying file LICENSE or visit the Thrift site at:
5// http://developers.facebook.com/thrift/
6
Mark Sleef5f2be42006-09-05 21:05:31 +00007#ifndef _THRIFT_PROTOCOL_TPROTOCOL_H_
8#define _THRIFT_PROTOCOL_TPROTOCOL_H_ 1
Mark Sleee8540632006-05-30 09:24:40 +00009
Marc Slemko16698852006-08-04 03:16:10 +000010#include <transport/TTransport.h>
Mark Sleef9831082007-02-20 20:59:21 +000011#include <protocol/TProtocolException.h>
Marc Slemko16698852006-08-04 03:16:10 +000012
13#include <boost/shared_ptr.hpp>
14
Mark Slee8d7e1f62006-06-07 06:48:56 +000015#include <netinet/in.h>
Mark Sleee8540632006-05-30 09:24:40 +000016#include <sys/types.h>
17#include <string>
18#include <map>
19
Marc Slemko6f038a72006-08-03 18:58:09 +000020namespace facebook { namespace thrift { namespace protocol {
21
Mark Slee5ea15f92007-03-05 22:55:59 +000022using facebook::thrift::transport::TTransport;
Marc Slemko6f038a72006-08-03 18:58:09 +000023
Mark Sleedc8a2a22006-09-19 22:20:18 +000024#if __BYTE_ORDER == __BIG_ENDIAN
25#define ntohll(n) (n)
26#define htonll(n) (n)
27#else
28#define ntohll(n) ( (((unsigned long long)ntohl(n)) << 32) + ntohl(n >> 32) )
29#define htonll(n) ( (((unsigned long long)htonl(n)) << 32) + htonl(n >> 32) )
30#endif
Mark Slee8d7e1f62006-06-07 06:48:56 +000031
Mark Sleee8540632006-05-30 09:24:40 +000032/**
Mark Slee8d7e1f62006-06-07 06:48:56 +000033 * Enumerated definition of the types that the Thrift protocol supports.
34 * Take special note of the T_END type which is used specifically to mark
35 * the end of a sequence of fields.
36 */
37enum TType {
Marc Slemkod42a2c22006-08-10 03:30:18 +000038 T_STOP = 0,
Marc Slemko5b126d62006-08-11 23:03:42 +000039 T_VOID = 1,
40 T_BOOL = 2,
41 T_BYTE = 3,
Mark Sleecfc01932006-09-01 22:18:16 +000042 T_I08 = 3,
Marc Slemko5b126d62006-08-11 23:03:42 +000043 T_I16 = 6,
Marc Slemko5b126d62006-08-11 23:03:42 +000044 T_I32 = 8,
45 T_U64 = 9,
46 T_I64 = 10,
Mark Sleec98d0502006-09-06 02:42:25 +000047 T_DOUBLE = 4,
Marc Slemko5b126d62006-08-11 23:03:42 +000048 T_STRING = 11,
Marc Slemkod97eb612006-08-24 23:37:36 +000049 T_UTF7 = 11,
50 T_STRUCT = 12,
51 T_MAP = 13,
52 T_SET = 14,
53 T_LIST = 15,
54 T_UTF8 = 16,
55 T_UTF16 = 17
Mark Slee8d7e1f62006-06-07 06:48:56 +000056};
57
58/**
Mark Sleef5f2be42006-09-05 21:05:31 +000059 * Enumerated definition of the message types that the Thrift protocol
60 * supports.
Marc Slemko16698852006-08-04 03:16:10 +000061 */
62enum TMessageType {
63 T_CALL = 1,
Mark Sleef9831082007-02-20 20:59:21 +000064 T_REPLY = 2,
65 T_EXCEPTION = 3
Marc Slemko16698852006-08-04 03:16:10 +000066};
67
68/**
Mark Sleee8540632006-05-30 09:24:40 +000069 * Abstract class for a thrift protocol driver. These are all the methods that
70 * a protocol must implement. Essentially, there must be some way of reading
71 * and writing all the base types, plus a mechanism for writing out structs
Mark Slee5d06fea2007-03-05 22:18:18 +000072 * with indexed fields.
73 *
74 * TProtocol objects should not be shared across multiple encoding contexts,
75 * as they may need to maintain internal state in some protocols (i.e. XML).
76 * Note that is is acceptable for the TProtocol module to do its own internal
77 * buffered reads/writes to the underlying TTransport where appropriate (i.e.
78 * when parsing an input XML stream, reading should be batched rather than
79 * looking ahead character by character for a close tag).
Mark Sleee8540632006-05-30 09:24:40 +000080 *
81 * @author Mark Slee <mcslee@facebook.com>
82 */
83class TProtocol {
84 public:
85 virtual ~TProtocol() {}
86
87 /**
Mark Slee8d7e1f62006-06-07 06:48:56 +000088 * Writing functions.
Mark Sleee8540632006-05-30 09:24:40 +000089 */
90
Mark Slee4af6ed72006-10-25 19:02:49 +000091 virtual uint32_t writeMessageBegin(const std::string name,
Marc Slemko0b4ffa92006-08-11 02:49:29 +000092 const TMessageType messageType,
Mark Slee4af6ed72006-10-25 19:02:49 +000093 const int32_t seqid) = 0;
Marc Slemko16698852006-08-04 03:16:10 +000094
Mark Slee4af6ed72006-10-25 19:02:49 +000095 virtual uint32_t writeMessageEnd() = 0;
Marc Slemko16698852006-08-04 03:16:10 +000096
97
Mark Slee4af6ed72006-10-25 19:02:49 +000098 virtual uint32_t writeStructBegin(const std::string& name) = 0;
Marc Slemko0b4ffa92006-08-11 02:49:29 +000099
Mark Slee4af6ed72006-10-25 19:02:49 +0000100 virtual uint32_t writeStructEnd() = 0;
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000101
Mark Slee4af6ed72006-10-25 19:02:49 +0000102 virtual uint32_t writeFieldBegin(const std::string& name,
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000103 const TType fieldType,
Mark Slee4af6ed72006-10-25 19:02:49 +0000104 const int16_t fieldId) = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000105
Mark Slee4af6ed72006-10-25 19:02:49 +0000106 virtual uint32_t writeFieldEnd() = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000107
Mark Slee4af6ed72006-10-25 19:02:49 +0000108 virtual uint32_t writeFieldStop() = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000109
Mark Slee4af6ed72006-10-25 19:02:49 +0000110 virtual uint32_t writeMapBegin(const TType keyType,
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000111 const TType valType,
Mark Slee4af6ed72006-10-25 19:02:49 +0000112 const uint32_t size) = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000113
Mark Slee4af6ed72006-10-25 19:02:49 +0000114 virtual uint32_t writeMapEnd() = 0;
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000115
Mark Slee4af6ed72006-10-25 19:02:49 +0000116 virtual uint32_t writeListBegin(const TType elemType,
117 const uint32_t size) = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000118
Mark Slee4af6ed72006-10-25 19:02:49 +0000119 virtual uint32_t writeListEnd() = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000120
Mark Slee4af6ed72006-10-25 19:02:49 +0000121 virtual uint32_t writeSetBegin(const TType elemType,
122 const uint32_t size) = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000123
Mark Slee4af6ed72006-10-25 19:02:49 +0000124 virtual uint32_t writeSetEnd() = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000125
Mark Slee4af6ed72006-10-25 19:02:49 +0000126 virtual uint32_t writeBool(const bool value) = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000127
Mark Slee4af6ed72006-10-25 19:02:49 +0000128 virtual uint32_t writeByte(const int8_t byte) = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000129
Mark Slee4af6ed72006-10-25 19:02:49 +0000130 virtual uint32_t writeI16(const int16_t i16) = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000131
Mark Slee4af6ed72006-10-25 19:02:49 +0000132 virtual uint32_t writeI32(const int32_t i32) = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000133
Mark Slee4af6ed72006-10-25 19:02:49 +0000134 virtual uint32_t writeI64(const int64_t i64) = 0;
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000135
Mark Slee4af6ed72006-10-25 19:02:49 +0000136 virtual uint32_t writeDouble(const double dub) = 0;
Mark Sleec98d0502006-09-06 02:42:25 +0000137
Mark Slee4af6ed72006-10-25 19:02:49 +0000138 virtual uint32_t writeString(const std::string& str) = 0;
Mark Sleee8540632006-05-30 09:24:40 +0000139
140 /**
Mark Slee8d7e1f62006-06-07 06:48:56 +0000141 * Reading functions
Mark Sleee8540632006-05-30 09:24:40 +0000142 */
143
Mark Slee4af6ed72006-10-25 19:02:49 +0000144 virtual uint32_t readMessageBegin(std::string& name,
Marc Slemko91f67482006-08-11 23:58:57 +0000145 TMessageType& messageType,
Mark Slee4af6ed72006-10-25 19:02:49 +0000146 int32_t& seqid) = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000147
Mark Slee4af6ed72006-10-25 19:02:49 +0000148 virtual uint32_t readMessageEnd() = 0;
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000149
Mark Slee4af6ed72006-10-25 19:02:49 +0000150 virtual uint32_t readStructBegin(std::string& name) = 0;
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000151
Mark Slee4af6ed72006-10-25 19:02:49 +0000152 virtual uint32_t readStructEnd() = 0;
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000153
Mark Slee4af6ed72006-10-25 19:02:49 +0000154 virtual uint32_t readFieldBegin(std::string& name,
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000155 TType& fieldType,
Mark Slee4af6ed72006-10-25 19:02:49 +0000156 int16_t& fieldId) = 0;
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000157
Mark Slee4af6ed72006-10-25 19:02:49 +0000158 virtual uint32_t readFieldEnd() = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000159
Mark Slee4af6ed72006-10-25 19:02:49 +0000160 virtual uint32_t readMapBegin(TType& keyType,
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000161 TType& valType,
Mark Slee4af6ed72006-10-25 19:02:49 +0000162 uint32_t& size) = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000163
Mark Slee4af6ed72006-10-25 19:02:49 +0000164 virtual uint32_t readMapEnd() = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000165
Mark Slee4af6ed72006-10-25 19:02:49 +0000166 virtual uint32_t readListBegin(TType& elemType,
167 uint32_t& size) = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000168
Mark Slee4af6ed72006-10-25 19:02:49 +0000169 virtual uint32_t readListEnd() = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000170
Mark Slee4af6ed72006-10-25 19:02:49 +0000171 virtual uint32_t readSetBegin(TType& elemType,
172 uint32_t& size) = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000173
Mark Slee4af6ed72006-10-25 19:02:49 +0000174 virtual uint32_t readSetEnd() = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000175
Mark Slee4af6ed72006-10-25 19:02:49 +0000176 virtual uint32_t readBool(bool& value) = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000177
Mark Slee4af6ed72006-10-25 19:02:49 +0000178 virtual uint32_t readByte(int8_t& byte) = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000179
Mark Slee4af6ed72006-10-25 19:02:49 +0000180 virtual uint32_t readI16(int16_t& i16) = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000181
Mark Slee4af6ed72006-10-25 19:02:49 +0000182 virtual uint32_t readI32(int32_t& i32) = 0;
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000183
Mark Slee4af6ed72006-10-25 19:02:49 +0000184 virtual uint32_t readI64(int64_t& i64) = 0;
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000185
Mark Slee4af6ed72006-10-25 19:02:49 +0000186 virtual uint32_t readDouble(double& dub) = 0;
Mark Sleec98d0502006-09-06 02:42:25 +0000187
Mark Slee4af6ed72006-10-25 19:02:49 +0000188 virtual uint32_t readString(std::string& str) = 0;
Mark Sleee8540632006-05-30 09:24:40 +0000189
190 /**
Mark Slee8d7e1f62006-06-07 06:48:56 +0000191 * Method to arbitrarily skip over data.
Mark Sleee8540632006-05-30 09:24:40 +0000192 */
Mark Slee4af6ed72006-10-25 19:02:49 +0000193 uint32_t skip(TType type) {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000194 switch (type) {
Mark Slee78f58e22006-09-02 04:17:07 +0000195 case T_BOOL:
196 {
197 bool boolv;
Mark Slee4af6ed72006-10-25 19:02:49 +0000198 return readBool(boolv);
Mark Slee78f58e22006-09-02 04:17:07 +0000199 }
Mark Slee8d7e1f62006-06-07 06:48:56 +0000200 case T_BYTE:
201 {
Mark Slee78f58e22006-09-02 04:17:07 +0000202 int8_t bytev;
Mark Slee4af6ed72006-10-25 19:02:49 +0000203 return readByte(bytev);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000204 }
Mark Sleecfc01932006-09-01 22:18:16 +0000205 case T_I16:
Mark Slee8d7e1f62006-06-07 06:48:56 +0000206 {
Mark Sleecfc01932006-09-01 22:18:16 +0000207 int16_t i16;
Mark Slee4af6ed72006-10-25 19:02:49 +0000208 return readI16(i16);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000209 }
210 case T_I32:
211 {
212 int32_t i32;
Mark Slee4af6ed72006-10-25 19:02:49 +0000213 return readI32(i32);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000214 }
Mark Slee8d7e1f62006-06-07 06:48:56 +0000215 case T_I64:
216 {
217 int64_t i64;
Mark Slee4af6ed72006-10-25 19:02:49 +0000218 return readI64(i64);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000219 }
Mark Sleec98d0502006-09-06 02:42:25 +0000220 case T_DOUBLE:
221 {
222 double dub;
Mark Slee4af6ed72006-10-25 19:02:49 +0000223 return readDouble(dub);
Mark Sleec98d0502006-09-06 02:42:25 +0000224 }
Mark Slee8d7e1f62006-06-07 06:48:56 +0000225 case T_STRING:
226 {
227 std::string str;
Mark Slee4af6ed72006-10-25 19:02:49 +0000228 return readString(str);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000229 }
230 case T_STRUCT:
231 {
232 uint32_t result = 0;
233 std::string name;
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000234 int16_t fid;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000235 TType ftype;
Mark Slee4af6ed72006-10-25 19:02:49 +0000236 result += readStructBegin(name);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000237 while (true) {
Mark Slee4af6ed72006-10-25 19:02:49 +0000238 result += readFieldBegin(name, ftype, fid);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000239 if (ftype == T_STOP) {
240 break;
241 }
Mark Slee4af6ed72006-10-25 19:02:49 +0000242 result += skip(ftype);
243 result += readFieldEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000244 }
Mark Slee4af6ed72006-10-25 19:02:49 +0000245 result += readStructEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000246 return result;
247 }
248 case T_MAP:
249 {
250 uint32_t result = 0;
251 TType keyType;
252 TType valType;
Marc Slemkob09f5882006-08-23 22:03:34 +0000253 uint32_t i, size;
Mark Slee4af6ed72006-10-25 19:02:49 +0000254 result += readMapBegin(keyType, valType, size);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000255 for (i = 0; i < size; i++) {
Mark Slee4af6ed72006-10-25 19:02:49 +0000256 result += skip(keyType);
257 result += skip(valType);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000258 }
Mark Slee4af6ed72006-10-25 19:02:49 +0000259 result += readMapEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000260 return result;
261 }
262 case T_SET:
263 {
264 uint32_t result = 0;
265 TType elemType;
Marc Slemkob09f5882006-08-23 22:03:34 +0000266 uint32_t i, size;
Mark Slee4af6ed72006-10-25 19:02:49 +0000267 result += readSetBegin(elemType, size);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000268 for (i = 0; i < size; i++) {
Mark Slee4af6ed72006-10-25 19:02:49 +0000269 result += skip(elemType);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000270 }
Mark Slee4af6ed72006-10-25 19:02:49 +0000271 result += readSetEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000272 return result;
273 }
274 case T_LIST:
275 {
276 uint32_t result = 0;
277 TType elemType;
Marc Slemkob09f5882006-08-23 22:03:34 +0000278 uint32_t i, size;
Mark Slee4af6ed72006-10-25 19:02:49 +0000279 result += readListBegin(elemType, size);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000280 for (i = 0; i < size; i++) {
Mark Slee4af6ed72006-10-25 19:02:49 +0000281 result += skip(elemType);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000282 }
Mark Slee4af6ed72006-10-25 19:02:49 +0000283 result += readListEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000284 return result;
285 }
286 default:
287 return 0;
288 }
289 }
Mark Sleee8540632006-05-30 09:24:40 +0000290
Mark Slee5ea15f92007-03-05 22:55:59 +0000291 inline boost::shared_ptr<TTransport> getTransport() {
Mark Slee43b6c632007-02-07 00:54:17 +0000292 return ptrans_;
Mark Slee4af6ed72006-10-25 19:02:49 +0000293 }
294
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000295 // TODO: remove these two calls, they are for backwards
296 // compatibility
Mark Slee5ea15f92007-03-05 22:55:59 +0000297 inline boost::shared_ptr<TTransport> getInputTransport() {
Mark Slee43b6c632007-02-07 00:54:17 +0000298 return ptrans_;
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000299 }
Mark Slee5ea15f92007-03-05 22:55:59 +0000300 inline boost::shared_ptr<TTransport> getOutputTransport() {
Mark Slee43b6c632007-02-07 00:54:17 +0000301 return ptrans_;
Mark Slee4af6ed72006-10-25 19:02:49 +0000302 }
303
Mark Sleee8540632006-05-30 09:24:40 +0000304 protected:
Mark Slee5ea15f92007-03-05 22:55:59 +0000305 TProtocol(boost::shared_ptr<TTransport> ptrans):
Mark Slee43b6c632007-02-07 00:54:17 +0000306 ptrans_(ptrans) {
307 trans_ = ptrans.get();
308 }
Mark Slee4af6ed72006-10-25 19:02:49 +0000309
Mark Slee5ea15f92007-03-05 22:55:59 +0000310 boost::shared_ptr<TTransport> ptrans_;
Mark Slee43b6c632007-02-07 00:54:17 +0000311 TTransport* trans_;
Mark Slee4af6ed72006-10-25 19:02:49 +0000312
313 private:
Mark Sleee8540632006-05-30 09:24:40 +0000314 TProtocol() {}
315};
316
Mark Slee4af6ed72006-10-25 19:02:49 +0000317/**
318 * Constructs input and output protocol objects given transports.
319 */
320class TProtocolFactory {
321 public:
322 TProtocolFactory() {}
323
324 virtual ~TProtocolFactory() {}
325
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000326 virtual boost::shared_ptr<TProtocol> getProtocol(boost::shared_ptr<TTransport> trans) = 0;
Mark Slee4af6ed72006-10-25 19:02:49 +0000327};
328
Marc Slemko6f038a72006-08-03 18:58:09 +0000329}}} // facebook::thrift::protocol
330
Mark Sleef5f2be42006-09-05 21:05:31 +0000331#endif // #define _THRIFT_PROTOCOL_TPROTOCOL_H_ 1