aboutsummaryrefslogtreecommitdiff
path: root/src/shared/net/packet.cc
blob: 230722dbe1b0e0d7cdf9a5b5b303492224a5c314 (plain)
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
#include "shared/net/packet.hh"

namespace shared {
namespace net {

upacket::upacket(const proto::packet& proto) {
    this->data = proto.SerializeAsString();
    this->data = shared::compress_string(this->data);
}

rpacket::rpacket(const proto::packet& proto) {
    const std::string serialised = [&]() {
        std::string ret = proto.SerializeAsString();
        return shared::compress_string(ret);
    }();

    const std::string header = [&]() {
        std::string ret(sizeof(packet_header_t), '\0');
        const packet_header_t size = htonl(static_cast<std::uint32_t>(
            std::size(serialised) + sizeof(packet_header_t)));
        std::memcpy(std::data(ret), &size, sizeof(size));
        return ret;
    }();

    this->data = std::move(header) + std::move(serialised);
}

} // namespace net
} // namespace shared