blob: 7fd3de673e30a5ac56142741ef8840a958d78cf2 [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"
Marc Slemko16698852006-08-04 03:16:10 +000024
25#include <boost/shared_ptr.hpp>
Mark Sleee8540632006-05-30 09:24:40 +000026
T Jake Lucianib5e62212009-01-31 22:36:20 +000027namespace apache { namespace thrift { namespace protocol {
Marc Slemko6f038a72006-08-03 18:58:09 +000028
Mark Sleee8540632006-05-30 09:24:40 +000029/**
30 * The default binary protocol for thrift. Writes all data in a very basic
31 * binary format, essentially just spitting out the raw bytes.
32 *
Mark Sleee8540632006-05-30 09:24:40 +000033 */
Mark Sleef9831082007-02-20 20:59:21 +000034class TBinaryProtocol : public TProtocol {
Mark Slee808454e2007-06-20 21:51:57 +000035 protected:
36 static const int32_t VERSION_MASK = 0xffff0000;
37 static const int32_t VERSION_1 = 0x80010000;
David Reiss4e7530d2007-09-04 21:49:53 +000038 // VERSION_2 (0x80020000) is taken by TDenseProtocol.
Mark Slee808454e2007-06-20 21:51:57 +000039
Marc Slemko0b4ffa92006-08-11 02:49:29 +000040 public:
Mark Slee5ea15f92007-03-05 22:55:59 +000041 TBinaryProtocol(boost::shared_ptr<TTransport> trans) :
Mark Sleef9831082007-02-20 20:59:21 +000042 TProtocol(trans),
43 string_limit_(0),
44 container_limit_(0),
Mark Slee808454e2007-06-20 21:51:57 +000045 strict_read_(false),
46 strict_write_(true),
Mark Sleef9831082007-02-20 20:59:21 +000047 string_buf_(NULL),
48 string_buf_size_(0) {}
Mark Slee4af6ed72006-10-25 19:02:49 +000049
Mark Slee5ea15f92007-03-05 22:55:59 +000050 TBinaryProtocol(boost::shared_ptr<TTransport> trans,
Mark Sleef9831082007-02-20 20:59:21 +000051 int32_t string_limit,
Mark Slee808454e2007-06-20 21:51:57 +000052 int32_t container_limit,
53 bool strict_read,
54 bool strict_write) :
Mark Sleef9831082007-02-20 20:59:21 +000055 TProtocol(trans),
56 string_limit_(string_limit),
57 container_limit_(container_limit),
Mark Slee808454e2007-06-20 21:51:57 +000058 strict_read_(strict_read),
59 strict_write_(strict_write),
Mark Sleef9831082007-02-20 20:59:21 +000060 string_buf_(NULL),
61 string_buf_size_(0) {}
62
63 ~TBinaryProtocol() {
64 if (string_buf_ != NULL) {
David Reissd7a16f42008-02-19 22:47:29 +000065 std::free(string_buf_);
Mark Sleef9831082007-02-20 20:59:21 +000066 string_buf_size_ = 0;
67 }
68 }
69
70 void setStringSizeLimit(int32_t string_limit) {
71 string_limit_ = string_limit;
72 }
73
74 void setContainerSizeLimit(int32_t container_limit) {
75 container_limit_ = container_limit;
76 }
Mark Sleee8540632006-05-30 09:24:40 +000077
Mark Slee808454e2007-06-20 21:51:57 +000078 void setStrict(bool strict_read, bool strict_write) {
79 strict_read_ = strict_read;
80 strict_write_ = strict_write;
81 }
82
Mark Slee8d7e1f62006-06-07 06:48:56 +000083 /**
84 * Writing functions.
85 */
Mark Sleee8540632006-05-30 09:24:40 +000086
Mark Slee82a6c0f2007-04-04 21:08:21 +000087 virtual uint32_t writeMessageBegin(const std::string& name,
88 const TMessageType messageType,
89 const int32_t seqid);
Marc Slemko16698852006-08-04 03:16:10 +000090
Mark Slee4af6ed72006-10-25 19:02:49 +000091 virtual uint32_t writeMessageEnd();
Marc Slemko16698852006-08-04 03:16:10 +000092
93
David Reiss64120002008-04-29 23:12:24 +000094 uint32_t writeStructBegin(const char* name);
Mark Sleee8540632006-05-30 09:24:40 +000095
Mark Slee4af6ed72006-10-25 19:02:49 +000096 uint32_t writeStructEnd();
Mark Sleee8540632006-05-30 09:24:40 +000097
David Reiss64120002008-04-29 23:12:24 +000098 uint32_t writeFieldBegin(const char* name,
Mark Slee4af6ed72006-10-25 19:02:49 +000099 const TType fieldType,
100 const int16_t fieldId);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000101
Mark Slee4af6ed72006-10-25 19:02:49 +0000102 uint32_t writeFieldEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000103
Mark Slee4af6ed72006-10-25 19:02:49 +0000104 uint32_t writeFieldStop();
David Reiss0c90f6f2008-02-06 22:18:40 +0000105
Mark Slee4af6ed72006-10-25 19:02:49 +0000106 uint32_t writeMapBegin(const TType keyType,
107 const TType valType,
108 const uint32_t size);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000109
Mark Slee4af6ed72006-10-25 19:02:49 +0000110 uint32_t writeMapEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000111
Mark Slee4af6ed72006-10-25 19:02:49 +0000112 uint32_t writeListBegin(const TType elemType,
113 const uint32_t size);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000114
Mark Slee4af6ed72006-10-25 19:02:49 +0000115 uint32_t writeListEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000116
Mark Slee4af6ed72006-10-25 19:02:49 +0000117 uint32_t writeSetBegin(const TType elemType,
118 const uint32_t size);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000119
Mark Slee4af6ed72006-10-25 19:02:49 +0000120 uint32_t writeSetEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000121
Mark Slee4af6ed72006-10-25 19:02:49 +0000122 uint32_t writeBool(const bool value);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000123
Mark Slee4af6ed72006-10-25 19:02:49 +0000124 uint32_t writeByte(const int8_t byte);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000125
Mark Slee4af6ed72006-10-25 19:02:49 +0000126 uint32_t writeI16(const int16_t i16);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000127
Mark Slee4af6ed72006-10-25 19:02:49 +0000128 uint32_t writeI32(const int32_t i32);
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000129
Mark Slee4af6ed72006-10-25 19:02:49 +0000130 uint32_t writeI64(const int64_t i64);
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000131
Mark Slee4af6ed72006-10-25 19:02:49 +0000132 uint32_t writeDouble(const double dub);
Mark Sleec98d0502006-09-06 02:42:25 +0000133
Mark Slee4af6ed72006-10-25 19:02:49 +0000134 uint32_t writeString(const std::string& str);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000135
David Reissc005b1b2008-02-15 01:38:18 +0000136 uint32_t writeBinary(const std::string& str);
137
Mark Slee8d7e1f62006-06-07 06:48:56 +0000138 /**
139 * Reading functions
140 */
141
Marc Slemko16698852006-08-04 03:16:10 +0000142
Mark Slee4af6ed72006-10-25 19:02:49 +0000143 uint32_t readMessageBegin(std::string& name,
David Reiss96d23882007-07-26 21:10:32 +0000144 TMessageType& messageType,
145 int32_t& seqid);
Marc Slemko16698852006-08-04 03:16:10 +0000146
Mark Slee4af6ed72006-10-25 19:02:49 +0000147 uint32_t readMessageEnd();
Marc Slemko16698852006-08-04 03:16:10 +0000148
Mark Slee4af6ed72006-10-25 19:02:49 +0000149 uint32_t readStructBegin(std::string& name);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000150
Mark Slee4af6ed72006-10-25 19:02:49 +0000151 uint32_t readStructEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000152
Mark Slee4af6ed72006-10-25 19:02:49 +0000153 uint32_t readFieldBegin(std::string& name,
David Reiss96d23882007-07-26 21:10:32 +0000154 TType& fieldType,
155 int16_t& fieldId);
David Reiss0c90f6f2008-02-06 22:18:40 +0000156
Mark Slee4af6ed72006-10-25 19:02:49 +0000157 uint32_t readFieldEnd();
David Reiss0c90f6f2008-02-06 22:18:40 +0000158
Mark Slee4af6ed72006-10-25 19:02:49 +0000159 uint32_t readMapBegin(TType& keyType,
David Reiss96d23882007-07-26 21:10:32 +0000160 TType& valType,
161 uint32_t& size);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000162
Mark Slee4af6ed72006-10-25 19:02:49 +0000163 uint32_t readMapEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000164
Mark Slee4af6ed72006-10-25 19:02:49 +0000165 uint32_t readListBegin(TType& elemType,
166 uint32_t& size);
David Reiss0c90f6f2008-02-06 22:18:40 +0000167
Mark Slee4af6ed72006-10-25 19:02:49 +0000168 uint32_t readListEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000169
Mark Slee4af6ed72006-10-25 19:02:49 +0000170 uint32_t readSetBegin(TType& elemType,
David Reiss96d23882007-07-26 21:10:32 +0000171 uint32_t& size);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000172
Mark Slee4af6ed72006-10-25 19:02:49 +0000173 uint32_t readSetEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000174
Mark Slee4af6ed72006-10-25 19:02:49 +0000175 uint32_t readBool(bool& value);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000176
Mark Slee4af6ed72006-10-25 19:02:49 +0000177 uint32_t readByte(int8_t& byte);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000178
Mark Slee4af6ed72006-10-25 19:02:49 +0000179 uint32_t readI16(int16_t& i16);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000180
Mark Slee4af6ed72006-10-25 19:02:49 +0000181 uint32_t readI32(int32_t& i32);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000182
Mark Slee4af6ed72006-10-25 19:02:49 +0000183 uint32_t readI64(int64_t& i64);
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000184
Mark Slee4af6ed72006-10-25 19:02:49 +0000185 uint32_t readDouble(double& dub);
Mark Sleec98d0502006-09-06 02:42:25 +0000186
Mark Slee4af6ed72006-10-25 19:02:49 +0000187 uint32_t readString(std::string& str);
Mark Sleef9831082007-02-20 20:59:21 +0000188
David Reissc005b1b2008-02-15 01:38:18 +0000189 uint32_t readBinary(std::string& str);
190
Mark Slee808454e2007-06-20 21:51:57 +0000191 protected:
192 uint32_t readStringBody(std::string& str, int32_t sz);
193
Mark Sleef9831082007-02-20 20:59:21 +0000194 int32_t string_limit_;
195 int32_t container_limit_;
196
Mark Slee808454e2007-06-20 21:51:57 +0000197 // Enforce presence of version identifier
198 bool strict_read_;
199 bool strict_write_;
200
Mark Sleef9831082007-02-20 20:59:21 +0000201 // Buffer for reading strings, save for the lifetime of the protocol to
202 // avoid memory churn allocating memory on every string read
203 uint8_t* string_buf_;
204 int32_t string_buf_size_;
205
Mark Slee4af6ed72006-10-25 19:02:49 +0000206};
207
208/**
209 * Constructs binary protocol handlers
210 */
211class TBinaryProtocolFactory : public TProtocolFactory {
212 public:
Mark Sleef9831082007-02-20 20:59:21 +0000213 TBinaryProtocolFactory() :
214 string_limit_(0),
Mark Slee808454e2007-06-20 21:51:57 +0000215 container_limit_(0),
216 strict_read_(false),
217 strict_write_(true) {}
Mark Sleef9831082007-02-20 20:59:21 +0000218
Mark Slee808454e2007-06-20 21:51:57 +0000219 TBinaryProtocolFactory(int32_t string_limit, int32_t container_limit, bool strict_read, bool strict_write) :
Mark Sleef9831082007-02-20 20:59:21 +0000220 string_limit_(string_limit),
Mark Slee808454e2007-06-20 21:51:57 +0000221 container_limit_(container_limit),
222 strict_read_(strict_read),
223 strict_write_(strict_write) {}
Mark Slee4af6ed72006-10-25 19:02:49 +0000224
225 virtual ~TBinaryProtocolFactory() {}
226
Mark Sleef9831082007-02-20 20:59:21 +0000227 void setStringSizeLimit(int32_t string_limit) {
228 string_limit_ = string_limit;
Mark Slee4af6ed72006-10-25 19:02:49 +0000229 }
Mark Sleef9831082007-02-20 20:59:21 +0000230
231 void setContainerSizeLimit(int32_t container_limit) {
232 container_limit_ = container_limit;
233 }
234
Mark Slee808454e2007-06-20 21:51:57 +0000235 void setStrict(bool strict_read, bool strict_write) {
236 strict_read_ = strict_read;
237 strict_write_ = strict_write;
238 }
239
Mark Sleef9831082007-02-20 20:59:21 +0000240 boost::shared_ptr<TProtocol> getProtocol(boost::shared_ptr<TTransport> trans) {
Mark Slee808454e2007-06-20 21:51:57 +0000241 return boost::shared_ptr<TProtocol>(new TBinaryProtocol(trans, string_limit_, container_limit_, strict_read_, strict_write_));
Mark Sleef9831082007-02-20 20:59:21 +0000242 }
243
244 private:
245 int32_t string_limit_;
246 int32_t container_limit_;
Mark Slee808454e2007-06-20 21:51:57 +0000247 bool strict_read_;
248 bool strict_write_;
Mark Sleef9831082007-02-20 20:59:21 +0000249
Mark Sleee8540632006-05-30 09:24:40 +0000250};
251
T Jake Lucianib5e62212009-01-31 22:36:20 +0000252}}} // apache::thrift::protocol
Marc Slemko6f038a72006-08-03 18:58:09 +0000253
Mark Sleef5f2be42006-09-05 21:05:31 +0000254#endif // #ifndef _THRIFT_PROTOCOL_TBINARYPROTOCOL_H_