Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 1 | #ifndef T_PROTOCOL_H |
| 2 | #define T_PROTOCOL_H |
| 3 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 4 | #include <netinet/in.h> |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 5 | #include <sys/types.h> |
| 6 | #include <string> |
| 7 | #include <map> |
| 8 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 9 | #include "transport/TTransport.h" |
| 10 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 11 | namespace facebook { namespace thrift { namespace protocol { |
| 12 | |
| 13 | using namespace facebook::thrift::transport; |
| 14 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 15 | #define ntohll(x) (((uint64_t)(ntohl((int)((x << 32) >> 32))) << 32) | (uint32_t)ntohl(((int)(x >> 32)))) |
| 16 | |
| 17 | #define htonll(x) ntohll(x) |
| 18 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 19 | /** Forward declaration for TProtocol */ |
| 20 | struct TBuf; |
| 21 | |
| 22 | /** |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 23 | * Enumerated definition of the types that the Thrift protocol supports. |
| 24 | * Take special note of the T_END type which is used specifically to mark |
| 25 | * the end of a sequence of fields. |
| 26 | */ |
| 27 | enum TType { |
| 28 | T_STOP = 1, |
| 29 | T_BYTE = 2, |
| 30 | T_U16 = 3, |
| 31 | T_I16 = 4, |
| 32 | T_U32 = 5, |
| 33 | T_I32 = 6, |
| 34 | T_U64 = 7, |
| 35 | T_I64 = 8, |
| 36 | T_STRING = 9, |
| 37 | T_STRUCT = 10, |
| 38 | T_MAP = 11, |
| 39 | T_SET = 12, |
| 40 | T_LIST = 13 |
| 41 | }; |
| 42 | |
| 43 | /** |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 44 | * Abstract class for a thrift protocol driver. These are all the methods that |
| 45 | * a protocol must implement. Essentially, there must be some way of reading |
| 46 | * and writing all the base types, plus a mechanism for writing out structs |
| 47 | * with indexed fields. Also notice that all methods are strictly const. This |
| 48 | * is by design. Protcol impelementations may NOT keep state, because the |
| 49 | * same TProtocol object may be used simultaneously by multiple threads. This |
| 50 | * theoretically introduces some limititations into the possible protocol |
| 51 | * formats, but with the benefit of performance, clarity, and simplicity. |
| 52 | * |
| 53 | * @author Mark Slee <mcslee@facebook.com> |
| 54 | */ |
| 55 | class TProtocol { |
| 56 | public: |
| 57 | virtual ~TProtocol() {} |
| 58 | |
| 59 | /** |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 60 | * Writing functions. |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 61 | */ |
| 62 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 63 | virtual uint32_t writeStructBegin (TTransport* out, |
| 64 | const std::string& name) const = 0; |
| 65 | |
| 66 | virtual uint32_t writeStructEnd (TTransport* out) const = 0; |
| 67 | |
| 68 | virtual uint32_t writeFieldBegin (TTransport* out, |
| 69 | const std::string& name, |
| 70 | const TType fieldType, |
| 71 | const uint16_t fieldId) const = 0; |
| 72 | |
| 73 | virtual uint32_t writeFieldEnd (TTransport* out) const = 0; |
| 74 | |
| 75 | virtual uint32_t writeFieldStop (TTransport* out) const = 0; |
| 76 | |
| 77 | virtual uint32_t writeMapBegin (TTransport* out, |
| 78 | const TType keyType, |
| 79 | const TType valType, |
Mark Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame] | 80 | const int32_t size) const = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 81 | |
| 82 | virtual uint32_t writeMapEnd (TTransport* out) const = 0; |
| 83 | |
| 84 | virtual uint32_t writeListBegin (TTransport* out, |
| 85 | const TType elemType, |
Mark Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame] | 86 | const int32_t size) const = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 87 | |
| 88 | virtual uint32_t writeListEnd (TTransport* out) const = 0; |
| 89 | |
| 90 | virtual uint32_t writeSetBegin (TTransport* out, |
| 91 | const TType elemType, |
Mark Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame] | 92 | const int32_t size) const = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 93 | |
| 94 | virtual uint32_t writeSetEnd (TTransport* out) const = 0; |
| 95 | |
| 96 | virtual uint32_t writeByte (TTransport* out, |
| 97 | const uint8_t byte) const = 0; |
| 98 | |
| 99 | virtual uint32_t writeU32 (TTransport* out, |
| 100 | const uint32_t u32) const = 0; |
| 101 | |
| 102 | virtual uint32_t writeI32 (TTransport* out, |
| 103 | const int32_t i32) const = 0; |
| 104 | |
| 105 | virtual uint32_t writeU64 (TTransport* out, |
| 106 | const uint64_t u64) const = 0; |
| 107 | |
| 108 | virtual uint32_t writeI64 (TTransport* out, |
| 109 | const int64_t i64) const = 0; |
| 110 | |
| 111 | virtual uint32_t writeString (TTransport* out, |
| 112 | const std::string& str) const = 0; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 113 | |
| 114 | /** |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 115 | * Reading functions |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 116 | */ |
| 117 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 118 | virtual uint32_t readStructBegin (TTransport* in, |
| 119 | std::string& name) const = 0; |
| 120 | |
| 121 | virtual uint32_t readStructEnd (TTransport* in) const = 0; |
| 122 | |
| 123 | virtual uint32_t readFieldBegin (TTransport* in, |
| 124 | std::string& name, |
| 125 | TType& fieldType, |
| 126 | uint16_t& fieldId) const = 0; |
| 127 | |
| 128 | virtual uint32_t readFieldEnd (TTransport* in) const = 0; |
| 129 | |
| 130 | virtual uint32_t readMapBegin (TTransport* in, |
| 131 | TType& keyType, |
| 132 | TType& valType, |
Mark Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame] | 133 | int32_t& size) const = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 134 | |
| 135 | virtual uint32_t readMapEnd (TTransport* in) const = 0; |
| 136 | |
| 137 | virtual uint32_t readListBegin (TTransport* in, |
| 138 | TType& elemType, |
Mark Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame] | 139 | int32_t& size) const = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 140 | |
| 141 | virtual uint32_t readListEnd (TTransport* in) const = 0; |
| 142 | |
| 143 | virtual uint32_t readSetBegin (TTransport* in, |
| 144 | TType& elemType, |
Mark Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame] | 145 | int32_t& size) const = 0; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 146 | |
| 147 | virtual uint32_t readSetEnd (TTransport* in) const = 0; |
| 148 | |
| 149 | virtual uint32_t readByte (TTransport* in, |
| 150 | uint8_t& byte) const = 0; |
| 151 | |
| 152 | virtual uint32_t readU32 (TTransport* in, |
| 153 | uint32_t& u32) const = 0; |
| 154 | |
| 155 | virtual uint32_t readI32 (TTransport* in, |
| 156 | int32_t& i32) const = 0; |
| 157 | |
| 158 | virtual uint32_t readU64 (TTransport* in, |
| 159 | uint64_t& u64) const = 0; |
| 160 | |
| 161 | virtual uint32_t readI64 (TTransport* in, |
| 162 | int64_t& i64) const = 0; |
| 163 | |
| 164 | virtual uint32_t readString (TTransport* in, |
| 165 | std::string& str) const = 0; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 166 | |
| 167 | /** |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 168 | * Method to arbitrarily skip over data. |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 169 | */ |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 170 | uint32_t skip(TTransport* in, TType type) const { |
| 171 | switch (type) { |
| 172 | case T_BYTE: |
| 173 | { |
| 174 | uint8_t byte; |
| 175 | return readByte(in, byte); |
| 176 | } |
| 177 | case T_U32: |
| 178 | { |
| 179 | uint32_t u32; |
| 180 | return readU32(in, u32); |
| 181 | } |
| 182 | case T_I32: |
| 183 | { |
| 184 | int32_t i32; |
| 185 | return readI32(in, i32); |
| 186 | } |
| 187 | case T_U64: |
| 188 | { |
| 189 | uint64_t u64; |
| 190 | return readU64(in, u64); |
| 191 | } |
| 192 | case T_I64: |
| 193 | { |
| 194 | int64_t i64; |
| 195 | return readI64(in, i64); |
| 196 | } |
| 197 | case T_STRING: |
| 198 | { |
| 199 | std::string str; |
| 200 | return readString(in, str); |
| 201 | } |
| 202 | case T_STRUCT: |
| 203 | { |
| 204 | uint32_t result = 0; |
| 205 | std::string name; |
| 206 | uint16_t fid; |
| 207 | TType ftype; |
| 208 | result += readStructBegin(in, name); |
| 209 | while (true) { |
| 210 | result += readFieldBegin(in, name, ftype, fid); |
| 211 | if (ftype == T_STOP) { |
| 212 | break; |
| 213 | } |
| 214 | result += skip(in, ftype); |
| 215 | result += readFieldEnd(in); |
| 216 | } |
| 217 | result += readStructEnd(in); |
| 218 | return result; |
| 219 | } |
| 220 | case T_MAP: |
| 221 | { |
| 222 | uint32_t result = 0; |
| 223 | TType keyType; |
| 224 | TType valType; |
Mark Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame] | 225 | int32_t i, size; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 226 | result += readMapBegin(in, keyType, valType, size); |
| 227 | for (i = 0; i < size; i++) { |
| 228 | result += skip(in, keyType); |
| 229 | result += skip(in, valType); |
| 230 | } |
| 231 | result += readMapEnd(in); |
| 232 | return result; |
| 233 | } |
| 234 | case T_SET: |
| 235 | { |
| 236 | uint32_t result = 0; |
| 237 | TType elemType; |
Mark Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame] | 238 | int32_t i, size; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 239 | result += readSetBegin(in, elemType, size); |
| 240 | for (i = 0; i < size; i++) { |
| 241 | result += skip(in, elemType); |
| 242 | } |
| 243 | result += readSetEnd(in); |
| 244 | return result; |
| 245 | } |
| 246 | case T_LIST: |
| 247 | { |
| 248 | uint32_t result = 0; |
| 249 | TType elemType; |
Mark Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame] | 250 | int32_t i, size; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 251 | result += readListBegin(in, elemType, size); |
| 252 | for (i = 0; i < size; i++) { |
| 253 | result += skip(in, elemType); |
| 254 | } |
| 255 | result += readListEnd(in); |
| 256 | return result; |
| 257 | } |
| 258 | default: |
| 259 | return 0; |
| 260 | } |
| 261 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 262 | |
| 263 | protected: |
| 264 | TProtocol() {} |
| 265 | }; |
| 266 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 267 | }}} // facebook::thrift::protocol |
| 268 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 269 | #endif |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 270 | |