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