blob: edb8834f90f8589b016604a382be97ab4951a282 [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);
David Reiss8dfc7322010-10-06 17:09:58 +0000177 // Provide the default readBool() implementation for std::vector<bool>
178 using TVirtualProtocol< TBinaryProtocolT<Transport_> >::readBool;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000179
David Reisse71115b2010-10-06 17:09:56 +0000180 inline uint32_t readByte(int8_t& byte);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000181
David Reisse71115b2010-10-06 17:09:56 +0000182 inline uint32_t readI16(int16_t& i16);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000183
David Reisse71115b2010-10-06 17:09:56 +0000184 inline uint32_t readI32(int32_t& i32);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000185
David Reisse71115b2010-10-06 17:09:56 +0000186 inline uint32_t readI64(int64_t& i64);
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000187
David Reisse71115b2010-10-06 17:09:56 +0000188 inline uint32_t readDouble(double& dub);
Mark Sleec98d0502006-09-06 02:42:25 +0000189
David Reisse71115b2010-10-06 17:09:56 +0000190 inline uint32_t readString(std::string& str);
Mark Sleef9831082007-02-20 20:59:21 +0000191
David Reisse71115b2010-10-06 17:09:56 +0000192 inline uint32_t readBinary(std::string& str);
David Reissc005b1b2008-02-15 01:38:18 +0000193
Mark Slee808454e2007-06-20 21:51:57 +0000194 protected:
195 uint32_t readStringBody(std::string& str, int32_t sz);
196
David Reisse71115b2010-10-06 17:09:56 +0000197 Transport_* trans_;
198
Mark Sleef9831082007-02-20 20:59:21 +0000199 int32_t string_limit_;
200 int32_t container_limit_;
201
Mark Slee808454e2007-06-20 21:51:57 +0000202 // Enforce presence of version identifier
203 bool strict_read_;
204 bool strict_write_;
205
Mark Sleef9831082007-02-20 20:59:21 +0000206 // Buffer for reading strings, save for the lifetime of the protocol to
207 // avoid memory churn allocating memory on every string read
208 uint8_t* string_buf_;
209 int32_t string_buf_size_;
210
Mark Slee4af6ed72006-10-25 19:02:49 +0000211};
212
David Reisse71115b2010-10-06 17:09:56 +0000213typedef TBinaryProtocolT<TTransport> TBinaryProtocol;
214
Mark Slee4af6ed72006-10-25 19:02:49 +0000215/**
216 * Constructs binary protocol handlers
217 */
David Reisse71115b2010-10-06 17:09:56 +0000218template <class Transport_>
219class TBinaryProtocolFactoryT : public TProtocolFactory {
Mark Slee4af6ed72006-10-25 19:02:49 +0000220 public:
David Reisse71115b2010-10-06 17:09:56 +0000221 TBinaryProtocolFactoryT() :
Mark Sleef9831082007-02-20 20:59:21 +0000222 string_limit_(0),
Mark Slee808454e2007-06-20 21:51:57 +0000223 container_limit_(0),
224 strict_read_(false),
225 strict_write_(true) {}
Mark Sleef9831082007-02-20 20:59:21 +0000226
David Reisse71115b2010-10-06 17:09:56 +0000227 TBinaryProtocolFactoryT(int32_t string_limit, int32_t container_limit,
228 bool strict_read, bool strict_write) :
Mark Sleef9831082007-02-20 20:59:21 +0000229 string_limit_(string_limit),
Mark Slee808454e2007-06-20 21:51:57 +0000230 container_limit_(container_limit),
231 strict_read_(strict_read),
232 strict_write_(strict_write) {}
Mark Slee4af6ed72006-10-25 19:02:49 +0000233
David Reisse71115b2010-10-06 17:09:56 +0000234 virtual ~TBinaryProtocolFactoryT() {}
Mark Slee4af6ed72006-10-25 19:02:49 +0000235
Mark Sleef9831082007-02-20 20:59:21 +0000236 void setStringSizeLimit(int32_t string_limit) {
237 string_limit_ = string_limit;
Mark Slee4af6ed72006-10-25 19:02:49 +0000238 }
Mark Sleef9831082007-02-20 20:59:21 +0000239
240 void setContainerSizeLimit(int32_t container_limit) {
241 container_limit_ = container_limit;
242 }
243
Mark Slee808454e2007-06-20 21:51:57 +0000244 void setStrict(bool strict_read, bool strict_write) {
245 strict_read_ = strict_read;
246 strict_write_ = strict_write;
247 }
248
Mark Sleef9831082007-02-20 20:59:21 +0000249 boost::shared_ptr<TProtocol> getProtocol(boost::shared_ptr<TTransport> trans) {
David Reisse71115b2010-10-06 17:09:56 +0000250 boost::shared_ptr<Transport_> specific_trans =
251 boost::dynamic_pointer_cast<Transport_>(trans);
252 TProtocol* prot;
253 if (specific_trans) {
254 prot = new TBinaryProtocolT<Transport_>(specific_trans, string_limit_,
255 container_limit_, strict_read_,
256 strict_write_);
257 } else {
258 prot = new TBinaryProtocol(trans, string_limit_, container_limit_,
259 strict_read_, strict_write_);
260 }
261
262 return boost::shared_ptr<TProtocol>(prot);
Mark Sleef9831082007-02-20 20:59:21 +0000263 }
264
265 private:
266 int32_t string_limit_;
267 int32_t container_limit_;
Mark Slee808454e2007-06-20 21:51:57 +0000268 bool strict_read_;
269 bool strict_write_;
Mark Sleef9831082007-02-20 20:59:21 +0000270
Mark Sleee8540632006-05-30 09:24:40 +0000271};
272
David Reisse71115b2010-10-06 17:09:56 +0000273typedef TBinaryProtocolFactoryT<TTransport> TBinaryProtocolFactory;
274
T Jake Lucianib5e62212009-01-31 22:36:20 +0000275}}} // apache::thrift::protocol
Marc Slemko6f038a72006-08-03 18:58:09 +0000276
David Reisse71115b2010-10-06 17:09:56 +0000277#include "TBinaryProtocol.tcc"
278
Mark Sleef5f2be42006-09-05 21:05:31 +0000279#endif // #ifndef _THRIFT_PROTOCOL_TBINARYPROTOCOL_H_