29 enum class EncodedType {
48 EncodedType EncodedTypeForValue(
const EncodableValue& value) {
49 switch (value.index()) {
51 return EncodedType::kNull;
53 return std::get<bool>(value) ? EncodedType::kTrue : EncodedType::kFalse;
55 return EncodedType::kInt32;
57 return EncodedType::kInt64;
59 return EncodedType::kFloat64;
61 return EncodedType::kString;
63 return EncodedType::kUInt8List;
65 return EncodedType::kInt32List;
67 return EncodedType::kInt64List;
69 return EncodedType::kFloat64List;
71 return EncodedType::kList;
73 return EncodedType::kMap;
75 return EncodedType::kFloat32List;
78 return EncodedType::kNull;
100 stream->
WriteByte(
static_cast<uint8_t
>(EncodedTypeForValue(value)));
102 switch (value.index()) {
118 const auto& string_value = std::get<std::string>(value);
119 size_t size = string_value.size();
123 reinterpret_cast<const uint8_t*
>(string_value.data()), size);
128 WriteVector(std::get<std::vector<uint8_t>>(value), stream);
131 WriteVector(std::get<std::vector<int32_t>>(value), stream);
134 WriteVector(std::get<std::vector<int64_t>>(value), stream);
137 WriteVector(std::get<std::vector<double>>(value), stream);
140 const auto& list = std::get<EncodableList>(value);
142 for (
const auto& item : list) {
148 const auto& map = std::get<EncodableMap>(value);
150 for (
const auto& pair : map) {
158 <<
"Unhandled custom type in StandardCodecSerializer::WriteValue. "
159 <<
"Custom types require codec extensions." << std::endl;
162 WriteVector(std::get<std::vector<float>>(value), stream);
171 switch (
static_cast<EncodedType
>(type)) {
172 case EncodedType::kNull:
174 case EncodedType::kTrue:
176 case EncodedType::kFalse:
178 case EncodedType::kInt32:
180 case EncodedType::kInt64:
182 case EncodedType::kFloat64:
185 case EncodedType::kLargeInt:
186 case EncodedType::kString: {
188 std::string string_value;
189 string_value.resize(size);
190 stream->
ReadBytes(
reinterpret_cast<uint8_t*
>(&string_value[0]), size);
193 case EncodedType::kUInt8List:
194 return ReadVector<uint8_t>(stream);
195 case EncodedType::kInt32List:
196 return ReadVector<int32_t>(stream);
197 case EncodedType::kInt64List:
198 return ReadVector<int64_t>(stream);
199 case EncodedType::kFloat64List:
200 return ReadVector<double>(stream);
201 case EncodedType::kList: {
204 list_value.reserve(length);
205 for (
size_t i = 0; i < length; ++i) {
210 case EncodedType::kMap: {
213 for (
size_t i = 0; i < length; ++i) {
216 map_value.emplace(std::move(key), std::move(value));
220 case EncodedType::kFloat32List: {
221 return ReadVector<float>(stream);
224 std::cerr <<
"Unknown type in StandardCodecSerializer::ReadValueOfType: "
225 <<
static_cast<int>(type) << std::endl;
233 }
else if (
byte == 254) {
235 stream->
ReadBytes(
reinterpret_cast<uint8_t*
>(&value), 2);
239 stream->
ReadBytes(
reinterpret_cast<uint8_t*
>(&value), 4);
247 stream->
WriteByte(
static_cast<uint8_t
>(size));
248 }
else if (size <= 0xffff) {
250 uint16_t value =
static_cast<uint16_t
>(size);
251 stream->
WriteBytes(
reinterpret_cast<uint8_t*
>(&value), 2);
254 uint32_t value =
static_cast<uint32_t
>(size);
255 stream->
WriteBytes(
reinterpret_cast<uint8_t*
>(&value), 4);
259 template <
typename T>
263 std::vector<T> vector;
264 vector.resize(count);
265 uint8_t type_size =
static_cast<uint8_t
>(
sizeof(T));
269 stream->
ReadBytes(
reinterpret_cast<uint8_t*
>(vector.data()),
271 return EncodableValue(vector);
274 template <
typename T>
275 void StandardCodecSerializer::WriteVector(
const std::vector<T> vector,
276 ByteStreamWriter* stream)
const {
277 size_t count = vector.size();
282 uint8_t type_size =
static_cast<uint8_t
>(
sizeof(T));
284 stream->WriteAlignment(type_size);
286 stream->WriteBytes(
reinterpret_cast<const uint8_t*
>(vector.data()),
299 std::unique_ptr<StandardMessageCodec>>;
300 auto it = sInstances->find(serializer);
301 if (it == sInstances->end()) {
304 auto emplace_result = sInstances->emplace(
305 serializer, std::unique_ptr<StandardMessageCodec>(
307 it = emplace_result.first;
309 return *(it->second);
314 : serializer_(serializer) {}
319 const uint8_t* binary_message,
320 size_t message_size)
const {
321 if (!binary_message) {
322 return std::make_unique<EncodableValue>();
325 return std::make_unique<EncodableValue>(serializer_->
ReadValue(&stream));
328 std::unique_ptr<std::vector<uint8_t>>
331 auto encoded = std::make_unique<std::vector<uint8_t>>();
346 std::unique_ptr<StandardMethodCodec>>;
347 auto it = sInstances->find(serializer);
348 if (it == sInstances->end()) {
351 auto emplace_result = sInstances->emplace(
352 serializer, std::unique_ptr<StandardMethodCodec>(
354 it = emplace_result.first;
356 return *(it->second);
361 : serializer_(serializer) {}
365 std::unique_ptr<MethodCall<EncodableValue>>
367 size_t message_size)
const {
370 const auto* method_name = std::get_if<std::string>(&method_name_value);
372 std::cerr <<
"Invalid method call; method name is not a string."
377 std::make_unique<EncodableValue>(serializer_->
ReadValue(&stream));
378 return std::make_unique<MethodCall<EncodableValue>>(*method_name,
379 std::move(arguments));
382 std::unique_ptr<std::vector<uint8_t>>
385 auto encoded = std::make_unique<std::vector<uint8_t>>();
396 std::unique_ptr<std::vector<uint8_t>>
399 auto encoded = std::make_unique<std::vector<uint8_t>>();
410 std::unique_ptr<std::vector<uint8_t>>
412 const std::string& error_code,
413 const std::string& error_message,
415 auto encoded = std::make_unique<std::vector<uint8_t>>();
419 if (error_message.empty()) {
425 serializer_->
WriteValue(*error_details, &stream);
433 const uint8_t* response,
434 size_t response_size,
452 const std::string& message_string =
453 message.
IsNull() ?
"" : std::get<std::string>(message);
455 result->
Error(std::get<std::string>(code), message_string);
457 result->
Error(std::get<std::string>(code), message_string, details);