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 | #ifdef _WIN32 |
24 | // Need to come before any Windows.h includes |
25 | #include <Winsock2.h> |
26 | #endif |
27 | |
28 | #include <thrift/transport/TTransport.h> |
29 | #include <thrift/protocol/TProtocolException.h> |
30 | |
31 | #include <thrift/stdcxx.h> |
32 | #include <boost/static_assert.hpp> |
33 | |
34 | #ifdef HAVE_NETINET_IN_H |
35 | #include <netinet/in.h> |
36 | #endif |
37 | #include <sys/types.h> |
38 | #include <string> |
39 | #include <map> |
40 | #include <vector> |
41 | #include <climits> |
42 | |
43 | // Use this to get around strict aliasing rules. |
44 | // For example, uint64_t i = bitwise_cast<uint64_t>(returns_double()); |
45 | // The most obvious implementation is to just cast a pointer, |
46 | // but that doesn't work. |
47 | // For a pretty in-depth explanation of the problem, see |
48 | // http://cellperformance.beyond3d.com/articles/2006/06/understanding-strict-aliasing.html |
49 | template <typename To, typename From> |
50 | static inline To bitwise_cast(From from) { |
51 | BOOST_STATIC_ASSERT(sizeof(From) == sizeof(To)); |
52 | |
53 | // BAD!!! These are all broken with -O2. |
54 | //return *reinterpret_cast<To*>(&from); // BAD!!! |
55 | //return *static_cast<To*>(static_cast<void*>(&from)); // BAD!!! |
56 | //return *(To*)(void*)&from; // BAD!!! |
57 | |
58 | // Super clean and paritally blessed by section 3.9 of the standard. |
59 | //unsigned char c[sizeof(from)]; |
60 | //memcpy(c, &from, sizeof(from)); |
61 | //To to; |
62 | //memcpy(&to, c, sizeof(c)); |
63 | //return to; |
64 | |
65 | // Slightly more questionable. |
66 | // Same code emitted by GCC. |
67 | //To to; |
68 | //memcpy(&to, &from, sizeof(from)); |
69 | //return to; |
70 | |
71 | // Technically undefined, but almost universally supported, |
72 | // and the most efficient implementation. |
73 | union { |
74 | From f; |
75 | To t; |
76 | } u; |
77 | u.f = from; |
78 | return u.t; |
79 | } |
80 | |
81 | |
82 | #ifdef HAVE_SYS_PARAM_H |
83 | #include <sys/param.h> |
84 | #endif |
85 | |
86 | #ifndef __THRIFT_BYTE_ORDER |
87 | # if defined(BYTE_ORDER) && defined(LITTLE_ENDIAN) && defined(BIG_ENDIAN) |
88 | # define __THRIFT_BYTE_ORDER BYTE_ORDER |
89 | # define __THRIFT_LITTLE_ENDIAN LITTLE_ENDIAN |
90 | # define __THRIFT_BIG_ENDIAN BIG_ENDIAN |
91 | # else |
92 | # include <boost/config.hpp> |
93 | # include <boost/detail/endian.hpp> |
94 | # define __THRIFT_BYTE_ORDER BOOST_BYTE_ORDER |
95 | # ifdef BOOST_LITTLE_ENDIAN |
96 | # define __THRIFT_LITTLE_ENDIAN __THRIFT_BYTE_ORDER |
97 | # define __THRIFT_BIG_ENDIAN 0 |
98 | # else |
99 | # define __THRIFT_LITTLE_ENDIAN 0 |
100 | # define __THRIFT_BIG_ENDIAN __THRIFT_BYTE_ORDER |
101 | # endif |
102 | # endif |
103 | #endif |
104 | |
105 | #if __THRIFT_BYTE_ORDER == __THRIFT_BIG_ENDIAN |
106 | # if !defined(THRIFT_ntohll) |
107 | # define THRIFT_ntohll(n) (n) |
108 | # define THRIFT_htonll(n) (n) |
109 | # endif |
110 | # if defined(__GNUC__) && defined(__GLIBC__) |
111 | # include <byteswap.h> |
112 | # define THRIFT_htolell(n) bswap_64(n) |
113 | # define THRIFT_letohll(n) bswap_64(n) |
114 | # define THRIFT_htolel(n) bswap_32(n) |
115 | # define THRIFT_letohl(n) bswap_32(n) |
116 | # define THRIFT_htoles(n) bswap_16(n) |
117 | # define THRIFT_letohs(n) bswap_16(n) |
118 | # else /* GNUC & GLIBC */ |
119 | # define bswap_64(n) \ |
120 | ( (((n) & 0xff00000000000000ull) >> 56) \ |
121 | | (((n) & 0x00ff000000000000ull) >> 40) \ |
122 | | (((n) & 0x0000ff0000000000ull) >> 24) \ |
123 | | (((n) & 0x000000ff00000000ull) >> 8) \ |
124 | | (((n) & 0x00000000ff000000ull) << 8) \ |
125 | | (((n) & 0x0000000000ff0000ull) << 24) \ |
126 | | (((n) & 0x000000000000ff00ull) << 40) \ |
127 | | (((n) & 0x00000000000000ffull) << 56) ) |
128 | # define bswap_32(n) \ |
129 | ( (((n) & 0xff000000ul) >> 24) \ |
130 | | (((n) & 0x00ff0000ul) >> 8) \ |
131 | | (((n) & 0x0000ff00ul) << 8) \ |
132 | | (((n) & 0x000000fful) << 24) ) |
133 | # define bswap_16(n) \ |
134 | ( (((n) & ((unsigned short)0xff00ul)) >> 8) \ |
135 | | (((n) & ((unsigned short)0x00fful)) << 8) ) |
136 | # define THRIFT_htolell(n) bswap_64(n) |
137 | # define THRIFT_letohll(n) bswap_64(n) |
138 | # define THRIFT_htolel(n) bswap_32(n) |
139 | # define THRIFT_letohl(n) bswap_32(n) |
140 | # define THRIFT_htoles(n) bswap_16(n) |
141 | # define THRIFT_letohs(n) bswap_16(n) |
142 | # endif /* GNUC & GLIBC */ |
143 | #elif __THRIFT_BYTE_ORDER == __THRIFT_LITTLE_ENDIAN |
144 | # define THRIFT_htolell(n) (n) |
145 | # define THRIFT_letohll(n) (n) |
146 | # define THRIFT_htolel(n) (n) |
147 | # define THRIFT_letohl(n) (n) |
148 | # define THRIFT_htoles(n) (n) |
149 | # define THRIFT_letohs(n) (n) |
150 | # if defined(__GNUC__) && defined(__GLIBC__) |
151 | # include <byteswap.h> |
152 | # define THRIFT_ntohll(n) bswap_64(n) |
153 | # define THRIFT_htonll(n) bswap_64(n) |
154 | # elif defined(_MSC_VER) /* Microsoft Visual C++ */ |
155 | # define THRIFT_ntohll(n) ( _byteswap_uint64((uint64_t)n) ) |
156 | # define THRIFT_htonll(n) ( _byteswap_uint64((uint64_t)n) ) |
157 | # elif !defined(THRIFT_ntohll) /* Not GNUC/GLIBC or MSVC */ |
158 | # define THRIFT_ntohll(n) ( (((uint64_t)ntohl((uint32_t)n)) << 32) + ntohl((uint32_t)(n >> 32)) ) |
159 | # define THRIFT_htonll(n) ( (((uint64_t)htonl((uint32_t)n)) << 32) + htonl((uint32_t)(n >> 32)) ) |
160 | # endif /* GNUC/GLIBC or MSVC or something else */ |
161 | #else /* __THRIFT_BYTE_ORDER */ |
162 | # error "Can't define THRIFT_htonll or THRIFT_ntohll!" |
163 | #endif |
164 | |
165 | namespace apache { |
166 | namespace thrift { |
167 | namespace protocol { |
168 | |
169 | using apache::thrift::transport::TTransport; |
170 | |
171 | /** |
172 | * Enumerated definition of the types that the Thrift protocol supports. |
173 | * Take special note of the T_END type which is used specifically to mark |
174 | * the end of a sequence of fields. |
175 | */ |
176 | enum TType { |
177 | T_STOP = 0, |
178 | T_VOID = 1, |
179 | T_BOOL = 2, |
180 | T_BYTE = 3, |
181 | T_I08 = 3, |
182 | T_I16 = 6, |
183 | T_I32 = 8, |
184 | T_U64 = 9, |
185 | T_I64 = 10, |
186 | T_DOUBLE = 4, |
187 | T_STRING = 11, |
188 | T_UTF7 = 11, |
189 | T_STRUCT = 12, |
190 | T_MAP = 13, |
191 | T_SET = 14, |
192 | T_LIST = 15, |
193 | T_UTF8 = 16, |
194 | T_UTF16 = 17 |
195 | }; |
196 | |
197 | /** |
198 | * Enumerated definition of the message types that the Thrift protocol |
199 | * supports. |
200 | */ |
201 | enum TMessageType { |
202 | T_CALL = 1, |
203 | T_REPLY = 2, |
204 | T_EXCEPTION = 3, |
205 | T_ONEWAY = 4 |
206 | }; |
207 | |
208 | static const uint32_t DEFAULT_RECURSION_LIMIT = 64; |
209 | |
210 | /** |
211 | * Abstract class for a thrift protocol driver. These are all the methods that |
212 | * a protocol must implement. Essentially, there must be some way of reading |
213 | * and writing all the base types, plus a mechanism for writing out structs |
214 | * with indexed fields. |
215 | * |
216 | * TProtocol objects should not be shared across multiple encoding contexts, |
217 | * as they may need to maintain internal state in some protocols (i.e. XML). |
218 | * Note that is is acceptable for the TProtocol module to do its own internal |
219 | * buffered reads/writes to the underlying TTransport where appropriate (i.e. |
220 | * when parsing an input XML stream, reading should be batched rather than |
221 | * looking ahead character by character for a close tag). |
222 | * |
223 | */ |
224 | class TProtocol { |
225 | public: |
226 | virtual ~TProtocol(); |
227 | |
228 | /** |
229 | * Writing functions. |
230 | */ |
231 | |
232 | virtual uint32_t writeMessageBegin_virt(const std::string& name, |
233 | const TMessageType messageType, |
234 | const int32_t seqid) = 0; |
235 | |
236 | virtual uint32_t writeMessageEnd_virt() = 0; |
237 | |
238 | virtual uint32_t writeStructBegin_virt(const char* name) = 0; |
239 | |
240 | virtual uint32_t writeStructEnd_virt() = 0; |
241 | |
242 | virtual uint32_t writeFieldBegin_virt(const char* name, |
243 | const TType fieldType, |
244 | const int16_t fieldId) = 0; |
245 | |
246 | virtual uint32_t writeFieldEnd_virt() = 0; |
247 | |
248 | virtual uint32_t writeFieldStop_virt() = 0; |
249 | |
250 | virtual uint32_t writeMapBegin_virt(const TType keyType, const TType valType, const uint32_t size) |
251 | = 0; |
252 | |
253 | virtual uint32_t writeMapEnd_virt() = 0; |
254 | |
255 | virtual uint32_t writeListBegin_virt(const TType elemType, const uint32_t size) = 0; |
256 | |
257 | virtual uint32_t writeListEnd_virt() = 0; |
258 | |
259 | virtual uint32_t writeSetBegin_virt(const TType elemType, const uint32_t size) = 0; |
260 | |
261 | virtual uint32_t writeSetEnd_virt() = 0; |
262 | |
263 | virtual uint32_t writeBool_virt(const bool value) = 0; |
264 | |
265 | virtual uint32_t writeByte_virt(const int8_t byte) = 0; |
266 | |
267 | virtual uint32_t writeI16_virt(const int16_t i16) = 0; |
268 | |
269 | virtual uint32_t writeI32_virt(const int32_t i32) = 0; |
270 | |
271 | virtual uint32_t writeI64_virt(const int64_t i64) = 0; |
272 | |
273 | virtual uint32_t writeDouble_virt(const double dub) = 0; |
274 | |
275 | virtual uint32_t writeString_virt(const std::string& str) = 0; |
276 | |
277 | virtual uint32_t writeBinary_virt(const std::string& str) = 0; |
278 | |
279 | uint32_t writeMessageBegin(const std::string& name, |
280 | const TMessageType messageType, |
281 | const int32_t seqid) { |
282 | T_VIRTUAL_CALL(); |
283 | return writeMessageBegin_virt(name, messageType, seqid); |
284 | } |
285 | |
286 | uint32_t writeMessageEnd() { |
287 | T_VIRTUAL_CALL(); |
288 | return writeMessageEnd_virt(); |
289 | } |
290 | |
291 | uint32_t writeStructBegin(const char* name) { |
292 | T_VIRTUAL_CALL(); |
293 | return writeStructBegin_virt(name); |
294 | } |
295 | |
296 | uint32_t writeStructEnd() { |
297 | T_VIRTUAL_CALL(); |
298 | return writeStructEnd_virt(); |
299 | } |
300 | |
301 | uint32_t writeFieldBegin(const char* name, const TType fieldType, const int16_t fieldId) { |
302 | T_VIRTUAL_CALL(); |
303 | return writeFieldBegin_virt(name, fieldType, fieldId); |
304 | } |
305 | |
306 | uint32_t writeFieldEnd() { |
307 | T_VIRTUAL_CALL(); |
308 | return writeFieldEnd_virt(); |
309 | } |
310 | |
311 | uint32_t writeFieldStop() { |
312 | T_VIRTUAL_CALL(); |
313 | return writeFieldStop_virt(); |
314 | } |
315 | |
316 | uint32_t writeMapBegin(const TType keyType, const TType valType, const uint32_t size) { |
317 | T_VIRTUAL_CALL(); |
318 | return writeMapBegin_virt(keyType, valType, size); |
319 | } |
320 | |
321 | uint32_t writeMapEnd() { |
322 | T_VIRTUAL_CALL(); |
323 | return writeMapEnd_virt(); |
324 | } |
325 | |
326 | uint32_t writeListBegin(const TType elemType, const uint32_t size) { |
327 | T_VIRTUAL_CALL(); |
328 | return writeListBegin_virt(elemType, size); |
329 | } |
330 | |
331 | uint32_t writeListEnd() { |
332 | T_VIRTUAL_CALL(); |
333 | return writeListEnd_virt(); |
334 | } |
335 | |
336 | uint32_t writeSetBegin(const TType elemType, const uint32_t size) { |
337 | T_VIRTUAL_CALL(); |
338 | return writeSetBegin_virt(elemType, size); |
339 | } |
340 | |
341 | uint32_t writeSetEnd() { |
342 | T_VIRTUAL_CALL(); |
343 | return writeSetEnd_virt(); |
344 | } |
345 | |
346 | uint32_t writeBool(const bool value) { |
347 | T_VIRTUAL_CALL(); |
348 | return writeBool_virt(value); |
349 | } |
350 | |
351 | uint32_t writeByte(const int8_t byte) { |
352 | T_VIRTUAL_CALL(); |
353 | return writeByte_virt(byte); |
354 | } |
355 | |
356 | uint32_t writeI16(const int16_t i16) { |
357 | T_VIRTUAL_CALL(); |
358 | return writeI16_virt(i16); |
359 | } |
360 | |
361 | uint32_t writeI32(const int32_t i32) { |
362 | T_VIRTUAL_CALL(); |
363 | return writeI32_virt(i32); |
364 | } |
365 | |
366 | uint32_t writeI64(const int64_t i64) { |
367 | T_VIRTUAL_CALL(); |
368 | return writeI64_virt(i64); |
369 | } |
370 | |
371 | uint32_t writeDouble(const double dub) { |
372 | T_VIRTUAL_CALL(); |
373 | return writeDouble_virt(dub); |
374 | } |
375 | |
376 | uint32_t writeString(const std::string& str) { |
377 | T_VIRTUAL_CALL(); |
378 | return writeString_virt(str); |
379 | } |
380 | |
381 | uint32_t writeBinary(const std::string& str) { |
382 | T_VIRTUAL_CALL(); |
383 | return writeBinary_virt(str); |
384 | } |
385 | |
386 | /** |
387 | * Reading functions |
388 | */ |
389 | |
390 | virtual uint32_t readMessageBegin_virt(std::string& name, |
391 | TMessageType& messageType, |
392 | int32_t& seqid) = 0; |
393 | |
394 | virtual uint32_t readMessageEnd_virt() = 0; |
395 | |
396 | virtual uint32_t readStructBegin_virt(std::string& name) = 0; |
397 | |
398 | virtual uint32_t readStructEnd_virt() = 0; |
399 | |
400 | virtual uint32_t readFieldBegin_virt(std::string& name, TType& fieldType, int16_t& fieldId) = 0; |
401 | |
402 | virtual uint32_t readFieldEnd_virt() = 0; |
403 | |
404 | virtual uint32_t readMapBegin_virt(TType& keyType, TType& valType, uint32_t& size) = 0; |
405 | |
406 | virtual uint32_t readMapEnd_virt() = 0; |
407 | |
408 | virtual uint32_t readListBegin_virt(TType& elemType, uint32_t& size) = 0; |
409 | |
410 | virtual uint32_t readListEnd_virt() = 0; |
411 | |
412 | virtual uint32_t readSetBegin_virt(TType& elemType, uint32_t& size) = 0; |
413 | |
414 | virtual uint32_t readSetEnd_virt() = 0; |
415 | |
416 | virtual uint32_t readBool_virt(bool& value) = 0; |
417 | |
418 | virtual uint32_t readBool_virt(std::vector<bool>::reference value) = 0; |
419 | |
420 | virtual uint32_t readByte_virt(int8_t& byte) = 0; |
421 | |
422 | virtual uint32_t readI16_virt(int16_t& i16) = 0; |
423 | |
424 | virtual uint32_t readI32_virt(int32_t& i32) = 0; |
425 | |
426 | virtual uint32_t readI64_virt(int64_t& i64) = 0; |
427 | |
428 | virtual uint32_t readDouble_virt(double& dub) = 0; |
429 | |
430 | virtual uint32_t readString_virt(std::string& str) = 0; |
431 | |
432 | virtual uint32_t readBinary_virt(std::string& str) = 0; |
433 | |
434 | uint32_t readMessageBegin(std::string& name, TMessageType& messageType, int32_t& seqid) { |
435 | T_VIRTUAL_CALL(); |
436 | return readMessageBegin_virt(name, messageType, seqid); |
437 | } |
438 | |
439 | uint32_t readMessageEnd() { |
440 | T_VIRTUAL_CALL(); |
441 | return readMessageEnd_virt(); |
442 | } |
443 | |
444 | uint32_t readStructBegin(std::string& name) { |
445 | T_VIRTUAL_CALL(); |
446 | return readStructBegin_virt(name); |
447 | } |
448 | |
449 | uint32_t readStructEnd() { |
450 | T_VIRTUAL_CALL(); |
451 | return readStructEnd_virt(); |
452 | } |
453 | |
454 | uint32_t readFieldBegin(std::string& name, TType& fieldType, int16_t& fieldId) { |
455 | T_VIRTUAL_CALL(); |
456 | return readFieldBegin_virt(name, fieldType, fieldId); |
457 | } |
458 | |
459 | uint32_t readFieldEnd() { |
460 | T_VIRTUAL_CALL(); |
461 | return readFieldEnd_virt(); |
462 | } |
463 | |
464 | uint32_t readMapBegin(TType& keyType, TType& valType, uint32_t& size) { |
465 | T_VIRTUAL_CALL(); |
466 | return readMapBegin_virt(keyType, valType, size); |
467 | } |
468 | |
469 | uint32_t readMapEnd() { |
470 | T_VIRTUAL_CALL(); |
471 | return readMapEnd_virt(); |
472 | } |
473 | |
474 | uint32_t readListBegin(TType& elemType, uint32_t& size) { |
475 | T_VIRTUAL_CALL(); |
476 | return readListBegin_virt(elemType, size); |
477 | } |
478 | |
479 | uint32_t readListEnd() { |
480 | T_VIRTUAL_CALL(); |
481 | return readListEnd_virt(); |
482 | } |
483 | |
484 | uint32_t readSetBegin(TType& elemType, uint32_t& size) { |
485 | T_VIRTUAL_CALL(); |
486 | return readSetBegin_virt(elemType, size); |
487 | } |
488 | |
489 | uint32_t readSetEnd() { |
490 | T_VIRTUAL_CALL(); |
491 | return readSetEnd_virt(); |
492 | } |
493 | |
494 | uint32_t readBool(bool& value) { |
495 | T_VIRTUAL_CALL(); |
496 | return readBool_virt(value); |
497 | } |
498 | |
499 | uint32_t readByte(int8_t& byte) { |
500 | T_VIRTUAL_CALL(); |
501 | return readByte_virt(byte); |
502 | } |
503 | |
504 | uint32_t readI16(int16_t& i16) { |
505 | T_VIRTUAL_CALL(); |
506 | return readI16_virt(i16); |
507 | } |
508 | |
509 | uint32_t readI32(int32_t& i32) { |
510 | T_VIRTUAL_CALL(); |
511 | return readI32_virt(i32); |
512 | } |
513 | |
514 | uint32_t readI64(int64_t& i64) { |
515 | T_VIRTUAL_CALL(); |
516 | return readI64_virt(i64); |
517 | } |
518 | |
519 | uint32_t readDouble(double& dub) { |
520 | T_VIRTUAL_CALL(); |
521 | return readDouble_virt(dub); |
522 | } |
523 | |
524 | uint32_t readString(std::string& str) { |
525 | T_VIRTUAL_CALL(); |
526 | return readString_virt(str); |
527 | } |
528 | |
529 | uint32_t readBinary(std::string& str) { |
530 | T_VIRTUAL_CALL(); |
531 | return readBinary_virt(str); |
532 | } |
533 | |
534 | /* |
535 | * std::vector is specialized for bool, and its elements are individual bits |
536 | * rather than bools. We need to define a different version of readBool() |
537 | * to work with std::vector<bool>. |
538 | */ |
539 | uint32_t readBool(std::vector<bool>::reference value) { |
540 | T_VIRTUAL_CALL(); |
541 | return readBool_virt(value); |
542 | } |
543 | |
544 | /** |
545 | * Method to arbitrarily skip over data. |
546 | */ |
547 | uint32_t skip(TType type) { |
548 | T_VIRTUAL_CALL(); |
549 | return skip_virt(type); |
550 | } |
551 | virtual uint32_t skip_virt(TType type); |
552 | |
553 | inline stdcxx::shared_ptr<TTransport> getTransport() { return ptrans_; } |
554 | |
555 | // TODO: remove these two calls, they are for backwards |
556 | // compatibility |
557 | inline stdcxx::shared_ptr<TTransport> getInputTransport() { return ptrans_; } |
558 | inline stdcxx::shared_ptr<TTransport> getOutputTransport() { return ptrans_; } |
559 | |
560 | // input and output recursion depth are kept separate so that one protocol |
561 | // can be used concurrently for both input and output. |
562 | void incrementInputRecursionDepth() { |
563 | if (recursion_limit_ < ++input_recursion_depth_) { |
564 | throw TProtocolException(TProtocolException::DEPTH_LIMIT); |
565 | } |
566 | } |
567 | void decrementInputRecursionDepth() { --input_recursion_depth_; } |
568 | |
569 | void incrementOutputRecursionDepth() { |
570 | if (recursion_limit_ < ++output_recursion_depth_) { |
571 | throw TProtocolException(TProtocolException::DEPTH_LIMIT); |
572 | } |
573 | } |
574 | void decrementOutputRecursionDepth() { --output_recursion_depth_; } |
575 | |
576 | uint32_t getRecursionLimit() const {return recursion_limit_;} |
577 | void setRecurisionLimit(uint32_t depth) {recursion_limit_ = depth;} |
578 | |
579 | protected: |
580 | TProtocol(stdcxx::shared_ptr<TTransport> ptrans) |
581 | : ptrans_(ptrans), input_recursion_depth_(0), output_recursion_depth_(0), recursion_limit_(DEFAULT_RECURSION_LIMIT) |
582 | {} |
583 | |
584 | stdcxx::shared_ptr<TTransport> ptrans_; |
585 | |
586 | private: |
587 | TProtocol() {} |
588 | uint32_t input_recursion_depth_; |
589 | uint32_t output_recursion_depth_; |
590 | uint32_t recursion_limit_; |
591 | }; |
592 | |
593 | /** |
594 | * Constructs input and output protocol objects given transports. |
595 | */ |
596 | class TProtocolFactory { |
597 | public: |
598 | TProtocolFactory() {} |
599 | |
600 | virtual ~TProtocolFactory(); |
601 | |
602 | virtual stdcxx::shared_ptr<TProtocol> getProtocol(stdcxx::shared_ptr<TTransport> trans) = 0; |
603 | virtual stdcxx::shared_ptr<TProtocol> getProtocol(stdcxx::shared_ptr<TTransport> inTrans, |
604 | stdcxx::shared_ptr<TTransport> outTrans) { |
605 | (void)outTrans; |
606 | return getProtocol(inTrans); |
607 | } |
608 | }; |
609 | |
610 | /** |
611 | * Dummy protocol class. |
612 | * |
613 | * This class does nothing, and should never be instantiated. |
614 | * It is used only by the generator code. |
615 | */ |
616 | class TDummyProtocol : public TProtocol {}; |
617 | |
618 | // This is the default / legacy choice |
619 | struct TNetworkBigEndian |
620 | { |
621 | static uint16_t toWire16(uint16_t x) {return htons(x);} |
622 | static uint32_t toWire32(uint32_t x) {return htonl(x);} |
623 | static uint64_t toWire64(uint64_t x) {return THRIFT_htonll(x);} |
624 | static uint16_t fromWire16(uint16_t x) {return ntohs(x);} |
625 | static uint32_t fromWire32(uint32_t x) {return ntohl(x);} |
626 | static uint64_t fromWire64(uint64_t x) {return THRIFT_ntohll(x);} |
627 | }; |
628 | |
629 | // On most systems, this will be a bit faster than TNetworkBigEndian |
630 | struct TNetworkLittleEndian |
631 | { |
632 | static uint16_t toWire16(uint16_t x) {return THRIFT_htoles(x);} |
633 | static uint32_t toWire32(uint32_t x) {return THRIFT_htolel(x);} |
634 | static uint64_t toWire64(uint64_t x) {return THRIFT_htolell(x);} |
635 | static uint16_t fromWire16(uint16_t x) {return THRIFT_letohs(x);} |
636 | static uint32_t fromWire32(uint32_t x) {return THRIFT_letohl(x);} |
637 | static uint64_t fromWire64(uint64_t x) {return THRIFT_letohll(x);} |
638 | }; |
639 | |
640 | struct TOutputRecursionTracker { |
641 | TProtocol &prot_; |
642 | TOutputRecursionTracker(TProtocol &prot) : prot_(prot) { |
643 | prot_.incrementOutputRecursionDepth(); |
644 | } |
645 | ~TOutputRecursionTracker() { |
646 | prot_.decrementOutputRecursionDepth(); |
647 | } |
648 | }; |
649 | |
650 | struct TInputRecursionTracker { |
651 | TProtocol &prot_; |
652 | TInputRecursionTracker(TProtocol &prot) : prot_(prot) { |
653 | prot_.incrementInputRecursionDepth(); |
654 | } |
655 | ~TInputRecursionTracker() { |
656 | prot_.decrementInputRecursionDepth(); |
657 | } |
658 | }; |
659 | |
660 | /** |
661 | * Helper template for implementing TProtocol::skip(). |
662 | * |
663 | * Templatized to avoid having to make virtual function calls. |
664 | */ |
665 | template <class Protocol_> |
666 | uint32_t skip(Protocol_& prot, TType type) { |
667 | TInputRecursionTracker tracker(prot); |
668 | |
669 | switch (type) { |
670 | case T_BOOL: { |
671 | bool boolv; |
672 | return prot.readBool(boolv); |
673 | } |
674 | case T_BYTE: { |
675 | int8_t bytev = 0; |
676 | return prot.readByte(bytev); |
677 | } |
678 | case T_I16: { |
679 | int16_t i16; |
680 | return prot.readI16(i16); |
681 | } |
682 | case T_I32: { |
683 | int32_t i32; |
684 | return prot.readI32(i32); |
685 | } |
686 | case T_I64: { |
687 | int64_t i64; |
688 | return prot.readI64(i64); |
689 | } |
690 | case T_DOUBLE: { |
691 | double dub; |
692 | return prot.readDouble(dub); |
693 | } |
694 | case T_STRING: { |
695 | std::string str; |
696 | return prot.readBinary(str); |
697 | } |
698 | case T_STRUCT: { |
699 | uint32_t result = 0; |
700 | std::string name; |
701 | int16_t fid; |
702 | TType ftype; |
703 | result += prot.readStructBegin(name); |
704 | while (true) { |
705 | result += prot.readFieldBegin(name, ftype, fid); |
706 | if (ftype == T_STOP) { |
707 | break; |
708 | } |
709 | result += skip(prot, ftype); |
710 | result += prot.readFieldEnd(); |
711 | } |
712 | result += prot.readStructEnd(); |
713 | return result; |
714 | } |
715 | case T_MAP: { |
716 | uint32_t result = 0; |
717 | TType keyType; |
718 | TType valType; |
719 | uint32_t i, size; |
720 | result += prot.readMapBegin(keyType, valType, size); |
721 | for (i = 0; i < size; i++) { |
722 | result += skip(prot, keyType); |
723 | result += skip(prot, valType); |
724 | } |
725 | result += prot.readMapEnd(); |
726 | return result; |
727 | } |
728 | case T_SET: { |
729 | uint32_t result = 0; |
730 | TType elemType; |
731 | uint32_t i, size; |
732 | result += prot.readSetBegin(elemType, size); |
733 | for (i = 0; i < size; i++) { |
734 | result += skip(prot, elemType); |
735 | } |
736 | result += prot.readSetEnd(); |
737 | return result; |
738 | } |
739 | case T_LIST: { |
740 | uint32_t result = 0; |
741 | TType elemType; |
742 | uint32_t i, size; |
743 | result += prot.readListBegin(elemType, size); |
744 | for (i = 0; i < size; i++) { |
745 | result += skip(prot, elemType); |
746 | } |
747 | result += prot.readListEnd(); |
748 | return result; |
749 | } |
750 | case T_STOP: |
751 | case T_VOID: |
752 | case T_U64: |
753 | case T_UTF8: |
754 | case T_UTF16: |
755 | break; |
756 | default: |
757 | throw TProtocolException(TProtocolException::INVALID_DATA); |
758 | } |
759 | return 0; |
760 | } |
761 | |
762 | }}} // apache::thrift::protocol |
763 | |
764 | #endif // #define _THRIFT_PROTOCOL_TPROTOCOL_H_ 1 |
765 | |