Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 1 | #include "protocol/TBinaryProtocol.h" |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 2 | using std::string; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 3 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 4 | namespace facebook { namespace thrift { namespace protocol { |
| 5 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame^] | 6 | uint32_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 | |
| 14 | uint32_t TBinaryProtocol::writeMessageEnd(shared_ptr<TTransport> out) const { |
| 15 | return 0; |
| 16 | } |
| 17 | |
| 18 | uint32_t TBinaryProtocol::writeStructBegin(shared_ptr<TTransport> out, |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 19 | const string& name) const { |
| 20 | return 0; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 21 | } |
| 22 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame^] | 23 | uint32_t TBinaryProtocol::writeStructEnd(shared_ptr<TTransport> out) const { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 24 | return 0; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 25 | } |
| 26 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame^] | 27 | uint32_t TBinaryProtocol::writeFieldBegin(shared_ptr<TTransport> out, |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 28 | const string& name, |
| 29 | const TType fieldType, |
| 30 | const uint16_t fieldId) const { |
| 31 | return |
| 32 | writeByte(out, (uint8_t)fieldType) + |
Mark Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame] | 33 | writeI32(out, (int32_t)fieldId); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame^] | 36 | uint32_t TBinaryProtocol::writeFieldEnd(shared_ptr<TTransport> out) const { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 37 | return 0; |
| 38 | } |
| 39 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame^] | 40 | uint32_t TBinaryProtocol::writeFieldStop(shared_ptr<TTransport> out) const { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 41 | return |
| 42 | writeByte(out, (uint8_t)T_STOP); |
| 43 | } |
| 44 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame^] | 45 | uint32_t TBinaryProtocol::writeMapBegin(shared_ptr<TTransport> out, |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 46 | const TType keyType, |
| 47 | const TType valType, |
Mark Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame] | 48 | const int32_t size) const { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 49 | return |
| 50 | writeByte(out, (uint8_t)keyType) + |
| 51 | writeByte(out, (uint8_t)valType) + |
Mark Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame] | 52 | writeI32(out, (int32_t)size); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame^] | 55 | uint32_t TBinaryProtocol::writeMapEnd(shared_ptr<TTransport> out) const { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 56 | return 0; |
| 57 | } |
| 58 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame^] | 59 | uint32_t TBinaryProtocol::writeListBegin(shared_ptr<TTransport> out, |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 60 | const TType elemType, |
Mark Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame] | 61 | const int32_t size) const { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 62 | return |
| 63 | writeByte(out, (uint8_t) elemType) + |
Mark Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame] | 64 | writeI32(out, (int32_t)size); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame^] | 67 | uint32_t TBinaryProtocol::writeListEnd(shared_ptr<TTransport> out) const { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 68 | return 0; |
| 69 | } |
| 70 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame^] | 71 | uint32_t TBinaryProtocol::writeSetBegin(shared_ptr<TTransport> out, |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 72 | const TType elemType, |
Mark Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame] | 73 | const int32_t size) const { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 74 | return |
| 75 | writeByte(out, (uint8_t)elemType) + |
Mark Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame] | 76 | writeI32(out, (int32_t)size); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame^] | 79 | uint32_t TBinaryProtocol::writeSetEnd(shared_ptr<TTransport> out) const { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 80 | return 0; |
| 81 | } |
| 82 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame^] | 83 | uint32_t TBinaryProtocol::writeByte(shared_ptr<TTransport> out, |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 84 | const uint8_t byte) const { |
| 85 | out->write(&byte, 1); |
| 86 | return 1; |
| 87 | } |
| 88 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame^] | 89 | uint32_t TBinaryProtocol::writeU32(shared_ptr<TTransport> out, |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 90 | 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 Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame^] | 96 | uint32_t TBinaryProtocol::writeI32(shared_ptr<TTransport> out, |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 97 | 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 Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame^] | 103 | uint32_t TBinaryProtocol::writeU64(shared_ptr<TTransport> out, |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 104 | 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 Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame^] | 110 | uint32_t TBinaryProtocol::writeI64(shared_ptr<TTransport> out, |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 111 | 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 Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame^] | 117 | uint32_t TBinaryProtocol::writeString(shared_ptr<TTransport> out, |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 118 | const string& str) const { |
Mark Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame] | 119 | uint32_t result = writeI32(out, str.size()); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 120 | out->write((uint8_t*)str.data(), str.size()); |
| 121 | return result + str.size(); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Reading functions |
| 126 | */ |
| 127 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame^] | 128 | uint32_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 | |
| 140 | uint32_t TBinaryProtocol::readMessageEnd(shared_ptr<TTransport> in) const{ |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | uint32_t TBinaryProtocol::readStructBegin(shared_ptr<TTransport> in, |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 145 | string& name) const { |
| 146 | name = ""; |
| 147 | return 0; |
| 148 | } |
| 149 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame^] | 150 | uint32_t TBinaryProtocol::readStructEnd(shared_ptr<TTransport> in) const { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 151 | return 0; |
| 152 | } |
| 153 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame^] | 154 | uint32_t TBinaryProtocol::readFieldBegin(shared_ptr<TTransport> in, |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 155 | 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 Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame] | 166 | int32_t id; |
| 167 | result += readI32(in, id); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 168 | fieldId = (uint16_t)id; |
| 169 | return result; |
| 170 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 171 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame^] | 172 | uint32_t TBinaryProtocol::readFieldEnd(shared_ptr<TTransport> in) const { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 173 | return 0; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 174 | } |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 175 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame^] | 176 | uint32_t TBinaryProtocol::readMapBegin(shared_ptr<TTransport> in, |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 177 | TType& keyType, |
| 178 | TType& valType, |
Mark Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame] | 179 | int32_t& size) const { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 180 | 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 Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame] | 186 | result += readI32(in, size); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 187 | return result; |
| 188 | } |
| 189 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame^] | 190 | uint32_t TBinaryProtocol::readMapEnd(shared_ptr<TTransport> in) const { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 191 | return 0; |
| 192 | } |
| 193 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame^] | 194 | uint32_t TBinaryProtocol::readListBegin(shared_ptr<TTransport> in, |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 195 | TType& elemType, |
Mark Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame] | 196 | int32_t& size) const { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 197 | uint8_t e; |
| 198 | uint32_t result = 0; |
| 199 | result += readByte(in, e); |
| 200 | elemType = (TType)e; |
Mark Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame] | 201 | result += readI32(in, size); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 202 | return result; |
| 203 | } |
| 204 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame^] | 205 | uint32_t TBinaryProtocol::readListEnd(shared_ptr<TTransport> in) const { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 206 | return 0; |
| 207 | } |
| 208 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame^] | 209 | uint32_t TBinaryProtocol::readSetBegin(shared_ptr<TTransport> in, |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 210 | TType& elemType, |
Mark Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame] | 211 | int32_t& size) const { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 212 | uint8_t e; |
| 213 | uint32_t result = 0; |
| 214 | result += readByte(in, e); |
| 215 | elemType = (TType)e; |
Mark Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame] | 216 | result += readI32(in, size); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 217 | return result; |
| 218 | } |
| 219 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame^] | 220 | uint32_t TBinaryProtocol::readSetEnd(shared_ptr<TTransport> in) const { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 221 | return 0; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame^] | 224 | uint32_t TBinaryProtocol::readByte(shared_ptr<TTransport> in, |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 225 | uint8_t& byte) const { |
| 226 | uint8_t b[1]; |
| 227 | in->readAll(b, 1); |
| 228 | byte = *(uint8_t*)b; |
| 229 | return 1; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame^] | 232 | uint32_t TBinaryProtocol::readU32(shared_ptr<TTransport> in, |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 233 | 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 Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame^] | 241 | uint32_t TBinaryProtocol::readI32(shared_ptr<TTransport> in, |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 242 | 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 Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame^] | 250 | uint32_t TBinaryProtocol::readU64(shared_ptr<TTransport> in, |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 251 | 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 Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame^] | 259 | uint32_t TBinaryProtocol::readI64(shared_ptr<TTransport> in, |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 260 | 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 Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame^] | 268 | uint32_t TBinaryProtocol::readString(shared_ptr<TTransport> in, |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 269 | string& str) const { |
Mark Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame] | 270 | uint32_t result; |
| 271 | int32_t size; |
| 272 | result = readI32(in, size); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 273 | |
| 274 | // Use the heap here to prevent stack overflow for v. large strings |
| 275 | uint8_t *b = new uint8_t[size]; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 276 | in->readAll(b, size); |
| 277 | str = string((char*)b, size); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 278 | delete [] b; |
| 279 | |
Mark Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame] | 280 | return result + (uint32_t)size; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 281 | } |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 282 | }}} // facebook::thrift::protocol |