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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
20 #ifndef THRIFT_PROTOCOL_THEADERPROTOCOL_H_
21 #define THRIFT_PROTOCOL_THEADERPROTOCOL_H_ 1
23 #include <thrift/protocol/TProtocol.h>
24 #include <thrift/protocol/TProtocolTypes.h>
25 #include <thrift/protocol/TVirtualProtocol.h>
26 #include <thrift/transport/THeaderTransport.h>
30 using apache::thrift::transport::THeaderTransport
;
37 * The header protocol for thrift. Reads unframed, framed, header format,
41 class THeaderProtocol
: public TVirtualProtocol
<THeaderProtocol
> {
46 explicit THeaderProtocol(const std::shared_ptr
<TTransport
>& trans
,
47 uint16_t protoId
= T_COMPACT_PROTOCOL
)
48 : TVirtualProtocol
<THeaderProtocol
>(std::shared_ptr
<TTransport
>(new THeaderTransport(trans
))),
49 trans_(std::dynamic_pointer_cast
<THeaderTransport
>(getTransport())),
51 trans_
->setProtocolId(protoId
);
55 THeaderProtocol(const std::shared_ptr
<TTransport
>& inTrans
,
56 const std::shared_ptr
<TTransport
>& outTrans
,
57 uint16_t protoId
= T_COMPACT_PROTOCOL
)
58 : TVirtualProtocol
<THeaderProtocol
>(
59 std::shared_ptr
<TTransport
>(new THeaderTransport(inTrans
, outTrans
))),
60 trans_(std::dynamic_pointer_cast
<THeaderTransport
>(getTransport())),
62 trans_
->setProtocolId(protoId
);
66 ~THeaderProtocol() override
= default;
69 * Functions to work with headers by calling into THeaderTransport
71 void setProtocolId(uint16_t protoId
) {
72 trans_
->setProtocolId(protoId
);
76 typedef THeaderTransport::StringToStringMap StringToStringMap
;
78 // these work with write headers
79 void setHeader(const std::string
& key
, const std::string
& value
) {
80 trans_
->setHeader(key
, value
);
83 void clearHeaders() { trans_
->clearHeaders(); }
85 StringToStringMap
& getWriteHeaders() { return trans_
->getWriteHeaders(); }
87 // these work with read headers
88 const StringToStringMap
& getHeaders() const { return trans_
->getHeaders(); }
94 /*ol*/ uint32_t writeMessageBegin(const std::string
& name
,
95 const TMessageType messageType
,
98 /*ol*/ uint32_t writeMessageEnd();
100 uint32_t writeStructBegin(const char* name
);
102 uint32_t writeStructEnd();
104 uint32_t writeFieldBegin(const char* name
, const TType fieldType
, const int16_t fieldId
);
106 uint32_t writeFieldEnd();
108 uint32_t writeFieldStop();
110 uint32_t writeMapBegin(const TType keyType
, const TType valType
, const uint32_t size
);
112 uint32_t writeMapEnd();
114 uint32_t writeListBegin(const TType elemType
, const uint32_t size
);
116 uint32_t writeListEnd();
118 uint32_t writeSetBegin(const TType elemType
, const uint32_t size
);
120 uint32_t writeSetEnd();
122 uint32_t writeBool(const bool value
);
124 uint32_t writeByte(const int8_t byte
);
126 uint32_t writeI16(const int16_t i16
);
128 uint32_t writeI32(const int32_t i32
);
130 uint32_t writeI64(const int64_t i64
);
132 uint32_t writeDouble(const double dub
);
134 uint32_t writeString(const std::string
& str
);
136 uint32_t writeBinary(const std::string
& str
);
142 /*ol*/ uint32_t readMessageBegin(std::string
& name
, TMessageType
& messageType
, int32_t& seqId
);
144 /*ol*/ uint32_t readMessageEnd();
146 uint32_t readStructBegin(std::string
& name
);
148 uint32_t readStructEnd();
150 uint32_t readFieldBegin(std::string
& name
, TType
& fieldType
, int16_t& fieldId
);
152 uint32_t readFieldEnd();
154 uint32_t readMapBegin(TType
& keyType
, TType
& valType
, uint32_t& size
);
156 uint32_t readMapEnd();
158 uint32_t readListBegin(TType
& elemType
, uint32_t& size
);
160 uint32_t readListEnd();
162 uint32_t readSetBegin(TType
& elemType
, uint32_t& size
);
164 uint32_t readSetEnd();
166 uint32_t readBool(bool& value
);
167 // Provide the default readBool() implementation for std::vector<bool>
168 using TVirtualProtocol
<THeaderProtocol
>::readBool
;
170 uint32_t readByte(int8_t& byte
);
172 uint32_t readI16(int16_t& i16
);
174 uint32_t readI32(int32_t& i32
);
176 uint32_t readI64(int64_t& i64
);
178 uint32_t readDouble(double& dub
);
180 uint32_t readString(std::string
& str
);
182 uint32_t readBinary(std::string
& binary
);
185 std::shared_ptr
<THeaderTransport
> trans_
;
187 std::shared_ptr
<TProtocol
> proto_
;
191 class THeaderProtocolFactory
: public TProtocolFactory
{
193 std::shared_ptr
<TProtocol
> getProtocol(std::shared_ptr
<transport::TTransport
> trans
) override
{
195 = new THeaderProtocol(trans
, trans
, T_BINARY_PROTOCOL
);
196 return std::shared_ptr
<TProtocol
>(headerProtocol
);
199 std::shared_ptr
<TProtocol
> getProtocol(
200 std::shared_ptr
<transport::TTransport
> inTrans
,
201 std::shared_ptr
<transport::TTransport
> outTrans
) override
{
202 auto* headerProtocol
= new THeaderProtocol(inTrans
, outTrans
, T_BINARY_PROTOCOL
);
203 return std::shared_ptr
<TProtocol
>(headerProtocol
);
208 } // apache::thrift::protocol
210 #endif // #ifndef THRIFT_PROTOCOL_THEADERPROTOCOL_H_