blob: 3414cec851d70a7fb22a6965bbfb58757e1797cc [file] [log] [blame]
Mark Sleef5f2be42006-09-05 21:05:31 +00001#ifndef _THRIFT_PROTOCOL_TBINARYPROTOCOL_H_
2#define _THRIFT_PROTOCOL_TBINARYPROTOCOL_H_ 1
Mark Sleee8540632006-05-30 09:24:40 +00003
Marc Slemkod42a2c22006-08-10 03:30:18 +00004#include "TProtocol.h"
Marc Slemko16698852006-08-04 03:16:10 +00005
6#include <boost/shared_ptr.hpp>
Mark Sleee8540632006-05-30 09:24:40 +00007
Marc Slemko6f038a72006-08-03 18:58:09 +00008namespace facebook { namespace thrift { namespace protocol {
9
Marc Slemko16698852006-08-04 03:16:10 +000010using namespace boost;
11
Mark Sleee8540632006-05-30 09:24:40 +000012/**
13 * The default binary protocol for thrift. Writes all data in a very basic
14 * binary format, essentially just spitting out the raw bytes.
15 *
16 * @author Mark Slee <mcslee@facebook.com>
17 */
Mark Sleef9831082007-02-20 20:59:21 +000018class TBinaryProtocol : public TProtocol {
Marc Slemko0b4ffa92006-08-11 02:49:29 +000019 public:
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000020 TBinaryProtocol(shared_ptr<TTransport> trans) :
Mark Sleef9831082007-02-20 20:59:21 +000021 TProtocol(trans),
22 string_limit_(0),
23 container_limit_(0),
24 string_buf_(NULL),
25 string_buf_size_(0) {}
Mark Slee4af6ed72006-10-25 19:02:49 +000026
Mark Sleef9831082007-02-20 20:59:21 +000027 TBinaryProtocol(shared_ptr<TTransport> trans,
28 int32_t string_limit,
29 int32_t container_limit) :
30 TProtocol(trans),
31 string_limit_(string_limit),
32 container_limit_(container_limit),
33 string_buf_(NULL),
34 string_buf_size_(0) {}
35
36 ~TBinaryProtocol() {
37 if (string_buf_ != NULL) {
38 free(string_buf_);
39 string_buf_size_ = 0;
40 }
41 }
42
43 void setStringSizeLimit(int32_t string_limit) {
44 string_limit_ = string_limit;
45 }
46
47 void setContainerSizeLimit(int32_t container_limit) {
48 container_limit_ = container_limit;
49 }
Mark Sleee8540632006-05-30 09:24:40 +000050
Mark Slee8d7e1f62006-06-07 06:48:56 +000051 /**
52 * Writing functions.
53 */
Mark Sleee8540632006-05-30 09:24:40 +000054
Mark Slee4af6ed72006-10-25 19:02:49 +000055 virtual uint32_t writeMessageBegin(const std::string name,
Marc Slemko16698852006-08-04 03:16:10 +000056 const TMessageType messageType,
Mark Slee4af6ed72006-10-25 19:02:49 +000057 const int32_t seqid);
Marc Slemko16698852006-08-04 03:16:10 +000058
Mark Slee4af6ed72006-10-25 19:02:49 +000059 virtual uint32_t writeMessageEnd();
Marc Slemko16698852006-08-04 03:16:10 +000060
61
Mark Slee4af6ed72006-10-25 19:02:49 +000062 uint32_t writeStructBegin(const std::string& name);
Mark Sleee8540632006-05-30 09:24:40 +000063
Mark Slee4af6ed72006-10-25 19:02:49 +000064 uint32_t writeStructEnd();
Mark Sleee8540632006-05-30 09:24:40 +000065
Mark Slee4af6ed72006-10-25 19:02:49 +000066 uint32_t writeFieldBegin(const std::string& name,
67 const TType fieldType,
68 const int16_t fieldId);
Mark Slee8d7e1f62006-06-07 06:48:56 +000069
Mark Slee4af6ed72006-10-25 19:02:49 +000070 uint32_t writeFieldEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +000071
Mark Slee4af6ed72006-10-25 19:02:49 +000072 uint32_t writeFieldStop();
Mark Slee8d7e1f62006-06-07 06:48:56 +000073
Mark Slee4af6ed72006-10-25 19:02:49 +000074 uint32_t writeMapBegin(const TType keyType,
75 const TType valType,
76 const uint32_t size);
Mark Slee8d7e1f62006-06-07 06:48:56 +000077
Mark Slee4af6ed72006-10-25 19:02:49 +000078 uint32_t writeMapEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +000079
Mark Slee4af6ed72006-10-25 19:02:49 +000080 uint32_t writeListBegin(const TType elemType,
81 const uint32_t size);
Mark Slee8d7e1f62006-06-07 06:48:56 +000082
Mark Slee4af6ed72006-10-25 19:02:49 +000083 uint32_t writeListEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +000084
Mark Slee4af6ed72006-10-25 19:02:49 +000085 uint32_t writeSetBegin(const TType elemType,
86 const uint32_t size);
Mark Slee8d7e1f62006-06-07 06:48:56 +000087
Mark Slee4af6ed72006-10-25 19:02:49 +000088 uint32_t writeSetEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +000089
Mark Slee4af6ed72006-10-25 19:02:49 +000090 uint32_t writeBool(const bool value);
Mark Slee8d7e1f62006-06-07 06:48:56 +000091
Mark Slee4af6ed72006-10-25 19:02:49 +000092 uint32_t writeByte(const int8_t byte);
Mark Slee8d7e1f62006-06-07 06:48:56 +000093
Mark Slee4af6ed72006-10-25 19:02:49 +000094 uint32_t writeI16(const int16_t i16);
Mark Slee8d7e1f62006-06-07 06:48:56 +000095
Mark Slee4af6ed72006-10-25 19:02:49 +000096 uint32_t writeI32(const int32_t i32);
Marc Slemko0b4ffa92006-08-11 02:49:29 +000097
Mark Slee4af6ed72006-10-25 19:02:49 +000098 uint32_t writeI64(const int64_t i64);
Marc Slemko0b4ffa92006-08-11 02:49:29 +000099
Mark Slee4af6ed72006-10-25 19:02:49 +0000100 uint32_t writeDouble(const double dub);
Mark Sleec98d0502006-09-06 02:42:25 +0000101
102
Mark Slee4af6ed72006-10-25 19:02:49 +0000103 uint32_t writeString(const std::string& str);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000104
105 /**
106 * Reading functions
107 */
108
Marc Slemko16698852006-08-04 03:16:10 +0000109
Mark Slee4af6ed72006-10-25 19:02:49 +0000110 uint32_t readMessageBegin(std::string& name,
Marc Slemkoe6889de2006-08-12 00:32:53 +0000111 TMessageType& messageType,
Mark Slee4af6ed72006-10-25 19:02:49 +0000112 int32_t& seqid);
Marc Slemko16698852006-08-04 03:16:10 +0000113
Mark Slee4af6ed72006-10-25 19:02:49 +0000114 uint32_t readMessageEnd();
Marc Slemko16698852006-08-04 03:16:10 +0000115
Mark Slee4af6ed72006-10-25 19:02:49 +0000116 uint32_t readStructBegin(std::string& name);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000117
Mark Slee4af6ed72006-10-25 19:02:49 +0000118 uint32_t readStructEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000119
Mark Slee4af6ed72006-10-25 19:02:49 +0000120 uint32_t readFieldBegin(std::string& name,
121 TType& fieldType,
122 int16_t& fieldId);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000123
Mark Slee4af6ed72006-10-25 19:02:49 +0000124 uint32_t readFieldEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000125
Mark Slee4af6ed72006-10-25 19:02:49 +0000126 uint32_t readMapBegin(TType& keyType,
127 TType& valType,
128 uint32_t& size);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000129
Mark Slee4af6ed72006-10-25 19:02:49 +0000130 uint32_t readMapEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000131
Mark Slee4af6ed72006-10-25 19:02:49 +0000132 uint32_t readListBegin(TType& elemType,
133 uint32_t& size);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000134
Mark Slee4af6ed72006-10-25 19:02:49 +0000135 uint32_t readListEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000136
Mark Slee4af6ed72006-10-25 19:02:49 +0000137 uint32_t readSetBegin(TType& elemType,
138 uint32_t& size);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000139
Mark Slee4af6ed72006-10-25 19:02:49 +0000140 uint32_t readSetEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000141
Mark Slee4af6ed72006-10-25 19:02:49 +0000142 uint32_t readBool(bool& value);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000143
Mark Slee4af6ed72006-10-25 19:02:49 +0000144 uint32_t readByte(int8_t& byte);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000145
Mark Slee4af6ed72006-10-25 19:02:49 +0000146 uint32_t readI16(int16_t& i16);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000147
Mark Slee4af6ed72006-10-25 19:02:49 +0000148 uint32_t readI32(int32_t& i32);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000149
Mark Slee4af6ed72006-10-25 19:02:49 +0000150 uint32_t readI64(int64_t& i64);
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000151
Mark Slee4af6ed72006-10-25 19:02:49 +0000152 uint32_t readDouble(double& dub);
Mark Sleec98d0502006-09-06 02:42:25 +0000153
Mark Slee4af6ed72006-10-25 19:02:49 +0000154 uint32_t readString(std::string& str);
Mark Sleef9831082007-02-20 20:59:21 +0000155
156 private:
157 int32_t string_limit_;
158 int32_t container_limit_;
159
160 // Buffer for reading strings, save for the lifetime of the protocol to
161 // avoid memory churn allocating memory on every string read
162 uint8_t* string_buf_;
163 int32_t string_buf_size_;
164
Mark Slee4af6ed72006-10-25 19:02:49 +0000165};
166
167/**
168 * Constructs binary protocol handlers
169 */
170class TBinaryProtocolFactory : public TProtocolFactory {
171 public:
Mark Sleef9831082007-02-20 20:59:21 +0000172 TBinaryProtocolFactory() :
173 string_limit_(0),
174 container_limit_(0) {}
175
176 TBinaryProtocolFactory(int32_t string_limit, int32_t container_limit) :
177 string_limit_(string_limit),
178 container_limit_(container_limit) {}
Mark Slee4af6ed72006-10-25 19:02:49 +0000179
180 virtual ~TBinaryProtocolFactory() {}
181
Mark Sleef9831082007-02-20 20:59:21 +0000182 void setStringSizeLimit(int32_t string_limit) {
183 string_limit_ = string_limit;
Mark Slee4af6ed72006-10-25 19:02:49 +0000184 }
Mark Sleef9831082007-02-20 20:59:21 +0000185
186 void setContainerSizeLimit(int32_t container_limit) {
187 container_limit_ = container_limit;
188 }
189
190 boost::shared_ptr<TProtocol> getProtocol(boost::shared_ptr<TTransport> trans) {
191 return boost::shared_ptr<TProtocol>(new TBinaryProtocol(trans, string_limit_, container_limit_));
192 }
193
194 private:
195 int32_t string_limit_;
196 int32_t container_limit_;
197
Mark Sleee8540632006-05-30 09:24:40 +0000198};
199
Marc Slemko6f038a72006-08-03 18:58:09 +0000200}}} // facebook::thrift::protocol
201
Mark Sleef5f2be42006-09-05 21:05:31 +0000202#endif // #ifndef _THRIFT_PROTOCOL_TBINARYPROTOCOL_H_
203
Marc Slemko6f038a72006-08-03 18:58:09 +0000204