blob: ca45294ea5f71b0728c97de1a0cd3264092dde27 [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
Mark Slee9f0c6512007-02-28 23:58:26 +000019
Mark Sleef5f2be42006-09-05 21:05:31 +000020#ifndef _THRIFT_PROTOCOL_TBINARYPROTOCOL_H_
21#define _THRIFT_PROTOCOL_TBINARYPROTOCOL_H_ 1
Mark Sleee8540632006-05-30 09:24:40 +000022
Marc Slemkod42a2c22006-08-10 03:30:18 +000023#include "TProtocol.h"
David Reiss6806fb82010-10-06 17:09:52 +000024#include "TVirtualProtocol.h"
Marc Slemko16698852006-08-04 03:16:10 +000025
26#include <boost/shared_ptr.hpp>
Mark Sleee8540632006-05-30 09:24:40 +000027
T Jake Lucianib5e62212009-01-31 22:36:20 +000028namespace apache { namespace thrift { namespace protocol {
Marc Slemko6f038a72006-08-03 18:58:09 +000029
Mark Sleee8540632006-05-30 09:24:40 +000030/**
31 * The default binary protocol for thrift. Writes all data in a very basic
32 * binary format, essentially just spitting out the raw bytes.
33 *
Mark Sleee8540632006-05-30 09:24:40 +000034 */
David Reisse71115b2010-10-06 17:09:56 +000035template <class Transport_>
36class TBinaryProtocolT
37 : public TVirtualProtocol< TBinaryProtocolT<Transport_> > {
Mark Slee808454e2007-06-20 21:51:57 +000038 protected:
39 static const int32_t VERSION_MASK = 0xffff0000;
40 static const int32_t VERSION_1 = 0x80010000;
David Reiss4e7530d2007-09-04 21:49:53 +000041 // VERSION_2 (0x80020000) is taken by TDenseProtocol.
Mark Slee808454e2007-06-20 21:51:57 +000042
Marc Slemko0b4ffa92006-08-11 02:49:29 +000043 public:
David Reisse71115b2010-10-06 17:09:56 +000044 TBinaryProtocolT(boost::shared_ptr<Transport_> trans) :
45 TVirtualProtocol< TBinaryProtocolT<Transport_> >(trans),
46 trans_(trans.get()),
Mark Sleef9831082007-02-20 20:59:21 +000047 string_limit_(0),
48 container_limit_(0),
Mark Slee808454e2007-06-20 21:51:57 +000049 strict_read_(false),
50 strict_write_(true),
Mark Sleef9831082007-02-20 20:59:21 +000051 string_buf_(NULL),
52 string_buf_size_(0) {}
Mark Slee4af6ed72006-10-25 19:02:49 +000053
David Reisse71115b2010-10-06 17:09:56 +000054 TBinaryProtocolT(boost::shared_ptr<Transport_> trans,
55 int32_t string_limit,
56 int32_t container_limit,
57 bool strict_read,
58 bool strict_write) :
59 TVirtualProtocol< TBinaryProtocolT<Transport_> >(trans),
60 trans_(trans.get()),
Mark Sleef9831082007-02-20 20:59:21 +000061 string_limit_(string_limit),
62 container_limit_(container_limit),
Mark Slee808454e2007-06-20 21:51:57 +000063 strict_read_(strict_read),
64 strict_write_(strict_write),
Mark Sleef9831082007-02-20 20:59:21 +000065 string_buf_(NULL),
66 string_buf_size_(0) {}
67
David Reisse71115b2010-10-06 17:09:56 +000068 ~TBinaryProtocolT() {
Mark Sleef9831082007-02-20 20:59:21 +000069 if (string_buf_ != NULL) {
David Reissd7a16f42008-02-19 22:47:29 +000070 std::free(string_buf_);
Mark Sleef9831082007-02-20 20:59:21 +000071 string_buf_size_ = 0;
72 }
73 }
74
75 void setStringSizeLimit(int32_t string_limit) {
76 string_limit_ = string_limit;
77 }
78
79 void setContainerSizeLimit(int32_t container_limit) {
80 container_limit_ = container_limit;
81 }
Mark Sleee8540632006-05-30 09:24:40 +000082
Mark Slee808454e2007-06-20 21:51:57 +000083 void setStrict(bool strict_read, bool strict_write) {
84 strict_read_ = strict_read;
85 strict_write_ = strict_write;
86 }
87
Mark Slee8d7e1f62006-06-07 06:48:56 +000088 /**
89 * Writing functions.
90 */
Mark Sleee8540632006-05-30 09:24:40 +000091
David Reisse71115b2010-10-06 17:09:56 +000092 /*ol*/ uint32_t writeMessageBegin(const std::string& name,
93 const TMessageType messageType,
94 const int32_t seqid);
Marc Slemko16698852006-08-04 03:16:10 +000095
David Reisse71115b2010-10-06 17:09:56 +000096 /*ol*/ uint32_t writeMessageEnd();
Marc Slemko16698852006-08-04 03:16:10 +000097
98
David Reisse71115b2010-10-06 17:09:56 +000099 inline uint32_t writeStructBegin(const char* name);
Mark Sleee8540632006-05-30 09:24:40 +0000100
David Reisse71115b2010-10-06 17:09:56 +0000101 inline uint32_t writeStructEnd();
Mark Sleee8540632006-05-30 09:24:40 +0000102
David Reisse71115b2010-10-06 17:09:56 +0000103 inline uint32_t writeFieldBegin(const char* name,
104 const TType fieldType,
105 const int16_t fieldId);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000106
David Reisse71115b2010-10-06 17:09:56 +0000107 inline uint32_t writeFieldEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000108
David Reisse71115b2010-10-06 17:09:56 +0000109 inline uint32_t writeFieldStop();
David Reiss0c90f6f2008-02-06 22:18:40 +0000110
David Reisse71115b2010-10-06 17:09:56 +0000111 inline uint32_t writeMapBegin(const TType keyType,
112 const TType valType,
113 const uint32_t size);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000114
David Reisse71115b2010-10-06 17:09:56 +0000115 inline uint32_t writeMapEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000116
David Reisse71115b2010-10-06 17:09:56 +0000117 inline uint32_t writeListBegin(const TType elemType, const uint32_t size);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000118
David Reisse71115b2010-10-06 17:09:56 +0000119 inline uint32_t writeListEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000120
David Reisse71115b2010-10-06 17:09:56 +0000121 inline uint32_t writeSetBegin(const TType elemType, const uint32_t size);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000122
David Reisse71115b2010-10-06 17:09:56 +0000123 inline uint32_t writeSetEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000124
David Reisse71115b2010-10-06 17:09:56 +0000125 inline uint32_t writeBool(const bool value);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000126
David Reisse71115b2010-10-06 17:09:56 +0000127 inline uint32_t writeByte(const int8_t byte);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000128
David Reisse71115b2010-10-06 17:09:56 +0000129 inline uint32_t writeI16(const int16_t i16);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000130
David Reisse71115b2010-10-06 17:09:56 +0000131 inline uint32_t writeI32(const int32_t i32);
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000132
David Reisse71115b2010-10-06 17:09:56 +0000133 inline uint32_t writeI64(const int64_t i64);
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000134
David Reisse71115b2010-10-06 17:09:56 +0000135 inline uint32_t writeDouble(const double dub);
Mark Sleec98d0502006-09-06 02:42:25 +0000136
David Reisse71115b2010-10-06 17:09:56 +0000137 inline uint32_t writeString(const std::string& str);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000138
David Reisse71115b2010-10-06 17:09:56 +0000139 inline uint32_t writeBinary(const std::string& str);
David Reissc005b1b2008-02-15 01:38:18 +0000140
Mark Slee8d7e1f62006-06-07 06:48:56 +0000141 /**
142 * Reading functions
143 */
144
Marc Slemko16698852006-08-04 03:16:10 +0000145
David Reisse71115b2010-10-06 17:09:56 +0000146 /*ol*/ uint32_t readMessageBegin(std::string& name,
147 TMessageType& messageType,
148 int32_t& seqid);
Marc Slemko16698852006-08-04 03:16:10 +0000149
David Reisse71115b2010-10-06 17:09:56 +0000150 /*ol*/ uint32_t readMessageEnd();
Marc Slemko16698852006-08-04 03:16:10 +0000151
David Reisse71115b2010-10-06 17:09:56 +0000152 inline uint32_t readStructBegin(std::string& name);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000153
David Reisse71115b2010-10-06 17:09:56 +0000154 inline uint32_t readStructEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000155
David Reisse71115b2010-10-06 17:09:56 +0000156 inline uint32_t readFieldBegin(std::string& name,
157 TType& fieldType,
158 int16_t& fieldId);
David Reiss0c90f6f2008-02-06 22:18:40 +0000159
David Reisse71115b2010-10-06 17:09:56 +0000160 inline uint32_t readFieldEnd();
David Reiss0c90f6f2008-02-06 22:18:40 +0000161
David Reisse71115b2010-10-06 17:09:56 +0000162 inline uint32_t readMapBegin(TType& keyType,
163 TType& valType,
164 uint32_t& size);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000165
David Reisse71115b2010-10-06 17:09:56 +0000166 inline uint32_t readMapEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000167
David Reisse71115b2010-10-06 17:09:56 +0000168 inline uint32_t readListBegin(TType& elemType, uint32_t& size);
David Reiss0c90f6f2008-02-06 22:18:40 +0000169
David Reisse71115b2010-10-06 17:09:56 +0000170 inline uint32_t readListEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000171
David Reisse71115b2010-10-06 17:09:56 +0000172 inline uint32_t readSetBegin(TType& elemType, uint32_t& size);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000173
David Reisse71115b2010-10-06 17:09:56 +0000174 inline uint32_t readSetEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000175
David Reisse71115b2010-10-06 17:09:56 +0000176 inline uint32_t readBool(bool& value);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000177
David Reisse71115b2010-10-06 17:09:56 +0000178 inline uint32_t readByte(int8_t& byte);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000179
David Reisse71115b2010-10-06 17:09:56 +0000180 inline uint32_t readI16(int16_t& i16);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000181
David Reisse71115b2010-10-06 17:09:56 +0000182 inline uint32_t readI32(int32_t& i32);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000183
David Reisse71115b2010-10-06 17:09:56 +0000184 inline uint32_t readI64(int64_t& i64);
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000185
David Reisse71115b2010-10-06 17:09:56 +0000186 inline uint32_t readDouble(double& dub);
Mark Sleec98d0502006-09-06 02:42:25 +0000187
David Reisse71115b2010-10-06 17:09:56 +0000188 inline uint32_t readString(std::string& str);
Mark Sleef9831082007-02-20 20:59:21 +0000189
David Reisse71115b2010-10-06 17:09:56 +0000190 inline uint32_t readBinary(std::string& str);
David Reissc005b1b2008-02-15 01:38:18 +0000191
Mark Slee808454e2007-06-20 21:51:57 +0000192 protected:
193 uint32_t readStringBody(std::string& str, int32_t sz);
194
David Reisse71115b2010-10-06 17:09:56 +0000195 Transport_* trans_;
196
Mark Sleef9831082007-02-20 20:59:21 +0000197 int32_t string_limit_;
198 int32_t container_limit_;
199
Mark Slee808454e2007-06-20 21:51:57 +0000200 // Enforce presence of version identifier
201 bool strict_read_;
202 bool strict_write_;
203
Mark Sleef9831082007-02-20 20:59:21 +0000204 // Buffer for reading strings, save for the lifetime of the protocol to
205 // avoid memory churn allocating memory on every string read
206 uint8_t* string_buf_;
207 int32_t string_buf_size_;
208
Mark Slee4af6ed72006-10-25 19:02:49 +0000209};
210
David Reisse71115b2010-10-06 17:09:56 +0000211typedef TBinaryProtocolT<TTransport> TBinaryProtocol;
212
Mark Slee4af6ed72006-10-25 19:02:49 +0000213/**
214 * Constructs binary protocol handlers
215 */
David Reisse71115b2010-10-06 17:09:56 +0000216template <class Transport_>
217class TBinaryProtocolFactoryT : public TProtocolFactory {
Mark Slee4af6ed72006-10-25 19:02:49 +0000218 public:
David Reisse71115b2010-10-06 17:09:56 +0000219 TBinaryProtocolFactoryT() :
Mark Sleef9831082007-02-20 20:59:21 +0000220 string_limit_(0),
Mark Slee808454e2007-06-20 21:51:57 +0000221 container_limit_(0),
222 strict_read_(false),
223 strict_write_(true) {}
Mark Sleef9831082007-02-20 20:59:21 +0000224
David Reisse71115b2010-10-06 17:09:56 +0000225 TBinaryProtocolFactoryT(int32_t string_limit, int32_t container_limit,
226 bool strict_read, bool strict_write) :
Mark Sleef9831082007-02-20 20:59:21 +0000227 string_limit_(string_limit),
Mark Slee808454e2007-06-20 21:51:57 +0000228 container_limit_(container_limit),
229 strict_read_(strict_read),
230 strict_write_(strict_write) {}
Mark Slee4af6ed72006-10-25 19:02:49 +0000231
David Reisse71115b2010-10-06 17:09:56 +0000232 virtual ~TBinaryProtocolFactoryT() {}
Mark Slee4af6ed72006-10-25 19:02:49 +0000233
Mark Sleef9831082007-02-20 20:59:21 +0000234 void setStringSizeLimit(int32_t string_limit) {
235 string_limit_ = string_limit;
Mark Slee4af6ed72006-10-25 19:02:49 +0000236 }
Mark Sleef9831082007-02-20 20:59:21 +0000237
238 void setContainerSizeLimit(int32_t container_limit) {
239 container_limit_ = container_limit;
240 }
241
Mark Slee808454e2007-06-20 21:51:57 +0000242 void setStrict(bool strict_read, bool strict_write) {
243 strict_read_ = strict_read;
244 strict_write_ = strict_write;
245 }
246
Mark Sleef9831082007-02-20 20:59:21 +0000247 boost::shared_ptr<TProtocol> getProtocol(boost::shared_ptr<TTransport> trans) {
David Reisse71115b2010-10-06 17:09:56 +0000248 boost::shared_ptr<Transport_> specific_trans =
249 boost::dynamic_pointer_cast<Transport_>(trans);
250 TProtocol* prot;
251 if (specific_trans) {
252 prot = new TBinaryProtocolT<Transport_>(specific_trans, string_limit_,
253 container_limit_, strict_read_,
254 strict_write_);
255 } else {
256 prot = new TBinaryProtocol(trans, string_limit_, container_limit_,
257 strict_read_, strict_write_);
258 }
259
260 return boost::shared_ptr<TProtocol>(prot);
Mark Sleef9831082007-02-20 20:59:21 +0000261 }
262
263 private:
264 int32_t string_limit_;
265 int32_t container_limit_;
Mark Slee808454e2007-06-20 21:51:57 +0000266 bool strict_read_;
267 bool strict_write_;
Mark Sleef9831082007-02-20 20:59:21 +0000268
Mark Sleee8540632006-05-30 09:24:40 +0000269};
270
David Reisse71115b2010-10-06 17:09:56 +0000271typedef TBinaryProtocolFactoryT<TTransport> TBinaryProtocolFactory;
272
T Jake Lucianib5e62212009-01-31 22:36:20 +0000273}}} // apache::thrift::protocol
Marc Slemko6f038a72006-08-03 18:58:09 +0000274
David Reisse71115b2010-10-06 17:09:56 +0000275#include "TBinaryProtocol.tcc"
276
Mark Sleef5f2be42006-09-05 21:05:31 +0000277#endif // #ifndef _THRIFT_PROTOCOL_TBINARYPROTOCOL_H_