blob: e0650cf678bda605c5025542e33893011d1de84a [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
Roger Meier4285ba22013-06-10 21:17:23 +020023#include <thrift/protocol/TProtocol.h>
24#include <thrift/protocol/TVirtualProtocol.h>
Marc Slemko16698852006-08-04 03:16:10 +000025
26#include <boost/shared_ptr.hpp>
Mark Sleee8540632006-05-30 09:24:40 +000027
Konrad Grochowski16a23a62014-11-13 15:33:38 +010028namespace apache {
29namespace thrift {
30namespace protocol {
Marc Slemko6f038a72006-08-03 18:58:09 +000031
Mark Sleee8540632006-05-30 09:24:40 +000032/**
33 * The default binary protocol for thrift. Writes all data in a very basic
34 * binary format, essentially just spitting out the raw bytes.
35 *
Mark Sleee8540632006-05-30 09:24:40 +000036 */
Ben Craig384f9762015-07-08 20:33:03 -050037template <class Transport_, class ByteOrder_ = TNetworkBigEndian>
38class TBinaryProtocolT : public TVirtualProtocol<TBinaryProtocolT<Transport_, ByteOrder_> > {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010039protected:
Roger Meier406fc742011-12-08 11:32:21 +000040 static const int32_t VERSION_MASK = ((int32_t)0xffff0000);
41 static const int32_t VERSION_1 = ((int32_t)0x80010000);
Randy Abernethy8dbe5f62015-08-01 22:57:02 -070042 // VERSION_2 (0x80020000) was taken by TDenseProtocol (which has since been removed)
Mark Slee808454e2007-06-20 21:51:57 +000043
Konrad Grochowski16a23a62014-11-13 15:33:38 +010044public:
45 TBinaryProtocolT(boost::shared_ptr<Transport_> trans)
Ben Craig384f9762015-07-08 20:33:03 -050046 : TVirtualProtocol<TBinaryProtocolT<Transport_, ByteOrder_> >(trans),
Konrad Grochowski16a23a62014-11-13 15:33:38 +010047 trans_(trans.get()),
48 string_limit_(0),
49 container_limit_(0),
50 strict_read_(false),
Konrad Grochowski37b7a0a2014-12-04 22:35:39 +010051 strict_write_(true) {}
Mark Slee4af6ed72006-10-25 19:02:49 +000052
David Reisse71115b2010-10-06 17:09:56 +000053 TBinaryProtocolT(boost::shared_ptr<Transport_> trans,
54 int32_t string_limit,
55 int32_t container_limit,
56 bool strict_read,
Konrad Grochowski16a23a62014-11-13 15:33:38 +010057 bool strict_write)
Ben Craig384f9762015-07-08 20:33:03 -050058 : TVirtualProtocol<TBinaryProtocolT<Transport_, ByteOrder_> >(trans),
Konrad Grochowski16a23a62014-11-13 15:33:38 +010059 trans_(trans.get()),
60 string_limit_(string_limit),
61 container_limit_(container_limit),
62 strict_read_(strict_read),
Konrad Grochowski37b7a0a2014-12-04 22:35:39 +010063 strict_write_(strict_write) {}
Mark Sleef9831082007-02-20 20:59:21 +000064
Konrad Grochowski16a23a62014-11-13 15:33:38 +010065 void setStringSizeLimit(int32_t string_limit) { string_limit_ = string_limit; }
Mark Sleef9831082007-02-20 20:59:21 +000066
Konrad Grochowski16a23a62014-11-13 15:33:38 +010067 void setContainerSizeLimit(int32_t container_limit) { container_limit_ = container_limit; }
Mark Sleee8540632006-05-30 09:24:40 +000068
Mark Slee808454e2007-06-20 21:51:57 +000069 void setStrict(bool strict_read, bool strict_write) {
70 strict_read_ = strict_read;
71 strict_write_ = strict_write;
72 }
73
Mark Slee8d7e1f62006-06-07 06:48:56 +000074 /**
75 * Writing functions.
76 */
Mark Sleee8540632006-05-30 09:24:40 +000077
David Reisse71115b2010-10-06 17:09:56 +000078 /*ol*/ uint32_t writeMessageBegin(const std::string& name,
79 const TMessageType messageType,
80 const int32_t seqid);
Marc Slemko16698852006-08-04 03:16:10 +000081
David Reisse71115b2010-10-06 17:09:56 +000082 /*ol*/ uint32_t writeMessageEnd();
Marc Slemko16698852006-08-04 03:16:10 +000083
David Reisse71115b2010-10-06 17:09:56 +000084 inline uint32_t writeStructBegin(const char* name);
Mark Sleee8540632006-05-30 09:24:40 +000085
David Reisse71115b2010-10-06 17:09:56 +000086 inline uint32_t writeStructEnd();
Mark Sleee8540632006-05-30 09:24:40 +000087
Konrad Grochowski16a23a62014-11-13 15:33:38 +010088 inline uint32_t writeFieldBegin(const char* name, const TType fieldType, const int16_t fieldId);
Mark Slee8d7e1f62006-06-07 06:48:56 +000089
David Reisse71115b2010-10-06 17:09:56 +000090 inline uint32_t writeFieldEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +000091
David Reisse71115b2010-10-06 17:09:56 +000092 inline uint32_t writeFieldStop();
David Reiss0c90f6f2008-02-06 22:18:40 +000093
Konrad Grochowski16a23a62014-11-13 15:33:38 +010094 inline uint32_t writeMapBegin(const TType keyType, const TType valType, const uint32_t size);
Mark Slee8d7e1f62006-06-07 06:48:56 +000095
David Reisse71115b2010-10-06 17:09:56 +000096 inline uint32_t writeMapEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +000097
David Reisse71115b2010-10-06 17:09:56 +000098 inline uint32_t writeListBegin(const TType elemType, const uint32_t size);
Mark Slee8d7e1f62006-06-07 06:48:56 +000099
David Reisse71115b2010-10-06 17:09:56 +0000100 inline uint32_t writeListEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000101
David Reisse71115b2010-10-06 17:09:56 +0000102 inline uint32_t writeSetBegin(const TType elemType, const uint32_t size);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000103
David Reisse71115b2010-10-06 17:09:56 +0000104 inline uint32_t writeSetEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000105
David Reisse71115b2010-10-06 17:09:56 +0000106 inline uint32_t writeBool(const bool value);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000107
David Reisse71115b2010-10-06 17:09:56 +0000108 inline uint32_t writeByte(const int8_t byte);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000109
David Reisse71115b2010-10-06 17:09:56 +0000110 inline uint32_t writeI16(const int16_t i16);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000111
David Reisse71115b2010-10-06 17:09:56 +0000112 inline uint32_t writeI32(const int32_t i32);
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000113
David Reisse71115b2010-10-06 17:09:56 +0000114 inline uint32_t writeI64(const int64_t i64);
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000115
David Reisse71115b2010-10-06 17:09:56 +0000116 inline uint32_t writeDouble(const double dub);
Mark Sleec98d0502006-09-06 02:42:25 +0000117
Jake Farrellf42ae012012-06-22 03:22:53 +0000118 template <typename StrType>
119 inline uint32_t writeString(const StrType& str);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000120
David Reisse71115b2010-10-06 17:09:56 +0000121 inline uint32_t writeBinary(const std::string& str);
David Reissc005b1b2008-02-15 01:38:18 +0000122
Mark Slee8d7e1f62006-06-07 06:48:56 +0000123 /**
124 * Reading functions
125 */
126
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100127 /*ol*/ uint32_t readMessageBegin(std::string& name, TMessageType& messageType, int32_t& seqid);
Marc Slemko16698852006-08-04 03:16:10 +0000128
David Reisse71115b2010-10-06 17:09:56 +0000129 /*ol*/ uint32_t readMessageEnd();
Marc Slemko16698852006-08-04 03:16:10 +0000130
David Reisse71115b2010-10-06 17:09:56 +0000131 inline uint32_t readStructBegin(std::string& name);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000132
David Reisse71115b2010-10-06 17:09:56 +0000133 inline uint32_t readStructEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000134
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100135 inline uint32_t readFieldBegin(std::string& name, TType& fieldType, int16_t& fieldId);
David Reiss0c90f6f2008-02-06 22:18:40 +0000136
David Reisse71115b2010-10-06 17:09:56 +0000137 inline uint32_t readFieldEnd();
David Reiss0c90f6f2008-02-06 22:18:40 +0000138
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100139 inline uint32_t readMapBegin(TType& keyType, TType& valType, uint32_t& size);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000140
David Reisse71115b2010-10-06 17:09:56 +0000141 inline uint32_t readMapEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000142
David Reisse71115b2010-10-06 17:09:56 +0000143 inline uint32_t readListBegin(TType& elemType, uint32_t& size);
David Reiss0c90f6f2008-02-06 22:18:40 +0000144
David Reisse71115b2010-10-06 17:09:56 +0000145 inline uint32_t readListEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000146
David Reisse71115b2010-10-06 17:09:56 +0000147 inline uint32_t readSetBegin(TType& elemType, uint32_t& size);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000148
David Reisse71115b2010-10-06 17:09:56 +0000149 inline uint32_t readSetEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000150
David Reisse71115b2010-10-06 17:09:56 +0000151 inline uint32_t readBool(bool& value);
David Reiss8dfc7322010-10-06 17:09:58 +0000152 // Provide the default readBool() implementation for std::vector<bool>
Ben Craig384f9762015-07-08 20:33:03 -0500153 using TVirtualProtocol<TBinaryProtocolT<Transport_, ByteOrder_> >::readBool;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000154
David Reisse71115b2010-10-06 17:09:56 +0000155 inline uint32_t readByte(int8_t& byte);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000156
David Reisse71115b2010-10-06 17:09:56 +0000157 inline uint32_t readI16(int16_t& i16);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000158
David Reisse71115b2010-10-06 17:09:56 +0000159 inline uint32_t readI32(int32_t& i32);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000160
David Reisse71115b2010-10-06 17:09:56 +0000161 inline uint32_t readI64(int64_t& i64);
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000162
David Reisse71115b2010-10-06 17:09:56 +0000163 inline uint32_t readDouble(double& dub);
Mark Sleec98d0502006-09-06 02:42:25 +0000164
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100165 template <typename StrType>
Jake Farrellf42ae012012-06-22 03:22:53 +0000166 inline uint32_t readString(StrType& str);
Mark Sleef9831082007-02-20 20:59:21 +0000167
David Reisse71115b2010-10-06 17:09:56 +0000168 inline uint32_t readBinary(std::string& str);
David Reissc005b1b2008-02-15 01:38:18 +0000169
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100170protected:
171 template <typename StrType>
Jake Farrellf42ae012012-06-22 03:22:53 +0000172 uint32_t readStringBody(StrType& str, int32_t sz);
Mark Slee808454e2007-06-20 21:51:57 +0000173
David Reisse71115b2010-10-06 17:09:56 +0000174 Transport_* trans_;
175
Mark Sleef9831082007-02-20 20:59:21 +0000176 int32_t string_limit_;
177 int32_t container_limit_;
178
Mark Slee808454e2007-06-20 21:51:57 +0000179 // Enforce presence of version identifier
180 bool strict_read_;
181 bool strict_write_;
Mark Slee4af6ed72006-10-25 19:02:49 +0000182};
183
David Reisse71115b2010-10-06 17:09:56 +0000184typedef TBinaryProtocolT<TTransport> TBinaryProtocol;
Ben Craig384f9762015-07-08 20:33:03 -0500185typedef TBinaryProtocolT<TTransport, TNetworkLittleEndian> TLEBinaryProtocol;
David Reisse71115b2010-10-06 17:09:56 +0000186
Mark Slee4af6ed72006-10-25 19:02:49 +0000187/**
188 * Constructs binary protocol handlers
189 */
Ben Craig384f9762015-07-08 20:33:03 -0500190template <class Transport_, class ByteOrder_ = TNetworkBigEndian>
David Reisse71115b2010-10-06 17:09:56 +0000191class TBinaryProtocolFactoryT : public TProtocolFactory {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100192public:
193 TBinaryProtocolFactoryT()
194 : string_limit_(0), container_limit_(0), strict_read_(false), strict_write_(true) {}
Mark Sleef9831082007-02-20 20:59:21 +0000195
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100196 TBinaryProtocolFactoryT(int32_t string_limit,
197 int32_t container_limit,
198 bool strict_read,
199 bool strict_write)
200 : string_limit_(string_limit),
201 container_limit_(container_limit),
202 strict_read_(strict_read),
203 strict_write_(strict_write) {}
Mark Slee4af6ed72006-10-25 19:02:49 +0000204
David Reisse71115b2010-10-06 17:09:56 +0000205 virtual ~TBinaryProtocolFactoryT() {}
Mark Slee4af6ed72006-10-25 19:02:49 +0000206
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100207 void setStringSizeLimit(int32_t string_limit) { string_limit_ = string_limit; }
Mark Sleef9831082007-02-20 20:59:21 +0000208
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100209 void setContainerSizeLimit(int32_t container_limit) { container_limit_ = container_limit; }
Mark Sleef9831082007-02-20 20:59:21 +0000210
Mark Slee808454e2007-06-20 21:51:57 +0000211 void setStrict(bool strict_read, bool strict_write) {
212 strict_read_ = strict_read;
213 strict_write_ = strict_write;
214 }
215
Mark Sleef9831082007-02-20 20:59:21 +0000216 boost::shared_ptr<TProtocol> getProtocol(boost::shared_ptr<TTransport> trans) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100217 boost::shared_ptr<Transport_> specific_trans = boost::dynamic_pointer_cast<Transport_>(trans);
David Reisse71115b2010-10-06 17:09:56 +0000218 TProtocol* prot;
219 if (specific_trans) {
Ben Craig384f9762015-07-08 20:33:03 -0500220 prot = new TBinaryProtocolT<Transport_, ByteOrder_>(
221 specific_trans,
222 string_limit_,
223 container_limit_,
224 strict_read_,
225 strict_write_);
David Reisse71115b2010-10-06 17:09:56 +0000226 } else {
Ben Craig384f9762015-07-08 20:33:03 -0500227 prot = new TBinaryProtocolT<TTransport, ByteOrder_>(
228 trans,
229 string_limit_,
230 container_limit_,
231 strict_read_,
232 strict_write_);
David Reisse71115b2010-10-06 17:09:56 +0000233 }
234
235 return boost::shared_ptr<TProtocol>(prot);
Mark Sleef9831082007-02-20 20:59:21 +0000236 }
237
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100238private:
Mark Sleef9831082007-02-20 20:59:21 +0000239 int32_t string_limit_;
240 int32_t container_limit_;
Mark Slee808454e2007-06-20 21:51:57 +0000241 bool strict_read_;
242 bool strict_write_;
Mark Sleee8540632006-05-30 09:24:40 +0000243};
244
David Reisse71115b2010-10-06 17:09:56 +0000245typedef TBinaryProtocolFactoryT<TTransport> TBinaryProtocolFactory;
Ben Craig384f9762015-07-08 20:33:03 -0500246typedef TBinaryProtocolFactoryT<TTransport, TNetworkLittleEndian> TLEBinaryProtocolFactory;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100247}
248}
249} // apache::thrift::protocol
Marc Slemko6f038a72006-08-03 18:58:09 +0000250
Roger Meier4285ba22013-06-10 21:17:23 +0200251#include <thrift/protocol/TBinaryProtocol.tcc>
David Reisse71115b2010-10-06 17:09:56 +0000252
Mark Sleef5f2be42006-09-05 21:05:31 +0000253#endif // #ifndef _THRIFT_PROTOCOL_TBINARYPROTOCOL_H_