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 | |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 14 | namespace facebook { namespace thrift { namespace protocol { |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 15 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 16 | /** |
| 17 | * The default binary protocol for thrift. Writes all data in a very basic |
| 18 | * binary format, essentially just spitting out the raw bytes. |
| 19 | * |
| 20 | * @author Mark Slee <mcslee@facebook.com> |
| 21 | */ |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 22 | class TBinaryProtocol : public TProtocol { |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 23 | protected: |
| 24 | static const int32_t VERSION_MASK = 0xffff0000; |
| 25 | static const int32_t VERSION_1 = 0x80010000; |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 26 | // VERSION_2 (0x80020000) is taken by TDenseProtocol. |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 27 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 28 | public: |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 29 | TBinaryProtocol(boost::shared_ptr<TTransport> trans) : |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 30 | TProtocol(trans), |
| 31 | string_limit_(0), |
| 32 | container_limit_(0), |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 33 | strict_read_(false), |
| 34 | strict_write_(true), |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 35 | string_buf_(NULL), |
| 36 | string_buf_size_(0) {} |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 37 | |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 38 | TBinaryProtocol(boost::shared_ptr<TTransport> trans, |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 39 | int32_t string_limit, |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 40 | int32_t container_limit, |
| 41 | bool strict_read, |
| 42 | bool strict_write) : |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 43 | TProtocol(trans), |
| 44 | string_limit_(string_limit), |
| 45 | container_limit_(container_limit), |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 46 | strict_read_(strict_read), |
| 47 | strict_write_(strict_write), |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 48 | string_buf_(NULL), |
| 49 | string_buf_size_(0) {} |
| 50 | |
| 51 | ~TBinaryProtocol() { |
| 52 | if (string_buf_ != NULL) { |
David Reiss | d7a16f4 | 2008-02-19 22:47:29 +0000 | [diff] [blame^] | 53 | std::free(string_buf_); |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 54 | string_buf_size_ = 0; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | void setStringSizeLimit(int32_t string_limit) { |
| 59 | string_limit_ = string_limit; |
| 60 | } |
| 61 | |
| 62 | void setContainerSizeLimit(int32_t container_limit) { |
| 63 | container_limit_ = container_limit; |
| 64 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 65 | |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 66 | void setStrict(bool strict_read, bool strict_write) { |
| 67 | strict_read_ = strict_read; |
| 68 | strict_write_ = strict_write; |
| 69 | } |
| 70 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 71 | /** |
| 72 | * Writing functions. |
| 73 | */ |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 74 | |
Mark Slee | 82a6c0f | 2007-04-04 21:08:21 +0000 | [diff] [blame] | 75 | virtual uint32_t writeMessageBegin(const std::string& name, |
| 76 | const TMessageType messageType, |
| 77 | const int32_t seqid); |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 78 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 79 | virtual uint32_t writeMessageEnd(); |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 80 | |
| 81 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 82 | uint32_t writeStructBegin(const std::string& name); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 83 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 84 | uint32_t writeStructEnd(); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 85 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 86 | uint32_t writeFieldBegin(const std::string& name, |
| 87 | const TType fieldType, |
| 88 | const int16_t fieldId); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 89 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 90 | uint32_t writeFieldEnd(); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 91 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 92 | uint32_t writeFieldStop(); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 93 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 94 | uint32_t writeMapBegin(const TType keyType, |
| 95 | const TType valType, |
| 96 | const uint32_t size); |
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 writeMapEnd(); |
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 writeListBegin(const TType elemType, |
| 101 | const uint32_t size); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 102 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 103 | uint32_t writeListEnd(); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 104 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 105 | uint32_t writeSetBegin(const TType elemType, |
| 106 | const uint32_t size); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 107 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 108 | uint32_t writeSetEnd(); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 109 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 110 | uint32_t writeBool(const bool value); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 111 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 112 | uint32_t writeByte(const int8_t byte); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 113 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 114 | uint32_t writeI16(const int16_t i16); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 115 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 116 | uint32_t writeI32(const int32_t i32); |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 117 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 118 | uint32_t writeI64(const int64_t i64); |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 119 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 120 | uint32_t writeDouble(const double dub); |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 121 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 122 | uint32_t writeString(const std::string& str); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 123 | |
David Reiss | c005b1b | 2008-02-15 01:38:18 +0000 | [diff] [blame] | 124 | uint32_t writeBinary(const std::string& str); |
| 125 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 126 | /** |
| 127 | * Reading functions |
| 128 | */ |
| 129 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 130 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 131 | uint32_t readMessageBegin(std::string& name, |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 132 | TMessageType& messageType, |
| 133 | int32_t& seqid); |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 134 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 135 | uint32_t readMessageEnd(); |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 136 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 137 | uint32_t readStructBegin(std::string& name); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 138 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 139 | uint32_t readStructEnd(); |
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 readFieldBegin(std::string& name, |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 142 | TType& fieldType, |
| 143 | int16_t& fieldId); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 144 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 145 | uint32_t readFieldEnd(); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 146 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 147 | uint32_t readMapBegin(TType& keyType, |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 148 | TType& valType, |
| 149 | uint32_t& size); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 150 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 151 | uint32_t readMapEnd(); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 152 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 153 | uint32_t readListBegin(TType& elemType, |
| 154 | uint32_t& size); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 155 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 156 | uint32_t readListEnd(); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 157 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 158 | uint32_t readSetBegin(TType& elemType, |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 159 | uint32_t& size); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 160 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 161 | uint32_t readSetEnd(); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 162 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 163 | uint32_t readBool(bool& value); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 164 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 165 | uint32_t readByte(int8_t& byte); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 166 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 167 | uint32_t readI16(int16_t& i16); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 168 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 169 | uint32_t readI32(int32_t& i32); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 170 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 171 | uint32_t readI64(int64_t& i64); |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 172 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 173 | uint32_t readDouble(double& dub); |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 174 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 175 | uint32_t readString(std::string& str); |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 176 | |
David Reiss | c005b1b | 2008-02-15 01:38:18 +0000 | [diff] [blame] | 177 | uint32_t readBinary(std::string& str); |
| 178 | |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 179 | protected: |
| 180 | uint32_t readStringBody(std::string& str, int32_t sz); |
| 181 | |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 182 | int32_t string_limit_; |
| 183 | int32_t container_limit_; |
| 184 | |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 185 | // Enforce presence of version identifier |
| 186 | bool strict_read_; |
| 187 | bool strict_write_; |
| 188 | |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 189 | // Buffer for reading strings, save for the lifetime of the protocol to |
| 190 | // avoid memory churn allocating memory on every string read |
| 191 | uint8_t* string_buf_; |
| 192 | int32_t string_buf_size_; |
| 193 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 194 | }; |
| 195 | |
| 196 | /** |
| 197 | * Constructs binary protocol handlers |
| 198 | */ |
| 199 | class TBinaryProtocolFactory : public TProtocolFactory { |
| 200 | public: |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 201 | TBinaryProtocolFactory() : |
| 202 | string_limit_(0), |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 203 | container_limit_(0), |
| 204 | strict_read_(false), |
| 205 | strict_write_(true) {} |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 206 | |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 207 | TBinaryProtocolFactory(int32_t string_limit, int32_t container_limit, bool strict_read, bool strict_write) : |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 208 | string_limit_(string_limit), |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 209 | container_limit_(container_limit), |
| 210 | strict_read_(strict_read), |
| 211 | strict_write_(strict_write) {} |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 212 | |
| 213 | virtual ~TBinaryProtocolFactory() {} |
| 214 | |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 215 | void setStringSizeLimit(int32_t string_limit) { |
| 216 | string_limit_ = string_limit; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 217 | } |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 218 | |
| 219 | void setContainerSizeLimit(int32_t container_limit) { |
| 220 | container_limit_ = container_limit; |
| 221 | } |
| 222 | |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 223 | void setStrict(bool strict_read, bool strict_write) { |
| 224 | strict_read_ = strict_read; |
| 225 | strict_write_ = strict_write; |
| 226 | } |
| 227 | |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 228 | boost::shared_ptr<TProtocol> getProtocol(boost::shared_ptr<TTransport> trans) { |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 229 | return boost::shared_ptr<TProtocol>(new TBinaryProtocol(trans, string_limit_, container_limit_, strict_read_, strict_write_)); |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | private: |
| 233 | int32_t string_limit_; |
| 234 | int32_t container_limit_; |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 235 | bool strict_read_; |
| 236 | bool strict_write_; |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 237 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 238 | }; |
| 239 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 240 | }}} // facebook::thrift::protocol |
| 241 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 242 | #endif // #ifndef _THRIFT_PROTOCOL_TBINARYPROTOCOL_H_ |