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