Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 1 | #ifndef T_PROTOCOL_H |
| 2 | #define T_PROTOCOL_H |
| 3 | |
| 4 | #include <sys/types.h> |
| 5 | #include <string> |
| 6 | #include <map> |
| 7 | |
| 8 | /** Forward declaration for TProtocol */ |
| 9 | struct TBuf; |
| 10 | |
| 11 | /** |
| 12 | * Abstract class for a thrift protocol driver. These are all the methods that |
| 13 | * a protocol must implement. Essentially, there must be some way of reading |
| 14 | * and writing all the base types, plus a mechanism for writing out structs |
| 15 | * with indexed fields. Also notice that all methods are strictly const. This |
| 16 | * is by design. Protcol impelementations may NOT keep state, because the |
| 17 | * same TProtocol object may be used simultaneously by multiple threads. This |
| 18 | * theoretically introduces some limititations into the possible protocol |
| 19 | * formats, but with the benefit of performance, clarity, and simplicity. |
| 20 | * |
| 21 | * @author Mark Slee <mcslee@facebook.com> |
| 22 | */ |
| 23 | class TProtocol { |
| 24 | public: |
| 25 | virtual ~TProtocol() {} |
| 26 | |
| 27 | /** |
| 28 | * Function call serialization. |
| 29 | */ |
| 30 | |
| 31 | virtual std::string |
| 32 | readFunction(TBuf& buf) const = 0; |
| 33 | virtual std::string |
| 34 | writeFunction(const std::string& name, const std::string& args) const = 0; |
| 35 | |
| 36 | /** |
| 37 | * Struct serialization. |
| 38 | */ |
| 39 | |
| 40 | virtual std::map<uint32_t, TBuf> |
| 41 | readStruct(TBuf& buf) const = 0; |
| 42 | virtual std::string |
| 43 | writeStruct(const std::map<uint32_t,std::string>& s) const = 0; |
| 44 | |
| 45 | /** |
| 46 | * Basic data type deserialization. Note that these read methods do not |
| 47 | * take a const reference to the TBuf object. They SHOULD change the TBuf |
| 48 | * object so that it reflects the buffer AFTER the basic data type has |
| 49 | * been consumed such that data may continue being read serially from the |
| 50 | * buffer. |
| 51 | */ |
| 52 | |
| 53 | virtual std::string readString (TBuf& buf) const = 0; |
| 54 | virtual uint8_t readByte (TBuf& buf) const = 0; |
| 55 | virtual uint32_t readU32 (TBuf& buf) const = 0; |
| 56 | virtual int32_t readI32 (TBuf& buf) const = 0; |
| 57 | virtual uint64_t readU64 (TBuf& buf) const = 0; |
| 58 | virtual int64_t readI64 (TBuf& buf) const = 0; |
| 59 | |
| 60 | virtual std::string writeString (const std::string& str) const = 0; |
| 61 | virtual std::string writeByte (const uint8_t byte) const = 0; |
| 62 | virtual std::string writeU32 (const uint32_t u32) const = 0; |
| 63 | virtual std::string writeI32 (const int32_t i32) const = 0; |
| 64 | virtual std::string writeU64 (const uint64_t u64) const = 0; |
| 65 | virtual std::string writeI64 (const int64_t i64) const = 0; |
| 66 | |
| 67 | protected: |
| 68 | TProtocol() {} |
| 69 | }; |
| 70 | |
| 71 | /** |
| 72 | * Wrapper around raw data that allows us to track the length of a data |
| 73 | * buffer. It is the responsibility of a robust TProtocol implementation |
| 74 | * to ensure that any reads that are done from data do NOT overrun the |
| 75 | * memory address at data+len. It is also a convention that TBuf objects |
| 76 | * do NOT own the memory pointed to by data. They are merely wrappers |
| 77 | * around buffers that have been allocated elsewhere. Therefore, the user |
| 78 | * should never allocate memory before putting it into a TBuf nor should |
| 79 | * they free the data pointed to by a TBuf. |
| 80 | */ |
| 81 | struct TBuf { |
| 82 | TBuf(const TBuf& that) : data(that.data), len(that.len) {} |
| 83 | TBuf(const uint8_t* d, uint32_t l) : data(d), len(l) {} |
| 84 | const uint8_t* data; |
| 85 | uint32_t len; |
| 86 | }; |
| 87 | |
| 88 | #endif |