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