Files
stoneheart_at_home/app/lan_wire.cpp
faumaray 083b84e11a small refactor and void library
Signed-off-by: Aleksei (Faumaray) Selikhov <contact@faumaray.com>
2026-12-02 19:16:31 +03:00

83 lines
3.6 KiB
C++

#include "lan_wire.hpp"
#include <Void/Binary/Reader.hpp>
#include <Void/Binary/Writer.hpp>
#include <span>
#include <stdexcept>
namespace settlement::viewer::lan_wire {
namespace {
template<std::size_t Size, class... Fields>
std::array<char, Size> encode(Fields... fields) {
static_assert((sizeof(Fields) + ...) == Size);
std::array<char, Size> payload{};
Void::Binary::Writer writer(std::as_writable_bytes(std::span{payload}));
(writer.writeLittle(fields).value(), ...);
return payload;
}
} // namespace
void append_frame(std::string& output, Message message, std::string_view payload) {
if (payload.size() >= frame_limit) throw std::runtime_error("The colony snapshot is too large for LAN play.");
const auto length = static_cast<std::uint32_t>(payload.size() + 1);
const auto header = encode<frame_header_size>(length, static_cast<std::uint8_t>(message));
output.reserve(output.size() + header.size() + payload.size());
output.append(header.data(), header.size());
output.append(payload);
}
std::optional<Frame> next_frame(std::string_view input) {
if (input.size() < sizeof(std::uint32_t)) return std::nullopt;
Void::Binary::Reader reader(std::as_bytes(std::span{input}));
const auto length = reader.readLittle<std::uint32_t>().value();
if (length == 0 || length > frame_limit) throw std::runtime_error("Invalid LAN message size.");
if (reader.remaining() < length) return std::nullopt;
const auto message = static_cast<Message>(reader.readLittle<std::uint8_t>().value());
const auto payload = input.substr(reader.position(), length - 1);
return Frame{message, payload, reader.position() + payload.size()};
}
std::array<char, 12> encode_hello(Hello hello) {
return encode<12>(hello.version, hello.key);
}
std::optional<Hello> decode_hello(std::string_view payload) {
if (payload.size() != 12) return std::nullopt;
Void::Binary::Reader reader(std::as_bytes(std::span{payload}));
return Hello{reader.readLittle<std::uint32_t>().value(), reader.readLittle<std::uint64_t>().value()};
}
std::array<char, 34> encode_command(const PlayerCommand& command) {
return encode<34>(command.sequence, static_cast<std::uint8_t>(command.kind), command.cell.x, command.cell.y,
command.value, command.destination.x, command.destination.y, command.resident);
}
std::optional<PlayerCommand> decode_command(std::string_view payload) {
if (payload.size() != 34) return std::nullopt;
Void::Binary::Reader reader(std::as_bytes(std::span{payload}));
return PlayerCommand{guest_player,
reader.readLittle<std::uint64_t>().value(),
static_cast<CommandKind>(reader.readLittle<std::uint8_t>().value()),
{reader.readLittle<std::int32_t>().value(), reader.readLittle<std::int32_t>().value()},
reader.readLittle<std::uint8_t>().value(),
{reader.readLittle<std::int32_t>().value(), reader.readLittle<std::int32_t>().value()},
reader.readLittle<std::uint64_t>().value()};
}
std::array<char, 9> encode_receipt(Receipt receipt) {
return encode<9>(static_cast<std::uint8_t>(receipt.status), receipt.next_sequence);
}
std::optional<Receipt> decode_receipt(std::string_view payload) {
if (payload.size() != 9) return std::nullopt;
Void::Binary::Reader reader(std::as_bytes(std::span{payload}));
return Receipt{static_cast<CommandStatus>(reader.readLittle<std::uint8_t>().value()),
reader.readLittle<std::uint64_t>().value()};
}
} // namespace settlement::viewer::lan_wire