Subversion Repositories SmartDukaan

Rev

Rev 30 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
30 ashish 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
 
20
#ifndef _THRIFT_PROTOCOL_TBINARYPROTOCOL_H_
21
#define _THRIFT_PROTOCOL_TBINARYPROTOCOL_H_ 1
22
 
23
#include "TProtocol.h"
24
 
25
#include <boost/shared_ptr.hpp>
26
 
27
namespace apache { namespace thrift { namespace protocol {
28
 
29
/**
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
 *
33
 */
34
class TBinaryProtocol : public TProtocol {
35
 protected:
36
  static const int32_t VERSION_MASK = 0xffff0000;
37
  static const int32_t VERSION_1 = 0x80010000;
38
  // VERSION_2 (0x80020000)  is taken by TDenseProtocol.
39
 
40
 public:
41
  TBinaryProtocol(boost::shared_ptr<TTransport> trans) :
42
    TProtocol(trans),
43
    string_limit_(0),
44
    container_limit_(0),
45
    strict_read_(false),
46
    strict_write_(true),
47
    string_buf_(NULL),
48
    string_buf_size_(0) {}
49
 
50
  TBinaryProtocol(boost::shared_ptr<TTransport> trans,
51
                  int32_t string_limit,
52
                  int32_t container_limit,
53
                  bool strict_read,
54
                  bool strict_write) :
55
    TProtocol(trans),
56
    string_limit_(string_limit),
57
    container_limit_(container_limit),
58
    strict_read_(strict_read),
59
    strict_write_(strict_write),
60
    string_buf_(NULL),
61
    string_buf_size_(0) {}
62
 
63
  ~TBinaryProtocol() {
64
    if (string_buf_ != NULL) {
65
      std::free(string_buf_);
66
      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
  }
77
 
78
  void setStrict(bool strict_read, bool strict_write) {
79
    strict_read_ = strict_read;
80
    strict_write_ = strict_write;
81
  }
82
 
83
  /**
84
   * Writing functions.
85
   */
86
 
87
  virtual uint32_t writeMessageBegin(const std::string& name,
88
                                     const TMessageType messageType,
89
                                     const int32_t seqid);
90
 
91
  virtual uint32_t writeMessageEnd();
92
 
93
 
94
  uint32_t writeStructBegin(const char* name);
95
 
96
  uint32_t writeStructEnd();
97
 
98
  uint32_t writeFieldBegin(const char* name,
99
                           const TType fieldType,
100
                           const int16_t fieldId);
101
 
102
  uint32_t writeFieldEnd();
103
 
104
  uint32_t writeFieldStop();
105
 
106
  uint32_t writeMapBegin(const TType keyType,
107
                         const TType valType,
108
                         const uint32_t size);
109
 
110
  uint32_t writeMapEnd();
111
 
112
  uint32_t writeListBegin(const TType elemType,
113
                          const uint32_t size);
114
 
115
  uint32_t writeListEnd();
116
 
117
  uint32_t writeSetBegin(const TType elemType,
118
                         const uint32_t size);
119
 
120
  uint32_t writeSetEnd();
121
 
122
  uint32_t writeBool(const bool value);
123
 
124
  uint32_t writeByte(const int8_t byte);
125
 
126
  uint32_t writeI16(const int16_t i16);
127
 
128
  uint32_t writeI32(const int32_t i32);
129
 
130
  uint32_t writeI64(const int64_t i64);
131
 
132
  uint32_t writeDouble(const double dub);
133
 
134
  uint32_t writeString(const std::string& str);
135
 
136
  uint32_t writeBinary(const std::string& str);
137
 
138
  /**
139
   * Reading functions
140
   */
141
 
142
 
143
  uint32_t readMessageBegin(std::string& name,
144
                            TMessageType& messageType,
145
                            int32_t& seqid);
146
 
147
  uint32_t readMessageEnd();
148
 
149
  uint32_t readStructBegin(std::string& name);
150
 
151
  uint32_t readStructEnd();
152
 
153
  uint32_t readFieldBegin(std::string& name,
154
                          TType& fieldType,
155
                          int16_t& fieldId);
156
 
157
  uint32_t readFieldEnd();
158
 
159
  uint32_t readMapBegin(TType& keyType,
160
                        TType& valType,
161
                        uint32_t& size);
162
 
163
  uint32_t readMapEnd();
164
 
165
  uint32_t readListBegin(TType& elemType,
166
                         uint32_t& size);
167
 
168
  uint32_t readListEnd();
169
 
170
  uint32_t readSetBegin(TType& elemType,
171
                        uint32_t& size);
172
 
173
  uint32_t readSetEnd();
174
 
175
  uint32_t readBool(bool& value);
176
 
177
  uint32_t readByte(int8_t& byte);
178
 
179
  uint32_t readI16(int16_t& i16);
180
 
181
  uint32_t readI32(int32_t& i32);
182
 
183
  uint32_t readI64(int64_t& i64);
184
 
185
  uint32_t readDouble(double& dub);
186
 
187
  uint32_t readString(std::string& str);
188
 
189
  uint32_t readBinary(std::string& str);
190
 
191
 protected:
192
  uint32_t readStringBody(std::string& str, int32_t sz);
193
 
194
  int32_t string_limit_;
195
  int32_t container_limit_;
196
 
197
  // Enforce presence of version identifier
198
  bool strict_read_;
199
  bool strict_write_;
200
 
201
  // 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
 
206
};
207
 
208
/**
209
 * Constructs binary protocol handlers
210
 */
211
class TBinaryProtocolFactory : public TProtocolFactory {
212
 public:
213
  TBinaryProtocolFactory() :
214
    string_limit_(0),
215
    container_limit_(0),
216
    strict_read_(false),
217
    strict_write_(true) {}
218
 
219
  TBinaryProtocolFactory(int32_t string_limit, int32_t container_limit, bool strict_read, bool strict_write) :
220
    string_limit_(string_limit),
221
    container_limit_(container_limit),
222
    strict_read_(strict_read),
223
    strict_write_(strict_write) {}
224
 
225
  virtual ~TBinaryProtocolFactory() {}
226
 
227
  void setStringSizeLimit(int32_t string_limit) {
228
    string_limit_ = string_limit;
229
  }
230
 
231
  void setContainerSizeLimit(int32_t container_limit) {
232
    container_limit_ = container_limit;
233
  }
234
 
235
  void setStrict(bool strict_read, bool strict_write) {
236
    strict_read_ = strict_read;
237
    strict_write_ = strict_write;
238
  }
239
 
240
  boost::shared_ptr<TProtocol> getProtocol(boost::shared_ptr<TTransport> trans) {
241
    return boost::shared_ptr<TProtocol>(new TBinaryProtocol(trans, string_limit_, container_limit_, strict_read_, strict_write_));
242
  }
243
 
244
 private:
245
  int32_t string_limit_;
246
  int32_t container_limit_;
247
  bool strict_read_;
248
  bool strict_write_;
249
 
250
};
251
 
252
}}} // apache::thrift::protocol
253
 
254
#endif // #ifndef _THRIFT_PROTOCOL_TBINARYPROTOCOL_H_