blob: 6be21e031f6bf9ebc7c04fbc83eca8518cfa1745 [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_TBINARYPROTOCOL_H_
8#define _THRIFT_PROTOCOL_TBINARYPROTOCOL_H_ 1
Mark Sleee8540632006-05-30 09:24:40 +00009
Marc Slemkod42a2c22006-08-10 03:30:18 +000010#include "TProtocol.h"
Marc Slemko16698852006-08-04 03:16:10 +000011
12#include <boost/shared_ptr.hpp>
Mark Sleee8540632006-05-30 09:24:40 +000013
T Jake Lucianib5e62212009-01-31 22:36:20 +000014namespace apache { namespace thrift { namespace protocol {
Marc Slemko6f038a72006-08-03 18:58:09 +000015
Mark Sleee8540632006-05-30 09:24:40 +000016/**
17 * The default binary protocol for thrift. Writes all data in a very basic
18 * binary format, essentially just spitting out the raw bytes.
19 *
Mark Sleee8540632006-05-30 09:24:40 +000020 */
Mark Sleef9831082007-02-20 20:59:21 +000021class TBinaryProtocol : public TProtocol {
Mark Slee808454e2007-06-20 21:51:57 +000022 protected:
23 static const int32_t VERSION_MASK = 0xffff0000;
24 static const int32_t VERSION_1 = 0x80010000;
David Reiss4e7530d2007-09-04 21:49:53 +000025 // VERSION_2 (0x80020000) is taken by TDenseProtocol.
Mark Slee808454e2007-06-20 21:51:57 +000026
Marc Slemko0b4ffa92006-08-11 02:49:29 +000027 public:
Mark Slee5ea15f92007-03-05 22:55:59 +000028 TBinaryProtocol(boost::shared_ptr<TTransport> trans) :
Mark Sleef9831082007-02-20 20:59:21 +000029 TProtocol(trans),
30 string_limit_(0),
31 container_limit_(0),
Mark Slee808454e2007-06-20 21:51:57 +000032 strict_read_(false),
33 strict_write_(true),
Mark Sleef9831082007-02-20 20:59:21 +000034 string_buf_(NULL),
35 string_buf_size_(0) {}
Mark Slee4af6ed72006-10-25 19:02:49 +000036
Mark Slee5ea15f92007-03-05 22:55:59 +000037 TBinaryProtocol(boost::shared_ptr<TTransport> trans,
Mark Sleef9831082007-02-20 20:59:21 +000038 int32_t string_limit,
Mark Slee808454e2007-06-20 21:51:57 +000039 int32_t container_limit,
40 bool strict_read,
41 bool strict_write) :
Mark Sleef9831082007-02-20 20:59:21 +000042 TProtocol(trans),
43 string_limit_(string_limit),
44 container_limit_(container_limit),
Mark Slee808454e2007-06-20 21:51:57 +000045 strict_read_(strict_read),
46 strict_write_(strict_write),
Mark Sleef9831082007-02-20 20:59:21 +000047 string_buf_(NULL),
48 string_buf_size_(0) {}
49
50 ~TBinaryProtocol() {
51 if (string_buf_ != NULL) {
David Reissd7a16f42008-02-19 22:47:29 +000052 std::free(string_buf_);
Mark Sleef9831082007-02-20 20:59:21 +000053 string_buf_size_ = 0;
54 }
55 }
56
57 void setStringSizeLimit(int32_t string_limit) {
58 string_limit_ = string_limit;
59 }
60
61 void setContainerSizeLimit(int32_t container_limit) {
62 container_limit_ = container_limit;
63 }
Mark Sleee8540632006-05-30 09:24:40 +000064
Mark Slee808454e2007-06-20 21:51:57 +000065 void setStrict(bool strict_read, bool strict_write) {
66 strict_read_ = strict_read;
67 strict_write_ = strict_write;
68 }
69
Mark Slee8d7e1f62006-06-07 06:48:56 +000070 /**
71 * Writing functions.
72 */
Mark Sleee8540632006-05-30 09:24:40 +000073
Mark Slee82a6c0f2007-04-04 21:08:21 +000074 virtual uint32_t writeMessageBegin(const std::string& name,
75 const TMessageType messageType,
76 const int32_t seqid);
Marc Slemko16698852006-08-04 03:16:10 +000077
Mark Slee4af6ed72006-10-25 19:02:49 +000078 virtual uint32_t writeMessageEnd();
Marc Slemko16698852006-08-04 03:16:10 +000079
80
David Reiss64120002008-04-29 23:12:24 +000081 uint32_t writeStructBegin(const char* name);
Mark Sleee8540632006-05-30 09:24:40 +000082
Mark Slee4af6ed72006-10-25 19:02:49 +000083 uint32_t writeStructEnd();
Mark Sleee8540632006-05-30 09:24:40 +000084
David Reiss64120002008-04-29 23:12:24 +000085 uint32_t writeFieldBegin(const char* name,
Mark Slee4af6ed72006-10-25 19:02:49 +000086 const TType fieldType,
87 const int16_t fieldId);
Mark Slee8d7e1f62006-06-07 06:48:56 +000088
Mark Slee4af6ed72006-10-25 19:02:49 +000089 uint32_t writeFieldEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +000090
Mark Slee4af6ed72006-10-25 19:02:49 +000091 uint32_t writeFieldStop();
David Reiss0c90f6f2008-02-06 22:18:40 +000092
Mark Slee4af6ed72006-10-25 19:02:49 +000093 uint32_t writeMapBegin(const TType keyType,
94 const TType valType,
95 const uint32_t size);
Mark Slee8d7e1f62006-06-07 06:48:56 +000096
Mark Slee4af6ed72006-10-25 19:02:49 +000097 uint32_t writeMapEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +000098
Mark Slee4af6ed72006-10-25 19:02:49 +000099 uint32_t writeListBegin(const TType elemType,
100 const uint32_t size);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000101
Mark Slee4af6ed72006-10-25 19:02:49 +0000102 uint32_t writeListEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000103
Mark Slee4af6ed72006-10-25 19:02:49 +0000104 uint32_t writeSetBegin(const TType elemType,
105 const uint32_t size);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000106
Mark Slee4af6ed72006-10-25 19:02:49 +0000107 uint32_t writeSetEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000108
Mark Slee4af6ed72006-10-25 19:02:49 +0000109 uint32_t writeBool(const bool value);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000110
Mark Slee4af6ed72006-10-25 19:02:49 +0000111 uint32_t writeByte(const int8_t byte);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000112
Mark Slee4af6ed72006-10-25 19:02:49 +0000113 uint32_t writeI16(const int16_t i16);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000114
Mark Slee4af6ed72006-10-25 19:02:49 +0000115 uint32_t writeI32(const int32_t i32);
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000116
Mark Slee4af6ed72006-10-25 19:02:49 +0000117 uint32_t writeI64(const int64_t i64);
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000118
Mark Slee4af6ed72006-10-25 19:02:49 +0000119 uint32_t writeDouble(const double dub);
Mark Sleec98d0502006-09-06 02:42:25 +0000120
Mark Slee4af6ed72006-10-25 19:02:49 +0000121 uint32_t writeString(const std::string& str);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000122
David Reissc005b1b2008-02-15 01:38:18 +0000123 uint32_t writeBinary(const std::string& str);
124
Mark Slee8d7e1f62006-06-07 06:48:56 +0000125 /**
126 * Reading functions
127 */
128
Marc Slemko16698852006-08-04 03:16:10 +0000129
Mark Slee4af6ed72006-10-25 19:02:49 +0000130 uint32_t readMessageBegin(std::string& name,
David Reiss96d23882007-07-26 21:10:32 +0000131 TMessageType& messageType,
132 int32_t& seqid);
Marc Slemko16698852006-08-04 03:16:10 +0000133
Mark Slee4af6ed72006-10-25 19:02:49 +0000134 uint32_t readMessageEnd();
Marc Slemko16698852006-08-04 03:16:10 +0000135
Mark Slee4af6ed72006-10-25 19:02:49 +0000136 uint32_t readStructBegin(std::string& name);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000137
Mark Slee4af6ed72006-10-25 19:02:49 +0000138 uint32_t readStructEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000139
Mark Slee4af6ed72006-10-25 19:02:49 +0000140 uint32_t readFieldBegin(std::string& name,
David Reiss96d23882007-07-26 21:10:32 +0000141 TType& fieldType,
142 int16_t& fieldId);
David Reiss0c90f6f2008-02-06 22:18:40 +0000143
Mark Slee4af6ed72006-10-25 19:02:49 +0000144 uint32_t readFieldEnd();
David Reiss0c90f6f2008-02-06 22:18:40 +0000145
Mark Slee4af6ed72006-10-25 19:02:49 +0000146 uint32_t readMapBegin(TType& keyType,
David Reiss96d23882007-07-26 21:10:32 +0000147 TType& valType,
148 uint32_t& size);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000149
Mark Slee4af6ed72006-10-25 19:02:49 +0000150 uint32_t readMapEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000151
Mark Slee4af6ed72006-10-25 19:02:49 +0000152 uint32_t readListBegin(TType& elemType,
153 uint32_t& size);
David Reiss0c90f6f2008-02-06 22:18:40 +0000154
Mark Slee4af6ed72006-10-25 19:02:49 +0000155 uint32_t readListEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000156
Mark Slee4af6ed72006-10-25 19:02:49 +0000157 uint32_t readSetBegin(TType& elemType,
David Reiss96d23882007-07-26 21:10:32 +0000158 uint32_t& size);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000159
Mark Slee4af6ed72006-10-25 19:02:49 +0000160 uint32_t readSetEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000161
Mark Slee4af6ed72006-10-25 19:02:49 +0000162 uint32_t readBool(bool& value);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000163
Mark Slee4af6ed72006-10-25 19:02:49 +0000164 uint32_t readByte(int8_t& byte);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000165
Mark Slee4af6ed72006-10-25 19:02:49 +0000166 uint32_t readI16(int16_t& i16);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000167
Mark Slee4af6ed72006-10-25 19:02:49 +0000168 uint32_t readI32(int32_t& i32);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000169
Mark Slee4af6ed72006-10-25 19:02:49 +0000170 uint32_t readI64(int64_t& i64);
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000171
Mark Slee4af6ed72006-10-25 19:02:49 +0000172 uint32_t readDouble(double& dub);
Mark Sleec98d0502006-09-06 02:42:25 +0000173
Mark Slee4af6ed72006-10-25 19:02:49 +0000174 uint32_t readString(std::string& str);
Mark Sleef9831082007-02-20 20:59:21 +0000175
David Reissc005b1b2008-02-15 01:38:18 +0000176 uint32_t readBinary(std::string& str);
177
Mark Slee808454e2007-06-20 21:51:57 +0000178 protected:
179 uint32_t readStringBody(std::string& str, int32_t sz);
180
Mark Sleef9831082007-02-20 20:59:21 +0000181 int32_t string_limit_;
182 int32_t container_limit_;
183
Mark Slee808454e2007-06-20 21:51:57 +0000184 // Enforce presence of version identifier
185 bool strict_read_;
186 bool strict_write_;
187
Mark Sleef9831082007-02-20 20:59:21 +0000188 // Buffer for reading strings, save for the lifetime of the protocol to
189 // avoid memory churn allocating memory on every string read
190 uint8_t* string_buf_;
191 int32_t string_buf_size_;
192
Mark Slee4af6ed72006-10-25 19:02:49 +0000193};
194
195/**
196 * Constructs binary protocol handlers
197 */
198class TBinaryProtocolFactory : public TProtocolFactory {
199 public:
Mark Sleef9831082007-02-20 20:59:21 +0000200 TBinaryProtocolFactory() :
201 string_limit_(0),
Mark Slee808454e2007-06-20 21:51:57 +0000202 container_limit_(0),
203 strict_read_(false),
204 strict_write_(true) {}
Mark Sleef9831082007-02-20 20:59:21 +0000205
Mark Slee808454e2007-06-20 21:51:57 +0000206 TBinaryProtocolFactory(int32_t string_limit, int32_t container_limit, bool strict_read, bool strict_write) :
Mark Sleef9831082007-02-20 20:59:21 +0000207 string_limit_(string_limit),
Mark Slee808454e2007-06-20 21:51:57 +0000208 container_limit_(container_limit),
209 strict_read_(strict_read),
210 strict_write_(strict_write) {}
Mark Slee4af6ed72006-10-25 19:02:49 +0000211
212 virtual ~TBinaryProtocolFactory() {}
213
Mark Sleef9831082007-02-20 20:59:21 +0000214 void setStringSizeLimit(int32_t string_limit) {
215 string_limit_ = string_limit;
Mark Slee4af6ed72006-10-25 19:02:49 +0000216 }
Mark Sleef9831082007-02-20 20:59:21 +0000217
218 void setContainerSizeLimit(int32_t container_limit) {
219 container_limit_ = container_limit;
220 }
221
Mark Slee808454e2007-06-20 21:51:57 +0000222 void setStrict(bool strict_read, bool strict_write) {
223 strict_read_ = strict_read;
224 strict_write_ = strict_write;
225 }
226
Mark Sleef9831082007-02-20 20:59:21 +0000227 boost::shared_ptr<TProtocol> getProtocol(boost::shared_ptr<TTransport> trans) {
Mark Slee808454e2007-06-20 21:51:57 +0000228 return boost::shared_ptr<TProtocol>(new TBinaryProtocol(trans, string_limit_, container_limit_, strict_read_, strict_write_));
Mark Sleef9831082007-02-20 20:59:21 +0000229 }
230
231 private:
232 int32_t string_limit_;
233 int32_t container_limit_;
Mark Slee808454e2007-06-20 21:51:57 +0000234 bool strict_read_;
235 bool strict_write_;
Mark Sleef9831082007-02-20 20:59:21 +0000236
Mark Sleee8540632006-05-30 09:24:40 +0000237};
238
T Jake Lucianib5e62212009-01-31 22:36:20 +0000239}}} // apache::thrift::protocol
Marc Slemko6f038a72006-08-03 18:58:09 +0000240
Mark Sleef5f2be42006-09-05 21:05:31 +0000241#endif // #ifndef _THRIFT_PROTOCOL_TBINARYPROTOCOL_H_