blob: 87c53f78e23940f230e8fe95e45ab575522a1318 [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_> > {
Dave Watson792db4e2015-01-16 11:22:01 -080039public:
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 +010044 TBinaryProtocolT(boost::shared_ptr<Transport_> trans)
Ben Craig384f9762015-07-08 20:33:03 -050045 : TVirtualProtocol<TBinaryProtocolT<Transport_, ByteOrder_> >(trans),
Konrad Grochowski16a23a62014-11-13 15:33:38 +010046 trans_(trans.get()),
47 string_limit_(0),
48 container_limit_(0),
49 strict_read_(false),
Konrad Grochowski37b7a0a2014-12-04 22:35:39 +010050 strict_write_(true) {}
Mark Slee4af6ed72006-10-25 19:02:49 +000051
David Reisse71115b2010-10-06 17:09:56 +000052 TBinaryProtocolT(boost::shared_ptr<Transport_> trans,
53 int32_t string_limit,
54 int32_t container_limit,
55 bool strict_read,
Konrad Grochowski16a23a62014-11-13 15:33:38 +010056 bool strict_write)
Ben Craig384f9762015-07-08 20:33:03 -050057 : TVirtualProtocol<TBinaryProtocolT<Transport_, ByteOrder_> >(trans),
Konrad Grochowski16a23a62014-11-13 15:33:38 +010058 trans_(trans.get()),
59 string_limit_(string_limit),
60 container_limit_(container_limit),
61 strict_read_(strict_read),
Konrad Grochowski37b7a0a2014-12-04 22:35:39 +010062 strict_write_(strict_write) {}
Mark Sleef9831082007-02-20 20:59:21 +000063
Konrad Grochowski16a23a62014-11-13 15:33:38 +010064 void setStringSizeLimit(int32_t string_limit) { string_limit_ = string_limit; }
Mark Sleef9831082007-02-20 20:59:21 +000065
Konrad Grochowski16a23a62014-11-13 15:33:38 +010066 void setContainerSizeLimit(int32_t container_limit) { container_limit_ = container_limit; }
Mark Sleee8540632006-05-30 09:24:40 +000067
Mark Slee808454e2007-06-20 21:51:57 +000068 void setStrict(bool strict_read, bool strict_write) {
69 strict_read_ = strict_read;
70 strict_write_ = strict_write;
71 }
72
Mark Slee8d7e1f62006-06-07 06:48:56 +000073 /**
74 * Writing functions.
75 */
Mark Sleee8540632006-05-30 09:24:40 +000076
David Reisse71115b2010-10-06 17:09:56 +000077 /*ol*/ uint32_t writeMessageBegin(const std::string& name,
78 const TMessageType messageType,
79 const int32_t seqid);
Marc Slemko16698852006-08-04 03:16:10 +000080
David Reisse71115b2010-10-06 17:09:56 +000081 /*ol*/ uint32_t writeMessageEnd();
Marc Slemko16698852006-08-04 03:16:10 +000082
David Reisse71115b2010-10-06 17:09:56 +000083 inline uint32_t writeStructBegin(const char* name);
Mark Sleee8540632006-05-30 09:24:40 +000084
David Reisse71115b2010-10-06 17:09:56 +000085 inline uint32_t writeStructEnd();
Mark Sleee8540632006-05-30 09:24:40 +000086
Konrad Grochowski16a23a62014-11-13 15:33:38 +010087 inline uint32_t writeFieldBegin(const char* name, const TType fieldType, const int16_t fieldId);
Mark Slee8d7e1f62006-06-07 06:48:56 +000088
David Reisse71115b2010-10-06 17:09:56 +000089 inline uint32_t writeFieldEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +000090
David Reisse71115b2010-10-06 17:09:56 +000091 inline uint32_t writeFieldStop();
David Reiss0c90f6f2008-02-06 22:18:40 +000092
Konrad Grochowski16a23a62014-11-13 15:33:38 +010093 inline uint32_t writeMapBegin(const TType keyType, const TType valType, const uint32_t size);
Mark Slee8d7e1f62006-06-07 06:48:56 +000094
David Reisse71115b2010-10-06 17:09:56 +000095 inline uint32_t writeMapEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +000096
David Reisse71115b2010-10-06 17:09:56 +000097 inline uint32_t writeListBegin(const TType elemType, const uint32_t size);
Mark Slee8d7e1f62006-06-07 06:48:56 +000098
David Reisse71115b2010-10-06 17:09:56 +000099 inline uint32_t writeListEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000100
David Reisse71115b2010-10-06 17:09:56 +0000101 inline uint32_t writeSetBegin(const TType elemType, const uint32_t size);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000102
David Reisse71115b2010-10-06 17:09:56 +0000103 inline uint32_t writeSetEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000104
David Reisse71115b2010-10-06 17:09:56 +0000105 inline uint32_t writeBool(const bool value);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000106
David Reisse71115b2010-10-06 17:09:56 +0000107 inline uint32_t writeByte(const int8_t byte);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000108
David Reisse71115b2010-10-06 17:09:56 +0000109 inline uint32_t writeI16(const int16_t i16);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000110
David Reisse71115b2010-10-06 17:09:56 +0000111 inline uint32_t writeI32(const int32_t i32);
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000112
David Reisse71115b2010-10-06 17:09:56 +0000113 inline uint32_t writeI64(const int64_t i64);
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000114
David Reisse71115b2010-10-06 17:09:56 +0000115 inline uint32_t writeDouble(const double dub);
Mark Sleec98d0502006-09-06 02:42:25 +0000116
Jake Farrellf42ae012012-06-22 03:22:53 +0000117 template <typename StrType>
118 inline uint32_t writeString(const StrType& str);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000119
David Reisse71115b2010-10-06 17:09:56 +0000120 inline uint32_t writeBinary(const std::string& str);
David Reissc005b1b2008-02-15 01:38:18 +0000121
Mark Slee8d7e1f62006-06-07 06:48:56 +0000122 /**
123 * Reading functions
124 */
125
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100126 /*ol*/ uint32_t readMessageBegin(std::string& name, TMessageType& messageType, int32_t& seqid);
Marc Slemko16698852006-08-04 03:16:10 +0000127
David Reisse71115b2010-10-06 17:09:56 +0000128 /*ol*/ uint32_t readMessageEnd();
Marc Slemko16698852006-08-04 03:16:10 +0000129
David Reisse71115b2010-10-06 17:09:56 +0000130 inline uint32_t readStructBegin(std::string& name);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000131
David Reisse71115b2010-10-06 17:09:56 +0000132 inline uint32_t readStructEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000133
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100134 inline uint32_t readFieldBegin(std::string& name, TType& fieldType, int16_t& fieldId);
David Reiss0c90f6f2008-02-06 22:18:40 +0000135
David Reisse71115b2010-10-06 17:09:56 +0000136 inline uint32_t readFieldEnd();
David Reiss0c90f6f2008-02-06 22:18:40 +0000137
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100138 inline uint32_t readMapBegin(TType& keyType, TType& valType, uint32_t& size);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000139
David Reisse71115b2010-10-06 17:09:56 +0000140 inline uint32_t readMapEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000141
David Reisse71115b2010-10-06 17:09:56 +0000142 inline uint32_t readListBegin(TType& elemType, uint32_t& size);
David Reiss0c90f6f2008-02-06 22:18:40 +0000143
David Reisse71115b2010-10-06 17:09:56 +0000144 inline uint32_t readListEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000145
David Reisse71115b2010-10-06 17:09:56 +0000146 inline uint32_t readSetBegin(TType& elemType, uint32_t& size);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000147
David Reisse71115b2010-10-06 17:09:56 +0000148 inline uint32_t readSetEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000149
David Reisse71115b2010-10-06 17:09:56 +0000150 inline uint32_t readBool(bool& value);
David Reiss8dfc7322010-10-06 17:09:58 +0000151 // Provide the default readBool() implementation for std::vector<bool>
Ben Craig384f9762015-07-08 20:33:03 -0500152 using TVirtualProtocol<TBinaryProtocolT<Transport_, ByteOrder_> >::readBool;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000153
David Reisse71115b2010-10-06 17:09:56 +0000154 inline uint32_t readByte(int8_t& byte);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000155
David Reisse71115b2010-10-06 17:09:56 +0000156 inline uint32_t readI16(int16_t& i16);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000157
David Reisse71115b2010-10-06 17:09:56 +0000158 inline uint32_t readI32(int32_t& i32);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000159
David Reisse71115b2010-10-06 17:09:56 +0000160 inline uint32_t readI64(int64_t& i64);
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000161
David Reisse71115b2010-10-06 17:09:56 +0000162 inline uint32_t readDouble(double& dub);
Mark Sleec98d0502006-09-06 02:42:25 +0000163
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100164 template <typename StrType>
Jake Farrellf42ae012012-06-22 03:22:53 +0000165 inline uint32_t readString(StrType& str);
Mark Sleef9831082007-02-20 20:59:21 +0000166
David Reisse71115b2010-10-06 17:09:56 +0000167 inline uint32_t readBinary(std::string& str);
David Reissc005b1b2008-02-15 01:38:18 +0000168
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100169protected:
170 template <typename StrType>
Jake Farrellf42ae012012-06-22 03:22:53 +0000171 uint32_t readStringBody(StrType& str, int32_t sz);
Mark Slee808454e2007-06-20 21:51:57 +0000172
David Reisse71115b2010-10-06 17:09:56 +0000173 Transport_* trans_;
174
Mark Sleef9831082007-02-20 20:59:21 +0000175 int32_t string_limit_;
176 int32_t container_limit_;
177
Mark Slee808454e2007-06-20 21:51:57 +0000178 // Enforce presence of version identifier
179 bool strict_read_;
180 bool strict_write_;
Mark Slee4af6ed72006-10-25 19:02:49 +0000181};
182
David Reisse71115b2010-10-06 17:09:56 +0000183typedef TBinaryProtocolT<TTransport> TBinaryProtocol;
Ben Craig384f9762015-07-08 20:33:03 -0500184typedef TBinaryProtocolT<TTransport, TNetworkLittleEndian> TLEBinaryProtocol;
David Reisse71115b2010-10-06 17:09:56 +0000185
Mark Slee4af6ed72006-10-25 19:02:49 +0000186/**
187 * Constructs binary protocol handlers
188 */
Ben Craig384f9762015-07-08 20:33:03 -0500189template <class Transport_, class ByteOrder_ = TNetworkBigEndian>
David Reisse71115b2010-10-06 17:09:56 +0000190class TBinaryProtocolFactoryT : public TProtocolFactory {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100191public:
192 TBinaryProtocolFactoryT()
193 : string_limit_(0), container_limit_(0), strict_read_(false), strict_write_(true) {}
Mark Sleef9831082007-02-20 20:59:21 +0000194
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100195 TBinaryProtocolFactoryT(int32_t string_limit,
196 int32_t container_limit,
197 bool strict_read,
198 bool strict_write)
199 : string_limit_(string_limit),
200 container_limit_(container_limit),
201 strict_read_(strict_read),
202 strict_write_(strict_write) {}
Mark Slee4af6ed72006-10-25 19:02:49 +0000203
David Reisse71115b2010-10-06 17:09:56 +0000204 virtual ~TBinaryProtocolFactoryT() {}
Mark Slee4af6ed72006-10-25 19:02:49 +0000205
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100206 void setStringSizeLimit(int32_t string_limit) { string_limit_ = string_limit; }
Mark Sleef9831082007-02-20 20:59:21 +0000207
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100208 void setContainerSizeLimit(int32_t container_limit) { container_limit_ = container_limit; }
Mark Sleef9831082007-02-20 20:59:21 +0000209
Mark Slee808454e2007-06-20 21:51:57 +0000210 void setStrict(bool strict_read, bool strict_write) {
211 strict_read_ = strict_read;
212 strict_write_ = strict_write;
213 }
214
Mark Sleef9831082007-02-20 20:59:21 +0000215 boost::shared_ptr<TProtocol> getProtocol(boost::shared_ptr<TTransport> trans) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100216 boost::shared_ptr<Transport_> specific_trans = boost::dynamic_pointer_cast<Transport_>(trans);
David Reisse71115b2010-10-06 17:09:56 +0000217 TProtocol* prot;
218 if (specific_trans) {
Konrad Grochowski7f4be5f2015-11-05 20:23:11 +0100219 prot = new TBinaryProtocolT<Transport_, ByteOrder_>(specific_trans,
220 string_limit_,
221 container_limit_,
222 strict_read_,
223 strict_write_);
David Reisse71115b2010-10-06 17:09:56 +0000224 } else {
Konrad Grochowski7f4be5f2015-11-05 20:23:11 +0100225 prot = new TBinaryProtocolT<TTransport, ByteOrder_>(trans,
226 string_limit_,
227 container_limit_,
228 strict_read_,
229 strict_write_);
David Reisse71115b2010-10-06 17:09:56 +0000230 }
231
232 return boost::shared_ptr<TProtocol>(prot);
Mark Sleef9831082007-02-20 20:59:21 +0000233 }
234
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100235private:
Mark Sleef9831082007-02-20 20:59:21 +0000236 int32_t string_limit_;
237 int32_t container_limit_;
Mark Slee808454e2007-06-20 21:51:57 +0000238 bool strict_read_;
239 bool strict_write_;
Mark Sleee8540632006-05-30 09:24:40 +0000240};
241
David Reisse71115b2010-10-06 17:09:56 +0000242typedef TBinaryProtocolFactoryT<TTransport> TBinaryProtocolFactory;
Ben Craig384f9762015-07-08 20:33:03 -0500243typedef TBinaryProtocolFactoryT<TTransport, TNetworkLittleEndian> TLEBinaryProtocolFactory;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100244}
245}
246} // apache::thrift::protocol
Marc Slemko6f038a72006-08-03 18:58:09 +0000247
Roger Meier4285ba22013-06-10 21:17:23 +0200248#include <thrift/protocol/TBinaryProtocol.tcc>
David Reisse71115b2010-10-06 17:09:56 +0000249
Mark Sleef5f2be42006-09-05 21:05:31 +0000250#endif // #ifndef _THRIFT_PROTOCOL_TBINARYPROTOCOL_H_