| 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_TPROTOCOL_H_
|
|
|
21 |
#define _THRIFT_PROTOCOL_TPROTOCOL_H_ 1
|
|
|
22 |
|
|
|
23 |
#include <transport/TTransport.h>
|
|
|
24 |
#include <protocol/TProtocolException.h>
|
|
|
25 |
|
|
|
26 |
#include <boost/shared_ptr.hpp>
|
|
|
27 |
#include <boost/static_assert.hpp>
|
|
|
28 |
|
|
|
29 |
#include <netinet/in.h>
|
|
|
30 |
#include <sys/types.h>
|
|
|
31 |
#include <string>
|
|
|
32 |
#include <map>
|
|
|
33 |
|
|
|
34 |
|
|
|
35 |
// Use this to get around strict aliasing rules.
|
|
|
36 |
// For example, uint64_t i = bitwise_cast<uint64_t>(returns_double());
|
|
|
37 |
// The most obvious implementation is to just cast a pointer,
|
|
|
38 |
// but that doesn't work.
|
|
|
39 |
// For a pretty in-depth explanation of the problem, see
|
|
|
40 |
// http://www.cellperformance.com/mike_acton/2006/06/ (...)
|
|
|
41 |
// understanding_strict_aliasing.html
|
|
|
42 |
template <typename To, typename From>
|
|
|
43 |
static inline To bitwise_cast(From from) {
|
|
|
44 |
BOOST_STATIC_ASSERT(sizeof(From) == sizeof(To));
|
|
|
45 |
|
|
|
46 |
// BAD!!! These are all broken with -O2.
|
|
|
47 |
//return *reinterpret_cast<To*>(&from); // BAD!!!
|
|
|
48 |
//return *static_cast<To*>(static_cast<void*>(&from)); // BAD!!!
|
|
|
49 |
//return *(To*)(void*)&from; // BAD!!!
|
|
|
50 |
|
|
|
51 |
// Super clean and paritally blessed by section 3.9 of the standard.
|
|
|
52 |
//unsigned char c[sizeof(from)];
|
|
|
53 |
//memcpy(c, &from, sizeof(from));
|
|
|
54 |
//To to;
|
|
|
55 |
//memcpy(&to, c, sizeof(c));
|
|
|
56 |
//return to;
|
|
|
57 |
|
|
|
58 |
// Slightly more questionable.
|
|
|
59 |
// Same code emitted by GCC.
|
|
|
60 |
//To to;
|
|
|
61 |
//memcpy(&to, &from, sizeof(from));
|
|
|
62 |
//return to;
|
|
|
63 |
|
|
|
64 |
// Technically undefined, but almost universally supported,
|
|
|
65 |
// and the most efficient implementation.
|
|
|
66 |
union {
|
|
|
67 |
From f;
|
|
|
68 |
To t;
|
|
|
69 |
} u;
|
|
|
70 |
u.f = from;
|
|
|
71 |
return u.t;
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
|
|
|
75 |
namespace apache { namespace thrift { namespace protocol {
|
|
|
76 |
|
|
|
77 |
using apache::thrift::transport::TTransport;
|
|
|
78 |
|
|
|
79 |
#ifdef HAVE_ENDIAN_H
|
|
|
80 |
#include <endian.h>
|
|
|
81 |
#endif
|
|
|
82 |
|
|
|
83 |
#ifndef __BYTE_ORDER
|
|
|
84 |
# if defined(BYTE_ORDER) && defined(LITTLE_ENDIAN) && defined(BIG_ENDIAN)
|
|
|
85 |
# define __BYTE_ORDER BYTE_ORDER
|
|
|
86 |
# define __LITTLE_ENDIAN LITTLE_ENDIAN
|
|
|
87 |
# define __BIG_ENDIAN BIG_ENDIAN
|
|
|
88 |
# else
|
|
|
89 |
# include <boost/config.hpp>
|
|
|
90 |
# define __BYTE_ORDER BOOST_BYTE_ORDER
|
|
|
91 |
# define __LITTLE_ENDIAN BOOST_LITTLE_ENDIAN
|
|
|
92 |
# define __BIG_ENDIAN BOOST_BIG_ENDIAN
|
|
|
93 |
# endif
|
|
|
94 |
#endif
|
|
|
95 |
|
|
|
96 |
#if __BYTE_ORDER == __BIG_ENDIAN
|
|
|
97 |
# define ntohll(n) (n)
|
|
|
98 |
# define htonll(n) (n)
|
|
|
99 |
# if defined(__GNUC__) && defined(__GLIBC__)
|
|
|
100 |
# include <byteswap.h>
|
|
|
101 |
# define htolell(n) bswap_64(n)
|
|
|
102 |
# define letohll(n) bswap_64(n)
|
|
|
103 |
# else /* GNUC & GLIBC */
|
|
|
104 |
# define bswap_64(n) \
|
|
|
105 |
( (((n) & 0xff00000000000000ull) >> 56) \
|
|
|
106 |
| (((n) & 0x00ff000000000000ull) >> 40) \
|
|
|
107 |
| (((n) & 0x0000ff0000000000ull) >> 24) \
|
|
|
108 |
| (((n) & 0x000000ff00000000ull) >> 8) \
|
|
|
109 |
| (((n) & 0x00000000ff000000ull) << 8) \
|
|
|
110 |
| (((n) & 0x0000000000ff0000ull) << 24) \
|
|
|
111 |
| (((n) & 0x000000000000ff00ull) << 40) \
|
|
|
112 |
| (((n) & 0x00000000000000ffull) << 56) )
|
|
|
113 |
# define htolell(n) bswap_64(n)
|
|
|
114 |
# define letohll(n) bswap_64(n)
|
|
|
115 |
# endif /* GNUC & GLIBC */
|
|
|
116 |
#elif __BYTE_ORDER == __LITTLE_ENDIAN
|
|
|
117 |
# define htolell(n) (n)
|
|
|
118 |
# define letohll(n) (n)
|
|
|
119 |
# if defined(__GNUC__) && defined(__GLIBC__)
|
|
|
120 |
# include <byteswap.h>
|
|
|
121 |
# define ntohll(n) bswap_64(n)
|
|
|
122 |
# define htonll(n) bswap_64(n)
|
|
|
123 |
# else /* GNUC & GLIBC */
|
|
|
124 |
# define ntohll(n) ( (((unsigned long long)ntohl(n)) << 32) + ntohl(n >> 32) )
|
|
|
125 |
# define htonll(n) ( (((unsigned long long)htonl(n)) << 32) + htonl(n >> 32) )
|
|
|
126 |
# endif /* GNUC & GLIBC */
|
|
|
127 |
#else /* __BYTE_ORDER */
|
|
|
128 |
# error "Can't define htonll or ntohll!"
|
|
|
129 |
#endif
|
|
|
130 |
|
|
|
131 |
/**
|
|
|
132 |
* Enumerated definition of the types that the Thrift protocol supports.
|
|
|
133 |
* Take special note of the T_END type which is used specifically to mark
|
|
|
134 |
* the end of a sequence of fields.
|
|
|
135 |
*/
|
|
|
136 |
enum TType {
|
|
|
137 |
T_STOP = 0,
|
|
|
138 |
T_VOID = 1,
|
|
|
139 |
T_BOOL = 2,
|
|
|
140 |
T_BYTE = 3,
|
|
|
141 |
T_I08 = 3,
|
|
|
142 |
T_I16 = 6,
|
|
|
143 |
T_I32 = 8,
|
|
|
144 |
T_U64 = 9,
|
|
|
145 |
T_I64 = 10,
|
|
|
146 |
T_DOUBLE = 4,
|
|
|
147 |
T_STRING = 11,
|
|
|
148 |
T_UTF7 = 11,
|
|
|
149 |
T_STRUCT = 12,
|
|
|
150 |
T_MAP = 13,
|
|
|
151 |
T_SET = 14,
|
|
|
152 |
T_LIST = 15,
|
|
|
153 |
T_UTF8 = 16,
|
|
|
154 |
T_UTF16 = 17
|
|
|
155 |
};
|
|
|
156 |
|
|
|
157 |
/**
|
|
|
158 |
* Enumerated definition of the message types that the Thrift protocol
|
|
|
159 |
* supports.
|
|
|
160 |
*/
|
|
|
161 |
enum TMessageType {
|
|
|
162 |
T_CALL = 1,
|
|
|
163 |
T_REPLY = 2,
|
|
|
164 |
T_EXCEPTION = 3,
|
|
|
165 |
T_ONEWAY = 4
|
|
|
166 |
};
|
|
|
167 |
|
|
|
168 |
/**
|
|
|
169 |
* Abstract class for a thrift protocol driver. These are all the methods that
|
|
|
170 |
* a protocol must implement. Essentially, there must be some way of reading
|
|
|
171 |
* and writing all the base types, plus a mechanism for writing out structs
|
|
|
172 |
* with indexed fields.
|
|
|
173 |
*
|
|
|
174 |
* TProtocol objects should not be shared across multiple encoding contexts,
|
|
|
175 |
* as they may need to maintain internal state in some protocols (i.e. XML).
|
|
|
176 |
* Note that is is acceptable for the TProtocol module to do its own internal
|
|
|
177 |
* buffered reads/writes to the underlying TTransport where appropriate (i.e.
|
|
|
178 |
* when parsing an input XML stream, reading should be batched rather than
|
|
|
179 |
* looking ahead character by character for a close tag).
|
|
|
180 |
*
|
|
|
181 |
*/
|
|
|
182 |
class TProtocol {
|
|
|
183 |
public:
|
|
|
184 |
virtual ~TProtocol() {}
|
|
|
185 |
|
|
|
186 |
/**
|
|
|
187 |
* Writing functions.
|
|
|
188 |
*/
|
|
|
189 |
|
|
|
190 |
virtual uint32_t writeMessageBegin(const std::string& name,
|
|
|
191 |
const TMessageType messageType,
|
|
|
192 |
const int32_t seqid) = 0;
|
|
|
193 |
|
|
|
194 |
virtual uint32_t writeMessageEnd() = 0;
|
|
|
195 |
|
|
|
196 |
|
|
|
197 |
virtual uint32_t writeStructBegin(const char* name) = 0;
|
|
|
198 |
|
|
|
199 |
virtual uint32_t writeStructEnd() = 0;
|
|
|
200 |
|
|
|
201 |
virtual uint32_t writeFieldBegin(const char* name,
|
|
|
202 |
const TType fieldType,
|
|
|
203 |
const int16_t fieldId) = 0;
|
|
|
204 |
|
|
|
205 |
virtual uint32_t writeFieldEnd() = 0;
|
|
|
206 |
|
|
|
207 |
virtual uint32_t writeFieldStop() = 0;
|
|
|
208 |
|
|
|
209 |
virtual uint32_t writeMapBegin(const TType keyType,
|
|
|
210 |
const TType valType,
|
|
|
211 |
const uint32_t size) = 0;
|
|
|
212 |
|
|
|
213 |
virtual uint32_t writeMapEnd() = 0;
|
|
|
214 |
|
|
|
215 |
virtual uint32_t writeListBegin(const TType elemType,
|
|
|
216 |
const uint32_t size) = 0;
|
|
|
217 |
|
|
|
218 |
virtual uint32_t writeListEnd() = 0;
|
|
|
219 |
|
|
|
220 |
virtual uint32_t writeSetBegin(const TType elemType,
|
|
|
221 |
const uint32_t size) = 0;
|
|
|
222 |
|
|
|
223 |
virtual uint32_t writeSetEnd() = 0;
|
|
|
224 |
|
|
|
225 |
virtual uint32_t writeBool(const bool value) = 0;
|
|
|
226 |
|
|
|
227 |
virtual uint32_t writeByte(const int8_t byte) = 0;
|
|
|
228 |
|
|
|
229 |
virtual uint32_t writeI16(const int16_t i16) = 0;
|
|
|
230 |
|
|
|
231 |
virtual uint32_t writeI32(const int32_t i32) = 0;
|
|
|
232 |
|
|
|
233 |
virtual uint32_t writeI64(const int64_t i64) = 0;
|
|
|
234 |
|
|
|
235 |
virtual uint32_t writeDouble(const double dub) = 0;
|
|
|
236 |
|
|
|
237 |
virtual uint32_t writeString(const std::string& str) = 0;
|
|
|
238 |
|
|
|
239 |
virtual uint32_t writeBinary(const std::string& str) = 0;
|
|
|
240 |
|
|
|
241 |
/**
|
|
|
242 |
* Reading functions
|
|
|
243 |
*/
|
|
|
244 |
|
|
|
245 |
virtual uint32_t readMessageBegin(std::string& name,
|
|
|
246 |
TMessageType& messageType,
|
|
|
247 |
int32_t& seqid) = 0;
|
|
|
248 |
|
|
|
249 |
virtual uint32_t readMessageEnd() = 0;
|
|
|
250 |
|
|
|
251 |
virtual uint32_t readStructBegin(std::string& name) = 0;
|
|
|
252 |
|
|
|
253 |
virtual uint32_t readStructEnd() = 0;
|
|
|
254 |
|
|
|
255 |
virtual uint32_t readFieldBegin(std::string& name,
|
|
|
256 |
TType& fieldType,
|
|
|
257 |
int16_t& fieldId) = 0;
|
|
|
258 |
|
|
|
259 |
virtual uint32_t readFieldEnd() = 0;
|
|
|
260 |
|
|
|
261 |
virtual uint32_t readMapBegin(TType& keyType,
|
|
|
262 |
TType& valType,
|
|
|
263 |
uint32_t& size) = 0;
|
|
|
264 |
|
|
|
265 |
virtual uint32_t readMapEnd() = 0;
|
|
|
266 |
|
|
|
267 |
virtual uint32_t readListBegin(TType& elemType,
|
|
|
268 |
uint32_t& size) = 0;
|
|
|
269 |
|
|
|
270 |
virtual uint32_t readListEnd() = 0;
|
|
|
271 |
|
|
|
272 |
virtual uint32_t readSetBegin(TType& elemType,
|
|
|
273 |
uint32_t& size) = 0;
|
|
|
274 |
|
|
|
275 |
virtual uint32_t readSetEnd() = 0;
|
|
|
276 |
|
|
|
277 |
virtual uint32_t readBool(bool& value) = 0;
|
|
|
278 |
|
|
|
279 |
virtual uint32_t readByte(int8_t& byte) = 0;
|
|
|
280 |
|
|
|
281 |
virtual uint32_t readI16(int16_t& i16) = 0;
|
|
|
282 |
|
|
|
283 |
virtual uint32_t readI32(int32_t& i32) = 0;
|
|
|
284 |
|
|
|
285 |
virtual uint32_t readI64(int64_t& i64) = 0;
|
|
|
286 |
|
|
|
287 |
virtual uint32_t readDouble(double& dub) = 0;
|
|
|
288 |
|
|
|
289 |
virtual uint32_t readString(std::string& str) = 0;
|
|
|
290 |
|
|
|
291 |
virtual uint32_t readBinary(std::string& str) = 0;
|
|
|
292 |
|
|
|
293 |
uint32_t readBool(std::vector<bool>::reference ref) {
|
|
|
294 |
bool value;
|
|
|
295 |
uint32_t rv = readBool(value);
|
|
|
296 |
ref = value;
|
|
|
297 |
return rv;
|
|
|
298 |
}
|
|
|
299 |
|
|
|
300 |
/**
|
|
|
301 |
* Method to arbitrarily skip over data.
|
|
|
302 |
*/
|
|
|
303 |
uint32_t skip(TType type) {
|
|
|
304 |
switch (type) {
|
|
|
305 |
case T_BOOL:
|
|
|
306 |
{
|
|
|
307 |
bool boolv;
|
|
|
308 |
return readBool(boolv);
|
|
|
309 |
}
|
|
|
310 |
case T_BYTE:
|
|
|
311 |
{
|
|
|
312 |
int8_t bytev;
|
|
|
313 |
return readByte(bytev);
|
|
|
314 |
}
|
|
|
315 |
case T_I16:
|
|
|
316 |
{
|
|
|
317 |
int16_t i16;
|
|
|
318 |
return readI16(i16);
|
|
|
319 |
}
|
|
|
320 |
case T_I32:
|
|
|
321 |
{
|
|
|
322 |
int32_t i32;
|
|
|
323 |
return readI32(i32);
|
|
|
324 |
}
|
|
|
325 |
case T_I64:
|
|
|
326 |
{
|
|
|
327 |
int64_t i64;
|
|
|
328 |
return readI64(i64);
|
|
|
329 |
}
|
|
|
330 |
case T_DOUBLE:
|
|
|
331 |
{
|
|
|
332 |
double dub;
|
|
|
333 |
return readDouble(dub);
|
|
|
334 |
}
|
|
|
335 |
case T_STRING:
|
|
|
336 |
{
|
|
|
337 |
std::string str;
|
|
|
338 |
return readBinary(str);
|
|
|
339 |
}
|
|
|
340 |
case T_STRUCT:
|
|
|
341 |
{
|
|
|
342 |
uint32_t result = 0;
|
|
|
343 |
std::string name;
|
|
|
344 |
int16_t fid;
|
|
|
345 |
TType ftype;
|
|
|
346 |
result += readStructBegin(name);
|
|
|
347 |
while (true) {
|
|
|
348 |
result += readFieldBegin(name, ftype, fid);
|
|
|
349 |
if (ftype == T_STOP) {
|
|
|
350 |
break;
|
|
|
351 |
}
|
|
|
352 |
result += skip(ftype);
|
|
|
353 |
result += readFieldEnd();
|
|
|
354 |
}
|
|
|
355 |
result += readStructEnd();
|
|
|
356 |
return result;
|
|
|
357 |
}
|
|
|
358 |
case T_MAP:
|
|
|
359 |
{
|
|
|
360 |
uint32_t result = 0;
|
|
|
361 |
TType keyType;
|
|
|
362 |
TType valType;
|
|
|
363 |
uint32_t i, size;
|
|
|
364 |
result += readMapBegin(keyType, valType, size);
|
|
|
365 |
for (i = 0; i < size; i++) {
|
|
|
366 |
result += skip(keyType);
|
|
|
367 |
result += skip(valType);
|
|
|
368 |
}
|
|
|
369 |
result += readMapEnd();
|
|
|
370 |
return result;
|
|
|
371 |
}
|
|
|
372 |
case T_SET:
|
|
|
373 |
{
|
|
|
374 |
uint32_t result = 0;
|
|
|
375 |
TType elemType;
|
|
|
376 |
uint32_t i, size;
|
|
|
377 |
result += readSetBegin(elemType, size);
|
|
|
378 |
for (i = 0; i < size; i++) {
|
|
|
379 |
result += skip(elemType);
|
|
|
380 |
}
|
|
|
381 |
result += readSetEnd();
|
|
|
382 |
return result;
|
|
|
383 |
}
|
|
|
384 |
case T_LIST:
|
|
|
385 |
{
|
|
|
386 |
uint32_t result = 0;
|
|
|
387 |
TType elemType;
|
|
|
388 |
uint32_t i, size;
|
|
|
389 |
result += readListBegin(elemType, size);
|
|
|
390 |
for (i = 0; i < size; i++) {
|
|
|
391 |
result += skip(elemType);
|
|
|
392 |
}
|
|
|
393 |
result += readListEnd();
|
|
|
394 |
return result;
|
|
|
395 |
}
|
|
|
396 |
default:
|
|
|
397 |
return 0;
|
|
|
398 |
}
|
|
|
399 |
}
|
|
|
400 |
|
|
|
401 |
inline boost::shared_ptr<TTransport> getTransport() {
|
|
|
402 |
return ptrans_;
|
|
|
403 |
}
|
|
|
404 |
|
|
|
405 |
// TODO: remove these two calls, they are for backwards
|
|
|
406 |
// compatibility
|
|
|
407 |
inline boost::shared_ptr<TTransport> getInputTransport() {
|
|
|
408 |
return ptrans_;
|
|
|
409 |
}
|
|
|
410 |
inline boost::shared_ptr<TTransport> getOutputTransport() {
|
|
|
411 |
return ptrans_;
|
|
|
412 |
}
|
|
|
413 |
|
|
|
414 |
protected:
|
|
|
415 |
TProtocol(boost::shared_ptr<TTransport> ptrans):
|
|
|
416 |
ptrans_(ptrans) {
|
|
|
417 |
trans_ = ptrans.get();
|
|
|
418 |
}
|
|
|
419 |
|
|
|
420 |
boost::shared_ptr<TTransport> ptrans_;
|
|
|
421 |
TTransport* trans_;
|
|
|
422 |
|
|
|
423 |
private:
|
|
|
424 |
TProtocol() {}
|
|
|
425 |
};
|
|
|
426 |
|
|
|
427 |
/**
|
|
|
428 |
* Constructs input and output protocol objects given transports.
|
|
|
429 |
*/
|
|
|
430 |
class TProtocolFactory {
|
|
|
431 |
public:
|
|
|
432 |
TProtocolFactory() {}
|
|
|
433 |
|
|
|
434 |
virtual ~TProtocolFactory() {}
|
|
|
435 |
|
|
|
436 |
virtual boost::shared_ptr<TProtocol> getProtocol(boost::shared_ptr<TTransport> trans) = 0;
|
|
|
437 |
};
|
|
|
438 |
|
|
|
439 |
}}} // apache::thrift::protocol
|
|
|
440 |
|
|
|
441 |
#endif // #define _THRIFT_PROTOCOL_TPROTOCOL_H_ 1
|