blob: 45c584272fb569dd6bc5264b6986255c153562fa [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 Reiss6806fb82010-10-06 17:09:52 +000035class TBinaryProtocol : public TVirtualProtocol<TBinaryProtocol> {
Mark Slee808454e2007-06-20 21:51:57 +000036 protected:
37 static const int32_t VERSION_MASK = 0xffff0000;
38 static const int32_t VERSION_1 = 0x80010000;
David Reiss4e7530d2007-09-04 21:49:53 +000039 // VERSION_2 (0x80020000) is taken by TDenseProtocol.
Mark Slee808454e2007-06-20 21:51:57 +000040
Marc Slemko0b4ffa92006-08-11 02:49:29 +000041 public:
Mark Slee5ea15f92007-03-05 22:55:59 +000042 TBinaryProtocol(boost::shared_ptr<TTransport> trans) :
David Reiss6806fb82010-10-06 17:09:52 +000043 TVirtualProtocol<TBinaryProtocol>(trans),
Mark Sleef9831082007-02-20 20:59:21 +000044 string_limit_(0),
45 container_limit_(0),
Mark Slee808454e2007-06-20 21:51:57 +000046 strict_read_(false),
47 strict_write_(true),
Mark Sleef9831082007-02-20 20:59:21 +000048 string_buf_(NULL),
49 string_buf_size_(0) {}
Mark Slee4af6ed72006-10-25 19:02:49 +000050
Mark Slee5ea15f92007-03-05 22:55:59 +000051 TBinaryProtocol(boost::shared_ptr<TTransport> trans,
Mark Sleef9831082007-02-20 20:59:21 +000052 int32_t string_limit,
Mark Slee808454e2007-06-20 21:51:57 +000053 int32_t container_limit,
54 bool strict_read,
55 bool strict_write) :
David Reiss6806fb82010-10-06 17:09:52 +000056 TVirtualProtocol<TBinaryProtocol>(trans),
Mark Sleef9831082007-02-20 20:59:21 +000057 string_limit_(string_limit),
58 container_limit_(container_limit),
Mark Slee808454e2007-06-20 21:51:57 +000059 strict_read_(strict_read),
60 strict_write_(strict_write),
Mark Sleef9831082007-02-20 20:59:21 +000061 string_buf_(NULL),
62 string_buf_size_(0) {}
63
64 ~TBinaryProtocol() {
65 if (string_buf_ != NULL) {
David Reissd7a16f42008-02-19 22:47:29 +000066 std::free(string_buf_);
Mark Sleef9831082007-02-20 20:59:21 +000067 string_buf_size_ = 0;
68 }
69 }
70
71 void setStringSizeLimit(int32_t string_limit) {
72 string_limit_ = string_limit;
73 }
74
75 void setContainerSizeLimit(int32_t container_limit) {
76 container_limit_ = container_limit;
77 }
Mark Sleee8540632006-05-30 09:24:40 +000078
Mark Slee808454e2007-06-20 21:51:57 +000079 void setStrict(bool strict_read, bool strict_write) {
80 strict_read_ = strict_read;
81 strict_write_ = strict_write;
82 }
83
Mark Slee8d7e1f62006-06-07 06:48:56 +000084 /**
85 * Writing functions.
86 */
Mark Sleee8540632006-05-30 09:24:40 +000087
David Reiss6806fb82010-10-06 17:09:52 +000088 uint32_t writeMessageBegin(const std::string& name,
89 const TMessageType messageType,
90 const int32_t seqid);
Marc Slemko16698852006-08-04 03:16:10 +000091
David Reiss6806fb82010-10-06 17:09:52 +000092 uint32_t writeMessageEnd();
Marc Slemko16698852006-08-04 03:16:10 +000093
94
David Reiss64120002008-04-29 23:12:24 +000095 uint32_t writeStructBegin(const char* name);
Mark Sleee8540632006-05-30 09:24:40 +000096
Mark Slee4af6ed72006-10-25 19:02:49 +000097 uint32_t writeStructEnd();
Mark Sleee8540632006-05-30 09:24:40 +000098
David Reiss64120002008-04-29 23:12:24 +000099 uint32_t writeFieldBegin(const char* name,
Mark Slee4af6ed72006-10-25 19:02:49 +0000100 const TType fieldType,
101 const int16_t fieldId);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000102
Mark Slee4af6ed72006-10-25 19:02:49 +0000103 uint32_t writeFieldEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000104
Mark Slee4af6ed72006-10-25 19:02:49 +0000105 uint32_t writeFieldStop();
David Reiss0c90f6f2008-02-06 22:18:40 +0000106
Mark Slee4af6ed72006-10-25 19:02:49 +0000107 uint32_t writeMapBegin(const TType keyType,
108 const TType valType,
109 const uint32_t size);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000110
Mark Slee4af6ed72006-10-25 19:02:49 +0000111 uint32_t writeMapEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000112
Mark Slee4af6ed72006-10-25 19:02:49 +0000113 uint32_t writeListBegin(const TType elemType,
114 const uint32_t size);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000115
Mark Slee4af6ed72006-10-25 19:02:49 +0000116 uint32_t writeListEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000117
Mark Slee4af6ed72006-10-25 19:02:49 +0000118 uint32_t writeSetBegin(const TType elemType,
119 const uint32_t size);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000120
Mark Slee4af6ed72006-10-25 19:02:49 +0000121 uint32_t writeSetEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000122
Mark Slee4af6ed72006-10-25 19:02:49 +0000123 uint32_t writeBool(const bool value);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000124
Mark Slee4af6ed72006-10-25 19:02:49 +0000125 uint32_t writeByte(const int8_t byte);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000126
Mark Slee4af6ed72006-10-25 19:02:49 +0000127 uint32_t writeI16(const int16_t i16);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000128
Mark Slee4af6ed72006-10-25 19:02:49 +0000129 uint32_t writeI32(const int32_t i32);
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000130
Mark Slee4af6ed72006-10-25 19:02:49 +0000131 uint32_t writeI64(const int64_t i64);
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000132
Mark Slee4af6ed72006-10-25 19:02:49 +0000133 uint32_t writeDouble(const double dub);
Mark Sleec98d0502006-09-06 02:42:25 +0000134
Mark Slee4af6ed72006-10-25 19:02:49 +0000135 uint32_t writeString(const std::string& str);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000136
David Reissc005b1b2008-02-15 01:38:18 +0000137 uint32_t writeBinary(const std::string& str);
138
Mark Slee8d7e1f62006-06-07 06:48:56 +0000139 /**
140 * Reading functions
141 */
142
Marc Slemko16698852006-08-04 03:16:10 +0000143
Mark Slee4af6ed72006-10-25 19:02:49 +0000144 uint32_t readMessageBegin(std::string& name,
David Reiss96d23882007-07-26 21:10:32 +0000145 TMessageType& messageType,
146 int32_t& seqid);
Marc Slemko16698852006-08-04 03:16:10 +0000147
Mark Slee4af6ed72006-10-25 19:02:49 +0000148 uint32_t readMessageEnd();
Marc Slemko16698852006-08-04 03:16:10 +0000149
Mark Slee4af6ed72006-10-25 19:02:49 +0000150 uint32_t readStructBegin(std::string& name);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000151
Mark Slee4af6ed72006-10-25 19:02:49 +0000152 uint32_t readStructEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000153
Mark Slee4af6ed72006-10-25 19:02:49 +0000154 uint32_t readFieldBegin(std::string& name,
David Reiss96d23882007-07-26 21:10:32 +0000155 TType& fieldType,
156 int16_t& fieldId);
David Reiss0c90f6f2008-02-06 22:18:40 +0000157
Mark Slee4af6ed72006-10-25 19:02:49 +0000158 uint32_t readFieldEnd();
David Reiss0c90f6f2008-02-06 22:18:40 +0000159
Mark Slee4af6ed72006-10-25 19:02:49 +0000160 uint32_t readMapBegin(TType& keyType,
David Reiss96d23882007-07-26 21:10:32 +0000161 TType& valType,
162 uint32_t& size);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000163
Mark Slee4af6ed72006-10-25 19:02:49 +0000164 uint32_t readMapEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000165
Mark Slee4af6ed72006-10-25 19:02:49 +0000166 uint32_t readListBegin(TType& elemType,
167 uint32_t& size);
David Reiss0c90f6f2008-02-06 22:18:40 +0000168
Mark Slee4af6ed72006-10-25 19:02:49 +0000169 uint32_t readListEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000170
Mark Slee4af6ed72006-10-25 19:02:49 +0000171 uint32_t readSetBegin(TType& elemType,
David Reiss96d23882007-07-26 21:10:32 +0000172 uint32_t& size);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000173
Mark Slee4af6ed72006-10-25 19:02:49 +0000174 uint32_t readSetEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000175
Mark Slee4af6ed72006-10-25 19:02:49 +0000176 uint32_t readBool(bool& value);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000177
Mark Slee4af6ed72006-10-25 19:02:49 +0000178 uint32_t readByte(int8_t& byte);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000179
Mark Slee4af6ed72006-10-25 19:02:49 +0000180 uint32_t readI16(int16_t& i16);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000181
Mark Slee4af6ed72006-10-25 19:02:49 +0000182 uint32_t readI32(int32_t& i32);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000183
Mark Slee4af6ed72006-10-25 19:02:49 +0000184 uint32_t readI64(int64_t& i64);
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000185
Mark Slee4af6ed72006-10-25 19:02:49 +0000186 uint32_t readDouble(double& dub);
Mark Sleec98d0502006-09-06 02:42:25 +0000187
Mark Slee4af6ed72006-10-25 19:02:49 +0000188 uint32_t readString(std::string& str);
Mark Sleef9831082007-02-20 20:59:21 +0000189
David Reissc005b1b2008-02-15 01:38:18 +0000190 uint32_t readBinary(std::string& str);
191
Mark Slee808454e2007-06-20 21:51:57 +0000192 protected:
193 uint32_t readStringBody(std::string& str, int32_t sz);
194
Mark Sleef9831082007-02-20 20:59:21 +0000195 int32_t string_limit_;
196 int32_t container_limit_;
197
Mark Slee808454e2007-06-20 21:51:57 +0000198 // Enforce presence of version identifier
199 bool strict_read_;
200 bool strict_write_;
201
Mark Sleef9831082007-02-20 20:59:21 +0000202 // Buffer for reading strings, save for the lifetime of the protocol to
203 // avoid memory churn allocating memory on every string read
204 uint8_t* string_buf_;
205 int32_t string_buf_size_;
206
Mark Slee4af6ed72006-10-25 19:02:49 +0000207};
208
209/**
210 * Constructs binary protocol handlers
211 */
212class TBinaryProtocolFactory : public TProtocolFactory {
213 public:
Mark Sleef9831082007-02-20 20:59:21 +0000214 TBinaryProtocolFactory() :
215 string_limit_(0),
Mark Slee808454e2007-06-20 21:51:57 +0000216 container_limit_(0),
217 strict_read_(false),
218 strict_write_(true) {}
Mark Sleef9831082007-02-20 20:59:21 +0000219
Mark Slee808454e2007-06-20 21:51:57 +0000220 TBinaryProtocolFactory(int32_t string_limit, int32_t container_limit, bool strict_read, bool strict_write) :
Mark Sleef9831082007-02-20 20:59:21 +0000221 string_limit_(string_limit),
Mark Slee808454e2007-06-20 21:51:57 +0000222 container_limit_(container_limit),
223 strict_read_(strict_read),
224 strict_write_(strict_write) {}
Mark Slee4af6ed72006-10-25 19:02:49 +0000225
226 virtual ~TBinaryProtocolFactory() {}
227
Mark Sleef9831082007-02-20 20:59:21 +0000228 void setStringSizeLimit(int32_t string_limit) {
229 string_limit_ = string_limit;
Mark Slee4af6ed72006-10-25 19:02:49 +0000230 }
Mark Sleef9831082007-02-20 20:59:21 +0000231
232 void setContainerSizeLimit(int32_t container_limit) {
233 container_limit_ = container_limit;
234 }
235
Mark Slee808454e2007-06-20 21:51:57 +0000236 void setStrict(bool strict_read, bool strict_write) {
237 strict_read_ = strict_read;
238 strict_write_ = strict_write;
239 }
240
Mark Sleef9831082007-02-20 20:59:21 +0000241 boost::shared_ptr<TProtocol> getProtocol(boost::shared_ptr<TTransport> trans) {
Mark Slee808454e2007-06-20 21:51:57 +0000242 return boost::shared_ptr<TProtocol>(new TBinaryProtocol(trans, string_limit_, container_limit_, strict_read_, strict_write_));
Mark Sleef9831082007-02-20 20:59:21 +0000243 }
244
245 private:
246 int32_t string_limit_;
247 int32_t container_limit_;
Mark Slee808454e2007-06-20 21:51:57 +0000248 bool strict_read_;
249 bool strict_write_;
Mark Sleef9831082007-02-20 20:59:21 +0000250
Mark Sleee8540632006-05-30 09:24:40 +0000251};
252
T Jake Lucianib5e62212009-01-31 22:36:20 +0000253}}} // apache::thrift::protocol
Marc Slemko6f038a72006-08-03 18:58:09 +0000254
Mark Sleef5f2be42006-09-05 21:05:31 +0000255#endif // #ifndef _THRIFT_PROTOCOL_TBINARYPROTOCOL_H_