blob: 51c9306881b87cf1259d4b650ef546257b1bfd93 [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,
Mark Sleecfc01932006-09-01 22:18:16 +000010 const int32_t seqid) const {
Marc Slemko16698852006-08-04 03:16:10 +000011 return
Marc Slemko91f67482006-08-11 23:58:57 +000012 writeString(out, name) +
Mark Sleecfc01932006-09-01 22:18:16 +000013 writeByte(out, (int8_t)messageType) +
14 writeI32(out, seqid);
Marc Slemko16698852006-08-04 03:16:10 +000015}
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
Mark Sleecfc01932006-09-01 22:18:16 +000035 writeByte(out, (int8_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
Mark Sleecfc01932006-09-01 22:18:16 +000045 writeByte(out, (int8_t)T_STOP);
Mark Slee8d7e1f62006-06-07 06:48:56 +000046}
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
Mark Sleecfc01932006-09-01 22:18:16 +000053 writeByte(out, (int8_t)keyType) +
54 writeByte(out, (int8_t)valType) +
55 writeI32(out, (int32_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
Mark Sleecfc01932006-09-01 22:18:16 +000066 writeByte(out, (int8_t) elemType) +
67 writeI32(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
Mark Sleecfc01932006-09-01 22:18:16 +000078 writeByte(out, (int8_t)elemType) +
79 writeI32(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 Sleecfc01932006-09-01 22:18:16 +000094 const int8_t byte) const {
95 out->write((uint8_t*)&byte, 1);
Mark Slee8d7e1f62006-06-07 06:48:56 +000096 return 1;
97}
98
Marc Slemko0b4ffa92006-08-11 02:49:29 +000099uint32_t TBinaryProtocol::writeI16(shared_ptr<TTransport> out,
100 const int16_t i16) const {
101 int16_t net = (int16_t)htons(i16);
102 out->write((uint8_t*)&net, 2);
103 return 2;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000104}
105
Marc Slemkoe6889de2006-08-12 00:32:53 +0000106uint32_t TBinaryProtocol::writeI32(shared_ptr<TTransport> out,
107 const int32_t i32) const {
108 int32_t net = (int32_t)htonl(i32);
109 out->write((uint8_t*)&net, 4);
110 return 4;
111}
112
Marc Slemko16698852006-08-04 03:16:10 +0000113uint32_t TBinaryProtocol::writeI64(shared_ptr<TTransport> out,
Mark Slee8d7e1f62006-06-07 06:48:56 +0000114 const int64_t i64) const {
115 int64_t net = (int64_t)htonll(i64);
116 out->write((uint8_t*)&net, 8);
117 return 8;
118}
119
Marc Slemko16698852006-08-04 03:16:10 +0000120uint32_t TBinaryProtocol::writeString(shared_ptr<TTransport> out,
Mark Slee8d7e1f62006-06-07 06:48:56 +0000121 const string& str) const {
Mark Sleef3c322b2006-06-26 23:52:22 +0000122 uint32_t result = writeI32(out, str.size());
Mark Slee8d7e1f62006-06-07 06:48:56 +0000123 out->write((uint8_t*)str.data(), str.size());
124 return result + str.size();
125}
126
127/**
128 * Reading functions
129 */
130
Marc Slemkoe6889de2006-08-12 00:32:53 +0000131uint32_t TBinaryProtocol::readMessageBegin(shared_ptr<TTransport> in,
132 std::string& name,
133 TMessageType& messageType,
Mark Sleecfc01932006-09-01 22:18:16 +0000134 int32_t& seqid) const {
Marc Slemko16698852006-08-04 03:16:10 +0000135
136 uint32_t result = 0;
Mark Sleecfc01932006-09-01 22:18:16 +0000137 int8_t type;
Marc Slemko91f67482006-08-11 23:58:57 +0000138 result+= readString(in, name);
Marc Slemko16698852006-08-04 03:16:10 +0000139 result+= readByte(in, type);
140 messageType = (TMessageType)type;
Mark Sleecfc01932006-09-01 22:18:16 +0000141 result+= readI32(in, seqid);
Marc Slemko16698852006-08-04 03:16:10 +0000142 return result;
143}
144
145uint32_t TBinaryProtocol::readMessageEnd(shared_ptr<TTransport> in) const{
146 return 0;
147}
148
149uint32_t TBinaryProtocol::readStructBegin(shared_ptr<TTransport> in,
Mark Slee8d7e1f62006-06-07 06:48:56 +0000150 string& name) const {
151 name = "";
152 return 0;
153}
154
Marc Slemko16698852006-08-04 03:16:10 +0000155uint32_t TBinaryProtocol::readStructEnd(shared_ptr<TTransport> in) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000156 return 0;
157}
158
Marc Slemko16698852006-08-04 03:16:10 +0000159uint32_t TBinaryProtocol::readFieldBegin(shared_ptr<TTransport> in,
Mark Slee8d7e1f62006-06-07 06:48:56 +0000160 string& name,
161 TType& fieldType,
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000162 int16_t& fieldId) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000163 uint32_t result = 0;
Mark Sleecfc01932006-09-01 22:18:16 +0000164 int8_t type;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000165 result += readByte(in, type);
166 fieldType = (TType)type;
167 if (fieldType == T_STOP) {
168 fieldId = 0;
169 return result;
170 }
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000171 result += readI16(in, fieldId);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000172 return result;
173}
Mark Sleee8540632006-05-30 09:24:40 +0000174
Marc Slemko16698852006-08-04 03:16:10 +0000175uint32_t TBinaryProtocol::readFieldEnd(shared_ptr<TTransport> in) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000176 return 0;
Mark Sleee8540632006-05-30 09:24:40 +0000177}
Mark Slee8d7e1f62006-06-07 06:48:56 +0000178
Marc Slemko16698852006-08-04 03:16:10 +0000179uint32_t TBinaryProtocol::readMapBegin(shared_ptr<TTransport> in,
Mark Slee8d7e1f62006-06-07 06:48:56 +0000180 TType& keyType,
181 TType& valType,
Marc Slemkob09f5882006-08-23 22:03:34 +0000182 uint32_t& size) const {
Mark Sleecfc01932006-09-01 22:18:16 +0000183 int8_t k, v;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000184 uint32_t result = 0;
Mark Sleecfc01932006-09-01 22:18:16 +0000185 int32_t sizei;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000186 result += readByte(in, k);
187 keyType = (TType)k;
188 result += readByte(in, v);
189 valType = (TType)v;
Mark Sleecfc01932006-09-01 22:18:16 +0000190 result += readI32(in, sizei);
191 // TODO(mcslee): check for negative size
192 size = (uint32_t)sizei;
Mark Sleee8540632006-05-30 09:24:40 +0000193 return result;
194}
195
Marc Slemko16698852006-08-04 03:16:10 +0000196uint32_t TBinaryProtocol::readMapEnd(shared_ptr<TTransport> in) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000197 return 0;
198}
199
Marc Slemko16698852006-08-04 03:16:10 +0000200uint32_t TBinaryProtocol::readListBegin(shared_ptr<TTransport> in,
Mark Slee8d7e1f62006-06-07 06:48:56 +0000201 TType& elemType,
Marc Slemkob09f5882006-08-23 22:03:34 +0000202 uint32_t& size) const {
Mark Sleecfc01932006-09-01 22:18:16 +0000203 int8_t e;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000204 uint32_t result = 0;
Mark Sleecfc01932006-09-01 22:18:16 +0000205 int32_t sizei;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000206 result += readByte(in, e);
207 elemType = (TType)e;
Mark Sleecfc01932006-09-01 22:18:16 +0000208 result += readI32(in, sizei);
209 // TODO(mcslee): check for negative size
210 size = (uint32_t)sizei;
Mark Sleee8540632006-05-30 09:24:40 +0000211 return result;
212}
213
Marc Slemko16698852006-08-04 03:16:10 +0000214uint32_t TBinaryProtocol::readListEnd(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::readSetBegin(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 Sleecfc01932006-09-01 22:18:16 +0000221 int8_t e;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000222 uint32_t result = 0;
Mark Sleecfc01932006-09-01 22:18:16 +0000223 int32_t sizei;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000224 result += readByte(in, e);
225 elemType = (TType)e;
Mark Sleecfc01932006-09-01 22:18:16 +0000226 result += readI32(in, sizei);
227 // TODO(mcslee): check for negative size
228 size = (uint32_t)sizei;
Mark Sleee8540632006-05-30 09:24:40 +0000229 return result;
230}
231
Marc Slemko16698852006-08-04 03:16:10 +0000232uint32_t TBinaryProtocol::readSetEnd(shared_ptr<TTransport> in) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000233 return 0;
Mark Sleee8540632006-05-30 09:24:40 +0000234}
235
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000236uint32_t TBinaryProtocol::readBool(shared_ptr<TTransport> in,
237 bool& value) const {
238 uint8_t b[1];
239 in->readAll(b, 1);
Mark Sleecfc01932006-09-01 22:18:16 +0000240 value = *(int8_t*)b != 0;
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000241 return 1;
242}
243
Marc Slemko16698852006-08-04 03:16:10 +0000244uint32_t TBinaryProtocol::readByte(shared_ptr<TTransport> in,
Mark Sleecfc01932006-09-01 22:18:16 +0000245 int8_t& byte) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000246 uint8_t b[1];
247 in->readAll(b, 1);
Mark Sleecfc01932006-09-01 22:18:16 +0000248 byte = *(int8_t*)b;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000249 return 1;
Mark Sleee8540632006-05-30 09:24:40 +0000250}
251
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000252uint32_t TBinaryProtocol::readI16(shared_ptr<TTransport> in,
253 int16_t& i16) const {
254 uint8_t b[2];
255 in->readAll(b, 2);
256 i16 = *(int16_t*)b;
257 i16 = (int16_t)ntohs(i16);
258 return 2;
259}
260
Marc Slemko16698852006-08-04 03:16:10 +0000261uint32_t TBinaryProtocol::readI32(shared_ptr<TTransport> in,
Mark Slee8d7e1f62006-06-07 06:48:56 +0000262 int32_t& i32) const {
263 uint8_t b[4];
264 in->readAll(b, 4);
265 i32 = *(int32_t*)b;
266 i32 = (int32_t)ntohl(i32);
267 return 4;
Mark Sleee8540632006-05-30 09:24:40 +0000268}
269
Marc Slemko16698852006-08-04 03:16:10 +0000270uint32_t TBinaryProtocol::readI64(shared_ptr<TTransport> in,
Mark Slee8d7e1f62006-06-07 06:48:56 +0000271 int64_t& i64) const {
272 uint8_t b[8];
273 in->readAll(b, 8);
274 i64 = *(int64_t*)b;
275 i64 = (int64_t)ntohll(i64);
276 return 8;
Mark Sleee8540632006-05-30 09:24:40 +0000277}
278
Marc Slemko16698852006-08-04 03:16:10 +0000279uint32_t TBinaryProtocol::readString(shared_ptr<TTransport> in,
Mark Slee8d7e1f62006-06-07 06:48:56 +0000280 string& str) const {
Mark Sleef3c322b2006-06-26 23:52:22 +0000281 uint32_t result;
Mark Sleecfc01932006-09-01 22:18:16 +0000282 int32_t size;
283 result = readI32(in, size);
284
285 // TODO(mcslee): check for negative size
Mark Slee6e536442006-06-30 18:28:50 +0000286
287 // Use the heap here to prevent stack overflow for v. large strings
288 uint8_t *b = new uint8_t[size];
Mark Slee8d7e1f62006-06-07 06:48:56 +0000289 in->readAll(b, size);
290 str = string((char*)b, size);
Mark Slee6e536442006-06-30 18:28:50 +0000291 delete [] b;
292
Mark Sleef3c322b2006-06-26 23:52:22 +0000293 return result + (uint32_t)size;
Mark Sleee8540632006-05-30 09:24:40 +0000294}
Mark Sleecfc01932006-09-01 22:18:16 +0000295
Marc Slemko6f038a72006-08-03 18:58:09 +0000296}}} // facebook::thrift::protocol