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