Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 1 | #ifndef _THRIFT_PROTOCOL_TPROTOCOL_H_ |
| 2 | #define _THRIFT_PROTOCOL_TPROTOCOL_H_ 1 |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 3 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 4 | #include <transport/TTransport.h> |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame^] | 5 | #include <protocol/TProtocolException.h> |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 6 | |
| 7 | #include <boost/shared_ptr.hpp> |
| 8 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 9 | #include <netinet/in.h> |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 10 | #include <sys/types.h> |
| 11 | #include <string> |
| 12 | #include <map> |
| 13 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 14 | namespace facebook { namespace thrift { namespace protocol { |
| 15 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 16 | using namespace boost; |
| 17 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 18 | using namespace facebook::thrift::transport; |
| 19 | |
Mark Slee | dc8a2a2 | 2006-09-19 22:20:18 +0000 | [diff] [blame] | 20 | #if __BYTE_ORDER == __BIG_ENDIAN |
| 21 | #define ntohll(n) (n) |
| 22 | #define htonll(n) (n) |
| 23 | #else |
| 24 | #define ntohll(n) ( (((unsigned long long)ntohl(n)) << 32) + ntohl(n >> 32) ) |
| 25 | #define htonll(n) ( (((unsigned long long)htonl(n)) << 32) + htonl(n >> 32) ) |
| 26 | #endif |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 27 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 28 | // Forward declaration for TProtocol |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 29 | struct TBuf; |
| 30 | |
| 31 | /** |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 32 | * Enumerated definition of the types that the Thrift protocol supports. |
| 33 | * Take special note of the T_END type which is used specifically to mark |
| 34 | * the end of a sequence of fields. |
| 35 | */ |
| 36 | enum TType { |
Marc Slemko | d42a2c2 | 2006-08-10 03:30:18 +0000 | [diff] [blame] | 37 | T_STOP = 0, |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 38 | T_VOID = 1, |
| 39 | T_BOOL = 2, |
| 40 | T_BYTE = 3, |
Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 41 | T_I08 = 3, |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 42 | T_I16 = 6, |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 43 | T_I32 = 8, |
| 44 | T_U64 = 9, |
| 45 | T_I64 = 10, |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 46 | T_DOUBLE = 4, |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 47 | T_STRING = 11, |
Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 48 | T_UTF7 = 11, |
| 49 | T_STRUCT = 12, |
| 50 | T_MAP = 13, |
| 51 | T_SET = 14, |
| 52 | T_LIST = 15, |
| 53 | T_UTF8 = 16, |
| 54 | T_UTF16 = 17 |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | /** |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 58 | * Enumerated definition of the message types that the Thrift protocol |
| 59 | * supports. |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 60 | */ |
| 61 | enum TMessageType { |
| 62 | T_CALL = 1, |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame^] | 63 | T_REPLY = 2, |
| 64 | T_EXCEPTION = 3 |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | /** |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 68 | * Abstract class for a thrift protocol driver. These are all the methods that |
| 69 | * a protocol must implement. Essentially, there must be some way of reading |
| 70 | * and writing all the base types, plus a mechanism for writing out structs |
| 71 | * with indexed fields. Also notice that all methods are strictly const. This |
| 72 | * is by design. Protcol impelementations may NOT keep state, because the |
| 73 | * same TProtocol object may be used simultaneously by multiple threads. This |
| 74 | * theoretically introduces some limititations into the possible protocol |
| 75 | * formats, but with the benefit of performance, clarity, and simplicity. |
| 76 | * |
| 77 | * @author Mark Slee <mcslee@facebook.com> |
| 78 | */ |
| 79 | class TProtocol { |
| 80 | public: |
| 81 | virtual ~TProtocol() {} |
| 82 | |
| 83 | /** |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 84 | * Writing functions. |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 85 | */ |
| 86 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 87 | virtual uint32_t writeMessageBegin(const std::string name, |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 88 | const TMessageType messageType, |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 89 | const int32_t seqid) = 0; |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 90 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 91 | virtual uint32_t writeMessageEnd() = 0; |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 92 | |
| 93 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 94 | virtual uint32_t writeStructBegin(const std::string& name) = 0; |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 95 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 96 | virtual uint32_t writeStructEnd() = 0; |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 97 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 98 | virtual uint32_t writeFieldBegin(const std::string& name, |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 99 | const TType fieldType, |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 100 | const int16_t fieldId) = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 101 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 102 | virtual uint32_t writeFieldEnd() = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 103 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 104 | virtual uint32_t writeFieldStop() = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 105 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 106 | virtual uint32_t writeMapBegin(const TType keyType, |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 107 | const TType valType, |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 108 | const uint32_t size) = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 109 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 110 | virtual uint32_t writeMapEnd() = 0; |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 111 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 112 | virtual uint32_t writeListBegin(const TType elemType, |
| 113 | const uint32_t size) = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 114 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 115 | virtual uint32_t writeListEnd() = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 116 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 117 | virtual uint32_t writeSetBegin(const TType elemType, |
| 118 | const uint32_t size) = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 119 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 120 | virtual uint32_t writeSetEnd() = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 121 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 122 | virtual uint32_t writeBool(const bool value) = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 123 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 124 | virtual uint32_t writeByte(const int8_t byte) = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 125 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 126 | virtual uint32_t writeI16(const int16_t i16) = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 127 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 128 | virtual uint32_t writeI32(const int32_t i32) = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 129 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 130 | virtual uint32_t writeI64(const int64_t i64) = 0; |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 131 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 132 | virtual uint32_t writeDouble(const double dub) = 0; |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 133 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 134 | virtual uint32_t writeString(const std::string& str) = 0; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 135 | |
| 136 | /** |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 137 | * Reading functions |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 138 | */ |
| 139 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 140 | virtual uint32_t readMessageBegin(std::string& name, |
Marc Slemko | 91f6748 | 2006-08-11 23:58:57 +0000 | [diff] [blame] | 141 | TMessageType& messageType, |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 142 | int32_t& seqid) = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 143 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 144 | virtual uint32_t readMessageEnd() = 0; |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 145 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 146 | virtual uint32_t readStructBegin(std::string& name) = 0; |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 147 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 148 | virtual uint32_t readStructEnd() = 0; |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 149 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 150 | virtual uint32_t readFieldBegin(std::string& name, |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 151 | TType& fieldType, |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 152 | int16_t& fieldId) = 0; |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 153 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 154 | virtual uint32_t readFieldEnd() = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 155 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 156 | virtual uint32_t readMapBegin(TType& keyType, |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 157 | TType& valType, |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 158 | uint32_t& size) = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 159 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 160 | virtual uint32_t readMapEnd() = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 161 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 162 | virtual uint32_t readListBegin(TType& elemType, |
| 163 | uint32_t& size) = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 164 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 165 | virtual uint32_t readListEnd() = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 166 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 167 | virtual uint32_t readSetBegin(TType& elemType, |
| 168 | uint32_t& size) = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 169 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 170 | virtual uint32_t readSetEnd() = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 171 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 172 | virtual uint32_t readBool(bool& value) = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 173 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 174 | virtual uint32_t readByte(int8_t& byte) = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 175 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 176 | virtual uint32_t readI16(int16_t& i16) = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 177 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 178 | virtual uint32_t readI32(int32_t& i32) = 0; |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 179 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 180 | virtual uint32_t readI64(int64_t& i64) = 0; |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 181 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 182 | virtual uint32_t readDouble(double& dub) = 0; |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 183 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 184 | virtual uint32_t readString(std::string& str) = 0; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 185 | |
| 186 | /** |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 187 | * Method to arbitrarily skip over data. |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 188 | */ |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 189 | uint32_t skip(TType type) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 190 | switch (type) { |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 191 | case T_BOOL: |
| 192 | { |
| 193 | bool boolv; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 194 | return readBool(boolv); |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 195 | } |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 196 | case T_BYTE: |
| 197 | { |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 198 | int8_t bytev; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 199 | return readByte(bytev); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 200 | } |
Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 201 | case T_I16: |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 202 | { |
Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 203 | int16_t i16; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 204 | return readI16(i16); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 205 | } |
| 206 | case T_I32: |
| 207 | { |
| 208 | int32_t i32; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 209 | return readI32(i32); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 210 | } |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 211 | case T_I64: |
| 212 | { |
| 213 | int64_t i64; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 214 | return readI64(i64); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 215 | } |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 216 | case T_DOUBLE: |
| 217 | { |
| 218 | double dub; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 219 | return readDouble(dub); |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 220 | } |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 221 | case T_STRING: |
| 222 | { |
| 223 | std::string str; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 224 | return readString(str); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 225 | } |
| 226 | case T_STRUCT: |
| 227 | { |
| 228 | uint32_t result = 0; |
| 229 | std::string name; |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 230 | int16_t fid; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 231 | TType ftype; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 232 | result += readStructBegin(name); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 233 | while (true) { |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 234 | result += readFieldBegin(name, ftype, fid); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 235 | if (ftype == T_STOP) { |
| 236 | break; |
| 237 | } |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 238 | result += skip(ftype); |
| 239 | result += readFieldEnd(); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 240 | } |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 241 | result += readStructEnd(); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 242 | return result; |
| 243 | } |
| 244 | case T_MAP: |
| 245 | { |
| 246 | uint32_t result = 0; |
| 247 | TType keyType; |
| 248 | TType valType; |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 249 | uint32_t i, size; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 250 | result += readMapBegin(keyType, valType, size); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 251 | for (i = 0; i < size; i++) { |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 252 | result += skip(keyType); |
| 253 | result += skip(valType); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 254 | } |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 255 | result += readMapEnd(); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 256 | return result; |
| 257 | } |
| 258 | case T_SET: |
| 259 | { |
| 260 | uint32_t result = 0; |
| 261 | TType elemType; |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 262 | uint32_t i, size; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 263 | result += readSetBegin(elemType, size); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 264 | for (i = 0; i < size; i++) { |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 265 | result += skip(elemType); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 266 | } |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 267 | result += readSetEnd(); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 268 | return result; |
| 269 | } |
| 270 | case T_LIST: |
| 271 | { |
| 272 | uint32_t result = 0; |
| 273 | TType elemType; |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 274 | uint32_t i, size; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 275 | result += readListBegin(elemType, size); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 276 | for (i = 0; i < size; i++) { |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 277 | result += skip(elemType); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 278 | } |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 279 | result += readListEnd(); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 280 | return result; |
| 281 | } |
| 282 | default: |
| 283 | return 0; |
| 284 | } |
| 285 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 286 | |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 287 | inline shared_ptr<TTransport> getTransport() { |
Mark Slee | 43b6c63 | 2007-02-07 00:54:17 +0000 | [diff] [blame] | 288 | return ptrans_; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 291 | // TODO: remove these two calls, they are for backwards |
| 292 | // compatibility |
| 293 | inline shared_ptr<TTransport> getInputTransport() { |
Mark Slee | 43b6c63 | 2007-02-07 00:54:17 +0000 | [diff] [blame] | 294 | return ptrans_; |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 295 | } |
| 296 | inline shared_ptr<TTransport> getOutputTransport() { |
Mark Slee | 43b6c63 | 2007-02-07 00:54:17 +0000 | [diff] [blame] | 297 | return ptrans_; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 300 | protected: |
Mark Slee | 43b6c63 | 2007-02-07 00:54:17 +0000 | [diff] [blame] | 301 | TProtocol(shared_ptr<TTransport> ptrans): |
| 302 | ptrans_(ptrans) { |
| 303 | trans_ = ptrans.get(); |
| 304 | } |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 305 | |
Mark Slee | 43b6c63 | 2007-02-07 00:54:17 +0000 | [diff] [blame] | 306 | shared_ptr<TTransport> ptrans_; |
| 307 | TTransport* trans_; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 308 | |
| 309 | private: |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 310 | TProtocol() {} |
| 311 | }; |
| 312 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 313 | /** |
| 314 | * Constructs input and output protocol objects given transports. |
| 315 | */ |
| 316 | class TProtocolFactory { |
| 317 | public: |
| 318 | TProtocolFactory() {} |
| 319 | |
| 320 | virtual ~TProtocolFactory() {} |
| 321 | |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 322 | virtual boost::shared_ptr<TProtocol> getProtocol(boost::shared_ptr<TTransport> trans) = 0; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 323 | }; |
| 324 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 325 | }}} // facebook::thrift::protocol |
| 326 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 327 | #endif // #define _THRIFT_PROTOCOL_TPROTOCOL_H_ 1 |