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