OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
TBinaryProtocol.h
Go to the documentation of this file.
1 /*
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  *
19  * Copyright 2006-2017 The Apache Software Foundation.
20  * https://github.com/apache/thrift
21  */
22 
23 #ifndef _SANDESH_PROTOCOL_TBINARYPROTOCOL_H_
24 #define _SANDESH_PROTOCOL_TBINARYPROTOCOL_H_ 1
25 
26 #include "TProtocol.h"
27 #include "TVirtualProtocol.h"
28 
29 #include <boost/shared_ptr.hpp>
30 
31 #include <base/logging.h>
32 
33 namespace contrail { namespace sandesh { namespace protocol {
34 
40 template <class Transport_>
42  : public TVirtualProtocol< TBinaryProtocolT<Transport_> > {
43  protected:
44  static const int32_t VERSION_MASK = 0xffff0000;
45  static const int32_t VERSION_1 = 0x80010000;
46  // VERSION_2 (0x80020000) is taken by TDenseProtocol.
47 
48  public:
49  TBinaryProtocolT(boost::shared_ptr<Transport_> trans) :
50  TVirtualProtocol< TBinaryProtocolT<Transport_> >(trans),
51  trans_(trans.get()),
52  string_limit_(0),
54  strict_read_(false),
55  strict_write_(true),
56  string_buf_(NULL),
57  string_buf_size_(0) {}
58 
59  TBinaryProtocolT(boost::shared_ptr<Transport_> trans,
60  int32_t string_limit,
61  int32_t container_limit,
62  bool strict_read,
63  bool strict_write) :
64  TVirtualProtocol< TBinaryProtocolT<Transport_> >(trans),
65  trans_(trans.get()),
66  string_limit_(string_limit),
67  container_limit_(container_limit),
68  strict_read_(strict_read),
69  strict_write_(strict_write),
70  string_buf_(NULL),
71  string_buf_size_(0) {}
72 
74  if (string_buf_ != NULL) {
75  std::free(string_buf_);
76  string_buf_size_ = 0;
77  }
78  }
79 
80  void setStringSizeLimit(int32_t string_limit) {
81  string_limit_ = string_limit;
82  }
83 
84  void setContainerSizeLimit(int32_t container_limit) {
85  container_limit_ = container_limit;
86  }
87 
88  void setStrict(bool strict_read, bool strict_write) {
89  strict_read_ = strict_read;
90  strict_write_ = strict_write;
91  }
92 
97  /*ol*/ int32_t writeMessageBegin(const std::string& name,
98  const TMessageType messageType,
99  const int32_t seqid);
100 
101  /*ol*/ int32_t writeMessageEnd();
102 
103 
104  inline int32_t writeStructBegin(const char* name);
105 
106  inline int32_t writeStructEnd();
107 
108  inline int32_t writeSandeshBegin(const char* name);
109 
110  inline int32_t writeSandeshEnd();
111 
112  inline int32_t writeContainerElementBegin();
113 
114  inline int32_t writeContainerElementEnd();
115 
116  inline int32_t writeFieldBegin(const char* name,
117  const TType fieldType,
118  const int16_t fieldId,
119  const std::map<std::string, std::string> *const amap);
120 
121  inline int32_t writeFieldEnd();
122 
123  inline int32_t writeFieldStop();
124 
125  inline int32_t writeMapBegin(const TType keyType,
126  const TType valType,
127  const uint32_t size);
128 
129  inline int32_t writeMapEnd();
130 
131  inline int32_t writeListBegin(const TType elemType, const uint32_t size);
132 
133  inline int32_t writeListEnd();
134 
135  inline int32_t writeSetBegin(const TType elemType, const uint32_t size);
136 
137  inline int32_t writeSetEnd();
138 
139  inline int32_t writeBool(const bool value);
140 
141  inline int32_t writeByte(const int8_t byte);
142 
143  inline int32_t writeI16(const int16_t i16);
144 
145  inline int32_t writeI32(const int32_t i32);
146 
147  inline int32_t writeI64(const int64_t i64);
148 
149  inline int32_t writeU16(const uint16_t u16);
150 
151  inline int32_t writeU32(const uint32_t u32);
152 
153  inline int32_t writeU64(const uint64_t u64);
154 
155  inline int32_t writeIPV4(const uint32_t ip4);
156 
157  inline int32_t writeIPADDR(const boost::asio::ip::address& ipaddress);
158 
159  inline int32_t writeDouble(const double dub);
160 
161  inline int32_t writeString(const std::string& str);
162 
163  inline int32_t writeBinary(const std::string& str);
164 
165  inline int32_t writeXML(const std::string& str);
166 
167  inline int32_t writeUUID(const boost::uuids::uuid& uuid);
168 
174  /*ol*/ int32_t readMessageBegin(std::string& name,
175  TMessageType& messageType,
176  int32_t& seqid);
177 
178  /*ol*/ int32_t readMessageEnd();
179 
180  inline int32_t readStructBegin(std::string& name);
181 
182  inline int32_t readStructEnd();
183 
184  int32_t readSandeshBegin(std::string& name);
185 
186  int32_t readSandeshEnd();
187 
188  inline int32_t readContainerElementBegin();
189 
190  inline int32_t readContainerElementEnd();
191 
192  inline int32_t readFieldBegin(std::string& name,
193  TType& fieldType,
194  int16_t& fieldId);
195 
196  inline int32_t readFieldEnd();
197 
198  inline int32_t readMapBegin(TType& keyType,
199  TType& valType,
200  uint32_t& size);
201 
202  inline int32_t readMapEnd();
203 
204  inline int32_t readListBegin(TType& elemType, uint32_t& size);
205 
206  inline int32_t readListEnd();
207 
208  inline int32_t readSetBegin(TType& elemType, uint32_t& size);
209 
210  inline int32_t readSetEnd();
211 
212  inline int32_t readBool(bool& value);
213  // Provide the default readBool() implementation for std::vector<bool>
215 
216  inline int32_t readByte(int8_t& byte);
217 
218  inline int32_t readI16(int16_t& i16);
219 
220  inline int32_t readI32(int32_t& i32);
221 
222  inline int32_t readI64(int64_t& i64);
223 
224  inline int32_t readU16(uint16_t& u16);
225 
226  inline int32_t readU32(uint32_t& u32);
227 
228  inline int32_t readU64(uint64_t& u64);
229 
230  inline int32_t readIPV4(uint32_t& ip4);
231 
232  inline int32_t readIPADDR(boost::asio::ip::address& ipaddress);
233 
234  inline int32_t readDouble(double& dub);
235 
236  inline int32_t readString(std::string& str);
237 
238  inline int32_t readBinary(std::string& str);
239 
240  inline int32_t readXML(std::string& str);
241 
242  inline int32_t readUUID(boost::uuids::uuid& uuid);
243 
244  protected:
245  int32_t readStringBody(std::string& str, int32_t sz);
246 
247  Transport_* trans_;
248 
249  int32_t string_limit_;
251 
252  // Enforce presence of version identifier
255 
256  // Buffer for reading strings, save for the lifetime of the protocol to
257  // avoid memory churn allocating memory on every string read
258  uint8_t* string_buf_;
260 
261 };
262 
264 
268 template <class Transport_>
270  public:
272  string_limit_(0),
273  container_limit_(0),
274  strict_read_(false),
275  strict_write_(true) {}
276 
277  TBinaryProtocolFactoryT(int32_t string_limit, int32_t container_limit,
278  bool strict_read, bool strict_write) :
279  string_limit_(string_limit),
280  container_limit_(container_limit),
281  strict_read_(strict_read),
282  strict_write_(strict_write) {}
283 
285 
286  void setStringSizeLimit(int32_t string_limit) {
287  string_limit_ = string_limit;
288  }
289 
290  void setContainerSizeLimit(int32_t container_limit) {
291  container_limit_ = container_limit;
292  }
293 
294  void setStrict(bool strict_read, bool strict_write) {
295  strict_read_ = strict_read;
296  strict_write_ = strict_write;
297  }
298 
299  boost::shared_ptr<TProtocol> getProtocol(boost::shared_ptr<TTransport> trans) {
300  boost::shared_ptr<Transport_> specific_trans =
301  boost::dynamic_pointer_cast<Transport_>(trans);
302  TProtocol* prot;
303  if (specific_trans) {
304  prot = new TBinaryProtocolT<Transport_>(specific_trans, string_limit_,
306  strict_write_);
307  } else {
310  }
311 
312  return boost::shared_ptr<TProtocol>(prot);
313  }
314 
315  private:
316  int32_t string_limit_;
320 
321 };
322 
324 
325 }}} // contrail::sandesh::protocol
326 
327 #include "sandesh/library/cpp/protocol/TBinaryProtocol.tcc"
328 
329 #endif // #ifndef _SANDESH_PROTOCOL_TBINARYPROTOCOL_H_
int32_t writeSetBegin(const TType elemType, const uint32_t size)
TBinaryProtocolT(boost::shared_ptr< Transport_ > trans, int32_t string_limit, int32_t container_limit, bool strict_read, bool strict_write)
int32_t writeUUID(const boost::uuids::uuid &uuid)
int32_t writeXML(const std::string &str)
int32_t writeFieldBegin(const char *name, const TType fieldType, const int16_t fieldId, const std::map< std::string, std::string > *const amap)
boost::shared_ptr< TProtocol > getProtocol(boost::shared_ptr< TTransport > trans)
TBinaryProtocolFactoryT(int32_t string_limit, int32_t container_limit, bool strict_read, bool strict_write)
int32_t readUUID(boost::uuids::uuid &uuid)
int32_t readSandeshBegin(std::string &name)
boost::uuids::uuid uuid
int32_t writeStructBegin(const char *name)
int32_t writeString(const std::string &str)
int32_t readSetBegin(TType &elemType, uint32_t &size)
int32_t readMapBegin(TType &keyType, TType &valType, uint32_t &size)
TBinaryProtocolT(boost::shared_ptr< Transport_ > trans)
void setContainerSizeLimit(int32_t container_limit)
int32_t writeIPADDR(const boost::asio::ip::address &ipaddress)
void setStrict(bool strict_read, bool strict_write)
TBinaryProtocolT< TTransport > TBinaryProtocol
int32_t readStringBody(std::string &str, int32_t sz)
void setStrict(bool strict_read, bool strict_write)
int32_t readStructBegin(std::string &name)
int32_t writeMessageBegin(const std::string &name, const TMessageType messageType, const int32_t seqid)
void setStringSizeLimit(int32_t string_limit)
int32_t writeListBegin(const TType elemType, const uint32_t size)
int32_t writeBinary(const std::string &str)
int32_t readListBegin(TType &elemType, uint32_t &size)
int32_t readIPADDR(boost::asio::ip::address &ipaddress)
TBinaryProtocolFactoryT< TTransport > TBinaryProtocolFactory
int32_t writeMapBegin(const TType keyType, const TType valType, const uint32_t size)
int32_t readMessageBegin(std::string &name, TMessageType &messageType, int32_t &seqid)
int32_t writeSandeshBegin(const char *name)
int32_t readFieldBegin(std::string &name, TType &fieldType, int16_t &fieldId)