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