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 | */ |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 19 | |
| 20 | #ifndef _THRIFT_PROTOCOL_TDENSEPROTOCOL_H_ |
| 21 | #define _THRIFT_PROTOCOL_TDENSEPROTOCOL_H_ 1 |
| 22 | |
Roger Meier | 4285ba2 | 2013-06-10 21:17:23 +0200 | [diff] [blame] | 23 | #include <thrift/protocol/TBinaryProtocol.h> |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 24 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 25 | namespace apache { |
| 26 | namespace thrift { |
| 27 | namespace protocol { |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 28 | |
| 29 | /** |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 30 | * !!!WARNING!!! |
| 31 | * This class is still highly experimental. Incompatible changes |
| 32 | * WILL be made to it without notice. DO NOT USE IT YET unless |
| 33 | * you are coordinating your testing with the author. |
| 34 | * |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 35 | * The dense protocol is designed to use as little space as possible. |
| 36 | * |
| 37 | * There are two types of dense protocol instances. Standalone instances |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 38 | * are not used for RPC and just encoded and decode structures of |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 39 | * a predetermined type. Non-standalone instances are used for RPC. |
| 40 | * Currently, only standalone instances exist. |
| 41 | * |
| 42 | * To use a standalone dense protocol object, you must set the type_spec |
| 43 | * property (either in the constructor, or with setTypeSpec) to the local |
| 44 | * reflection TypeSpec of the structures you will write to (or read from) the |
| 45 | * protocol instance. |
| 46 | * |
| 47 | * BEST PRACTICES: |
| 48 | * - Never use optional for primitives or containers. |
| 49 | * - Only use optional for structures if they are very big and very rarely set. |
| 50 | * - All integers are variable-length, so you can use i64 without bloating. |
| 51 | * - NEVER EVER change the struct definitions IN ANY WAY without either |
| 52 | * changing your cache keys or talking to dreiss. |
| 53 | * |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 54 | * TODO(dreiss): New class write with old meta. |
| 55 | * |
| 56 | * We override all of TBinaryProtocol's methods. |
Konrad Grochowski | 6794698 | 2014-12-04 23:22:41 +0100 | [diff] [blame] | 57 | * We inherit so that we can explicitly call TBPs's primitive-writing |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 58 | * methods within our versions. |
| 59 | * |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 60 | */ |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 61 | class TDenseProtocol : public TVirtualProtocol<TDenseProtocol, TBinaryProtocol> { |
| 62 | protected: |
Roger Meier | 406fc74 | 2011-12-08 11:32:21 +0000 | [diff] [blame] | 63 | static const int32_t VERSION_MASK = ((int32_t)0xffff0000); |
David Reiss | e67c0e6 | 2007-09-07 01:34:12 +0000 | [diff] [blame] | 64 | // VERSION_1 (0x80010000) is taken by TBinaryProtocol. |
Roger Meier | 406fc74 | 2011-12-08 11:32:21 +0000 | [diff] [blame] | 65 | static const int32_t VERSION_2 = ((int32_t)0x80020000); |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 66 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 67 | public: |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 68 | typedef apache::thrift::reflection::local::TypeSpec TypeSpec; |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 69 | static const int FP_PREFIX_LEN; |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 70 | |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 71 | /** |
| 72 | * @param tran The transport to use. |
| 73 | * @param type_spec The TypeSpec of the structures using this protocol. |
| 74 | */ |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 75 | TDenseProtocol(boost::shared_ptr<TTransport> trans, TypeSpec* type_spec = NULL) |
| 76 | : TVirtualProtocol<TDenseProtocol, TBinaryProtocol>(trans), |
| 77 | type_spec_(type_spec), |
| 78 | standalone_(true) {} |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 79 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 80 | void setTypeSpec(TypeSpec* type_spec) { type_spec_ = type_spec; } |
| 81 | TypeSpec* getTypeSpec() { return type_spec_; } |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 82 | |
| 83 | /* |
| 84 | * Writing functions. |
| 85 | */ |
| 86 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 87 | uint32_t writeMessageBegin(const std::string& name, |
| 88 | const TMessageType messageType, |
| 89 | const int32_t seqid); |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 90 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 91 | uint32_t writeMessageEnd(); |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 92 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 93 | uint32_t writeStructBegin(const char* name); |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 94 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 95 | uint32_t writeStructEnd(); |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 96 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 97 | uint32_t writeFieldBegin(const char* name, const TType fieldType, const int16_t fieldId); |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 98 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 99 | uint32_t writeFieldEnd(); |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 100 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 101 | uint32_t writeFieldStop(); |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 102 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 103 | uint32_t writeMapBegin(const TType keyType, const TType valType, const uint32_t size); |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 104 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 105 | uint32_t writeMapEnd(); |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 106 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 107 | uint32_t writeListBegin(const TType elemType, const uint32_t size); |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 108 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 109 | uint32_t writeListEnd(); |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 110 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 111 | uint32_t writeSetBegin(const TType elemType, const uint32_t size); |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 112 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 113 | uint32_t writeSetEnd(); |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 114 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 115 | uint32_t writeBool(const bool value); |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 116 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 117 | uint32_t writeByte(const int8_t byte); |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 118 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 119 | uint32_t writeI16(const int16_t i16); |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 120 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 121 | uint32_t writeI32(const int32_t i32); |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 122 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 123 | uint32_t writeI64(const int64_t i64); |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 124 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 125 | uint32_t writeDouble(const double dub); |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 126 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 127 | uint32_t writeString(const std::string& str); |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 128 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 129 | uint32_t writeBinary(const std::string& str); |
David Reiss | c005b1b | 2008-02-15 01:38:18 +0000 | [diff] [blame] | 130 | |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 131 | /* |
| 132 | * Helper writing functions (don't do state transitions). |
| 133 | */ |
| 134 | inline uint32_t subWriteI32(const int32_t i32); |
| 135 | |
David Reiss | e67c0e6 | 2007-09-07 01:34:12 +0000 | [diff] [blame] | 136 | inline uint32_t subWriteString(const std::string& str); |
| 137 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 138 | uint32_t subWriteBool(const bool value) { return TBinaryProtocol::writeBool(value); } |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 139 | |
| 140 | /* |
| 141 | * Reading functions |
| 142 | */ |
| 143 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 144 | uint32_t readMessageBegin(std::string& name, TMessageType& messageType, int32_t& seqid); |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 145 | |
| 146 | uint32_t readMessageEnd(); |
| 147 | |
| 148 | uint32_t readStructBegin(std::string& name); |
| 149 | |
| 150 | uint32_t readStructEnd(); |
| 151 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 152 | uint32_t readFieldBegin(std::string& name, TType& fieldType, int16_t& fieldId); |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 153 | |
| 154 | uint32_t readFieldEnd(); |
| 155 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 156 | uint32_t readMapBegin(TType& keyType, TType& valType, uint32_t& size); |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 157 | |
| 158 | uint32_t readMapEnd(); |
| 159 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 160 | uint32_t readListBegin(TType& elemType, uint32_t& size); |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 161 | |
| 162 | uint32_t readListEnd(); |
| 163 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 164 | uint32_t readSetBegin(TType& elemType, uint32_t& size); |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 165 | |
| 166 | uint32_t readSetEnd(); |
| 167 | |
| 168 | uint32_t readBool(bool& value); |
David Reiss | 8dfc732 | 2010-10-06 17:09:58 +0000 | [diff] [blame] | 169 | // Provide the default readBool() implementation for std::vector<bool> |
| 170 | using TVirtualProtocol<TDenseProtocol, TBinaryProtocol>::readBool; |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 171 | |
| 172 | uint32_t readByte(int8_t& byte); |
| 173 | |
| 174 | uint32_t readI16(int16_t& i16); |
| 175 | |
| 176 | uint32_t readI32(int32_t& i32); |
| 177 | |
| 178 | uint32_t readI64(int64_t& i64); |
| 179 | |
| 180 | uint32_t readDouble(double& dub); |
| 181 | |
| 182 | uint32_t readString(std::string& str); |
| 183 | |
David Reiss | c005b1b | 2008-02-15 01:38:18 +0000 | [diff] [blame] | 184 | uint32_t readBinary(std::string& str); |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 185 | |
| 186 | /* |
| 187 | * Helper reading functions (don't do state transitions). |
| 188 | */ |
David Reiss | e67c0e6 | 2007-09-07 01:34:12 +0000 | [diff] [blame] | 189 | inline uint32_t subReadI32(int32_t& i32); |
| 190 | |
| 191 | inline uint32_t subReadString(std::string& str); |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 192 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 193 | uint32_t subReadBool(bool& value) { return TBinaryProtocol::readBool(value); } |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 194 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 195 | private: |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 196 | // Implementation functions, documented in the .cpp. |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 197 | inline void checkTType(const TType ttype); |
| 198 | inline void stateTransition(); |
| 199 | |
David Reiss | e67c0e6 | 2007-09-07 01:34:12 +0000 | [diff] [blame] | 200 | // Read and write variable-length integers. |
| 201 | // Uses the same technique as the MIDI file format. |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 202 | inline uint32_t vlqRead(uint64_t& vlq); |
| 203 | inline uint32_t vlqWrite(uint64_t vlq); |
David Reiss | e67c0e6 | 2007-09-07 01:34:12 +0000 | [diff] [blame] | 204 | |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 205 | // Called before throwing an exception to make the object reusable. |
| 206 | void resetState() { |
| 207 | ts_stack_.clear(); |
| 208 | idx_stack_.clear(); |
| 209 | mkv_stack_.clear(); |
| 210 | } |
| 211 | |
| 212 | // TypeSpec of the top-level structure to write, |
| 213 | // for standalone protocol objects. |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 214 | TypeSpec* type_spec_; |
| 215 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 216 | std::vector<TypeSpec*> ts_stack_; // TypeSpec stack. |
| 217 | std::vector<int> idx_stack_; // InDeX stack. |
| 218 | std::vector<bool> mkv_stack_; // Map Key/Vlue stack. |
| 219 | // True = key, False = value. |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 220 | |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 221 | // True iff this is a standalone instance (no RPC). |
| 222 | bool standalone_; |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 223 | }; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | } // apache::thrift::protocol |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 227 | |
| 228 | #endif // #ifndef _THRIFT_PROTOCOL_TDENSEPROTOCOL_H_ |