David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 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 | */ |
Mark Slee | 9f0c651 | 2007-02-28 23:58:26 +0000 | [diff] [blame] | 19 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 20 | #ifndef _THRIFT_PROTOCOL_TPROTOCOL_H_ |
| 21 | #define _THRIFT_PROTOCOL_TPROTOCOL_H_ 1 |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 22 | |
Nobuaki Sukegawa | 11da87e | 2016-09-10 14:02:19 +0900 | [diff] [blame] | 23 | #ifdef _WIN32 |
Mario Emmenlauer | 49c6ec7 | 2023-10-10 10:46:09 +0200 | [diff] [blame] | 24 | // Including Winsock2.h adds problematic macros like min() and max(). |
| 25 | // Try to work around: |
| 26 | #ifndef NOMINMAX |
| 27 | #define NOMINMAX |
| 28 | #define _THRIFT_UNDEF_NOMINMAX |
| 29 | #endif |
| 30 | #ifndef WIN32_LEAN_AND_MEAN |
| 31 | #define WIN32_LEAN_AND_MEAN |
| 32 | #define _THRIFT_UNDEF_WIN32_LEAN_AND_MEAN |
| 33 | #endif |
Nobuaki Sukegawa | 11da87e | 2016-09-10 14:02:19 +0900 | [diff] [blame] | 34 | // Need to come before any Windows.h includes |
Jeremiah | 50819ce | 2022-02-08 12:46:45 +0100 | [diff] [blame] | 35 | #include <winsock2.h> |
Mario Emmenlauer | 49c6ec7 | 2023-10-10 10:46:09 +0200 | [diff] [blame] | 36 | #ifdef _THRIFT_UNDEF_NOMINMAX |
| 37 | #undef NOMINMAX |
| 38 | #undef _THRIFT_UNDEF_NOMINMAX |
| 39 | #endif |
| 40 | #ifdef _THRIFT_UNDEF_WIN32_LEAN_AND_MEAN |
| 41 | #undef WIN32_LEAN_AND_MEAN |
| 42 | #undef _THRIFT_UNDEF_WIN32_LEAN_AND_MEAN |
| 43 | #endif |
Nobuaki Sukegawa | 11da87e | 2016-09-10 14:02:19 +0900 | [diff] [blame] | 44 | #endif |
| 45 | |
Roger Meier | 49ff8b1 | 2012-04-13 09:12:31 +0000 | [diff] [blame] | 46 | #include <thrift/transport/TTransport.h> |
| 47 | #include <thrift/protocol/TProtocolException.h> |
zeshuai007 | 86352b4 | 2020-06-15 17:00:33 +0800 | [diff] [blame] | 48 | #include <thrift/protocol/TEnum.h> |
| 49 | #include <thrift/protocol/TList.h> |
| 50 | #include <thrift/protocol/TSet.h> |
| 51 | #include <thrift/protocol/TMap.h> |
CJCombrink | 4b90909 | 2024-04-27 19:51:39 +0200 | [diff] [blame] | 52 | #include <thrift/TUuid.h> |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 53 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 54 | #include <memory> |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 55 | |
Roger Meier | 2fa9c31 | 2011-09-05 19:15:53 +0000 | [diff] [blame] | 56 | #ifdef HAVE_NETINET_IN_H |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 57 | #include <netinet/in.h> |
Roger Meier | 2fa9c31 | 2011-09-05 19:15:53 +0000 | [diff] [blame] | 58 | #endif |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 59 | #include <sys/types.h> |
| 60 | #include <string> |
| 61 | #include <map> |
David Reiss | 8dfc732 | 2010-10-06 17:09:58 +0000 | [diff] [blame] | 62 | #include <vector> |
Ben Craig | cfaadcc | 2015-07-08 20:50:33 -0500 | [diff] [blame] | 63 | #include <climits> |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 64 | |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 65 | // Use this to get around strict aliasing rules. |
| 66 | // For example, uint64_t i = bitwise_cast<uint64_t>(returns_double()); |
| 67 | // The most obvious implementation is to just cast a pointer, |
| 68 | // but that doesn't work. |
| 69 | // For a pretty in-depth explanation of the problem, see |
Jens Geyer | 1a8e048 | 2015-04-30 20:29:20 +0200 | [diff] [blame] | 70 | // http://cellperformance.beyond3d.com/articles/2006/06/understanding-strict-aliasing.html |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 71 | template <typename To, typename From> |
| 72 | static inline To bitwise_cast(From from) { |
cyy | 863262d | 2019-01-06 10:40:58 +0800 | [diff] [blame] | 73 | static_assert(sizeof(From) == sizeof(To), "sizeof(From) == sizeof(To)"); |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 74 | |
| 75 | // BAD!!! These are all broken with -O2. |
| 76 | //return *reinterpret_cast<To*>(&from); // BAD!!! |
| 77 | //return *static_cast<To*>(static_cast<void*>(&from)); // BAD!!! |
| 78 | //return *(To*)(void*)&from; // BAD!!! |
| 79 | |
| 80 | // Super clean and paritally blessed by section 3.9 of the standard. |
| 81 | //unsigned char c[sizeof(from)]; |
| 82 | //memcpy(c, &from, sizeof(from)); |
| 83 | //To to; |
| 84 | //memcpy(&to, c, sizeof(c)); |
| 85 | //return to; |
| 86 | |
| 87 | // Slightly more questionable. |
| 88 | // Same code emitted by GCC. |
| 89 | //To to; |
| 90 | //memcpy(&to, &from, sizeof(from)); |
| 91 | //return to; |
| 92 | |
| 93 | // Technically undefined, but almost universally supported, |
| 94 | // and the most efficient implementation. |
| 95 | union { |
| 96 | From f; |
| 97 | To t; |
| 98 | } u; |
| 99 | u.f = from; |
| 100 | return u.t; |
| 101 | } |
| 102 | |
| 103 | |
Jake Farrell | c1fe30b | 2011-06-01 20:34:26 +0000 | [diff] [blame] | 104 | #ifdef HAVE_SYS_PARAM_H |
Bryan Duxbury | 184d262 | 2010-08-17 17:43:58 +0000 | [diff] [blame] | 105 | #include <sys/param.h> |
Mark Slee | 4f261c5 | 2007-04-13 00:33:24 +0000 | [diff] [blame] | 106 | #endif |
| 107 | |
Chris Friedt | 0bc818f | 2022-09-26 16:02:50 -0400 | [diff] [blame] | 108 | #ifdef __ZEPHYR__ |
| 109 | # include <zephyr/sys/byteorder.h> |
| 110 | |
| 111 | # define __THRIFT_BYTE_ORDER __BYTE_ORDER__ |
| 112 | # define __THRIFT_LITTLE_ENDIAN __ORDER_LITTLE_ENDIAN__ |
| 113 | # define __THRIFT_BIG_ENDIAN __ORDER_BIG_ENDIAN__ |
| 114 | |
| 115 | # if __THRIFT_BYTE_ORDER == __THRIFT_BIG_ENDIAN |
| 116 | # undef bswap_64 |
| 117 | # undef bswap_32 |
| 118 | # undef bswap_16 |
| 119 | # endif |
| 120 | #endif |
| 121 | |
Roger Meier | f9f841d | 2012-06-19 20:42:33 +0000 | [diff] [blame] | 122 | #ifndef __THRIFT_BYTE_ORDER |
Mark Slee | 1d2ead3 | 2007-06-09 01:23:04 +0000 | [diff] [blame] | 123 | # if defined(BYTE_ORDER) && defined(LITTLE_ENDIAN) && defined(BIG_ENDIAN) |
Roger Meier | f9f841d | 2012-06-19 20:42:33 +0000 | [diff] [blame] | 124 | # define __THRIFT_BYTE_ORDER BYTE_ORDER |
| 125 | # define __THRIFT_LITTLE_ENDIAN LITTLE_ENDIAN |
| 126 | # define __THRIFT_BIG_ENDIAN BIG_ENDIAN |
Mark Slee | 1d2ead3 | 2007-06-09 01:23:04 +0000 | [diff] [blame] | 127 | # else |
James E. King III | ee8caca | 2019-05-04 08:20:59 -0400 | [diff] [blame] | 128 | # include <boost/predef/other/endian.h> |
| 129 | # if BOOST_ENDIAN_BIG_BYTE |
| 130 | # define __THRIFT_BYTE_ORDER 4321 |
| 131 | # define __THRIFT_LITTLE_ENDIAN 0 |
| 132 | # define __THRIFT_BIG_ENDIAN __THRIFT_BYTE_ORDER |
| 133 | # elif BOOST_ENDIAN_LITTLE_BYTE |
| 134 | # define __THRIFT_BYTE_ORDER 1234 |
| 135 | # define __THRIFT_LITTLE_ENDIAN __THRIFT_BYTE_ORDER |
| 136 | # define __THRIFT_BIG_ENDIAN 0 |
| 137 | # endif |
Roger Meier | a93848b | 2011-09-12 22:20:11 +0000 | [diff] [blame] | 138 | # ifdef BOOST_LITTLE_ENDIAN |
Roger Meier | a93848b | 2011-09-12 22:20:11 +0000 | [diff] [blame] | 139 | # else |
Roger Meier | a93848b | 2011-09-12 22:20:11 +0000 | [diff] [blame] | 140 | # endif |
Mark Slee | 1d2ead3 | 2007-06-09 01:23:04 +0000 | [diff] [blame] | 141 | # endif |
| 142 | #endif |
| 143 | |
Roger Meier | f9f841d | 2012-06-19 20:42:33 +0000 | [diff] [blame] | 144 | #if __THRIFT_BYTE_ORDER == __THRIFT_BIG_ENDIAN |
jfarrell | ad3a955 | 2015-09-24 23:27:34 -0400 | [diff] [blame] | 145 | # if !defined(THRIFT_ntohll) |
| 146 | # define THRIFT_ntohll(n) (n) |
| 147 | # define THRIFT_htonll(n) (n) |
Jim King | 9dfe7b8 | 2015-05-06 09:51:54 -0400 | [diff] [blame] | 148 | # endif |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 149 | # if defined(__GNUC__) && defined(__GLIBC__) |
| 150 | # include <byteswap.h> |
jfarrell | ad3a955 | 2015-09-24 23:27:34 -0400 | [diff] [blame] | 151 | # define THRIFT_htolell(n) bswap_64(n) |
| 152 | # define THRIFT_letohll(n) bswap_64(n) |
Ben Craig | 384f976 | 2015-07-08 20:33:03 -0500 | [diff] [blame] | 153 | # define THRIFT_htolel(n) bswap_32(n) |
| 154 | # define THRIFT_letohl(n) bswap_32(n) |
| 155 | # define THRIFT_htoles(n) bswap_16(n) |
| 156 | # define THRIFT_letohs(n) bswap_16(n) |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 157 | # else /* GNUC & GLIBC */ |
| 158 | # define bswap_64(n) \ |
| 159 | ( (((n) & 0xff00000000000000ull) >> 56) \ |
| 160 | | (((n) & 0x00ff000000000000ull) >> 40) \ |
| 161 | | (((n) & 0x0000ff0000000000ull) >> 24) \ |
| 162 | | (((n) & 0x000000ff00000000ull) >> 8) \ |
| 163 | | (((n) & 0x00000000ff000000ull) << 8) \ |
| 164 | | (((n) & 0x0000000000ff0000ull) << 24) \ |
| 165 | | (((n) & 0x000000000000ff00ull) << 40) \ |
| 166 | | (((n) & 0x00000000000000ffull) << 56) ) |
Ben Craig | 384f976 | 2015-07-08 20:33:03 -0500 | [diff] [blame] | 167 | # define bswap_32(n) \ |
| 168 | ( (((n) & 0xff000000ul) >> 24) \ |
| 169 | | (((n) & 0x00ff0000ul) >> 8) \ |
| 170 | | (((n) & 0x0000ff00ul) << 8) \ |
| 171 | | (((n) & 0x000000fful) << 24) ) |
| 172 | # define bswap_16(n) \ |
Patrick Gansterer | b7a1d8b | 2025-06-05 16:56:38 +0200 | [diff] [blame^] | 173 | ( (((n) & static_cast<unsigned short>(0xff00ul)) >> 8) \ |
| 174 | | (((n) & static_cast<unsigned short>(0x00fful)) << 8) ) |
jfarrell | ad3a955 | 2015-09-24 23:27:34 -0400 | [diff] [blame] | 175 | # define THRIFT_htolell(n) bswap_64(n) |
| 176 | # define THRIFT_letohll(n) bswap_64(n) |
Ben Craig | 384f976 | 2015-07-08 20:33:03 -0500 | [diff] [blame] | 177 | # define THRIFT_htolel(n) bswap_32(n) |
| 178 | # define THRIFT_letohl(n) bswap_32(n) |
| 179 | # define THRIFT_htoles(n) bswap_16(n) |
| 180 | # define THRIFT_letohs(n) bswap_16(n) |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 181 | # endif /* GNUC & GLIBC */ |
Roger Meier | f9f841d | 2012-06-19 20:42:33 +0000 | [diff] [blame] | 182 | #elif __THRIFT_BYTE_ORDER == __THRIFT_LITTLE_ENDIAN |
jfarrell | ad3a955 | 2015-09-24 23:27:34 -0400 | [diff] [blame] | 183 | # define THRIFT_htolell(n) (n) |
| 184 | # define THRIFT_letohll(n) (n) |
Ben Craig | 384f976 | 2015-07-08 20:33:03 -0500 | [diff] [blame] | 185 | # define THRIFT_htolel(n) (n) |
| 186 | # define THRIFT_letohl(n) (n) |
| 187 | # define THRIFT_htoles(n) (n) |
| 188 | # define THRIFT_letohs(n) (n) |
Mark Slee | 4f261c5 | 2007-04-13 00:33:24 +0000 | [diff] [blame] | 189 | # if defined(__GNUC__) && defined(__GLIBC__) |
| 190 | # include <byteswap.h> |
jfarrell | ad3a955 | 2015-09-24 23:27:34 -0400 | [diff] [blame] | 191 | # define THRIFT_ntohll(n) bswap_64(n) |
| 192 | # define THRIFT_htonll(n) bswap_64(n) |
Roger Meier | b69d24d | 2012-10-04 18:02:15 +0000 | [diff] [blame] | 193 | # elif defined(_MSC_VER) /* Microsoft Visual C++ */ |
Maximilian Bandle | 7c94335 | 2025-02-07 10:58:32 +0100 | [diff] [blame] | 194 | # define THRIFT_ntohll(n) ( _byteswap_uint64(static_cast<uint64_t>(n)) ) |
| 195 | # define THRIFT_htonll(n) ( _byteswap_uint64(static_cast<uint64_t>(n)) ) |
jfarrell | ad3a955 | 2015-09-24 23:27:34 -0400 | [diff] [blame] | 196 | # elif !defined(THRIFT_ntohll) /* Not GNUC/GLIBC or MSVC */ |
Maximilian Bandle | 7c94335 | 2025-02-07 10:58:32 +0100 | [diff] [blame] | 197 | # define THRIFT_ntohll(n) ( (static_cast<uint64_t>(ntohl(static_cast<uint32_t>(n))) << 32) + ntohl(static_cast<uint32_t>(n >> 32)) ) |
| 198 | # define THRIFT_htonll(n) ( (static_cast<uint64_t>(htonl(static_cast<uint32_t>(n))) << 32) + htonl(static_cast<uint32_t>(n >> 32)) ) |
Roger Meier | b69d24d | 2012-10-04 18:02:15 +0000 | [diff] [blame] | 199 | # endif /* GNUC/GLIBC or MSVC or something else */ |
Roger Meier | f9f841d | 2012-06-19 20:42:33 +0000 | [diff] [blame] | 200 | #else /* __THRIFT_BYTE_ORDER */ |
jfarrell | ad3a955 | 2015-09-24 23:27:34 -0400 | [diff] [blame] | 201 | # error "Can't define THRIFT_htonll or THRIFT_ntohll!" |
Mark Slee | dc8a2a2 | 2006-09-19 22:20:18 +0000 | [diff] [blame] | 202 | #endif |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 203 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 204 | namespace apache { |
| 205 | namespace thrift { |
| 206 | namespace protocol { |
Ben Craig | f4e6e62 | 2013-11-05 19:49:12 -0600 | [diff] [blame] | 207 | |
| 208 | using apache::thrift::transport::TTransport; |
| 209 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 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 |
Mark Slee | 5d06fea | 2007-03-05 22:18:18 +0000 | [diff] [blame] | 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). |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 222 | * |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 223 | */ |
| 224 | class TProtocol { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 225 | public: |
Ben Craig | cfaadcc | 2015-07-08 20:50:33 -0500 | [diff] [blame] | 226 | virtual ~TProtocol(); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 227 | |
| 228 | /** |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 229 | * Writing functions. |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 230 | */ |
| 231 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 232 | virtual uint32_t writeMessageBegin_virt(const std::string& name, |
| 233 | const TMessageType messageType, |
| 234 | const int32_t seqid) = 0; |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 235 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 236 | virtual uint32_t writeMessageEnd_virt() = 0; |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 237 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 238 | virtual uint32_t writeStructBegin_virt(const char* name) = 0; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 239 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 240 | virtual uint32_t writeStructEnd_virt() = 0; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 241 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 242 | virtual uint32_t writeFieldBegin_virt(const char* name, |
| 243 | const TType fieldType, |
| 244 | const int16_t fieldId) = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 245 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 246 | virtual uint32_t writeFieldEnd_virt() = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 247 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 248 | virtual uint32_t writeFieldStop_virt() = 0; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 249 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 250 | virtual uint32_t writeMapBegin_virt(const TType keyType, const TType valType, const uint32_t size) |
| 251 | = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 252 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 253 | virtual uint32_t writeMapEnd_virt() = 0; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 254 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 255 | virtual uint32_t writeListBegin_virt(const TType elemType, const uint32_t size) = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 256 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 257 | virtual uint32_t writeListEnd_virt() = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 258 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 259 | virtual uint32_t writeSetBegin_virt(const TType elemType, const uint32_t size) = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 260 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 261 | virtual uint32_t writeSetEnd_virt() = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 262 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 263 | virtual uint32_t writeBool_virt(const bool value) = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 264 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 265 | virtual uint32_t writeByte_virt(const int8_t byte) = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 266 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 267 | virtual uint32_t writeI16_virt(const int16_t i16) = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 268 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 269 | virtual uint32_t writeI32_virt(const int32_t i32) = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 270 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 271 | virtual uint32_t writeI64_virt(const int64_t i64) = 0; |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 272 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 273 | virtual uint32_t writeDouble_virt(const double dub) = 0; |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 274 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 275 | virtual uint32_t writeString_virt(const std::string& str) = 0; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 276 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 277 | virtual uint32_t writeBinary_virt(const std::string& str) = 0; |
| 278 | |
CJCombrink | 4b90909 | 2024-04-27 19:51:39 +0200 | [diff] [blame] | 279 | virtual uint32_t writeUUID_virt(const TUuid& uuid) = 0; |
CJCombrink | 1d886ca | 2024-03-23 21:32:28 +0100 | [diff] [blame] | 280 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 281 | uint32_t writeMessageBegin(const std::string& name, |
| 282 | const TMessageType messageType, |
| 283 | const int32_t seqid) { |
| 284 | T_VIRTUAL_CALL(); |
| 285 | return writeMessageBegin_virt(name, messageType, seqid); |
| 286 | } |
| 287 | |
| 288 | uint32_t writeMessageEnd() { |
| 289 | T_VIRTUAL_CALL(); |
| 290 | return writeMessageEnd_virt(); |
| 291 | } |
| 292 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 293 | uint32_t writeStructBegin(const char* name) { |
| 294 | T_VIRTUAL_CALL(); |
| 295 | return writeStructBegin_virt(name); |
| 296 | } |
| 297 | |
| 298 | uint32_t writeStructEnd() { |
| 299 | T_VIRTUAL_CALL(); |
| 300 | return writeStructEnd_virt(); |
| 301 | } |
| 302 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 303 | uint32_t writeFieldBegin(const char* name, const TType fieldType, const int16_t fieldId) { |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 304 | T_VIRTUAL_CALL(); |
| 305 | return writeFieldBegin_virt(name, fieldType, fieldId); |
| 306 | } |
| 307 | |
| 308 | uint32_t writeFieldEnd() { |
| 309 | T_VIRTUAL_CALL(); |
| 310 | return writeFieldEnd_virt(); |
| 311 | } |
| 312 | |
| 313 | uint32_t writeFieldStop() { |
| 314 | T_VIRTUAL_CALL(); |
| 315 | return writeFieldStop_virt(); |
| 316 | } |
| 317 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 318 | uint32_t writeMapBegin(const TType keyType, const TType valType, const uint32_t size) { |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 319 | T_VIRTUAL_CALL(); |
| 320 | return writeMapBegin_virt(keyType, valType, size); |
| 321 | } |
| 322 | |
| 323 | uint32_t writeMapEnd() { |
| 324 | T_VIRTUAL_CALL(); |
| 325 | return writeMapEnd_virt(); |
| 326 | } |
| 327 | |
| 328 | uint32_t writeListBegin(const TType elemType, const uint32_t size) { |
| 329 | T_VIRTUAL_CALL(); |
| 330 | return writeListBegin_virt(elemType, size); |
| 331 | } |
| 332 | |
| 333 | uint32_t writeListEnd() { |
| 334 | T_VIRTUAL_CALL(); |
| 335 | return writeListEnd_virt(); |
| 336 | } |
| 337 | |
| 338 | uint32_t writeSetBegin(const TType elemType, const uint32_t size) { |
| 339 | T_VIRTUAL_CALL(); |
| 340 | return writeSetBegin_virt(elemType, size); |
| 341 | } |
| 342 | |
| 343 | uint32_t writeSetEnd() { |
| 344 | T_VIRTUAL_CALL(); |
| 345 | return writeSetEnd_virt(); |
| 346 | } |
| 347 | |
| 348 | uint32_t writeBool(const bool value) { |
| 349 | T_VIRTUAL_CALL(); |
| 350 | return writeBool_virt(value); |
| 351 | } |
| 352 | |
| 353 | uint32_t writeByte(const int8_t byte) { |
| 354 | T_VIRTUAL_CALL(); |
| 355 | return writeByte_virt(byte); |
| 356 | } |
| 357 | |
| 358 | uint32_t writeI16(const int16_t i16) { |
| 359 | T_VIRTUAL_CALL(); |
| 360 | return writeI16_virt(i16); |
| 361 | } |
| 362 | |
| 363 | uint32_t writeI32(const int32_t i32) { |
| 364 | T_VIRTUAL_CALL(); |
| 365 | return writeI32_virt(i32); |
| 366 | } |
| 367 | |
| 368 | uint32_t writeI64(const int64_t i64) { |
| 369 | T_VIRTUAL_CALL(); |
| 370 | return writeI64_virt(i64); |
| 371 | } |
| 372 | |
| 373 | uint32_t writeDouble(const double dub) { |
| 374 | T_VIRTUAL_CALL(); |
| 375 | return writeDouble_virt(dub); |
| 376 | } |
| 377 | |
| 378 | uint32_t writeString(const std::string& str) { |
| 379 | T_VIRTUAL_CALL(); |
| 380 | return writeString_virt(str); |
| 381 | } |
| 382 | |
| 383 | uint32_t writeBinary(const std::string& str) { |
| 384 | T_VIRTUAL_CALL(); |
| 385 | return writeBinary_virt(str); |
| 386 | } |
David Reiss | c005b1b | 2008-02-15 01:38:18 +0000 | [diff] [blame] | 387 | |
CJCombrink | 4b90909 | 2024-04-27 19:51:39 +0200 | [diff] [blame] | 388 | uint32_t writeUUID(const TUuid& uuid) { |
CJCombrink | 1d886ca | 2024-03-23 21:32:28 +0100 | [diff] [blame] | 389 | T_VIRTUAL_CALL(); |
CJCombrink | 4b90909 | 2024-04-27 19:51:39 +0200 | [diff] [blame] | 390 | return writeUUID_virt(uuid); |
CJCombrink | 1d886ca | 2024-03-23 21:32:28 +0100 | [diff] [blame] | 391 | } |
| 392 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 393 | /** |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 394 | * Reading functions |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 395 | */ |
| 396 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 397 | virtual uint32_t readMessageBegin_virt(std::string& name, |
| 398 | TMessageType& messageType, |
| 399 | int32_t& seqid) = 0; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 400 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 401 | virtual uint32_t readMessageEnd_virt() = 0; |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 402 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 403 | virtual uint32_t readStructBegin_virt(std::string& name) = 0; |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 404 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 405 | virtual uint32_t readStructEnd_virt() = 0; |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 406 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 407 | virtual uint32_t readFieldBegin_virt(std::string& name, TType& fieldType, int16_t& fieldId) = 0; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 408 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 409 | virtual uint32_t readFieldEnd_virt() = 0; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 410 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 411 | virtual uint32_t readMapBegin_virt(TType& keyType, TType& valType, uint32_t& size) = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 412 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 413 | virtual uint32_t readMapEnd_virt() = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 414 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 415 | virtual uint32_t readListBegin_virt(TType& elemType, uint32_t& size) = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 416 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 417 | virtual uint32_t readListEnd_virt() = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 418 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 419 | virtual uint32_t readSetBegin_virt(TType& elemType, uint32_t& size) = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 420 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 421 | virtual uint32_t readSetEnd_virt() = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 422 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 423 | virtual uint32_t readBool_virt(bool& value) = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 424 | |
David Reiss | 8dfc732 | 2010-10-06 17:09:58 +0000 | [diff] [blame] | 425 | virtual uint32_t readBool_virt(std::vector<bool>::reference value) = 0; |
| 426 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 427 | virtual uint32_t readByte_virt(int8_t& byte) = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 428 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 429 | virtual uint32_t readI16_virt(int16_t& i16) = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 430 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 431 | virtual uint32_t readI32_virt(int32_t& i32) = 0; |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 432 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 433 | virtual uint32_t readI64_virt(int64_t& i64) = 0; |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 434 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 435 | virtual uint32_t readDouble_virt(double& dub) = 0; |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 436 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 437 | virtual uint32_t readString_virt(std::string& str) = 0; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 438 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 439 | virtual uint32_t readBinary_virt(std::string& str) = 0; |
| 440 | |
CJCombrink | 4b90909 | 2024-04-27 19:51:39 +0200 | [diff] [blame] | 441 | virtual uint32_t readUUID_virt(TUuid& uuid) = 0; |
CJCombrink | 1d886ca | 2024-03-23 21:32:28 +0100 | [diff] [blame] | 442 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 443 | uint32_t readMessageBegin(std::string& name, TMessageType& messageType, int32_t& seqid) { |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 444 | T_VIRTUAL_CALL(); |
| 445 | return readMessageBegin_virt(name, messageType, seqid); |
| 446 | } |
| 447 | |
| 448 | uint32_t readMessageEnd() { |
| 449 | T_VIRTUAL_CALL(); |
| 450 | return readMessageEnd_virt(); |
| 451 | } |
| 452 | |
| 453 | uint32_t readStructBegin(std::string& name) { |
| 454 | T_VIRTUAL_CALL(); |
| 455 | return readStructBegin_virt(name); |
| 456 | } |
| 457 | |
| 458 | uint32_t readStructEnd() { |
| 459 | T_VIRTUAL_CALL(); |
| 460 | return readStructEnd_virt(); |
| 461 | } |
| 462 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 463 | uint32_t readFieldBegin(std::string& name, TType& fieldType, int16_t& fieldId) { |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 464 | T_VIRTUAL_CALL(); |
| 465 | return readFieldBegin_virt(name, fieldType, fieldId); |
| 466 | } |
| 467 | |
| 468 | uint32_t readFieldEnd() { |
| 469 | T_VIRTUAL_CALL(); |
| 470 | return readFieldEnd_virt(); |
| 471 | } |
| 472 | |
| 473 | uint32_t readMapBegin(TType& keyType, TType& valType, uint32_t& size) { |
| 474 | T_VIRTUAL_CALL(); |
| 475 | return readMapBegin_virt(keyType, valType, size); |
| 476 | } |
| 477 | |
| 478 | uint32_t readMapEnd() { |
| 479 | T_VIRTUAL_CALL(); |
| 480 | return readMapEnd_virt(); |
| 481 | } |
| 482 | |
| 483 | uint32_t readListBegin(TType& elemType, uint32_t& size) { |
| 484 | T_VIRTUAL_CALL(); |
| 485 | return readListBegin_virt(elemType, size); |
| 486 | } |
| 487 | |
| 488 | uint32_t readListEnd() { |
| 489 | T_VIRTUAL_CALL(); |
| 490 | return readListEnd_virt(); |
| 491 | } |
| 492 | |
| 493 | uint32_t readSetBegin(TType& elemType, uint32_t& size) { |
| 494 | T_VIRTUAL_CALL(); |
| 495 | return readSetBegin_virt(elemType, size); |
| 496 | } |
| 497 | |
| 498 | uint32_t readSetEnd() { |
| 499 | T_VIRTUAL_CALL(); |
| 500 | return readSetEnd_virt(); |
| 501 | } |
| 502 | |
| 503 | uint32_t readBool(bool& value) { |
| 504 | T_VIRTUAL_CALL(); |
| 505 | return readBool_virt(value); |
| 506 | } |
| 507 | |
| 508 | uint32_t readByte(int8_t& byte) { |
| 509 | T_VIRTUAL_CALL(); |
| 510 | return readByte_virt(byte); |
| 511 | } |
| 512 | |
| 513 | uint32_t readI16(int16_t& i16) { |
| 514 | T_VIRTUAL_CALL(); |
| 515 | return readI16_virt(i16); |
| 516 | } |
| 517 | |
| 518 | uint32_t readI32(int32_t& i32) { |
| 519 | T_VIRTUAL_CALL(); |
| 520 | return readI32_virt(i32); |
| 521 | } |
| 522 | |
| 523 | uint32_t readI64(int64_t& i64) { |
| 524 | T_VIRTUAL_CALL(); |
| 525 | return readI64_virt(i64); |
| 526 | } |
| 527 | |
| 528 | uint32_t readDouble(double& dub) { |
| 529 | T_VIRTUAL_CALL(); |
| 530 | return readDouble_virt(dub); |
| 531 | } |
| 532 | |
| 533 | uint32_t readString(std::string& str) { |
| 534 | T_VIRTUAL_CALL(); |
| 535 | return readString_virt(str); |
| 536 | } |
| 537 | |
| 538 | uint32_t readBinary(std::string& str) { |
| 539 | T_VIRTUAL_CALL(); |
| 540 | return readBinary_virt(str); |
| 541 | } |
David Reiss | c005b1b | 2008-02-15 01:38:18 +0000 | [diff] [blame] | 542 | |
CJCombrink | 4b90909 | 2024-04-27 19:51:39 +0200 | [diff] [blame] | 543 | uint32_t readUUID(TUuid& uuid) { |
CJCombrink | 1d886ca | 2024-03-23 21:32:28 +0100 | [diff] [blame] | 544 | T_VIRTUAL_CALL(); |
CJCombrink | 4b90909 | 2024-04-27 19:51:39 +0200 | [diff] [blame] | 545 | return readUUID_virt(uuid); |
CJCombrink | 1d886ca | 2024-03-23 21:32:28 +0100 | [diff] [blame] | 546 | } |
| 547 | |
David Reiss | 8dfc732 | 2010-10-06 17:09:58 +0000 | [diff] [blame] | 548 | /* |
| 549 | * std::vector is specialized for bool, and its elements are individual bits |
| 550 | * rather than bools. We need to define a different version of readBool() |
| 551 | * to work with std::vector<bool>. |
| 552 | */ |
| 553 | uint32_t readBool(std::vector<bool>::reference value) { |
| 554 | T_VIRTUAL_CALL(); |
| 555 | return readBool_virt(value); |
David Reiss | 035aed9 | 2009-02-10 21:38:48 +0000 | [diff] [blame] | 556 | } |
| 557 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 558 | /** |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 559 | * Method to arbitrarily skip over data. |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 560 | */ |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 561 | uint32_t skip(TType type) { |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 562 | T_VIRTUAL_CALL(); |
| 563 | return skip_virt(type); |
| 564 | } |
Ben Craig | cfaadcc | 2015-07-08 20:50:33 -0500 | [diff] [blame] | 565 | virtual uint32_t skip_virt(TType type); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 566 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 567 | inline std::shared_ptr<TTransport> getTransport() { return ptrans_; } |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 568 | |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 569 | // TODO: remove these two calls, they are for backwards |
| 570 | // compatibility |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 571 | inline std::shared_ptr<TTransport> getInputTransport() { return ptrans_; } |
| 572 | inline std::shared_ptr<TTransport> getOutputTransport() { return ptrans_; } |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 573 | |
ben-craig | 02bade1 | 2015-07-17 08:40:48 -0500 | [diff] [blame] | 574 | // input and output recursion depth are kept separate so that one protocol |
| 575 | // can be used concurrently for both input and output. |
| 576 | void incrementInputRecursionDepth() { |
| 577 | if (recursion_limit_ < ++input_recursion_depth_) { |
Jens Geyer | 885c679 | 2014-05-02 21:31:55 +0200 | [diff] [blame] | 578 | throw TProtocolException(TProtocolException::DEPTH_LIMIT); |
| 579 | } |
Mark Slee | 43b6c63 | 2007-02-07 00:54:17 +0000 | [diff] [blame] | 580 | } |
ben-craig | 02bade1 | 2015-07-17 08:40:48 -0500 | [diff] [blame] | 581 | void decrementInputRecursionDepth() { --input_recursion_depth_; } |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 582 | |
ben-craig | 02bade1 | 2015-07-17 08:40:48 -0500 | [diff] [blame] | 583 | void incrementOutputRecursionDepth() { |
| 584 | if (recursion_limit_ < ++output_recursion_depth_) { |
| 585 | throw TProtocolException(TProtocolException::DEPTH_LIMIT); |
| 586 | } |
| 587 | } |
| 588 | void decrementOutputRecursionDepth() { --output_recursion_depth_; } |
| 589 | |
Ben Craig | cfaadcc | 2015-07-08 20:50:33 -0500 | [diff] [blame] | 590 | uint32_t getRecursionLimit() const {return recursion_limit_;} |
| 591 | void setRecurisionLimit(uint32_t depth) {recursion_limit_ = depth;} |
Jens Geyer | 885c679 | 2014-05-02 21:31:55 +0200 | [diff] [blame] | 592 | |
zeshuai007 | 86352b4 | 2020-06-15 17:00:33 +0800 | [diff] [blame] | 593 | // Returns the minimum amount of bytes needed to store the smallest possible instance of TType. |
Mario Emmenlauer | 49c6ec7 | 2023-10-10 10:46:09 +0200 | [diff] [blame] | 594 | virtual int getMinSerializedSize(TType type) { |
zeshuai007 | 86352b4 | 2020-06-15 17:00:33 +0800 | [diff] [blame] | 595 | THRIFT_UNUSED_VARIABLE(type); |
| 596 | return 0; |
| 597 | } |
| 598 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 599 | protected: |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 600 | TProtocol(std::shared_ptr<TTransport> ptrans) |
Mario Emmenlauer | 49c6ec7 | 2023-10-10 10:46:09 +0200 | [diff] [blame] | 601 | : ptrans_(ptrans), input_recursion_depth_(0), output_recursion_depth_(0), |
zeshuai007 | 86352b4 | 2020-06-15 17:00:33 +0800 | [diff] [blame] | 602 | recursion_limit_(ptrans->getConfiguration()->getRecursionLimit()) |
Ben Craig | cfaadcc | 2015-07-08 20:50:33 -0500 | [diff] [blame] | 603 | {} |
Jens Geyer | 885c679 | 2014-05-02 21:31:55 +0200 | [diff] [blame] | 604 | |
zeshuai007 | 86352b4 | 2020-06-15 17:00:33 +0800 | [diff] [blame] | 605 | virtual void checkReadBytesAvailable(TSet& set) |
| 606 | { |
| 607 | ptrans_->checkReadBytesAvailable(set.size_ * getMinSerializedSize(set.elemType_)); |
| 608 | } |
| 609 | |
| 610 | virtual void checkReadBytesAvailable(TList& list) |
| 611 | { |
| 612 | ptrans_->checkReadBytesAvailable(list.size_ * getMinSerializedSize(list.elemType_)); |
| 613 | } |
| 614 | |
| 615 | virtual void checkReadBytesAvailable(TMap& map) |
| 616 | { |
| 617 | int elmSize = getMinSerializedSize(map.keyType_) + getMinSerializedSize(map.valueType_); |
| 618 | ptrans_->checkReadBytesAvailable(map.size_ * elmSize); |
| 619 | } |
| 620 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 621 | std::shared_ptr<TTransport> ptrans_; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 622 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 623 | private: |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 624 | TProtocol() = default; |
ben-craig | 02bade1 | 2015-07-17 08:40:48 -0500 | [diff] [blame] | 625 | uint32_t input_recursion_depth_; |
| 626 | uint32_t output_recursion_depth_; |
Jens Geyer | 885c679 | 2014-05-02 21:31:55 +0200 | [diff] [blame] | 627 | uint32_t recursion_limit_; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 628 | }; |
| 629 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 630 | /** |
| 631 | * Constructs input and output protocol objects given transports. |
| 632 | */ |
| 633 | class TProtocolFactory { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 634 | public: |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 635 | TProtocolFactory() = default; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 636 | |
Ben Craig | cfaadcc | 2015-07-08 20:50:33 -0500 | [diff] [blame] | 637 | virtual ~TProtocolFactory(); |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 638 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 639 | virtual std::shared_ptr<TProtocol> getProtocol(std::shared_ptr<TTransport> trans) = 0; |
| 640 | virtual std::shared_ptr<TProtocol> getProtocol(std::shared_ptr<TTransport> inTrans, |
| 641 | std::shared_ptr<TTransport> outTrans) { |
Dave Watson | 792db4e | 2015-01-16 11:22:01 -0800 | [diff] [blame] | 642 | (void)outTrans; |
| 643 | return getProtocol(inTrans); |
| 644 | } |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 645 | }; |
| 646 | |
David Reiss | b7762a0 | 2010-10-06 17:10:00 +0000 | [diff] [blame] | 647 | /** |
| 648 | * Dummy protocol class. |
| 649 | * |
| 650 | * This class does nothing, and should never be instantiated. |
| 651 | * It is used only by the generator code. |
| 652 | */ |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 653 | class TDummyProtocol : public TProtocol {}; |
Ben Craig | 384f976 | 2015-07-08 20:33:03 -0500 | [diff] [blame] | 654 | |
| 655 | // This is the default / legacy choice |
| 656 | struct TNetworkBigEndian |
| 657 | { |
| 658 | static uint16_t toWire16(uint16_t x) {return htons(x);} |
| 659 | static uint32_t toWire32(uint32_t x) {return htonl(x);} |
jfarrell | ad3a955 | 2015-09-24 23:27:34 -0400 | [diff] [blame] | 660 | static uint64_t toWire64(uint64_t x) {return THRIFT_htonll(x);} |
Ben Craig | 384f976 | 2015-07-08 20:33:03 -0500 | [diff] [blame] | 661 | static uint16_t fromWire16(uint16_t x) {return ntohs(x);} |
| 662 | static uint32_t fromWire32(uint32_t x) {return ntohl(x);} |
jfarrell | ad3a955 | 2015-09-24 23:27:34 -0400 | [diff] [blame] | 663 | static uint64_t fromWire64(uint64_t x) {return THRIFT_ntohll(x);} |
Ben Craig | 384f976 | 2015-07-08 20:33:03 -0500 | [diff] [blame] | 664 | }; |
| 665 | |
| 666 | // On most systems, this will be a bit faster than TNetworkBigEndian |
| 667 | struct TNetworkLittleEndian |
| 668 | { |
| 669 | static uint16_t toWire16(uint16_t x) {return THRIFT_htoles(x);} |
| 670 | static uint32_t toWire32(uint32_t x) {return THRIFT_htolel(x);} |
jfarrell | ad3a955 | 2015-09-24 23:27:34 -0400 | [diff] [blame] | 671 | static uint64_t toWire64(uint64_t x) {return THRIFT_htolell(x);} |
Ben Craig | 384f976 | 2015-07-08 20:33:03 -0500 | [diff] [blame] | 672 | static uint16_t fromWire16(uint16_t x) {return THRIFT_letohs(x);} |
| 673 | static uint32_t fromWire32(uint32_t x) {return THRIFT_letohl(x);} |
jfarrell | ad3a955 | 2015-09-24 23:27:34 -0400 | [diff] [blame] | 674 | static uint64_t fromWire64(uint64_t x) {return THRIFT_letohll(x);} |
Ben Craig | 384f976 | 2015-07-08 20:33:03 -0500 | [diff] [blame] | 675 | }; |
| 676 | |
ben-craig | 02bade1 | 2015-07-17 08:40:48 -0500 | [diff] [blame] | 677 | struct TOutputRecursionTracker { |
Ben Craig | cfaadcc | 2015-07-08 20:50:33 -0500 | [diff] [blame] | 678 | TProtocol &prot_; |
ben-craig | 02bade1 | 2015-07-17 08:40:48 -0500 | [diff] [blame] | 679 | TOutputRecursionTracker(TProtocol &prot) : prot_(prot) { |
| 680 | prot_.incrementOutputRecursionDepth(); |
Ben Craig | cfaadcc | 2015-07-08 20:50:33 -0500 | [diff] [blame] | 681 | } |
ben-craig | 02bade1 | 2015-07-17 08:40:48 -0500 | [diff] [blame] | 682 | ~TOutputRecursionTracker() { |
| 683 | prot_.decrementOutputRecursionDepth(); |
| 684 | } |
| 685 | }; |
| 686 | |
| 687 | struct TInputRecursionTracker { |
| 688 | TProtocol &prot_; |
| 689 | TInputRecursionTracker(TProtocol &prot) : prot_(prot) { |
| 690 | prot_.incrementInputRecursionDepth(); |
| 691 | } |
| 692 | ~TInputRecursionTracker() { |
| 693 | prot_.decrementInputRecursionDepth(); |
Ben Craig | cfaadcc | 2015-07-08 20:50:33 -0500 | [diff] [blame] | 694 | } |
| 695 | }; |
| 696 | |
| 697 | /** |
| 698 | * Helper template for implementing TProtocol::skip(). |
| 699 | * |
| 700 | * Templatized to avoid having to make virtual function calls. |
| 701 | */ |
| 702 | template <class Protocol_> |
| 703 | uint32_t skip(Protocol_& prot, TType type) { |
ben-craig | 02bade1 | 2015-07-17 08:40:48 -0500 | [diff] [blame] | 704 | TInputRecursionTracker tracker(prot); |
Ben Craig | cfaadcc | 2015-07-08 20:50:33 -0500 | [diff] [blame] | 705 | |
| 706 | switch (type) { |
| 707 | case T_BOOL: { |
| 708 | bool boolv; |
| 709 | return prot.readBool(boolv); |
| 710 | } |
| 711 | case T_BYTE: { |
James E. King, III | 07f5997 | 2017-03-10 06:18:33 -0500 | [diff] [blame] | 712 | int8_t bytev = 0; |
Ben Craig | cfaadcc | 2015-07-08 20:50:33 -0500 | [diff] [blame] | 713 | return prot.readByte(bytev); |
| 714 | } |
| 715 | case T_I16: { |
| 716 | int16_t i16; |
| 717 | return prot.readI16(i16); |
| 718 | } |
| 719 | case T_I32: { |
| 720 | int32_t i32; |
| 721 | return prot.readI32(i32); |
| 722 | } |
| 723 | case T_I64: { |
| 724 | int64_t i64; |
| 725 | return prot.readI64(i64); |
| 726 | } |
| 727 | case T_DOUBLE: { |
| 728 | double dub; |
| 729 | return prot.readDouble(dub); |
| 730 | } |
| 731 | case T_STRING: { |
| 732 | std::string str; |
| 733 | return prot.readBinary(str); |
| 734 | } |
| 735 | case T_STRUCT: { |
| 736 | uint32_t result = 0; |
| 737 | std::string name; |
| 738 | int16_t fid; |
| 739 | TType ftype; |
| 740 | result += prot.readStructBegin(name); |
| 741 | while (true) { |
| 742 | result += prot.readFieldBegin(name, ftype, fid); |
| 743 | if (ftype == T_STOP) { |
| 744 | break; |
| 745 | } |
| 746 | result += skip(prot, ftype); |
| 747 | result += prot.readFieldEnd(); |
| 748 | } |
| 749 | result += prot.readStructEnd(); |
| 750 | return result; |
| 751 | } |
| 752 | case T_MAP: { |
| 753 | uint32_t result = 0; |
| 754 | TType keyType; |
| 755 | TType valType; |
| 756 | uint32_t i, size; |
| 757 | result += prot.readMapBegin(keyType, valType, size); |
| 758 | for (i = 0; i < size; i++) { |
| 759 | result += skip(prot, keyType); |
| 760 | result += skip(prot, valType); |
| 761 | } |
| 762 | result += prot.readMapEnd(); |
| 763 | return result; |
| 764 | } |
| 765 | case T_SET: { |
| 766 | uint32_t result = 0; |
| 767 | TType elemType; |
| 768 | uint32_t i, size; |
| 769 | result += prot.readSetBegin(elemType, size); |
| 770 | for (i = 0; i < size; i++) { |
| 771 | result += skip(prot, elemType); |
| 772 | } |
| 773 | result += prot.readSetEnd(); |
| 774 | return result; |
| 775 | } |
| 776 | case T_LIST: { |
| 777 | uint32_t result = 0; |
| 778 | TType elemType; |
| 779 | uint32_t i, size; |
| 780 | result += prot.readListBegin(elemType, size); |
| 781 | for (i = 0; i < size; i++) { |
| 782 | result += skip(prot, elemType); |
| 783 | } |
| 784 | result += prot.readListEnd(); |
| 785 | return result; |
| 786 | } |
James E. King, III | 9ce1fd9 | 2017-09-23 21:20:08 -0700 | [diff] [blame] | 787 | default: |
James E. King III | dbc1f8d | 2019-02-14 16:46:38 -0500 | [diff] [blame] | 788 | break; |
Ben Craig | cfaadcc | 2015-07-08 20:50:33 -0500 | [diff] [blame] | 789 | } |
James E. King III | dbc1f8d | 2019-02-14 16:46:38 -0500 | [diff] [blame] | 790 | |
| 791 | throw TProtocolException(TProtocolException::INVALID_DATA, |
| 792 | "invalid TType"); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 793 | } |
Ben Craig | cfaadcc | 2015-07-08 20:50:33 -0500 | [diff] [blame] | 794 | |
| 795 | }}} // apache::thrift::protocol |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 796 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 797 | #endif // #define _THRIFT_PROTOCOL_TPROTOCOL_H_ 1 |