David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
Mark Slee | 9f0c651 | 2007-02-28 23:58:26 +0000 | [diff] [blame] | 19 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 20 | #ifndef _THRIFT_PROTOCOL_TBINARYPROTOCOL_H_ |
| 21 | #define _THRIFT_PROTOCOL_TBINARYPROTOCOL_H_ 1 |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 22 | |
Marc Slemko | d42a2c2 | 2006-08-10 03:30:18 +0000 | [diff] [blame] | 23 | #include "TProtocol.h" |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 24 | |
| 25 | #include <boost/shared_ptr.hpp> |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 26 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 27 | namespace apache { namespace thrift { namespace protocol { |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 28 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 29 | /** |
| 30 | * The default binary protocol for thrift. Writes all data in a very basic |
| 31 | * binary format, essentially just spitting out the raw bytes. |
| 32 | * |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 33 | */ |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 34 | class TBinaryProtocol : public TProtocol { |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 35 | protected: |
| 36 | static const int32_t VERSION_MASK = 0xffff0000; |
| 37 | static const int32_t VERSION_1 = 0x80010000; |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 38 | // VERSION_2 (0x80020000) is taken by TDenseProtocol. |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 39 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 40 | public: |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 41 | TBinaryProtocol(boost::shared_ptr<TTransport> trans) : |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 42 | TProtocol(trans), |
| 43 | string_limit_(0), |
| 44 | container_limit_(0), |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 45 | strict_read_(false), |
| 46 | strict_write_(true), |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 47 | string_buf_(NULL), |
| 48 | string_buf_size_(0) {} |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 49 | |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 50 | TBinaryProtocol(boost::shared_ptr<TTransport> trans, |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 51 | int32_t string_limit, |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 52 | int32_t container_limit, |
| 53 | bool strict_read, |
| 54 | bool strict_write) : |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 55 | TProtocol(trans), |
| 56 | string_limit_(string_limit), |
| 57 | container_limit_(container_limit), |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 58 | strict_read_(strict_read), |
| 59 | strict_write_(strict_write), |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 60 | string_buf_(NULL), |
| 61 | string_buf_size_(0) {} |
| 62 | |
| 63 | ~TBinaryProtocol() { |
| 64 | if (string_buf_ != NULL) { |
David Reiss | d7a16f4 | 2008-02-19 22:47:29 +0000 | [diff] [blame] | 65 | std::free(string_buf_); |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 66 | string_buf_size_ = 0; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | void setStringSizeLimit(int32_t string_limit) { |
| 71 | string_limit_ = string_limit; |
| 72 | } |
| 73 | |
| 74 | void setContainerSizeLimit(int32_t container_limit) { |
| 75 | container_limit_ = container_limit; |
| 76 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 77 | |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 78 | void setStrict(bool strict_read, bool strict_write) { |
| 79 | strict_read_ = strict_read; |
| 80 | strict_write_ = strict_write; |
| 81 | } |
| 82 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 83 | /** |
| 84 | * Writing functions. |
| 85 | */ |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 86 | |
Mark Slee | 82a6c0f | 2007-04-04 21:08:21 +0000 | [diff] [blame] | 87 | virtual uint32_t writeMessageBegin(const std::string& name, |
| 88 | const TMessageType messageType, |
| 89 | const int32_t seqid); |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 90 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 91 | virtual uint32_t writeMessageEnd(); |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 92 | |
| 93 | |
David Reiss | 6412000 | 2008-04-29 23:12:24 +0000 | [diff] [blame] | 94 | uint32_t writeStructBegin(const char* name); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 95 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 96 | uint32_t writeStructEnd(); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 97 | |
David Reiss | 6412000 | 2008-04-29 23:12:24 +0000 | [diff] [blame] | 98 | uint32_t writeFieldBegin(const char* name, |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 99 | const TType fieldType, |
| 100 | const int16_t fieldId); |
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 writeFieldEnd(); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 103 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 104 | uint32_t writeFieldStop(); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 105 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 106 | uint32_t writeMapBegin(const TType keyType, |
| 107 | const TType valType, |
| 108 | const uint32_t size); |
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 writeMapEnd(); |
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 writeListBegin(const TType elemType, |
| 113 | const uint32_t size); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 114 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 115 | uint32_t writeListEnd(); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 116 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 117 | uint32_t writeSetBegin(const TType elemType, |
| 118 | const uint32_t size); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 119 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 120 | uint32_t writeSetEnd(); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 121 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 122 | uint32_t writeBool(const bool value); |
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 writeByte(const int8_t byte); |
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 writeI16(const int16_t i16); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 127 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 128 | uint32_t writeI32(const int32_t i32); |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 129 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 130 | uint32_t writeI64(const int64_t i64); |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 131 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 132 | uint32_t writeDouble(const double dub); |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 133 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 134 | uint32_t writeString(const std::string& str); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 135 | |
David Reiss | c005b1b | 2008-02-15 01:38:18 +0000 | [diff] [blame] | 136 | uint32_t writeBinary(const std::string& str); |
| 137 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 138 | /** |
| 139 | * Reading functions |
| 140 | */ |
| 141 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 142 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 143 | uint32_t readMessageBegin(std::string& name, |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 144 | TMessageType& messageType, |
| 145 | int32_t& seqid); |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 146 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 147 | uint32_t readMessageEnd(); |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 148 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 149 | uint32_t readStructBegin(std::string& name); |
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 readStructEnd(); |
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 readFieldBegin(std::string& name, |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 154 | TType& fieldType, |
| 155 | int16_t& fieldId); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 156 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 157 | uint32_t readFieldEnd(); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 158 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 159 | uint32_t readMapBegin(TType& keyType, |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 160 | TType& valType, |
| 161 | uint32_t& size); |
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 readMapEnd(); |
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 readListBegin(TType& elemType, |
| 166 | uint32_t& size); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 167 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 168 | uint32_t readListEnd(); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 169 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 170 | uint32_t readSetBegin(TType& elemType, |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 171 | uint32_t& size); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 172 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 173 | uint32_t readSetEnd(); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 174 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 175 | uint32_t readBool(bool& value); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 176 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 177 | uint32_t readByte(int8_t& byte); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 178 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 179 | uint32_t readI16(int16_t& i16); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 180 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 181 | uint32_t readI32(int32_t& i32); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 182 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 183 | uint32_t readI64(int64_t& i64); |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 184 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 185 | uint32_t readDouble(double& dub); |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 186 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 187 | uint32_t readString(std::string& str); |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 188 | |
David Reiss | c005b1b | 2008-02-15 01:38:18 +0000 | [diff] [blame] | 189 | uint32_t readBinary(std::string& str); |
| 190 | |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 191 | protected: |
| 192 | uint32_t readStringBody(std::string& str, int32_t sz); |
| 193 | |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 194 | int32_t string_limit_; |
| 195 | int32_t container_limit_; |
| 196 | |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 197 | // Enforce presence of version identifier |
| 198 | bool strict_read_; |
| 199 | bool strict_write_; |
| 200 | |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 201 | // Buffer for reading strings, save for the lifetime of the protocol to |
| 202 | // avoid memory churn allocating memory on every string read |
| 203 | uint8_t* string_buf_; |
| 204 | int32_t string_buf_size_; |
| 205 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 206 | }; |
| 207 | |
| 208 | /** |
| 209 | * Constructs binary protocol handlers |
| 210 | */ |
| 211 | class TBinaryProtocolFactory : public TProtocolFactory { |
| 212 | public: |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 213 | TBinaryProtocolFactory() : |
| 214 | string_limit_(0), |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 215 | container_limit_(0), |
| 216 | strict_read_(false), |
| 217 | strict_write_(true) {} |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 218 | |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 219 | 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] | 220 | string_limit_(string_limit), |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 221 | container_limit_(container_limit), |
| 222 | strict_read_(strict_read), |
| 223 | strict_write_(strict_write) {} |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 224 | |
| 225 | virtual ~TBinaryProtocolFactory() {} |
| 226 | |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 227 | void setStringSizeLimit(int32_t string_limit) { |
| 228 | string_limit_ = string_limit; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 229 | } |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 230 | |
| 231 | void setContainerSizeLimit(int32_t container_limit) { |
| 232 | container_limit_ = container_limit; |
| 233 | } |
| 234 | |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 235 | void setStrict(bool strict_read, bool strict_write) { |
| 236 | strict_read_ = strict_read; |
| 237 | strict_write_ = strict_write; |
| 238 | } |
| 239 | |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 240 | boost::shared_ptr<TProtocol> getProtocol(boost::shared_ptr<TTransport> trans) { |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 241 | 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] | 242 | } |
| 243 | |
| 244 | private: |
| 245 | int32_t string_limit_; |
| 246 | int32_t container_limit_; |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 247 | bool strict_read_; |
| 248 | bool strict_write_; |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 249 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 250 | }; |
| 251 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 252 | }}} // apache::thrift::protocol |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 253 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 254 | #endif // #ifndef _THRIFT_PROTOCOL_TBINARYPROTOCOL_H_ |