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