Dave Watson | 792db4e | 2015-01-16 11:22:01 -0800 | [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 | #ifndef THRIFT_PROTOCOL_THEADERPROTOCOL_CPP_ |
| 20 | #define THRIFT_PROTOCOL_THEADERPROTOCOL_CPP_ 1 |
| 21 | |
| 22 | #include <thrift/protocol/THeaderProtocol.h> |
| 23 | #include <thrift/protocol/TCompactProtocol.h> |
| 24 | #include <thrift/TApplicationException.h> |
| 25 | |
| 26 | #include <limits> |
| 27 | #include <boost/static_assert.hpp> |
| 28 | |
| 29 | namespace apache { |
| 30 | namespace thrift { |
| 31 | namespace protocol { |
| 32 | |
| 33 | void THeaderProtocol::resetProtocol() { |
| 34 | if (proto_ && protoId_ == trans_->getProtocolId()) { |
| 35 | return; |
| 36 | } |
| 37 | |
| 38 | protoId_ = trans_->getProtocolId(); |
| 39 | |
| 40 | switch (protoId_) { |
| 41 | case T_BINARY_PROTOCOL: |
| 42 | proto_ = boost::make_shared<TBinaryProtocolT<THeaderTransport> >(trans_); |
| 43 | break; |
| 44 | |
| 45 | case T_COMPACT_PROTOCOL: |
| 46 | proto_ = boost::make_shared<TCompactProtocolT<THeaderTransport> >(trans_); |
| 47 | break; |
| 48 | |
| 49 | default: |
| 50 | throw TApplicationException(TApplicationException::INVALID_PROTOCOL, |
| 51 | "Unknown protocol requested"); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | uint32_t THeaderProtocol::writeMessageBegin(const std::string& name, |
| 56 | const TMessageType messageType, |
| 57 | const int32_t seqId) { |
| 58 | resetProtocol(); // Reset in case we changed protocols |
| 59 | trans_->setSequenceNumber(seqId); |
| 60 | return proto_->writeMessageBegin(name, messageType, seqId); |
| 61 | } |
| 62 | |
| 63 | uint32_t THeaderProtocol::writeMessageEnd() { |
| 64 | return proto_->writeMessageEnd(); |
| 65 | } |
| 66 | |
| 67 | uint32_t THeaderProtocol::writeStructBegin(const char* name) { |
| 68 | return proto_->writeStructBegin(name); |
| 69 | } |
| 70 | |
| 71 | uint32_t THeaderProtocol::writeStructEnd() { |
| 72 | return proto_->writeStructEnd(); |
| 73 | } |
| 74 | |
| 75 | uint32_t THeaderProtocol::writeFieldBegin(const char* name, |
| 76 | const TType fieldType, |
| 77 | const int16_t fieldId) { |
| 78 | return proto_->writeFieldBegin(name, fieldType, fieldId); |
| 79 | } |
| 80 | |
| 81 | uint32_t THeaderProtocol::writeFieldEnd() { |
| 82 | return proto_->writeFieldEnd(); |
| 83 | } |
| 84 | |
| 85 | uint32_t THeaderProtocol::writeFieldStop() { |
| 86 | return proto_->writeFieldStop(); |
| 87 | } |
| 88 | |
| 89 | uint32_t THeaderProtocol::writeMapBegin(const TType keyType, |
| 90 | const TType valType, |
| 91 | const uint32_t size) { |
| 92 | return proto_->writeMapBegin(keyType, valType, size); |
| 93 | } |
| 94 | |
| 95 | uint32_t THeaderProtocol::writeMapEnd() { |
| 96 | return proto_->writeMapEnd(); |
| 97 | } |
| 98 | |
| 99 | uint32_t THeaderProtocol::writeListBegin(const TType elemType, const uint32_t size) { |
| 100 | return proto_->writeListBegin(elemType, size); |
| 101 | } |
| 102 | |
| 103 | uint32_t THeaderProtocol::writeListEnd() { |
| 104 | return proto_->writeListEnd(); |
| 105 | } |
| 106 | |
| 107 | uint32_t THeaderProtocol::writeSetBegin(const TType elemType, const uint32_t size) { |
| 108 | return proto_->writeSetBegin(elemType, size); |
| 109 | } |
| 110 | |
| 111 | uint32_t THeaderProtocol::writeSetEnd() { |
| 112 | return proto_->writeSetEnd(); |
| 113 | } |
| 114 | |
| 115 | uint32_t THeaderProtocol::writeBool(const bool value) { |
| 116 | return proto_->writeBool(value); |
| 117 | } |
| 118 | |
| 119 | uint32_t THeaderProtocol::writeByte(const int8_t byte) { |
| 120 | return proto_->writeByte(byte); |
| 121 | } |
| 122 | |
| 123 | uint32_t THeaderProtocol::writeI16(const int16_t i16) { |
| 124 | return proto_->writeI16(i16); |
| 125 | } |
| 126 | |
| 127 | uint32_t THeaderProtocol::writeI32(const int32_t i32) { |
| 128 | return proto_->writeI32(i32); |
| 129 | } |
| 130 | |
| 131 | uint32_t THeaderProtocol::writeI64(const int64_t i64) { |
| 132 | return proto_->writeI64(i64); |
| 133 | } |
| 134 | |
| 135 | uint32_t THeaderProtocol::writeDouble(const double dub) { |
| 136 | return proto_->writeDouble(dub); |
| 137 | } |
| 138 | |
| 139 | uint32_t THeaderProtocol::writeString(const std::string& str) { |
| 140 | return proto_->writeString(str); |
| 141 | } |
| 142 | |
| 143 | uint32_t THeaderProtocol::writeBinary(const std::string& str) { |
| 144 | return proto_->writeBinary(str); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Reading functions |
| 149 | */ |
| 150 | |
| 151 | uint32_t THeaderProtocol::readMessageBegin(std::string& name, |
| 152 | TMessageType& messageType, |
| 153 | int32_t& seqId) { |
| 154 | // Read the next frame, and change protocols if needed |
| 155 | try { |
| 156 | trans_->resetProtocol(); |
| 157 | resetProtocol(); |
| 158 | } catch (const TApplicationException& ex) { |
| 159 | writeMessageBegin("", T_EXCEPTION, 0); |
| 160 | ex.write((TProtocol*)this); |
| 161 | writeMessageEnd(); |
| 162 | trans_->flush(); |
| 163 | |
| 164 | // The framing is still good, but we don't know about this protocol. |
| 165 | // In the future, this could be made a client-side only error if |
| 166 | // connection pooling is used. |
| 167 | throw ex; |
| 168 | } |
| 169 | return proto_->readMessageBegin(name, messageType, seqId); |
| 170 | } |
| 171 | |
| 172 | uint32_t THeaderProtocol::readMessageEnd() { |
| 173 | return proto_->readMessageEnd(); |
| 174 | } |
| 175 | |
| 176 | uint32_t THeaderProtocol::readStructBegin(std::string& name) { |
| 177 | return proto_->readStructBegin(name); |
| 178 | } |
| 179 | |
| 180 | uint32_t THeaderProtocol::readStructEnd() { |
| 181 | return proto_->readStructEnd(); |
| 182 | } |
| 183 | |
| 184 | uint32_t THeaderProtocol::readFieldBegin(std::string& name, TType& fieldType, int16_t& fieldId) { |
| 185 | return proto_->readFieldBegin(name, fieldType, fieldId); |
| 186 | } |
| 187 | |
| 188 | uint32_t THeaderProtocol::readFieldEnd() { |
| 189 | return proto_->readFieldEnd(); |
| 190 | } |
| 191 | |
| 192 | uint32_t THeaderProtocol::readMapBegin(TType& keyType, TType& valType, uint32_t& size) { |
| 193 | return proto_->readMapBegin(keyType, valType, size); |
| 194 | } |
| 195 | |
| 196 | uint32_t THeaderProtocol::readMapEnd() { |
| 197 | return proto_->readMapEnd(); |
| 198 | } |
| 199 | |
| 200 | uint32_t THeaderProtocol::readListBegin(TType& elemType, uint32_t& size) { |
| 201 | return proto_->readListBegin(elemType, size); |
| 202 | } |
| 203 | |
| 204 | uint32_t THeaderProtocol::readListEnd() { |
| 205 | return proto_->readListEnd(); |
| 206 | } |
| 207 | |
| 208 | uint32_t THeaderProtocol::readSetBegin(TType& elemType, uint32_t& size) { |
| 209 | return proto_->readSetBegin(elemType, size); |
| 210 | } |
| 211 | |
| 212 | uint32_t THeaderProtocol::readSetEnd() { |
| 213 | return proto_->readSetEnd(); |
| 214 | } |
| 215 | |
| 216 | uint32_t THeaderProtocol::readBool(bool& value) { |
| 217 | return proto_->readBool(value); |
| 218 | } |
| 219 | |
| 220 | uint32_t THeaderProtocol::readByte(int8_t& byte) { |
| 221 | return proto_->readByte(byte); |
| 222 | } |
| 223 | |
| 224 | uint32_t THeaderProtocol::readI16(int16_t& i16) { |
| 225 | return proto_->readI16(i16); |
| 226 | } |
| 227 | |
| 228 | uint32_t THeaderProtocol::readI32(int32_t& i32) { |
| 229 | return proto_->readI32(i32); |
| 230 | } |
| 231 | |
| 232 | uint32_t THeaderProtocol::readI64(int64_t& i64) { |
| 233 | return proto_->readI64(i64); |
| 234 | } |
| 235 | |
| 236 | uint32_t THeaderProtocol::readDouble(double& dub) { |
| 237 | return proto_->readDouble(dub); |
| 238 | } |
| 239 | |
| 240 | uint32_t THeaderProtocol::readString(std::string& str) { |
| 241 | return proto_->readString(str); |
| 242 | } |
| 243 | |
| 244 | uint32_t THeaderProtocol::readBinary(std::string& binary) { |
| 245 | return proto_->readBinary(binary); |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | } // apache::thrift::protocol |
| 250 | |
| 251 | #endif // #ifndef THRIFT_PROTOCOL_THEADERPROTOCOL_CPP_ |