-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathResponse.cpp
More file actions
31 lines (25 loc) · 866 Bytes
/
Response.cpp
File metadata and controls
31 lines (25 loc) · 866 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//
// Created by Miroslav Baudys on 05/03/2018.
//
#include "Response.h"
Response::Response(const nlohmann::json &json) {
/*
* 4 bytes data size (little-endian)
* xx bytes data
*/
const auto response = json.dump();
const auto size = static_cast<HeaderSize>(response.size());
m_data.reserve(sizeof(size) + size);
// length prefix in fixed little-endian byte order
for (size_t i = 0; i < sizeof(size); ++i) {
m_data.push_back(static_cast<uint8_t>(size >> (8 * i) & 0xFF));
}
m_data.insert(m_data.end(), response.begin(), response.end());
}
std::string_view Response::data_repr() const noexcept {
constexpr auto headerSize = sizeof(HeaderSize);
if (m_data.size() < headerSize) {
return {};
}
return {reinterpret_cast<const char *>(&m_data[headerSize]), m_data.size() - headerSize};
}