Mark Slee | 9f0c651 | 2007-02-28 23:58:26 +0000 | [diff] [blame] | 1 | // Copyright (c) 2006- Facebook |
| 2 | // Distributed under the Thrift Software License |
| 3 | // |
| 4 | // See accompanying file LICENSE or visit the Thrift site at: |
| 5 | // http://developers.facebook.com/thrift/ |
| 6 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 7 | #ifndef _THRIFT_PROTOCOL_TBINARYPROTOCOL_H_ |
| 8 | #define _THRIFT_PROTOCOL_TBINARYPROTOCOL_H_ 1 |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 9 | |
Marc Slemko | d42a2c2 | 2006-08-10 03:30:18 +0000 | [diff] [blame] | 10 | #include "TProtocol.h" |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 11 | |
| 12 | #include <boost/shared_ptr.hpp> |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 13 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 14 | namespace facebook { namespace thrift { namespace protocol { |
| 15 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 16 | using namespace boost; |
| 17 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 18 | /** |
| 19 | * The default binary protocol for thrift. Writes all data in a very basic |
| 20 | * binary format, essentially just spitting out the raw bytes. |
| 21 | * |
| 22 | * @author Mark Slee <mcslee@facebook.com> |
| 23 | */ |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 24 | class TBinaryProtocol : public TProtocol { |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 25 | public: |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 26 | TBinaryProtocol(shared_ptr<TTransport> trans) : |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 27 | TProtocol(trans), |
| 28 | string_limit_(0), |
| 29 | container_limit_(0), |
| 30 | string_buf_(NULL), |
| 31 | string_buf_size_(0) {} |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 32 | |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 33 | TBinaryProtocol(shared_ptr<TTransport> trans, |
| 34 | int32_t string_limit, |
| 35 | int32_t container_limit) : |
| 36 | TProtocol(trans), |
| 37 | string_limit_(string_limit), |
| 38 | container_limit_(container_limit), |
| 39 | string_buf_(NULL), |
| 40 | string_buf_size_(0) {} |
| 41 | |
| 42 | ~TBinaryProtocol() { |
| 43 | if (string_buf_ != NULL) { |
| 44 | free(string_buf_); |
| 45 | string_buf_size_ = 0; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | void setStringSizeLimit(int32_t string_limit) { |
| 50 | string_limit_ = string_limit; |
| 51 | } |
| 52 | |
| 53 | void setContainerSizeLimit(int32_t container_limit) { |
| 54 | container_limit_ = container_limit; |
| 55 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 56 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 57 | /** |
| 58 | * Writing functions. |
| 59 | */ |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 60 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 61 | virtual uint32_t writeMessageBegin(const std::string name, |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 62 | const TMessageType messageType, |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 63 | const int32_t seqid); |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 64 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 65 | virtual uint32_t writeMessageEnd(); |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 66 | |
| 67 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 68 | uint32_t writeStructBegin(const std::string& name); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 69 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 70 | uint32_t writeStructEnd(); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 71 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 72 | uint32_t writeFieldBegin(const std::string& name, |
| 73 | const TType fieldType, |
| 74 | const int16_t fieldId); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 75 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 76 | uint32_t writeFieldEnd(); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 77 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 78 | uint32_t writeFieldStop(); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 79 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 80 | uint32_t writeMapBegin(const TType keyType, |
| 81 | const TType valType, |
| 82 | const uint32_t size); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 83 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 84 | uint32_t writeMapEnd(); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 85 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 86 | uint32_t writeListBegin(const TType elemType, |
| 87 | const uint32_t size); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 88 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 89 | uint32_t writeListEnd(); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 90 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 91 | uint32_t writeSetBegin(const TType elemType, |
| 92 | const uint32_t size); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 93 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 94 | uint32_t writeSetEnd(); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 95 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 96 | uint32_t writeBool(const bool value); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 97 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 98 | uint32_t writeByte(const int8_t byte); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 99 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 100 | uint32_t writeI16(const int16_t i16); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 101 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 102 | uint32_t writeI32(const int32_t i32); |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 103 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 104 | uint32_t writeI64(const int64_t i64); |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 105 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 106 | uint32_t writeDouble(const double dub); |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 107 | |
| 108 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 109 | uint32_t writeString(const std::string& str); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 110 | |
| 111 | /** |
| 112 | * Reading functions |
| 113 | */ |
| 114 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 115 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 116 | uint32_t readMessageBegin(std::string& name, |
Marc Slemko | e6889de | 2006-08-12 00:32:53 +0000 | [diff] [blame] | 117 | TMessageType& messageType, |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 118 | int32_t& seqid); |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 119 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 120 | uint32_t readMessageEnd(); |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 121 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 122 | uint32_t readStructBegin(std::string& name); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 123 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 124 | uint32_t readStructEnd(); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 125 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 126 | uint32_t readFieldBegin(std::string& name, |
| 127 | TType& fieldType, |
| 128 | int16_t& fieldId); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 129 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 130 | uint32_t readFieldEnd(); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 131 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 132 | uint32_t readMapBegin(TType& keyType, |
| 133 | TType& valType, |
| 134 | uint32_t& size); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 135 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 136 | uint32_t readMapEnd(); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 137 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 138 | uint32_t readListBegin(TType& elemType, |
| 139 | uint32_t& size); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 140 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 141 | uint32_t readListEnd(); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 142 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 143 | uint32_t readSetBegin(TType& elemType, |
| 144 | uint32_t& size); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 145 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 146 | uint32_t readSetEnd(); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 147 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 148 | uint32_t readBool(bool& value); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 149 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 150 | uint32_t readByte(int8_t& byte); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 151 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 152 | uint32_t readI16(int16_t& i16); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 153 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 154 | uint32_t readI32(int32_t& i32); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 155 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 156 | uint32_t readI64(int64_t& i64); |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 157 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 158 | uint32_t readDouble(double& dub); |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 159 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 160 | uint32_t readString(std::string& str); |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 161 | |
| 162 | private: |
| 163 | int32_t string_limit_; |
| 164 | int32_t container_limit_; |
| 165 | |
| 166 | // Buffer for reading strings, save for the lifetime of the protocol to |
| 167 | // avoid memory churn allocating memory on every string read |
| 168 | uint8_t* string_buf_; |
| 169 | int32_t string_buf_size_; |
| 170 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 171 | }; |
| 172 | |
| 173 | /** |
| 174 | * Constructs binary protocol handlers |
| 175 | */ |
| 176 | class TBinaryProtocolFactory : public TProtocolFactory { |
| 177 | public: |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 178 | TBinaryProtocolFactory() : |
| 179 | string_limit_(0), |
| 180 | container_limit_(0) {} |
| 181 | |
| 182 | TBinaryProtocolFactory(int32_t string_limit, int32_t container_limit) : |
| 183 | string_limit_(string_limit), |
| 184 | container_limit_(container_limit) {} |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 185 | |
| 186 | virtual ~TBinaryProtocolFactory() {} |
| 187 | |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 188 | void setStringSizeLimit(int32_t string_limit) { |
| 189 | string_limit_ = string_limit; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 190 | } |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 191 | |
| 192 | void setContainerSizeLimit(int32_t container_limit) { |
| 193 | container_limit_ = container_limit; |
| 194 | } |
| 195 | |
| 196 | boost::shared_ptr<TProtocol> getProtocol(boost::shared_ptr<TTransport> trans) { |
| 197 | return boost::shared_ptr<TProtocol>(new TBinaryProtocol(trans, string_limit_, container_limit_)); |
| 198 | } |
| 199 | |
| 200 | private: |
| 201 | int32_t string_limit_; |
| 202 | int32_t container_limit_; |
| 203 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 204 | }; |
| 205 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 206 | }}} // facebook::thrift::protocol |
| 207 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 208 | #endif // #ifndef _THRIFT_PROTOCOL_TBINARYPROTOCOL_H_ |
| 209 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 210 | |