]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
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_THEADERPROTOCOL_H_ | |
21 | #define THRIFT_PROTOCOL_THEADERPROTOCOL_H_ 1 | |
22 | ||
23 | #include <thrift/protocol/TProtocol.h> | |
24 | #include <thrift/protocol/TProtocolTypes.h> | |
25 | #include <thrift/protocol/TVirtualProtocol.h> | |
26 | #include <thrift/transport/THeaderTransport.h> | |
27 | ||
28 | #include <memory> | |
29 | ||
30 | using apache::thrift::transport::THeaderTransport; | |
31 | ||
32 | namespace apache { | |
33 | namespace thrift { | |
34 | namespace protocol { | |
35 | ||
36 | /** | |
37 | * The header protocol for thrift. Reads unframed, framed, header format, | |
38 | * and http | |
39 | * | |
40 | */ | |
41 | class THeaderProtocol : public TVirtualProtocol<THeaderProtocol> { | |
42 | protected: | |
43 | public: | |
44 | void resetProtocol(); | |
45 | ||
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())), | |
50 | protoId_(protoId) { | |
51 | trans_->setProtocolId(protoId); | |
52 | resetProtocol(); | |
53 | } | |
54 | ||
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())), | |
61 | protoId_(protoId) { | |
62 | trans_->setProtocolId(protoId); | |
63 | resetProtocol(); | |
64 | } | |
65 | ||
66 | ~THeaderProtocol() override = default; | |
67 | ||
68 | /** | |
69 | * Functions to work with headers by calling into THeaderTransport | |
70 | */ | |
71 | void setProtocolId(uint16_t protoId) { | |
72 | trans_->setProtocolId(protoId); | |
73 | resetProtocol(); | |
74 | } | |
75 | ||
76 | typedef THeaderTransport::StringToStringMap StringToStringMap; | |
77 | ||
78 | // these work with write headers | |
79 | void setHeader(const std::string& key, const std::string& value) { | |
80 | trans_->setHeader(key, value); | |
81 | } | |
82 | ||
83 | void clearHeaders() { trans_->clearHeaders(); } | |
84 | ||
85 | StringToStringMap& getWriteHeaders() { return trans_->getWriteHeaders(); } | |
86 | ||
87 | // these work with read headers | |
88 | const StringToStringMap& getHeaders() const { return trans_->getHeaders(); } | |
89 | ||
90 | /** | |
91 | * Writing functions. | |
92 | */ | |
93 | ||
94 | /*ol*/ uint32_t writeMessageBegin(const std::string& name, | |
95 | const TMessageType messageType, | |
96 | const int32_t seqId); | |
97 | ||
98 | /*ol*/ uint32_t writeMessageEnd(); | |
99 | ||
100 | uint32_t writeStructBegin(const char* name); | |
101 | ||
102 | uint32_t writeStructEnd(); | |
103 | ||
104 | uint32_t writeFieldBegin(const char* name, const TType fieldType, const int16_t fieldId); | |
105 | ||
106 | uint32_t writeFieldEnd(); | |
107 | ||
108 | uint32_t writeFieldStop(); | |
109 | ||
110 | uint32_t writeMapBegin(const TType keyType, const TType valType, const uint32_t size); | |
111 | ||
112 | uint32_t writeMapEnd(); | |
113 | ||
114 | uint32_t writeListBegin(const TType elemType, const uint32_t size); | |
115 | ||
116 | uint32_t writeListEnd(); | |
117 | ||
118 | uint32_t writeSetBegin(const TType elemType, const uint32_t size); | |
119 | ||
120 | uint32_t writeSetEnd(); | |
121 | ||
122 | uint32_t writeBool(const bool value); | |
123 | ||
124 | uint32_t writeByte(const int8_t byte); | |
125 | ||
126 | uint32_t writeI16(const int16_t i16); | |
127 | ||
128 | uint32_t writeI32(const int32_t i32); | |
129 | ||
130 | uint32_t writeI64(const int64_t i64); | |
131 | ||
132 | uint32_t writeDouble(const double dub); | |
133 | ||
134 | uint32_t writeString(const std::string& str); | |
135 | ||
136 | uint32_t writeBinary(const std::string& str); | |
137 | ||
138 | /** | |
139 | * Reading functions | |
140 | */ | |
141 | ||
142 | /*ol*/ uint32_t readMessageBegin(std::string& name, TMessageType& messageType, int32_t& seqId); | |
143 | ||
144 | /*ol*/ uint32_t readMessageEnd(); | |
145 | ||
146 | uint32_t readStructBegin(std::string& name); | |
147 | ||
148 | uint32_t readStructEnd(); | |
149 | ||
150 | uint32_t readFieldBegin(std::string& name, TType& fieldType, int16_t& fieldId); | |
151 | ||
152 | uint32_t readFieldEnd(); | |
153 | ||
154 | uint32_t readMapBegin(TType& keyType, TType& valType, uint32_t& size); | |
155 | ||
156 | uint32_t readMapEnd(); | |
157 | ||
158 | uint32_t readListBegin(TType& elemType, uint32_t& size); | |
159 | ||
160 | uint32_t readListEnd(); | |
161 | ||
162 | uint32_t readSetBegin(TType& elemType, uint32_t& size); | |
163 | ||
164 | uint32_t readSetEnd(); | |
165 | ||
166 | uint32_t readBool(bool& value); | |
167 | // Provide the default readBool() implementation for std::vector<bool> | |
168 | using TVirtualProtocol<THeaderProtocol>::readBool; | |
169 | ||
170 | uint32_t readByte(int8_t& byte); | |
171 | ||
172 | uint32_t readI16(int16_t& i16); | |
173 | ||
174 | uint32_t readI32(int32_t& i32); | |
175 | ||
176 | uint32_t readI64(int64_t& i64); | |
177 | ||
178 | uint32_t readDouble(double& dub); | |
179 | ||
180 | uint32_t readString(std::string& str); | |
181 | ||
182 | uint32_t readBinary(std::string& binary); | |
183 | ||
184 | protected: | |
185 | std::shared_ptr<THeaderTransport> trans_; | |
186 | ||
187 | std::shared_ptr<TProtocol> proto_; | |
188 | uint32_t protoId_; | |
189 | }; | |
190 | ||
191 | class THeaderProtocolFactory : public TProtocolFactory { | |
192 | public: | |
193 | std::shared_ptr<TProtocol> getProtocol(std::shared_ptr<transport::TTransport> trans) override { | |
194 | auto* headerProtocol | |
195 | = new THeaderProtocol(trans, trans, T_BINARY_PROTOCOL); | |
196 | return std::shared_ptr<TProtocol>(headerProtocol); | |
197 | } | |
198 | ||
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); | |
204 | } | |
205 | }; | |
206 | } | |
207 | } | |
208 | } // apache::thrift::protocol | |
209 | ||
210 | #endif // #ifndef THRIFT_PROTOCOL_THEADERPROTOCOL_H_ |