Marc Slemko | d42a2c2 | 2006-08-10 03:30:18 +0000 | [diff] [blame^] | 1 | #include "TBinaryProtocol.h" |
| 2 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 3 | using std::string; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 4 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 5 | namespace facebook { namespace thrift { namespace protocol { |
| 6 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 7 | uint32_t TBinaryProtocol::writeMessageBegin(shared_ptr<TTransport> out, |
| 8 | const TMessageType messageType, |
| 9 | const uint32_t seqid) const { |
| 10 | return |
| 11 | writeByte(out, (uint8_t)messageType) + |
| 12 | writeU32(out, seqid); |
| 13 | } |
| 14 | |
| 15 | uint32_t TBinaryProtocol::writeMessageEnd(shared_ptr<TTransport> out) const { |
| 16 | return 0; |
| 17 | } |
| 18 | |
| 19 | uint32_t TBinaryProtocol::writeStructBegin(shared_ptr<TTransport> out, |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 20 | const string& name) const { |
| 21 | return 0; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 22 | } |
| 23 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 24 | uint32_t TBinaryProtocol::writeStructEnd(shared_ptr<TTransport> out) const { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 25 | return 0; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 26 | } |
| 27 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 28 | uint32_t TBinaryProtocol::writeFieldBegin(shared_ptr<TTransport> out, |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 29 | const string& name, |
| 30 | const TType fieldType, |
| 31 | const uint16_t fieldId) const { |
| 32 | return |
| 33 | writeByte(out, (uint8_t)fieldType) + |
Mark Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame] | 34 | writeI32(out, (int32_t)fieldId); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 35 | } |
| 36 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 37 | uint32_t TBinaryProtocol::writeFieldEnd(shared_ptr<TTransport> out) const { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 38 | return 0; |
| 39 | } |
| 40 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 41 | uint32_t TBinaryProtocol::writeFieldStop(shared_ptr<TTransport> out) const { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 42 | return |
| 43 | writeByte(out, (uint8_t)T_STOP); |
| 44 | } |
| 45 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 46 | uint32_t TBinaryProtocol::writeMapBegin(shared_ptr<TTransport> out, |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 47 | const TType keyType, |
| 48 | const TType valType, |
Mark Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame] | 49 | const int32_t size) const { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 50 | return |
| 51 | writeByte(out, (uint8_t)keyType) + |
| 52 | writeByte(out, (uint8_t)valType) + |
Mark Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame] | 53 | writeI32(out, (int32_t)size); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 56 | uint32_t TBinaryProtocol::writeMapEnd(shared_ptr<TTransport> out) const { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 57 | return 0; |
| 58 | } |
| 59 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 60 | uint32_t TBinaryProtocol::writeListBegin(shared_ptr<TTransport> out, |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 61 | const TType elemType, |
Mark Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame] | 62 | const int32_t size) const { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 63 | return |
| 64 | writeByte(out, (uint8_t) elemType) + |
Mark Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame] | 65 | writeI32(out, (int32_t)size); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 68 | uint32_t TBinaryProtocol::writeListEnd(shared_ptr<TTransport> out) const { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 69 | return 0; |
| 70 | } |
| 71 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 72 | uint32_t TBinaryProtocol::writeSetBegin(shared_ptr<TTransport> out, |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 73 | const TType elemType, |
Mark Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame] | 74 | const int32_t size) const { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 75 | return |
| 76 | writeByte(out, (uint8_t)elemType) + |
Mark Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame] | 77 | writeI32(out, (int32_t)size); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 80 | uint32_t TBinaryProtocol::writeSetEnd(shared_ptr<TTransport> out) const { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 81 | return 0; |
| 82 | } |
| 83 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 84 | uint32_t TBinaryProtocol::writeByte(shared_ptr<TTransport> out, |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 85 | const uint8_t byte) const { |
| 86 | out->write(&byte, 1); |
| 87 | return 1; |
| 88 | } |
| 89 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 90 | uint32_t TBinaryProtocol::writeU32(shared_ptr<TTransport> out, |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 91 | const uint32_t u32) const { |
| 92 | uint32_t net = (uint32_t)htonl(u32); |
| 93 | out->write((uint8_t*)&net, 4); |
| 94 | return 4; |
| 95 | } |
| 96 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 97 | uint32_t TBinaryProtocol::writeI32(shared_ptr<TTransport> out, |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 98 | const int32_t i32) const { |
| 99 | int32_t net = (int32_t)htonl(i32); |
| 100 | out->write((uint8_t*)&net, 4); |
| 101 | return 4; |
| 102 | } |
| 103 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 104 | uint32_t TBinaryProtocol::writeU64(shared_ptr<TTransport> out, |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 105 | const uint64_t u64) const { |
| 106 | uint64_t net = (uint64_t)htonll(u64); |
| 107 | out->write((uint8_t*)&net, 8); |
| 108 | return 8; |
| 109 | } |
| 110 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 111 | uint32_t TBinaryProtocol::writeI64(shared_ptr<TTransport> out, |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 112 | const int64_t i64) const { |
| 113 | int64_t net = (int64_t)htonll(i64); |
| 114 | out->write((uint8_t*)&net, 8); |
| 115 | return 8; |
| 116 | } |
| 117 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 118 | uint32_t TBinaryProtocol::writeString(shared_ptr<TTransport> out, |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 119 | const string& str) const { |
Mark Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame] | 120 | uint32_t result = writeI32(out, str.size()); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 121 | out->write((uint8_t*)str.data(), str.size()); |
| 122 | return result + str.size(); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Reading functions |
| 127 | */ |
| 128 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 129 | uint32_t TBinaryProtocol::readMessasgeBegin(shared_ptr<TTransport> in, |
| 130 | TMessageType& messageType, |
| 131 | uint32_t& seqid) const { |
| 132 | |
| 133 | uint32_t result = 0; |
| 134 | uint8_t type; |
| 135 | result+= readByte(in, type); |
| 136 | messageType = (TMessageType)type; |
| 137 | result+= readU32(in, seqid); |
| 138 | return result; |
| 139 | } |
| 140 | |
| 141 | uint32_t TBinaryProtocol::readMessageEnd(shared_ptr<TTransport> in) const{ |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | uint32_t TBinaryProtocol::readStructBegin(shared_ptr<TTransport> in, |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 146 | string& name) const { |
| 147 | name = ""; |
| 148 | return 0; |
| 149 | } |
| 150 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 151 | uint32_t TBinaryProtocol::readStructEnd(shared_ptr<TTransport> in) const { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 152 | return 0; |
| 153 | } |
| 154 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 155 | uint32_t TBinaryProtocol::readFieldBegin(shared_ptr<TTransport> in, |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 156 | string& name, |
| 157 | TType& fieldType, |
| 158 | uint16_t& fieldId) const { |
| 159 | uint32_t result = 0; |
| 160 | uint8_t type; |
| 161 | result += readByte(in, type); |
| 162 | fieldType = (TType)type; |
| 163 | if (fieldType == T_STOP) { |
| 164 | fieldId = 0; |
| 165 | return result; |
| 166 | } |
Mark Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame] | 167 | int32_t id; |
| 168 | result += readI32(in, id); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 169 | fieldId = (uint16_t)id; |
| 170 | return result; |
| 171 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 172 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 173 | uint32_t TBinaryProtocol::readFieldEnd(shared_ptr<TTransport> in) const { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 174 | return 0; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 175 | } |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 176 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 177 | uint32_t TBinaryProtocol::readMapBegin(shared_ptr<TTransport> in, |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 178 | TType& keyType, |
| 179 | TType& valType, |
Mark Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame] | 180 | int32_t& size) const { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 181 | uint8_t k, v; |
| 182 | uint32_t result = 0; |
| 183 | result += readByte(in, k); |
| 184 | keyType = (TType)k; |
| 185 | result += readByte(in, v); |
| 186 | valType = (TType)v; |
Mark Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame] | 187 | result += readI32(in, size); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 188 | return result; |
| 189 | } |
| 190 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 191 | uint32_t TBinaryProtocol::readMapEnd(shared_ptr<TTransport> in) const { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 192 | return 0; |
| 193 | } |
| 194 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 195 | uint32_t TBinaryProtocol::readListBegin(shared_ptr<TTransport> in, |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 196 | TType& elemType, |
Mark Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame] | 197 | int32_t& size) const { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 198 | uint8_t e; |
| 199 | uint32_t result = 0; |
| 200 | result += readByte(in, e); |
| 201 | elemType = (TType)e; |
Mark Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame] | 202 | result += readI32(in, size); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 203 | return result; |
| 204 | } |
| 205 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 206 | uint32_t TBinaryProtocol::readListEnd(shared_ptr<TTransport> in) const { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 207 | return 0; |
| 208 | } |
| 209 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 210 | uint32_t TBinaryProtocol::readSetBegin(shared_ptr<TTransport> in, |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 211 | TType& elemType, |
Mark Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame] | 212 | int32_t& size) const { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 213 | uint8_t e; |
| 214 | uint32_t result = 0; |
| 215 | result += readByte(in, e); |
| 216 | elemType = (TType)e; |
Mark Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame] | 217 | result += readI32(in, size); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 218 | return result; |
| 219 | } |
| 220 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 221 | uint32_t TBinaryProtocol::readSetEnd(shared_ptr<TTransport> in) const { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 222 | return 0; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 225 | uint32_t TBinaryProtocol::readByte(shared_ptr<TTransport> in, |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 226 | uint8_t& byte) const { |
| 227 | uint8_t b[1]; |
| 228 | in->readAll(b, 1); |
| 229 | byte = *(uint8_t*)b; |
| 230 | return 1; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 233 | uint32_t TBinaryProtocol::readU32(shared_ptr<TTransport> in, |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 234 | uint32_t& u32) const { |
| 235 | uint8_t b[4]; |
| 236 | in->readAll(b, 4); |
| 237 | u32 = *(uint32_t*)b; |
| 238 | u32 = (uint32_t)ntohl(u32); |
| 239 | return 4; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 242 | uint32_t TBinaryProtocol::readI32(shared_ptr<TTransport> in, |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 243 | int32_t& i32) const { |
| 244 | uint8_t b[4]; |
| 245 | in->readAll(b, 4); |
| 246 | i32 = *(int32_t*)b; |
| 247 | i32 = (int32_t)ntohl(i32); |
| 248 | return 4; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 251 | uint32_t TBinaryProtocol::readU64(shared_ptr<TTransport> in, |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 252 | uint64_t& u64) const { |
| 253 | uint8_t b[8]; |
| 254 | in->readAll(b, 8); |
| 255 | u64 = *(uint64_t*)b; |
| 256 | u64 = (uint64_t)ntohll(u64); |
| 257 | return 8; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 260 | uint32_t TBinaryProtocol::readI64(shared_ptr<TTransport> in, |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 261 | int64_t& i64) const { |
| 262 | uint8_t b[8]; |
| 263 | in->readAll(b, 8); |
| 264 | i64 = *(int64_t*)b; |
| 265 | i64 = (int64_t)ntohll(i64); |
| 266 | return 8; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 269 | uint32_t TBinaryProtocol::readString(shared_ptr<TTransport> in, |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 270 | string& str) const { |
Mark Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame] | 271 | uint32_t result; |
| 272 | int32_t size; |
| 273 | result = readI32(in, size); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 274 | |
| 275 | // Use the heap here to prevent stack overflow for v. large strings |
| 276 | uint8_t *b = new uint8_t[size]; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 277 | in->readAll(b, size); |
| 278 | str = string((char*)b, size); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 279 | delete [] b; |
| 280 | |
Mark Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame] | 281 | return result + (uint32_t)size; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 282 | } |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 283 | }}} // facebook::thrift::protocol |