blob: fef8ab48b147a714911d1739b54e03cff8ee00a3 [file] [log] [blame]
Mark Sleee8540632006-05-30 09:24:40 +00001#include "protocol/TBinaryProtocol.h"
Mark Slee8d7e1f62006-06-07 06:48:56 +00002using std::string;
Mark Sleee8540632006-05-30 09:24:40 +00003
Marc Slemko6f038a72006-08-03 18:58:09 +00004namespace facebook { namespace thrift { namespace protocol {
5
Marc Slemko16698852006-08-04 03:16:10 +00006uint32_t TBinaryProtocol::writeMessageBegin(shared_ptr<TTransport> out,
7 const TMessageType messageType,
8 const uint32_t seqid) const {
9 return
10 writeByte(out, (uint8_t)messageType) +
11 writeU32(out, seqid);
12}
13
14uint32_t TBinaryProtocol::writeMessageEnd(shared_ptr<TTransport> out) const {
15 return 0;
16}
17
18uint32_t TBinaryProtocol::writeStructBegin(shared_ptr<TTransport> out,
Mark Slee8d7e1f62006-06-07 06:48:56 +000019 const string& name) const {
20 return 0;
Mark Sleee8540632006-05-30 09:24:40 +000021}
22
Marc Slemko16698852006-08-04 03:16:10 +000023uint32_t TBinaryProtocol::writeStructEnd(shared_ptr<TTransport> out) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +000024 return 0;
Mark Sleee8540632006-05-30 09:24:40 +000025}
26
Marc Slemko16698852006-08-04 03:16:10 +000027uint32_t TBinaryProtocol::writeFieldBegin(shared_ptr<TTransport> out,
Mark Slee8d7e1f62006-06-07 06:48:56 +000028 const string& name,
29 const TType fieldType,
30 const uint16_t fieldId) const {
31 return
32 writeByte(out, (uint8_t)fieldType) +
Mark Sleef3c322b2006-06-26 23:52:22 +000033 writeI32(out, (int32_t)fieldId);
Mark Slee8d7e1f62006-06-07 06:48:56 +000034}
35
Marc Slemko16698852006-08-04 03:16:10 +000036uint32_t TBinaryProtocol::writeFieldEnd(shared_ptr<TTransport> out) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +000037 return 0;
38}
39
Marc Slemko16698852006-08-04 03:16:10 +000040uint32_t TBinaryProtocol::writeFieldStop(shared_ptr<TTransport> out) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +000041 return
42 writeByte(out, (uint8_t)T_STOP);
43}
44
Marc Slemko16698852006-08-04 03:16:10 +000045uint32_t TBinaryProtocol::writeMapBegin(shared_ptr<TTransport> out,
Mark Slee8d7e1f62006-06-07 06:48:56 +000046 const TType keyType,
47 const TType valType,
Mark Sleef3c322b2006-06-26 23:52:22 +000048 const int32_t size) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +000049 return
50 writeByte(out, (uint8_t)keyType) +
51 writeByte(out, (uint8_t)valType) +
Mark Sleef3c322b2006-06-26 23:52:22 +000052 writeI32(out, (int32_t)size);
Mark Slee8d7e1f62006-06-07 06:48:56 +000053}
54
Marc Slemko16698852006-08-04 03:16:10 +000055uint32_t TBinaryProtocol::writeMapEnd(shared_ptr<TTransport> out) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +000056 return 0;
57}
58
Marc Slemko16698852006-08-04 03:16:10 +000059uint32_t TBinaryProtocol::writeListBegin(shared_ptr<TTransport> out,
Mark Slee8d7e1f62006-06-07 06:48:56 +000060 const TType elemType,
Mark Sleef3c322b2006-06-26 23:52:22 +000061 const int32_t size) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +000062 return
63 writeByte(out, (uint8_t) elemType) +
Mark Sleef3c322b2006-06-26 23:52:22 +000064 writeI32(out, (int32_t)size);
Mark Slee8d7e1f62006-06-07 06:48:56 +000065}
66
Marc Slemko16698852006-08-04 03:16:10 +000067uint32_t TBinaryProtocol::writeListEnd(shared_ptr<TTransport> out) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +000068 return 0;
69}
70
Marc Slemko16698852006-08-04 03:16:10 +000071uint32_t TBinaryProtocol::writeSetBegin(shared_ptr<TTransport> out,
Mark Slee8d7e1f62006-06-07 06:48:56 +000072 const TType elemType,
Mark Sleef3c322b2006-06-26 23:52:22 +000073 const int32_t size) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +000074 return
75 writeByte(out, (uint8_t)elemType) +
Mark Sleef3c322b2006-06-26 23:52:22 +000076 writeI32(out, (int32_t)size);
Mark Slee8d7e1f62006-06-07 06:48:56 +000077}
78
Marc Slemko16698852006-08-04 03:16:10 +000079uint32_t TBinaryProtocol::writeSetEnd(shared_ptr<TTransport> out) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +000080 return 0;
81}
82
Marc Slemko16698852006-08-04 03:16:10 +000083uint32_t TBinaryProtocol::writeByte(shared_ptr<TTransport> out,
Mark Slee8d7e1f62006-06-07 06:48:56 +000084 const uint8_t byte) const {
85 out->write(&byte, 1);
86 return 1;
87}
88
Marc Slemko16698852006-08-04 03:16:10 +000089uint32_t TBinaryProtocol::writeU32(shared_ptr<TTransport> out,
Mark Slee8d7e1f62006-06-07 06:48:56 +000090 const uint32_t u32) const {
91 uint32_t net = (uint32_t)htonl(u32);
92 out->write((uint8_t*)&net, 4);
93 return 4;
94}
95
Marc Slemko16698852006-08-04 03:16:10 +000096uint32_t TBinaryProtocol::writeI32(shared_ptr<TTransport> out,
Mark Slee8d7e1f62006-06-07 06:48:56 +000097 const int32_t i32) const {
98 int32_t net = (int32_t)htonl(i32);
99 out->write((uint8_t*)&net, 4);
100 return 4;
101}
102
Marc Slemko16698852006-08-04 03:16:10 +0000103uint32_t TBinaryProtocol::writeU64(shared_ptr<TTransport> out,
Mark Slee8d7e1f62006-06-07 06:48:56 +0000104 const uint64_t u64) const {
105 uint64_t net = (uint64_t)htonll(u64);
106 out->write((uint8_t*)&net, 8);
107 return 8;
108}
109
Marc Slemko16698852006-08-04 03:16:10 +0000110uint32_t TBinaryProtocol::writeI64(shared_ptr<TTransport> out,
Mark Slee8d7e1f62006-06-07 06:48:56 +0000111 const int64_t i64) const {
112 int64_t net = (int64_t)htonll(i64);
113 out->write((uint8_t*)&net, 8);
114 return 8;
115}
116
Marc Slemko16698852006-08-04 03:16:10 +0000117uint32_t TBinaryProtocol::writeString(shared_ptr<TTransport> out,
Mark Slee8d7e1f62006-06-07 06:48:56 +0000118 const string& str) const {
Mark Sleef3c322b2006-06-26 23:52:22 +0000119 uint32_t result = writeI32(out, str.size());
Mark Slee8d7e1f62006-06-07 06:48:56 +0000120 out->write((uint8_t*)str.data(), str.size());
121 return result + str.size();
122}
123
124/**
125 * Reading functions
126 */
127
Marc Slemko16698852006-08-04 03:16:10 +0000128uint32_t TBinaryProtocol::readMessasgeBegin(shared_ptr<TTransport> in,
129 TMessageType& messageType,
130 uint32_t& seqid) const {
131
132 uint32_t result = 0;
133 uint8_t type;
134 result+= readByte(in, type);
135 messageType = (TMessageType)type;
136 result+= readU32(in, seqid);
137 return result;
138}
139
140uint32_t TBinaryProtocol::readMessageEnd(shared_ptr<TTransport> in) const{
141 return 0;
142}
143
144uint32_t TBinaryProtocol::readStructBegin(shared_ptr<TTransport> in,
Mark Slee8d7e1f62006-06-07 06:48:56 +0000145 string& name) const {
146 name = "";
147 return 0;
148}
149
Marc Slemko16698852006-08-04 03:16:10 +0000150uint32_t TBinaryProtocol::readStructEnd(shared_ptr<TTransport> in) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000151 return 0;
152}
153
Marc Slemko16698852006-08-04 03:16:10 +0000154uint32_t TBinaryProtocol::readFieldBegin(shared_ptr<TTransport> in,
Mark Slee8d7e1f62006-06-07 06:48:56 +0000155 string& name,
156 TType& fieldType,
157 uint16_t& fieldId) const {
158 uint32_t result = 0;
159 uint8_t type;
160 result += readByte(in, type);
161 fieldType = (TType)type;
162 if (fieldType == T_STOP) {
163 fieldId = 0;
164 return result;
165 }
Mark Sleef3c322b2006-06-26 23:52:22 +0000166 int32_t id;
167 result += readI32(in, id);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000168 fieldId = (uint16_t)id;
169 return result;
170}
Mark Sleee8540632006-05-30 09:24:40 +0000171
Marc Slemko16698852006-08-04 03:16:10 +0000172uint32_t TBinaryProtocol::readFieldEnd(shared_ptr<TTransport> in) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000173 return 0;
Mark Sleee8540632006-05-30 09:24:40 +0000174}
Mark Slee8d7e1f62006-06-07 06:48:56 +0000175
Marc Slemko16698852006-08-04 03:16:10 +0000176uint32_t TBinaryProtocol::readMapBegin(shared_ptr<TTransport> in,
Mark Slee8d7e1f62006-06-07 06:48:56 +0000177 TType& keyType,
178 TType& valType,
Mark Sleef3c322b2006-06-26 23:52:22 +0000179 int32_t& size) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000180 uint8_t k, v;
181 uint32_t result = 0;
182 result += readByte(in, k);
183 keyType = (TType)k;
184 result += readByte(in, v);
185 valType = (TType)v;
Mark Sleef3c322b2006-06-26 23:52:22 +0000186 result += readI32(in, size);
Mark Sleee8540632006-05-30 09:24:40 +0000187 return result;
188}
189
Marc Slemko16698852006-08-04 03:16:10 +0000190uint32_t TBinaryProtocol::readMapEnd(shared_ptr<TTransport> in) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000191 return 0;
192}
193
Marc Slemko16698852006-08-04 03:16:10 +0000194uint32_t TBinaryProtocol::readListBegin(shared_ptr<TTransport> in,
Mark Slee8d7e1f62006-06-07 06:48:56 +0000195 TType& elemType,
Mark Sleef3c322b2006-06-26 23:52:22 +0000196 int32_t& size) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000197 uint8_t e;
198 uint32_t result = 0;
199 result += readByte(in, e);
200 elemType = (TType)e;
Mark Sleef3c322b2006-06-26 23:52:22 +0000201 result += readI32(in, size);
Mark Sleee8540632006-05-30 09:24:40 +0000202 return result;
203}
204
Marc Slemko16698852006-08-04 03:16:10 +0000205uint32_t TBinaryProtocol::readListEnd(shared_ptr<TTransport> in) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000206 return 0;
207}
208
Marc Slemko16698852006-08-04 03:16:10 +0000209uint32_t TBinaryProtocol::readSetBegin(shared_ptr<TTransport> in,
Mark Slee8d7e1f62006-06-07 06:48:56 +0000210 TType& elemType,
Mark Sleef3c322b2006-06-26 23:52:22 +0000211 int32_t& size) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000212 uint8_t e;
213 uint32_t result = 0;
214 result += readByte(in, e);
215 elemType = (TType)e;
Mark Sleef3c322b2006-06-26 23:52:22 +0000216 result += readI32(in, size);
Mark Sleee8540632006-05-30 09:24:40 +0000217 return result;
218}
219
Marc Slemko16698852006-08-04 03:16:10 +0000220uint32_t TBinaryProtocol::readSetEnd(shared_ptr<TTransport> in) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000221 return 0;
Mark Sleee8540632006-05-30 09:24:40 +0000222}
223
Marc Slemko16698852006-08-04 03:16:10 +0000224uint32_t TBinaryProtocol::readByte(shared_ptr<TTransport> in,
Mark Slee8d7e1f62006-06-07 06:48:56 +0000225 uint8_t& byte) const {
226 uint8_t b[1];
227 in->readAll(b, 1);
228 byte = *(uint8_t*)b;
229 return 1;
Mark Sleee8540632006-05-30 09:24:40 +0000230}
231
Marc Slemko16698852006-08-04 03:16:10 +0000232uint32_t TBinaryProtocol::readU32(shared_ptr<TTransport> in,
Mark Slee8d7e1f62006-06-07 06:48:56 +0000233 uint32_t& u32) const {
234 uint8_t b[4];
235 in->readAll(b, 4);
236 u32 = *(uint32_t*)b;
237 u32 = (uint32_t)ntohl(u32);
238 return 4;
Mark Sleee8540632006-05-30 09:24:40 +0000239}
240
Marc Slemko16698852006-08-04 03:16:10 +0000241uint32_t TBinaryProtocol::readI32(shared_ptr<TTransport> in,
Mark Slee8d7e1f62006-06-07 06:48:56 +0000242 int32_t& i32) const {
243 uint8_t b[4];
244 in->readAll(b, 4);
245 i32 = *(int32_t*)b;
246 i32 = (int32_t)ntohl(i32);
247 return 4;
Mark Sleee8540632006-05-30 09:24:40 +0000248}
249
Marc Slemko16698852006-08-04 03:16:10 +0000250uint32_t TBinaryProtocol::readU64(shared_ptr<TTransport> in,
Mark Slee8d7e1f62006-06-07 06:48:56 +0000251 uint64_t& u64) const {
252 uint8_t b[8];
253 in->readAll(b, 8);
254 u64 = *(uint64_t*)b;
255 u64 = (uint64_t)ntohll(u64);
256 return 8;
Mark Sleee8540632006-05-30 09:24:40 +0000257}
258
Marc Slemko16698852006-08-04 03:16:10 +0000259uint32_t TBinaryProtocol::readI64(shared_ptr<TTransport> in,
Mark Slee8d7e1f62006-06-07 06:48:56 +0000260 int64_t& i64) const {
261 uint8_t b[8];
262 in->readAll(b, 8);
263 i64 = *(int64_t*)b;
264 i64 = (int64_t)ntohll(i64);
265 return 8;
Mark Sleee8540632006-05-30 09:24:40 +0000266}
267
Marc Slemko16698852006-08-04 03:16:10 +0000268uint32_t TBinaryProtocol::readString(shared_ptr<TTransport> in,
Mark Slee8d7e1f62006-06-07 06:48:56 +0000269 string& str) const {
Mark Sleef3c322b2006-06-26 23:52:22 +0000270 uint32_t result;
271 int32_t size;
272 result = readI32(in, size);
Mark Slee6e536442006-06-30 18:28:50 +0000273
274 // Use the heap here to prevent stack overflow for v. large strings
275 uint8_t *b = new uint8_t[size];
Mark Slee8d7e1f62006-06-07 06:48:56 +0000276 in->readAll(b, size);
277 str = string((char*)b, size);
Mark Slee6e536442006-06-30 18:28:50 +0000278 delete [] b;
279
Mark Sleef3c322b2006-06-26 23:52:22 +0000280 return result + (uint32_t)size;
Mark Sleee8540632006-05-30 09:24:40 +0000281}
Marc Slemko6f038a72006-08-03 18:58:09 +0000282}}} // facebook::thrift::protocol