blob: f2fbe8736c78b66c34fa4c6fb0d41d8dfae63399 [file] [log] [blame]
Marc Slemkod42a2c22006-08-10 03:30:18 +00001#include "TBinaryProtocol.h"
2
Mark Slee8d7e1f62006-06-07 06:48:56 +00003using std::string;
Mark Sleee8540632006-05-30 09:24:40 +00004
Marc Slemko6f038a72006-08-03 18:58:09 +00005namespace facebook { namespace thrift { namespace protocol {
6
Marc Slemko16698852006-08-04 03:16:10 +00007uint32_t TBinaryProtocol::writeMessageBegin(shared_ptr<TTransport> out,
Marc Slemko91f67482006-08-11 23:58:57 +00008 const std::string name,
Marc Slemko16698852006-08-04 03:16:10 +00009 const TMessageType messageType,
10 const uint32_t seqid) const {
11 return
Marc Slemko91f67482006-08-11 23:58:57 +000012 writeString(out, name) +
Marc Slemko16698852006-08-04 03:16:10 +000013 writeByte(out, (uint8_t)messageType) +
14 writeU32(out, seqid);
15}
16
17uint32_t TBinaryProtocol::writeMessageEnd(shared_ptr<TTransport> out) const {
18 return 0;
19}
20
21uint32_t TBinaryProtocol::writeStructBegin(shared_ptr<TTransport> out,
Mark Slee8d7e1f62006-06-07 06:48:56 +000022 const string& name) const {
23 return 0;
Mark Sleee8540632006-05-30 09:24:40 +000024}
25
Marc Slemko16698852006-08-04 03:16:10 +000026uint32_t TBinaryProtocol::writeStructEnd(shared_ptr<TTransport> out) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +000027 return 0;
Mark Sleee8540632006-05-30 09:24:40 +000028}
29
Marc Slemko16698852006-08-04 03:16:10 +000030uint32_t TBinaryProtocol::writeFieldBegin(shared_ptr<TTransport> out,
Mark Slee8d7e1f62006-06-07 06:48:56 +000031 const string& name,
32 const TType fieldType,
Marc Slemko0b4ffa92006-08-11 02:49:29 +000033 const int16_t fieldId) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +000034 return
35 writeByte(out, (uint8_t)fieldType) +
Marc Slemko0b4ffa92006-08-11 02:49:29 +000036 writeI16(out, fieldId);
Mark Slee8d7e1f62006-06-07 06:48:56 +000037}
38
Marc Slemko16698852006-08-04 03:16:10 +000039uint32_t TBinaryProtocol::writeFieldEnd(shared_ptr<TTransport> out) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +000040 return 0;
41}
42
Marc Slemko16698852006-08-04 03:16:10 +000043uint32_t TBinaryProtocol::writeFieldStop(shared_ptr<TTransport> out) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +000044 return
45 writeByte(out, (uint8_t)T_STOP);
46}
47
Marc Slemko16698852006-08-04 03:16:10 +000048uint32_t TBinaryProtocol::writeMapBegin(shared_ptr<TTransport> out,
Mark Slee8d7e1f62006-06-07 06:48:56 +000049 const TType keyType,
50 const TType valType,
Marc Slemkob09f5882006-08-23 22:03:34 +000051 const uint32_t size) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +000052 return
53 writeByte(out, (uint8_t)keyType) +
54 writeByte(out, (uint8_t)valType) +
Marc Slemkob09f5882006-08-23 22:03:34 +000055 writeU32(out, (uint32_t)size);
Mark Slee8d7e1f62006-06-07 06:48:56 +000056}
57
Marc Slemko16698852006-08-04 03:16:10 +000058uint32_t TBinaryProtocol::writeMapEnd(shared_ptr<TTransport> out) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +000059 return 0;
60}
61
Marc Slemko16698852006-08-04 03:16:10 +000062uint32_t TBinaryProtocol::writeListBegin(shared_ptr<TTransport> out,
Mark Slee8d7e1f62006-06-07 06:48:56 +000063 const TType elemType,
Marc Slemkob09f5882006-08-23 22:03:34 +000064 const uint32_t size) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +000065 return
66 writeByte(out, (uint8_t) elemType) +
Marc Slemkob09f5882006-08-23 22:03:34 +000067 writeU32(out, (int32_t)size);
Mark Slee8d7e1f62006-06-07 06:48:56 +000068}
69
Marc Slemko16698852006-08-04 03:16:10 +000070uint32_t TBinaryProtocol::writeListEnd(shared_ptr<TTransport> out) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +000071 return 0;
72}
73
Marc Slemko16698852006-08-04 03:16:10 +000074uint32_t TBinaryProtocol::writeSetBegin(shared_ptr<TTransport> out,
Mark Slee8d7e1f62006-06-07 06:48:56 +000075 const TType elemType,
Marc Slemkob09f5882006-08-23 22:03:34 +000076 const uint32_t size) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +000077 return
78 writeByte(out, (uint8_t)elemType) +
Marc Slemkob09f5882006-08-23 22:03:34 +000079 writeU32(out, (int32_t)size);
Mark Slee8d7e1f62006-06-07 06:48:56 +000080}
81
Marc Slemko16698852006-08-04 03:16:10 +000082uint32_t TBinaryProtocol::writeSetEnd(shared_ptr<TTransport> out) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +000083 return 0;
84}
85
Marc Slemko0b4ffa92006-08-11 02:49:29 +000086uint32_t TBinaryProtocol::writeBool(shared_ptr<TTransport> out,
87 const bool value) const {
88 uint8_t tmp = value ? 1 : 0;
89 out->write(&tmp, 1);
90 return 1;
91}
92
Marc Slemko16698852006-08-04 03:16:10 +000093uint32_t TBinaryProtocol::writeByte(shared_ptr<TTransport> out,
Mark Slee8d7e1f62006-06-07 06:48:56 +000094 const uint8_t byte) const {
95 out->write(&byte, 1);
96 return 1;
97}
98
Marc Slemko0b4ffa92006-08-11 02:49:29 +000099uint32_t TBinaryProtocol::writeU16(shared_ptr<TTransport> out,
100 const uint16_t u16) const {
101 uint16_t net = (uint16_t)htons(u16);
102 out->write((uint8_t*)&net, 2);
103 return 2;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000104}
105
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000106uint32_t TBinaryProtocol::writeI16(shared_ptr<TTransport> out,
107 const int16_t i16) const {
108 int16_t net = (int16_t)htons(i16);
109 out->write((uint8_t*)&net, 2);
110 return 2;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000111}
112
Marc Slemkoe6889de2006-08-12 00:32:53 +0000113uint32_t TBinaryProtocol::writeU32(shared_ptr<TTransport> out,
114 const uint32_t u32) const {
115 uint32_t net = (uint32_t)htonl(u32);
116 out->write((uint8_t*)&net, 4);
117 return 4;
118}
119
120uint32_t TBinaryProtocol::writeI32(shared_ptr<TTransport> out,
121 const int32_t i32) const {
122 int32_t net = (int32_t)htonl(i32);
123 out->write((uint8_t*)&net, 4);
124 return 4;
125}
126
Marc Slemko16698852006-08-04 03:16:10 +0000127uint32_t TBinaryProtocol::writeU64(shared_ptr<TTransport> out,
Mark Slee8d7e1f62006-06-07 06:48:56 +0000128 const uint64_t u64) const {
129 uint64_t net = (uint64_t)htonll(u64);
130 out->write((uint8_t*)&net, 8);
131 return 8;
132}
133
Marc Slemko16698852006-08-04 03:16:10 +0000134uint32_t TBinaryProtocol::writeI64(shared_ptr<TTransport> out,
Mark Slee8d7e1f62006-06-07 06:48:56 +0000135 const int64_t i64) const {
136 int64_t net = (int64_t)htonll(i64);
137 out->write((uint8_t*)&net, 8);
138 return 8;
139}
140
Marc Slemko16698852006-08-04 03:16:10 +0000141uint32_t TBinaryProtocol::writeString(shared_ptr<TTransport> out,
Mark Slee8d7e1f62006-06-07 06:48:56 +0000142 const string& str) const {
Mark Sleef3c322b2006-06-26 23:52:22 +0000143 uint32_t result = writeI32(out, str.size());
Mark Slee8d7e1f62006-06-07 06:48:56 +0000144 out->write((uint8_t*)str.data(), str.size());
145 return result + str.size();
146}
147
148/**
149 * Reading functions
150 */
151
Marc Slemkoe6889de2006-08-12 00:32:53 +0000152uint32_t TBinaryProtocol::readMessageBegin(shared_ptr<TTransport> in,
153 std::string& name,
154 TMessageType& messageType,
155 uint32_t& seqid) const {
Marc Slemko16698852006-08-04 03:16:10 +0000156
157 uint32_t result = 0;
158 uint8_t type;
Marc Slemko91f67482006-08-11 23:58:57 +0000159 result+= readString(in, name);
Marc Slemko16698852006-08-04 03:16:10 +0000160 result+= readByte(in, type);
161 messageType = (TMessageType)type;
162 result+= readU32(in, seqid);
163 return result;
164}
165
166uint32_t TBinaryProtocol::readMessageEnd(shared_ptr<TTransport> in) const{
167 return 0;
168}
169
170uint32_t TBinaryProtocol::readStructBegin(shared_ptr<TTransport> in,
Mark Slee8d7e1f62006-06-07 06:48:56 +0000171 string& name) const {
172 name = "";
173 return 0;
174}
175
Marc Slemko16698852006-08-04 03:16:10 +0000176uint32_t TBinaryProtocol::readStructEnd(shared_ptr<TTransport> in) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000177 return 0;
178}
179
Marc Slemko16698852006-08-04 03:16:10 +0000180uint32_t TBinaryProtocol::readFieldBegin(shared_ptr<TTransport> in,
Mark Slee8d7e1f62006-06-07 06:48:56 +0000181 string& name,
182 TType& fieldType,
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000183 int16_t& fieldId) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000184 uint32_t result = 0;
185 uint8_t type;
186 result += readByte(in, type);
187 fieldType = (TType)type;
188 if (fieldType == T_STOP) {
189 fieldId = 0;
190 return result;
191 }
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000192 result += readI16(in, fieldId);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000193 return result;
194}
Mark Sleee8540632006-05-30 09:24:40 +0000195
Marc Slemko16698852006-08-04 03:16:10 +0000196uint32_t TBinaryProtocol::readFieldEnd(shared_ptr<TTransport> in) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000197 return 0;
Mark Sleee8540632006-05-30 09:24:40 +0000198}
Mark Slee8d7e1f62006-06-07 06:48:56 +0000199
Marc Slemko16698852006-08-04 03:16:10 +0000200uint32_t TBinaryProtocol::readMapBegin(shared_ptr<TTransport> in,
Mark Slee8d7e1f62006-06-07 06:48:56 +0000201 TType& keyType,
202 TType& valType,
Marc Slemkob09f5882006-08-23 22:03:34 +0000203 uint32_t& size) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000204 uint8_t k, v;
205 uint32_t result = 0;
206 result += readByte(in, k);
207 keyType = (TType)k;
208 result += readByte(in, v);
209 valType = (TType)v;
Marc Slemkob09f5882006-08-23 22:03:34 +0000210 result += readU32(in, size);
Mark Sleee8540632006-05-30 09:24:40 +0000211 return result;
212}
213
Marc Slemko16698852006-08-04 03:16:10 +0000214uint32_t TBinaryProtocol::readMapEnd(shared_ptr<TTransport> in) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000215 return 0;
216}
217
Marc Slemko16698852006-08-04 03:16:10 +0000218uint32_t TBinaryProtocol::readListBegin(shared_ptr<TTransport> in,
Mark Slee8d7e1f62006-06-07 06:48:56 +0000219 TType& elemType,
Marc Slemkob09f5882006-08-23 22:03:34 +0000220 uint32_t& size) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000221 uint8_t e;
222 uint32_t result = 0;
223 result += readByte(in, e);
224 elemType = (TType)e;
Marc Slemkob09f5882006-08-23 22:03:34 +0000225 result += readU32(in, size);
Mark Sleee8540632006-05-30 09:24:40 +0000226 return result;
227}
228
Marc Slemko16698852006-08-04 03:16:10 +0000229uint32_t TBinaryProtocol::readListEnd(shared_ptr<TTransport> in) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000230 return 0;
231}
232
Marc Slemko16698852006-08-04 03:16:10 +0000233uint32_t TBinaryProtocol::readSetBegin(shared_ptr<TTransport> in,
Mark Slee8d7e1f62006-06-07 06:48:56 +0000234 TType& elemType,
Marc Slemkob09f5882006-08-23 22:03:34 +0000235 uint32_t& size) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000236 uint8_t e;
237 uint32_t result = 0;
238 result += readByte(in, e);
239 elemType = (TType)e;
Marc Slemkob09f5882006-08-23 22:03:34 +0000240 result += readU32(in, size);
Mark Sleee8540632006-05-30 09:24:40 +0000241 return result;
242}
243
Marc Slemko16698852006-08-04 03:16:10 +0000244uint32_t TBinaryProtocol::readSetEnd(shared_ptr<TTransport> in) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000245 return 0;
Mark Sleee8540632006-05-30 09:24:40 +0000246}
247
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000248uint32_t TBinaryProtocol::readBool(shared_ptr<TTransport> in,
249 bool& value) const {
250 uint8_t b[1];
251 in->readAll(b, 1);
252 value = *(uint8_t*)b != 0;
253 return 1;
254}
255
Marc Slemko16698852006-08-04 03:16:10 +0000256uint32_t TBinaryProtocol::readByte(shared_ptr<TTransport> in,
Mark Slee8d7e1f62006-06-07 06:48:56 +0000257 uint8_t& byte) const {
258 uint8_t b[1];
259 in->readAll(b, 1);
260 byte = *(uint8_t*)b;
261 return 1;
Mark Sleee8540632006-05-30 09:24:40 +0000262}
263
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000264uint32_t TBinaryProtocol::readU16(shared_ptr<TTransport> in,
265 uint16_t& u16) const {
266 uint8_t b[2];
267 in->readAll(b, 2);
268 u16 = *(uint16_t*)b;
269 u16 = (uint16_t)ntohs(u16);
270 return 2;
271}
272
273uint32_t TBinaryProtocol::readI16(shared_ptr<TTransport> in,
274 int16_t& i16) const {
275 uint8_t b[2];
276 in->readAll(b, 2);
277 i16 = *(int16_t*)b;
278 i16 = (int16_t)ntohs(i16);
279 return 2;
280}
281
Marc Slemko16698852006-08-04 03:16:10 +0000282uint32_t TBinaryProtocol::readU32(shared_ptr<TTransport> in,
Mark Slee8d7e1f62006-06-07 06:48:56 +0000283 uint32_t& u32) const {
284 uint8_t b[4];
285 in->readAll(b, 4);
286 u32 = *(uint32_t*)b;
287 u32 = (uint32_t)ntohl(u32);
288 return 4;
Mark Sleee8540632006-05-30 09:24:40 +0000289}
290
Marc Slemko16698852006-08-04 03:16:10 +0000291uint32_t TBinaryProtocol::readI32(shared_ptr<TTransport> in,
Mark Slee8d7e1f62006-06-07 06:48:56 +0000292 int32_t& i32) const {
293 uint8_t b[4];
294 in->readAll(b, 4);
295 i32 = *(int32_t*)b;
296 i32 = (int32_t)ntohl(i32);
297 return 4;
Mark Sleee8540632006-05-30 09:24:40 +0000298}
299
Marc Slemko16698852006-08-04 03:16:10 +0000300uint32_t TBinaryProtocol::readU64(shared_ptr<TTransport> in,
Mark Slee8d7e1f62006-06-07 06:48:56 +0000301 uint64_t& u64) const {
302 uint8_t b[8];
303 in->readAll(b, 8);
304 u64 = *(uint64_t*)b;
305 u64 = (uint64_t)ntohll(u64);
306 return 8;
Mark Sleee8540632006-05-30 09:24:40 +0000307}
308
Marc Slemko16698852006-08-04 03:16:10 +0000309uint32_t TBinaryProtocol::readI64(shared_ptr<TTransport> in,
Mark Slee8d7e1f62006-06-07 06:48:56 +0000310 int64_t& i64) const {
311 uint8_t b[8];
312 in->readAll(b, 8);
313 i64 = *(int64_t*)b;
314 i64 = (int64_t)ntohll(i64);
315 return 8;
Mark Sleee8540632006-05-30 09:24:40 +0000316}
317
Marc Slemko16698852006-08-04 03:16:10 +0000318uint32_t TBinaryProtocol::readString(shared_ptr<TTransport> in,
Mark Slee8d7e1f62006-06-07 06:48:56 +0000319 string& str) const {
Mark Sleef3c322b2006-06-26 23:52:22 +0000320 uint32_t result;
Marc Slemkob09f5882006-08-23 22:03:34 +0000321 uint32_t size;
322 result = readU32(in, size);
Mark Slee6e536442006-06-30 18:28:50 +0000323
324 // Use the heap here to prevent stack overflow for v. large strings
325 uint8_t *b = new uint8_t[size];
Mark Slee8d7e1f62006-06-07 06:48:56 +0000326 in->readAll(b, size);
327 str = string((char*)b, size);
Mark Slee6e536442006-06-30 18:28:50 +0000328 delete [] b;
329
Mark Sleef3c322b2006-06-26 23:52:22 +0000330 return result + (uint32_t)size;
Mark Sleee8540632006-05-30 09:24:40 +0000331}
Marc Slemko6f038a72006-08-03 18:58:09 +0000332}}} // facebook::thrift::protocol