blob: 1d940a2ac2a6abfac3ad64c9806b6f3dd5b806b4 [file] [log] [blame]
David Reisse4d4ea02009-04-02 21:37:17 +00001/*
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_TCOMPACTPROTOCOL_H_
21#define _THRIFT_PROTOCOL_TCOMPACTPROTOCOL_H_ 1
22
23#include "TProtocol.h"
24
25#include <stack>
26#include <boost/shared_ptr.hpp>
27
28namespace apache { namespace thrift { namespace protocol {
29
30/**
31 * C++ Implementation of the Compact Protocol as described in THRIFT-110
32 */
33class TCompactProtocol : public TProtocol {
34
35 protected:
36 static const int8_t PROTOCOL_ID = 0x82;
37 static const int8_t VERSION_N = 1;
38 static const int8_t VERSION_MASK = 0x1f; // 0001 1111
39 static const int8_t TYPE_MASK = 0xE0; // 1110 0000
40 static const int32_t TYPE_SHIFT_AMOUNT = 5;
41
42 /**
43 * (Writing) If we encounter a boolean field begin, save the TField here
44 * so it can have the value incorporated.
45 */
46 struct {
47 const char* name;
48 TType fieldType;
49 int16_t fieldId;
50 } booleanField_;
51
52 /**
53 * (Reading) If we read a field header, and it's a boolean field, save
54 * the boolean value here so that readBool can use it.
55 */
56 struct {
57 bool hasBoolValue;
58 bool boolValue;
59 } boolValue_;
60
61 /**
62 * Used to keep track of the last field for the current and previous structs,
63 * so we can do the delta stuff.
64 */
65
66 std::stack<int16_t> lastField_;
67 int16_t lastFieldId_;
68
69 enum Types {
70 CT_STOP = 0x00,
71 CT_BOOLEAN_TRUE = 0x01,
72 CT_BOOLEAN_FALSE = 0x02,
73 CT_BYTE = 0x03,
74 CT_I16 = 0x04,
75 CT_I32 = 0x05,
76 CT_I64 = 0x06,
77 CT_DOUBLE = 0x07,
78 CT_BINARY = 0x08,
79 CT_LIST = 0x09,
80 CT_SET = 0x0A,
81 CT_MAP = 0x0B,
82 CT_STRUCT = 0x0C,
83 };
84
85 static const int8_t TTypeToCType[16];
86
87 public:
88 TCompactProtocol(boost::shared_ptr<TTransport> trans) :
89 TProtocol(trans),
90 lastFieldId_(0),
91 string_limit_(0),
92 string_buf_(NULL),
93 string_buf_size_(0),
94 container_limit_(0) {
95 booleanField_.name = NULL;
96 boolValue_.hasBoolValue = false;
97 }
98
99 TCompactProtocol(boost::shared_ptr<TTransport> trans,
100 int32_t string_limit,
101 int32_t container_limit) :
102 TProtocol(trans),
103 lastFieldId_(0),
104 string_limit_(string_limit),
105 string_buf_(NULL),
106 string_buf_size_(0),
107 container_limit_(container_limit) {
108 booleanField_.name = NULL;
109 boolValue_.hasBoolValue = false;
110 }
111
David Reiss4ae87802010-03-09 00:49:14 +0000112 ~TCompactProtocol() {
113 free(string_buf_);
114 }
David Reisse4d4ea02009-04-02 21:37:17 +0000115
116
117 /**
118 * Writing functions
119 */
120
121 virtual uint32_t writeMessageBegin(const std::string& name,
122 const TMessageType messageType,
123 const int32_t seqid);
124
125 uint32_t writeStructBegin(const char* name);
126
127 uint32_t writeStructEnd();
128
129 uint32_t writeFieldBegin(const char* name,
130 const TType fieldType,
131 const int16_t fieldId);
132
133 uint32_t writeFieldStop();
134
135 uint32_t writeListBegin(const TType elemType,
136 const uint32_t size);
137
138 uint32_t writeSetBegin(const TType elemType,
139 const uint32_t size);
140
141 virtual uint32_t writeMapBegin(const TType keyType,
142 const TType valType,
143 const uint32_t size);
144
145 uint32_t writeBool(const bool value);
146
147 uint32_t writeByte(const int8_t byte);
148
149 uint32_t writeI16(const int16_t i16);
150
151 uint32_t writeI32(const int32_t i32);
152
153 uint32_t writeI64(const int64_t i64);
154
155 uint32_t writeDouble(const double dub);
156
157 uint32_t writeString(const std::string& str);
158
159 uint32_t writeBinary(const std::string& str);
160
161 /**
162 * These methods are called by structs, but don't actually have any wired
163 * output or purpose
164 */
165 virtual uint32_t writeMessageEnd() { return 0; }
166 uint32_t writeMapEnd() { return 0; }
167 uint32_t writeListEnd() { return 0; }
168 uint32_t writeSetEnd() { return 0; }
169 uint32_t writeFieldEnd() { return 0; }
170
171 protected:
172 int32_t writeFieldBeginInternal(const char* name,
173 const TType fieldType,
174 const int16_t fieldId,
175 int8_t typeOverride);
176 uint32_t writeCollectionBegin(int8_t elemType, int32_t size);
177 uint32_t writeVarint32(uint32_t n);
178 uint32_t writeVarint64(uint64_t n);
179 uint64_t i64ToZigzag(const int64_t l);
180 uint32_t i32ToZigzag(const int32_t n);
181 inline int8_t getCompactType(int8_t ttype);
182
183 public:
184 uint32_t readMessageBegin(std::string& name,
185 TMessageType& messageType,
186 int32_t& seqid);
187
188 uint32_t readStructBegin(std::string& name);
189
190 uint32_t readStructEnd();
191
192 uint32_t readFieldBegin(std::string& name,
193 TType& fieldType,
194 int16_t& fieldId);
195
196 uint32_t readMapBegin(TType& keyType,
197 TType& valType,
198 uint32_t& size);
199
200 uint32_t readListBegin(TType& elemType,
201 uint32_t& size);
202
203 uint32_t readSetBegin(TType& elemType,
204 uint32_t& size);
205
206 uint32_t readBool(bool& value);
207
208 uint32_t readByte(int8_t& byte);
209
210 uint32_t readI16(int16_t& i16);
211
212 uint32_t readI32(int32_t& i32);
213
214 uint32_t readI64(int64_t& i64);
215
216 uint32_t readDouble(double& dub);
217
218 uint32_t readString(std::string& str);
219
220 uint32_t readBinary(std::string& str);
221
222 /*
223 *These methods are here for the struct to call, but don't have any wire
224 * encoding.
225 */
226 uint32_t readMessageEnd() { return 0; }
227 uint32_t readFieldEnd() { return 0; }
228 uint32_t readMapEnd() { return 0; }
229 uint32_t readListEnd() { return 0; }
230 uint32_t readSetEnd() { return 0; }
231
232 protected:
233 uint32_t readVarint32(int32_t& i32);
234 uint32_t readVarint64(int64_t& i64);
235 int32_t zigzagToI32(uint32_t n);
236 int64_t zigzagToI64(uint64_t n);
237 TType getTType(int8_t type);
238
239 // Buffer for reading strings, save for the lifetime of the protocol to
240 // avoid memory churn allocating memory on every string read
241 int32_t string_limit_;
242 uint8_t* string_buf_;
243 int32_t string_buf_size_;
244 int32_t container_limit_;
245};
246
247/**
248 * Constructs compact protocol handlers
249 */
250class TCompactProtocolFactory : public TProtocolFactory {
251 public:
252 TCompactProtocolFactory() :
253 string_limit_(0),
254 container_limit_(0) {}
255
256 TCompactProtocolFactory(int32_t string_limit, int32_t container_limit) :
257 string_limit_(string_limit),
258 container_limit_(container_limit) {}
259
260 virtual ~TCompactProtocolFactory() {}
261
262 void setStringSizeLimit(int32_t string_limit) {
263 string_limit_ = string_limit;
264 }
265
266 void setContainerSizeLimit(int32_t container_limit) {
267 container_limit_ = container_limit;
268 }
269
270 boost::shared_ptr<TProtocol> getProtocol(boost::shared_ptr<TTransport> trans) {
271 return boost::shared_ptr<TProtocol>(new TCompactProtocol(trans, string_limit_, container_limit_));
272 }
273
274 private:
275 int32_t string_limit_;
276 int32_t container_limit_;
277
278};
279
280}}} // apache::thrift::protocol
281
282#endif