David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +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 | */ |
| 19 | |
| 20 | #ifndef _THRIFT_PROTOCOL_TCOMPACTPROTOCOL_H_ |
| 21 | #define _THRIFT_PROTOCOL_TCOMPACTPROTOCOL_H_ 1 |
| 22 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 23 | #include "TVirtualProtocol.h" |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 24 | |
| 25 | #include <stack> |
| 26 | #include <boost/shared_ptr.hpp> |
| 27 | |
| 28 | namespace apache { namespace thrift { namespace protocol { |
| 29 | |
| 30 | /** |
| 31 | * C++ Implementation of the Compact Protocol as described in THRIFT-110 |
| 32 | */ |
David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 33 | template <class Transport_> |
| 34 | class TCompactProtocolT |
| 35 | : public TVirtualProtocol< TCompactProtocolT<Transport_> > { |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 36 | |
| 37 | protected: |
| 38 | static const int8_t PROTOCOL_ID = 0x82; |
| 39 | static const int8_t VERSION_N = 1; |
| 40 | static const int8_t VERSION_MASK = 0x1f; // 0001 1111 |
| 41 | static const int8_t TYPE_MASK = 0xE0; // 1110 0000 |
| 42 | static const int32_t TYPE_SHIFT_AMOUNT = 5; |
| 43 | |
David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 44 | Transport_* trans_; |
| 45 | |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 46 | /** |
| 47 | * (Writing) If we encounter a boolean field begin, save the TField here |
| 48 | * so it can have the value incorporated. |
| 49 | */ |
| 50 | struct { |
| 51 | const char* name; |
| 52 | TType fieldType; |
| 53 | int16_t fieldId; |
| 54 | } booleanField_; |
| 55 | |
| 56 | /** |
| 57 | * (Reading) If we read a field header, and it's a boolean field, save |
| 58 | * the boolean value here so that readBool can use it. |
| 59 | */ |
| 60 | struct { |
| 61 | bool hasBoolValue; |
| 62 | bool boolValue; |
| 63 | } boolValue_; |
| 64 | |
| 65 | /** |
| 66 | * Used to keep track of the last field for the current and previous structs, |
| 67 | * so we can do the delta stuff. |
| 68 | */ |
| 69 | |
| 70 | std::stack<int16_t> lastField_; |
| 71 | int16_t lastFieldId_; |
| 72 | |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 73 | public: |
David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 74 | TCompactProtocolT(boost::shared_ptr<Transport_> trans) : |
| 75 | TVirtualProtocol< TCompactProtocolT<Transport_> >(trans), |
| 76 | trans_(trans.get()), |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 77 | lastFieldId_(0), |
| 78 | string_limit_(0), |
| 79 | string_buf_(NULL), |
| 80 | string_buf_size_(0), |
| 81 | container_limit_(0) { |
| 82 | booleanField_.name = NULL; |
| 83 | boolValue_.hasBoolValue = false; |
| 84 | } |
| 85 | |
David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 86 | TCompactProtocolT(boost::shared_ptr<Transport_> trans, |
| 87 | int32_t string_limit, |
| 88 | int32_t container_limit) : |
| 89 | TVirtualProtocol< TCompactProtocolT<Transport_> >(trans), |
| 90 | trans_(trans.get()), |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 91 | lastFieldId_(0), |
| 92 | string_limit_(string_limit), |
| 93 | string_buf_(NULL), |
| 94 | string_buf_size_(0), |
| 95 | container_limit_(container_limit) { |
| 96 | booleanField_.name = NULL; |
| 97 | boolValue_.hasBoolValue = false; |
| 98 | } |
| 99 | |
David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 100 | ~TCompactProtocolT() { |
David Reiss | 4ae8780 | 2010-03-09 00:49:14 +0000 | [diff] [blame] | 101 | free(string_buf_); |
| 102 | } |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 103 | |
| 104 | |
| 105 | /** |
| 106 | * Writing functions |
| 107 | */ |
| 108 | |
| 109 | virtual uint32_t writeMessageBegin(const std::string& name, |
| 110 | const TMessageType messageType, |
| 111 | const int32_t seqid); |
| 112 | |
| 113 | uint32_t writeStructBegin(const char* name); |
| 114 | |
| 115 | uint32_t writeStructEnd(); |
| 116 | |
| 117 | uint32_t writeFieldBegin(const char* name, |
| 118 | const TType fieldType, |
| 119 | const int16_t fieldId); |
| 120 | |
| 121 | uint32_t writeFieldStop(); |
| 122 | |
| 123 | uint32_t writeListBegin(const TType elemType, |
| 124 | const uint32_t size); |
| 125 | |
| 126 | uint32_t writeSetBegin(const TType elemType, |
| 127 | const uint32_t size); |
| 128 | |
| 129 | virtual uint32_t writeMapBegin(const TType keyType, |
| 130 | const TType valType, |
| 131 | const uint32_t size); |
| 132 | |
| 133 | uint32_t writeBool(const bool value); |
| 134 | |
| 135 | uint32_t writeByte(const int8_t byte); |
| 136 | |
| 137 | uint32_t writeI16(const int16_t i16); |
| 138 | |
| 139 | uint32_t writeI32(const int32_t i32); |
| 140 | |
| 141 | uint32_t writeI64(const int64_t i64); |
| 142 | |
| 143 | uint32_t writeDouble(const double dub); |
| 144 | |
| 145 | uint32_t writeString(const std::string& str); |
| 146 | |
| 147 | uint32_t writeBinary(const std::string& str); |
| 148 | |
| 149 | /** |
| 150 | * These methods are called by structs, but don't actually have any wired |
| 151 | * output or purpose |
| 152 | */ |
| 153 | virtual uint32_t writeMessageEnd() { return 0; } |
| 154 | uint32_t writeMapEnd() { return 0; } |
| 155 | uint32_t writeListEnd() { return 0; } |
| 156 | uint32_t writeSetEnd() { return 0; } |
| 157 | uint32_t writeFieldEnd() { return 0; } |
| 158 | |
| 159 | protected: |
| 160 | int32_t writeFieldBeginInternal(const char* name, |
| 161 | const TType fieldType, |
| 162 | const int16_t fieldId, |
| 163 | int8_t typeOverride); |
| 164 | uint32_t writeCollectionBegin(int8_t elemType, int32_t size); |
| 165 | uint32_t writeVarint32(uint32_t n); |
| 166 | uint32_t writeVarint64(uint64_t n); |
| 167 | uint64_t i64ToZigzag(const int64_t l); |
| 168 | uint32_t i32ToZigzag(const int32_t n); |
| 169 | inline int8_t getCompactType(int8_t ttype); |
| 170 | |
| 171 | public: |
| 172 | uint32_t readMessageBegin(std::string& name, |
| 173 | TMessageType& messageType, |
| 174 | int32_t& seqid); |
| 175 | |
| 176 | uint32_t readStructBegin(std::string& name); |
| 177 | |
| 178 | uint32_t readStructEnd(); |
| 179 | |
| 180 | uint32_t readFieldBegin(std::string& name, |
| 181 | TType& fieldType, |
| 182 | int16_t& fieldId); |
| 183 | |
| 184 | uint32_t readMapBegin(TType& keyType, |
| 185 | TType& valType, |
| 186 | uint32_t& size); |
| 187 | |
| 188 | uint32_t readListBegin(TType& elemType, |
| 189 | uint32_t& size); |
| 190 | |
| 191 | uint32_t readSetBegin(TType& elemType, |
| 192 | uint32_t& size); |
| 193 | |
| 194 | uint32_t readBool(bool& value); |
David Reiss | 8dfc732 | 2010-10-06 17:09:58 +0000 | [diff] [blame] | 195 | // Provide the default readBool() implementation for std::vector<bool> |
| 196 | using TVirtualProtocol< TCompactProtocolT<Transport_> >::readBool; |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 197 | |
| 198 | uint32_t readByte(int8_t& byte); |
| 199 | |
| 200 | uint32_t readI16(int16_t& i16); |
| 201 | |
| 202 | uint32_t readI32(int32_t& i32); |
| 203 | |
| 204 | uint32_t readI64(int64_t& i64); |
| 205 | |
| 206 | uint32_t readDouble(double& dub); |
| 207 | |
| 208 | uint32_t readString(std::string& str); |
| 209 | |
| 210 | uint32_t readBinary(std::string& str); |
| 211 | |
| 212 | /* |
| 213 | *These methods are here for the struct to call, but don't have any wire |
| 214 | * encoding. |
| 215 | */ |
| 216 | uint32_t readMessageEnd() { return 0; } |
| 217 | uint32_t readFieldEnd() { return 0; } |
| 218 | uint32_t readMapEnd() { return 0; } |
| 219 | uint32_t readListEnd() { return 0; } |
| 220 | uint32_t readSetEnd() { return 0; } |
| 221 | |
| 222 | protected: |
| 223 | uint32_t readVarint32(int32_t& i32); |
| 224 | uint32_t readVarint64(int64_t& i64); |
| 225 | int32_t zigzagToI32(uint32_t n); |
| 226 | int64_t zigzagToI64(uint64_t n); |
| 227 | TType getTType(int8_t type); |
| 228 | |
| 229 | // Buffer for reading strings, save for the lifetime of the protocol to |
| 230 | // avoid memory churn allocating memory on every string read |
| 231 | int32_t string_limit_; |
| 232 | uint8_t* string_buf_; |
| 233 | int32_t string_buf_size_; |
| 234 | int32_t container_limit_; |
| 235 | }; |
| 236 | |
David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 237 | typedef TCompactProtocolT<TTransport> TCompactProtocol; |
| 238 | |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 239 | /** |
| 240 | * Constructs compact protocol handlers |
| 241 | */ |
David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 242 | template <class Transport_> |
| 243 | class TCompactProtocolFactoryT : public TProtocolFactory { |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 244 | public: |
David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 245 | TCompactProtocolFactoryT() : |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 246 | string_limit_(0), |
| 247 | container_limit_(0) {} |
| 248 | |
David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 249 | TCompactProtocolFactoryT(int32_t string_limit, int32_t container_limit) : |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 250 | string_limit_(string_limit), |
| 251 | container_limit_(container_limit) {} |
| 252 | |
David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 253 | virtual ~TCompactProtocolFactoryT() {} |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 254 | |
| 255 | void setStringSizeLimit(int32_t string_limit) { |
| 256 | string_limit_ = string_limit; |
| 257 | } |
| 258 | |
| 259 | void setContainerSizeLimit(int32_t container_limit) { |
| 260 | container_limit_ = container_limit; |
| 261 | } |
| 262 | |
| 263 | boost::shared_ptr<TProtocol> getProtocol(boost::shared_ptr<TTransport> trans) { |
David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 264 | boost::shared_ptr<Transport_> specific_trans = |
| 265 | boost::dynamic_pointer_cast<Transport_>(trans); |
| 266 | TProtocol* prot; |
| 267 | if (specific_trans) { |
| 268 | prot = new TCompactProtocolT<Transport_>(specific_trans, string_limit_, |
| 269 | container_limit_); |
| 270 | } else { |
| 271 | prot = new TCompactProtocol(trans, string_limit_, container_limit_); |
| 272 | } |
| 273 | |
| 274 | return boost::shared_ptr<TProtocol>(prot); |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | private: |
| 278 | int32_t string_limit_; |
| 279 | int32_t container_limit_; |
| 280 | |
| 281 | }; |
| 282 | |
David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 283 | typedef TCompactProtocolFactoryT<TTransport> TCompactProtocolFactory; |
| 284 | |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 285 | }}} // apache::thrift::protocol |
| 286 | |
David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 287 | #include "TCompactProtocol.tcc" |
| 288 | |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 289 | #endif |