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