blob: 666a6a4e3d8c55d2a865fd9f83bbb0769df29905 [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
Mark Slee8d7e1f62006-06-07 06:48:56 +00004uint32_t TBinaryProtocol::writeStructBegin(TTransport* out,
5 const string& name) const {
6 return 0;
Mark Sleee8540632006-05-30 09:24:40 +00007}
8
Mark Slee8d7e1f62006-06-07 06:48:56 +00009uint32_t TBinaryProtocol::writeStructEnd(TTransport* out) const {
10 return 0;
Mark Sleee8540632006-05-30 09:24:40 +000011}
12
Mark Slee8d7e1f62006-06-07 06:48:56 +000013uint32_t TBinaryProtocol::writeFieldBegin(TTransport* out,
14 const string& name,
15 const TType fieldType,
16 const uint16_t fieldId) const {
17 return
18 writeByte(out, (uint8_t)fieldType) +
Mark Sleef3c322b2006-06-26 23:52:22 +000019 writeI32(out, (int32_t)fieldId);
Mark Slee8d7e1f62006-06-07 06:48:56 +000020}
21
22uint32_t TBinaryProtocol::writeFieldEnd(TTransport* out) const {
23 return 0;
24}
25
26uint32_t TBinaryProtocol::writeFieldStop(TTransport* out) const {
27 return
28 writeByte(out, (uint8_t)T_STOP);
29}
30
31uint32_t TBinaryProtocol::writeMapBegin(TTransport* out,
32 const TType keyType,
33 const TType valType,
Mark Sleef3c322b2006-06-26 23:52:22 +000034 const int32_t size) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +000035 return
36 writeByte(out, (uint8_t)keyType) +
37 writeByte(out, (uint8_t)valType) +
Mark Sleef3c322b2006-06-26 23:52:22 +000038 writeI32(out, (int32_t)size);
Mark Slee8d7e1f62006-06-07 06:48:56 +000039}
40
41uint32_t TBinaryProtocol::writeMapEnd(TTransport* out) const {
42 return 0;
43}
44
45uint32_t TBinaryProtocol::writeListBegin(TTransport* out,
46 const TType elemType,
Mark Sleef3c322b2006-06-26 23:52:22 +000047 const int32_t size) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +000048 return
49 writeByte(out, (uint8_t) elemType) +
Mark Sleef3c322b2006-06-26 23:52:22 +000050 writeI32(out, (int32_t)size);
Mark Slee8d7e1f62006-06-07 06:48:56 +000051}
52
53uint32_t TBinaryProtocol::writeListEnd(TTransport* out) const {
54 return 0;
55}
56
57uint32_t TBinaryProtocol::writeSetBegin(TTransport* out,
58 const TType elemType,
Mark Sleef3c322b2006-06-26 23:52:22 +000059 const int32_t size) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +000060 return
61 writeByte(out, (uint8_t)elemType) +
Mark Sleef3c322b2006-06-26 23:52:22 +000062 writeI32(out, (int32_t)size);
Mark Slee8d7e1f62006-06-07 06:48:56 +000063}
64
65uint32_t TBinaryProtocol::writeSetEnd(TTransport* out) const {
66 return 0;
67}
68
69uint32_t TBinaryProtocol::writeByte(TTransport* out,
70 const uint8_t byte) const {
71 out->write(&byte, 1);
72 return 1;
73}
74
75uint32_t TBinaryProtocol::writeU32(TTransport* out,
76 const uint32_t u32) const {
77 uint32_t net = (uint32_t)htonl(u32);
78 out->write((uint8_t*)&net, 4);
79 return 4;
80}
81
82uint32_t TBinaryProtocol::writeI32(TTransport* out,
83 const int32_t i32) const {
84 int32_t net = (int32_t)htonl(i32);
85 out->write((uint8_t*)&net, 4);
86 return 4;
87}
88
89uint32_t TBinaryProtocol::writeU64(TTransport* out,
90 const uint64_t u64) const {
91 uint64_t net = (uint64_t)htonll(u64);
92 out->write((uint8_t*)&net, 8);
93 return 8;
94}
95
96uint32_t TBinaryProtocol::writeI64(TTransport* out,
97 const int64_t i64) const {
98 int64_t net = (int64_t)htonll(i64);
99 out->write((uint8_t*)&net, 8);
100 return 8;
101}
102
103uint32_t TBinaryProtocol::writeString(TTransport* out,
104 const string& str) const {
Mark Sleef3c322b2006-06-26 23:52:22 +0000105 uint32_t result = writeI32(out, str.size());
Mark Slee8d7e1f62006-06-07 06:48:56 +0000106 out->write((uint8_t*)str.data(), str.size());
107 return result + str.size();
108}
109
110/**
111 * Reading functions
112 */
113
114uint32_t TBinaryProtocol::readStructBegin(TTransport* in,
115 string& name) const {
116 name = "";
117 return 0;
118}
119
120uint32_t TBinaryProtocol::readStructEnd(TTransport* in) const {
121 return 0;
122}
123
124uint32_t TBinaryProtocol::readFieldBegin(TTransport* in,
125 string& name,
126 TType& fieldType,
127 uint16_t& fieldId) const {
128 uint32_t result = 0;
129 uint8_t type;
130 result += readByte(in, type);
131 fieldType = (TType)type;
132 if (fieldType == T_STOP) {
133 fieldId = 0;
134 return result;
135 }
Mark Sleef3c322b2006-06-26 23:52:22 +0000136 int32_t id;
137 result += readI32(in, id);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000138 fieldId = (uint16_t)id;
139 return result;
140}
Mark Sleee8540632006-05-30 09:24:40 +0000141
Mark Slee8d7e1f62006-06-07 06:48:56 +0000142uint32_t TBinaryProtocol::readFieldEnd(TTransport* in) const {
143 return 0;
Mark Sleee8540632006-05-30 09:24:40 +0000144}
Mark Slee8d7e1f62006-06-07 06:48:56 +0000145
146uint32_t TBinaryProtocol::readMapBegin(TTransport* in,
147 TType& keyType,
148 TType& valType,
Mark Sleef3c322b2006-06-26 23:52:22 +0000149 int32_t& size) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000150 uint8_t k, v;
151 uint32_t result = 0;
152 result += readByte(in, k);
153 keyType = (TType)k;
154 result += readByte(in, v);
155 valType = (TType)v;
Mark Sleef3c322b2006-06-26 23:52:22 +0000156 result += readI32(in, size);
Mark Sleee8540632006-05-30 09:24:40 +0000157 return result;
158}
159
Mark Slee8d7e1f62006-06-07 06:48:56 +0000160uint32_t TBinaryProtocol::readMapEnd(TTransport* in) const {
161 return 0;
162}
163
164uint32_t TBinaryProtocol::readListBegin(TTransport* in,
165 TType& elemType,
Mark Sleef3c322b2006-06-26 23:52:22 +0000166 int32_t& size) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000167 uint8_t e;
168 uint32_t result = 0;
169 result += readByte(in, e);
170 elemType = (TType)e;
Mark Sleef3c322b2006-06-26 23:52:22 +0000171 result += readI32(in, size);
Mark Sleee8540632006-05-30 09:24:40 +0000172 return result;
173}
174
Mark Slee8d7e1f62006-06-07 06:48:56 +0000175uint32_t TBinaryProtocol::readListEnd(TTransport* in) const {
176 return 0;
177}
178
179uint32_t TBinaryProtocol::readSetBegin(TTransport* in,
180 TType& elemType,
Mark Sleef3c322b2006-06-26 23:52:22 +0000181 int32_t& size) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000182 uint8_t e;
183 uint32_t result = 0;
184 result += readByte(in, e);
185 elemType = (TType)e;
Mark Sleef3c322b2006-06-26 23:52:22 +0000186 result += readI32(in, size);
Mark Sleee8540632006-05-30 09:24:40 +0000187 return result;
188}
189
Mark Slee8d7e1f62006-06-07 06:48:56 +0000190uint32_t TBinaryProtocol::readSetEnd(TTransport* in) const {
191 return 0;
Mark Sleee8540632006-05-30 09:24:40 +0000192}
193
Mark Slee8d7e1f62006-06-07 06:48:56 +0000194uint32_t TBinaryProtocol::readByte(TTransport* in,
195 uint8_t& byte) const {
196 uint8_t b[1];
197 in->readAll(b, 1);
198 byte = *(uint8_t*)b;
199 return 1;
Mark Sleee8540632006-05-30 09:24:40 +0000200}
201
Mark Slee8d7e1f62006-06-07 06:48:56 +0000202uint32_t TBinaryProtocol::readU32(TTransport* in,
203 uint32_t& u32) const {
204 uint8_t b[4];
205 in->readAll(b, 4);
206 u32 = *(uint32_t*)b;
207 u32 = (uint32_t)ntohl(u32);
208 return 4;
Mark Sleee8540632006-05-30 09:24:40 +0000209}
210
Mark Slee8d7e1f62006-06-07 06:48:56 +0000211uint32_t TBinaryProtocol::readI32(TTransport* in,
212 int32_t& i32) const {
213 uint8_t b[4];
214 in->readAll(b, 4);
215 i32 = *(int32_t*)b;
216 i32 = (int32_t)ntohl(i32);
217 return 4;
Mark Sleee8540632006-05-30 09:24:40 +0000218}
219
Mark Slee8d7e1f62006-06-07 06:48:56 +0000220uint32_t TBinaryProtocol::readU64(TTransport* in,
221 uint64_t& u64) const {
222 uint8_t b[8];
223 in->readAll(b, 8);
224 u64 = *(uint64_t*)b;
225 u64 = (uint64_t)ntohll(u64);
226 return 8;
Mark Sleee8540632006-05-30 09:24:40 +0000227}
228
Mark Slee8d7e1f62006-06-07 06:48:56 +0000229uint32_t TBinaryProtocol::readI64(TTransport* in,
230 int64_t& i64) const {
231 uint8_t b[8];
232 in->readAll(b, 8);
233 i64 = *(int64_t*)b;
234 i64 = (int64_t)ntohll(i64);
235 return 8;
Mark Sleee8540632006-05-30 09:24:40 +0000236}
237
Mark Slee8d7e1f62006-06-07 06:48:56 +0000238uint32_t TBinaryProtocol::readString(TTransport* in,
239 string& str) const {
Mark Sleef3c322b2006-06-26 23:52:22 +0000240 uint32_t result;
241 int32_t size;
242 result = readI32(in, size);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000243 uint8_t b[size];
244 in->readAll(b, size);
245 str = string((char*)b, size);
Mark Sleef3c322b2006-06-26 23:52:22 +0000246 return result + (uint32_t)size;
Mark Sleee8540632006-05-30 09:24:40 +0000247}