53 lines
1.6 KiB
C++
53 lines
1.6 KiB
C++
#pragma once
|
|
|
|
#include "settlement/core/world.hpp"
|
|
|
|
#include <array>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <optional>
|
|
#include <string>
|
|
#include <string_view>
|
|
|
|
namespace settlement::viewer::lan_wire {
|
|
|
|
inline constexpr std::uint32_t protocol_version = 11;
|
|
inline constexpr std::uint8_t guest_player = 2;
|
|
inline constexpr std::size_t frame_limit = 4 * 1024 * 1024;
|
|
inline constexpr std::size_t frame_header_size = sizeof(std::uint32_t) + sizeof(std::uint8_t);
|
|
|
|
enum class Message : std::uint8_t { Hello = 1, Snapshot, Command, Receipt, Reject, Ping };
|
|
|
|
struct Frame {
|
|
Message message;
|
|
std::string_view payload;
|
|
std::size_t size;
|
|
};
|
|
|
|
struct Hello {
|
|
std::uint32_t version;
|
|
std::uint64_t key;
|
|
};
|
|
|
|
struct Receipt {
|
|
CommandStatus status;
|
|
std::uint64_t next_sequence;
|
|
};
|
|
|
|
// The payload must be backed by storage separate from output.
|
|
void append_frame(std::string& output, Message message, std::string_view payload = {});
|
|
|
|
// A complete frame borrows its payload from input. Invalid lengths throw even before the body arrives.
|
|
[[nodiscard]] std::optional<Frame> next_frame(std::string_view input);
|
|
|
|
[[nodiscard]] std::array<char, 12> encode_hello(Hello hello);
|
|
[[nodiscard]] std::optional<Hello> decode_hello(std::string_view payload);
|
|
|
|
[[nodiscard]] std::array<char, 34> encode_command(const PlayerCommand& command);
|
|
[[nodiscard]] std::optional<PlayerCommand> decode_command(std::string_view payload);
|
|
|
|
[[nodiscard]] std::array<char, 9> encode_receipt(Receipt receipt);
|
|
[[nodiscard]] std::optional<Receipt> decode_receipt(std::string_view payload);
|
|
|
|
} // namespace settlement::viewer::lan_wire
|