blob: 89220e2be50b42db3b0e73f8698ccf64c320526a [file] [log] [blame]
Mark Slee9f0c6512007-02-28 23:58:26 +00001// Copyright (c) 2006- Facebook
2// Distributed under the Thrift Software License
3//
4// See accompanying file LICENSE or visit the Thrift site at:
5// http://developers.facebook.com/thrift/
6
Mark Sleef5f2be42006-09-05 21:05:31 +00007#ifndef _THRIFT_PROTOCOL_TBINARYPROTOCOL_H_
8#define _THRIFT_PROTOCOL_TBINARYPROTOCOL_H_ 1
Mark Sleee8540632006-05-30 09:24:40 +00009
Marc Slemkod42a2c22006-08-10 03:30:18 +000010#include "TProtocol.h"
Marc Slemko16698852006-08-04 03:16:10 +000011
12#include <boost/shared_ptr.hpp>
Mark Sleee8540632006-05-30 09:24:40 +000013
Marc Slemko6f038a72006-08-03 18:58:09 +000014namespace facebook { namespace thrift { namespace protocol {
15
Mark Sleee8540632006-05-30 09:24:40 +000016/**
17 * The default binary protocol for thrift. Writes all data in a very basic
18 * binary format, essentially just spitting out the raw bytes.
19 *
20 * @author Mark Slee <mcslee@facebook.com>
21 */
Mark Sleef9831082007-02-20 20:59:21 +000022class TBinaryProtocol : public TProtocol {
Marc Slemko0b4ffa92006-08-11 02:49:29 +000023 public:
Mark Slee5ea15f92007-03-05 22:55:59 +000024 TBinaryProtocol(boost::shared_ptr<TTransport> trans) :
Mark Sleef9831082007-02-20 20:59:21 +000025 TProtocol(trans),
26 string_limit_(0),
27 container_limit_(0),
28 string_buf_(NULL),
29 string_buf_size_(0) {}
Mark Slee4af6ed72006-10-25 19:02:49 +000030
Mark Slee5ea15f92007-03-05 22:55:59 +000031 TBinaryProtocol(boost::shared_ptr<TTransport> trans,
Mark Sleef9831082007-02-20 20:59:21 +000032 int32_t string_limit,
33 int32_t container_limit) :
34 TProtocol(trans),
35 string_limit_(string_limit),
36 container_limit_(container_limit),
37 string_buf_(NULL),
38 string_buf_size_(0) {}
39
40 ~TBinaryProtocol() {
41 if (string_buf_ != NULL) {
42 free(string_buf_);
43 string_buf_size_ = 0;
44 }
45 }
46
47 void setStringSizeLimit(int32_t string_limit) {
48 string_limit_ = string_limit;
49 }
50
51 void setContainerSizeLimit(int32_t container_limit) {
52 container_limit_ = container_limit;
53 }
Mark Sleee8540632006-05-30 09:24:40 +000054
Mark Slee8d7e1f62006-06-07 06:48:56 +000055 /**
56 * Writing functions.
57 */
Mark Sleee8540632006-05-30 09:24:40 +000058
Mark Slee82a6c0f2007-04-04 21:08:21 +000059 virtual uint32_t writeMessageBegin(const std::string& name,
60 const TMessageType messageType,
61 const int32_t seqid);
Marc Slemko16698852006-08-04 03:16:10 +000062
Mark Slee4af6ed72006-10-25 19:02:49 +000063 virtual uint32_t writeMessageEnd();
Marc Slemko16698852006-08-04 03:16:10 +000064
65
Mark Slee4af6ed72006-10-25 19:02:49 +000066 uint32_t writeStructBegin(const std::string& name);
Mark Sleee8540632006-05-30 09:24:40 +000067
Mark Slee4af6ed72006-10-25 19:02:49 +000068 uint32_t writeStructEnd();
Mark Sleee8540632006-05-30 09:24:40 +000069
Mark Slee4af6ed72006-10-25 19:02:49 +000070 uint32_t writeFieldBegin(const std::string& name,
71 const TType fieldType,
72 const int16_t fieldId);
Mark Slee8d7e1f62006-06-07 06:48:56 +000073
Mark Slee4af6ed72006-10-25 19:02:49 +000074 uint32_t writeFieldEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +000075
Mark Slee4af6ed72006-10-25 19:02:49 +000076 uint32_t writeFieldStop();
Mark Slee8d7e1f62006-06-07 06:48:56 +000077
Mark Slee4af6ed72006-10-25 19:02:49 +000078 uint32_t writeMapBegin(const TType keyType,
79 const TType valType,
80 const uint32_t size);
Mark Slee8d7e1f62006-06-07 06:48:56 +000081
Mark Slee4af6ed72006-10-25 19:02:49 +000082 uint32_t writeMapEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +000083
Mark Slee4af6ed72006-10-25 19:02:49 +000084 uint32_t writeListBegin(const TType elemType,
85 const uint32_t size);
Mark Slee8d7e1f62006-06-07 06:48:56 +000086
Mark Slee4af6ed72006-10-25 19:02:49 +000087 uint32_t writeListEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +000088
Mark Slee4af6ed72006-10-25 19:02:49 +000089 uint32_t writeSetBegin(const TType elemType,
90 const uint32_t size);
Mark Slee8d7e1f62006-06-07 06:48:56 +000091
Mark Slee4af6ed72006-10-25 19:02:49 +000092 uint32_t writeSetEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +000093
Mark Slee4af6ed72006-10-25 19:02:49 +000094 uint32_t writeBool(const bool value);
Mark Slee8d7e1f62006-06-07 06:48:56 +000095
Mark Slee4af6ed72006-10-25 19:02:49 +000096 uint32_t writeByte(const int8_t byte);
Mark Slee8d7e1f62006-06-07 06:48:56 +000097
Mark Slee4af6ed72006-10-25 19:02:49 +000098 uint32_t writeI16(const int16_t i16);
Mark Slee8d7e1f62006-06-07 06:48:56 +000099
Mark Slee4af6ed72006-10-25 19:02:49 +0000100 uint32_t writeI32(const int32_t i32);
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000101
Mark Slee4af6ed72006-10-25 19:02:49 +0000102 uint32_t writeI64(const int64_t i64);
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000103
Mark Slee4af6ed72006-10-25 19:02:49 +0000104 uint32_t writeDouble(const double dub);
Mark Sleec98d0502006-09-06 02:42:25 +0000105
106
Mark Slee4af6ed72006-10-25 19:02:49 +0000107 uint32_t writeString(const std::string& str);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000108
109 /**
110 * Reading functions
111 */
112
Marc Slemko16698852006-08-04 03:16:10 +0000113
Mark Slee4af6ed72006-10-25 19:02:49 +0000114 uint32_t readMessageBegin(std::string& name,
Marc Slemkoe6889de2006-08-12 00:32:53 +0000115 TMessageType& messageType,
Mark Slee4af6ed72006-10-25 19:02:49 +0000116 int32_t& seqid);
Marc Slemko16698852006-08-04 03:16:10 +0000117
Mark Slee4af6ed72006-10-25 19:02:49 +0000118 uint32_t readMessageEnd();
Marc Slemko16698852006-08-04 03:16:10 +0000119
Mark Slee4af6ed72006-10-25 19:02:49 +0000120 uint32_t readStructBegin(std::string& name);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000121
Mark Slee4af6ed72006-10-25 19:02:49 +0000122 uint32_t readStructEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000123
Mark Slee4af6ed72006-10-25 19:02:49 +0000124 uint32_t readFieldBegin(std::string& name,
125 TType& fieldType,
126 int16_t& fieldId);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000127
Mark Slee4af6ed72006-10-25 19:02:49 +0000128 uint32_t readFieldEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000129
Mark Slee4af6ed72006-10-25 19:02:49 +0000130 uint32_t readMapBegin(TType& keyType,
131 TType& valType,
132 uint32_t& size);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000133
Mark Slee4af6ed72006-10-25 19:02:49 +0000134 uint32_t readMapEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000135
Mark Slee4af6ed72006-10-25 19:02:49 +0000136 uint32_t readListBegin(TType& elemType,
137 uint32_t& size);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000138
Mark Slee4af6ed72006-10-25 19:02:49 +0000139 uint32_t readListEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000140
Mark Slee4af6ed72006-10-25 19:02:49 +0000141 uint32_t readSetBegin(TType& elemType,
142 uint32_t& size);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000143
Mark Slee4af6ed72006-10-25 19:02:49 +0000144 uint32_t readSetEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000145
Mark Slee4af6ed72006-10-25 19:02:49 +0000146 uint32_t readBool(bool& value);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000147
Mark Slee4af6ed72006-10-25 19:02:49 +0000148 uint32_t readByte(int8_t& byte);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000149
Mark Slee4af6ed72006-10-25 19:02:49 +0000150 uint32_t readI16(int16_t& i16);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000151
Mark Slee4af6ed72006-10-25 19:02:49 +0000152 uint32_t readI32(int32_t& i32);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000153
Mark Slee4af6ed72006-10-25 19:02:49 +0000154 uint32_t readI64(int64_t& i64);
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000155
Mark Slee4af6ed72006-10-25 19:02:49 +0000156 uint32_t readDouble(double& dub);
Mark Sleec98d0502006-09-06 02:42:25 +0000157
Mark Slee4af6ed72006-10-25 19:02:49 +0000158 uint32_t readString(std::string& str);
Mark Sleef9831082007-02-20 20:59:21 +0000159
160 private:
161 int32_t string_limit_;
162 int32_t container_limit_;
163
164 // Buffer for reading strings, save for the lifetime of the protocol to
165 // avoid memory churn allocating memory on every string read
166 uint8_t* string_buf_;
167 int32_t string_buf_size_;
168
Mark Slee4af6ed72006-10-25 19:02:49 +0000169};
170
171/**
172 * Constructs binary protocol handlers
173 */
174class TBinaryProtocolFactory : public TProtocolFactory {
175 public:
Mark Sleef9831082007-02-20 20:59:21 +0000176 TBinaryProtocolFactory() :
177 string_limit_(0),
178 container_limit_(0) {}
179
180 TBinaryProtocolFactory(int32_t string_limit, int32_t container_limit) :
181 string_limit_(string_limit),
182 container_limit_(container_limit) {}
Mark Slee4af6ed72006-10-25 19:02:49 +0000183
184 virtual ~TBinaryProtocolFactory() {}
185
Mark Sleef9831082007-02-20 20:59:21 +0000186 void setStringSizeLimit(int32_t string_limit) {
187 string_limit_ = string_limit;
Mark Slee4af6ed72006-10-25 19:02:49 +0000188 }
Mark Sleef9831082007-02-20 20:59:21 +0000189
190 void setContainerSizeLimit(int32_t container_limit) {
191 container_limit_ = container_limit;
192 }
193
194 boost::shared_ptr<TProtocol> getProtocol(boost::shared_ptr<TTransport> trans) {
195 return boost::shared_ptr<TProtocol>(new TBinaryProtocol(trans, string_limit_, container_limit_));
196 }
197
198 private:
199 int32_t string_limit_;
200 int32_t container_limit_;
201
Mark Sleee8540632006-05-30 09:24:40 +0000202};
203
Marc Slemko6f038a72006-08-03 18:58:09 +0000204}}} // facebook::thrift::protocol
205
Mark Sleef5f2be42006-09-05 21:05:31 +0000206#endif // #ifndef _THRIFT_PROTOCOL_TBINARYPROTOCOL_H_
207
Marc Slemko6f038a72006-08-03 18:58:09 +0000208