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