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 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 4 | uint32_t TBinaryProtocol::writeStructBegin(TTransport* out, |
| 5 | const string& name) const { |
| 6 | return 0; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 7 | } |
| 8 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 9 | uint32_t TBinaryProtocol::writeStructEnd(TTransport* out) const { |
| 10 | return 0; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 11 | } |
| 12 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 13 | uint32_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 Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame^] | 19 | writeI32(out, (int32_t)fieldId); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | uint32_t TBinaryProtocol::writeFieldEnd(TTransport* out) const { |
| 23 | return 0; |
| 24 | } |
| 25 | |
| 26 | uint32_t TBinaryProtocol::writeFieldStop(TTransport* out) const { |
| 27 | return |
| 28 | writeByte(out, (uint8_t)T_STOP); |
| 29 | } |
| 30 | |
| 31 | uint32_t TBinaryProtocol::writeMapBegin(TTransport* out, |
| 32 | const TType keyType, |
| 33 | const TType valType, |
Mark Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame^] | 34 | const int32_t size) const { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 35 | return |
| 36 | writeByte(out, (uint8_t)keyType) + |
| 37 | writeByte(out, (uint8_t)valType) + |
Mark Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame^] | 38 | writeI32(out, (int32_t)size); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | uint32_t TBinaryProtocol::writeMapEnd(TTransport* out) const { |
| 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | uint32_t TBinaryProtocol::writeListBegin(TTransport* out, |
| 46 | const TType elemType, |
Mark Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame^] | 47 | const int32_t size) const { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 48 | return |
| 49 | writeByte(out, (uint8_t) elemType) + |
Mark Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame^] | 50 | writeI32(out, (int32_t)size); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | uint32_t TBinaryProtocol::writeListEnd(TTransport* out) const { |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | uint32_t TBinaryProtocol::writeSetBegin(TTransport* out, |
| 58 | const TType elemType, |
Mark Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame^] | 59 | const int32_t size) const { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 60 | return |
| 61 | writeByte(out, (uint8_t)elemType) + |
Mark Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame^] | 62 | writeI32(out, (int32_t)size); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | uint32_t TBinaryProtocol::writeSetEnd(TTransport* out) const { |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | uint32_t TBinaryProtocol::writeByte(TTransport* out, |
| 70 | const uint8_t byte) const { |
| 71 | out->write(&byte, 1); |
| 72 | return 1; |
| 73 | } |
| 74 | |
| 75 | uint32_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 | |
| 82 | uint32_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 | |
| 89 | uint32_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 | |
| 96 | uint32_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 | |
| 103 | uint32_t TBinaryProtocol::writeString(TTransport* out, |
| 104 | const string& str) const { |
Mark Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame^] | 105 | uint32_t result = writeI32(out, str.size()); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 106 | out->write((uint8_t*)str.data(), str.size()); |
| 107 | return result + str.size(); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Reading functions |
| 112 | */ |
| 113 | |
| 114 | uint32_t TBinaryProtocol::readStructBegin(TTransport* in, |
| 115 | string& name) const { |
| 116 | name = ""; |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | uint32_t TBinaryProtocol::readStructEnd(TTransport* in) const { |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | uint32_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 Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame^] | 136 | int32_t id; |
| 137 | result += readI32(in, id); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 138 | fieldId = (uint16_t)id; |
| 139 | return result; |
| 140 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 141 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 142 | uint32_t TBinaryProtocol::readFieldEnd(TTransport* in) const { |
| 143 | return 0; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 144 | } |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 145 | |
| 146 | uint32_t TBinaryProtocol::readMapBegin(TTransport* in, |
| 147 | TType& keyType, |
| 148 | TType& valType, |
Mark Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame^] | 149 | int32_t& size) const { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 150 | 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 Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame^] | 156 | result += readI32(in, size); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 157 | return result; |
| 158 | } |
| 159 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 160 | uint32_t TBinaryProtocol::readMapEnd(TTransport* in) const { |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | uint32_t TBinaryProtocol::readListBegin(TTransport* in, |
| 165 | TType& elemType, |
Mark Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame^] | 166 | int32_t& size) const { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 167 | uint8_t e; |
| 168 | uint32_t result = 0; |
| 169 | result += readByte(in, e); |
| 170 | elemType = (TType)e; |
Mark Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame^] | 171 | result += readI32(in, size); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 172 | return result; |
| 173 | } |
| 174 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 175 | uint32_t TBinaryProtocol::readListEnd(TTransport* in) const { |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | uint32_t TBinaryProtocol::readSetBegin(TTransport* in, |
| 180 | TType& elemType, |
Mark Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame^] | 181 | int32_t& size) const { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 182 | uint8_t e; |
| 183 | uint32_t result = 0; |
| 184 | result += readByte(in, e); |
| 185 | elemType = (TType)e; |
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 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 190 | uint32_t TBinaryProtocol::readSetEnd(TTransport* in) const { |
| 191 | return 0; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 194 | uint32_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 Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 202 | uint32_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 Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 211 | uint32_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 Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 220 | uint32_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 Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 229 | uint32_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 Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 238 | uint32_t TBinaryProtocol::readString(TTransport* in, |
| 239 | string& str) const { |
Mark Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame^] | 240 | uint32_t result; |
| 241 | int32_t size; |
| 242 | result = readI32(in, size); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 243 | uint8_t b[size]; |
| 244 | in->readAll(b, size); |
| 245 | str = string((char*)b, size); |
Mark Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame^] | 246 | return result + (uint32_t)size; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 247 | } |