Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
packet.hpp
Go to the documentation of this file.
1
4#pragma once
6#include <aegis2d/net/export.hpp>
7
8#include <cstddef>
9#include <cstdint>
10#include <string_view>
11#include <variant>
12
13namespace aegis2d::net {
14
17inline constexpr std::size_t max_datagram_size = 1200;
18
22enum class PacketType : std::uint8_t {
24 Connect = 1,
26 Accept = 2,
28 Input = 3,
30 Snapshot = 4,
33 Event = 5,
36 Heartbeat = 6,
38 Disconnect = 7,
39};
40
44AEGIS2D_NET_EXPORT std::string_view to_string(PacketType type) noexcept;
45
54 static constexpr std::uint32_t magic = 0x50595452;
56 static constexpr std::uint8_t version = 1;
58 static constexpr std::size_t size = 18;
60 static constexpr std::size_t max_payload_size = max_datagram_size - size;
62 static constexpr std::uint8_t compressed_flag = 0x80;
63
68 std::uint16_t sequence = 0;
70 std::uint16_t ack = 0;
72 std::uint32_t ack_bits = 0;
74 std::uint32_t session = 0;
76 bool compressed = false;
77
80 bool operator==(const PacketHeader&) const = default;
81};
82
84enum class HeaderError : std::uint8_t {
93};
94
98AEGIS2D_NET_EXPORT std::string_view to_string(HeaderError error) noexcept;
99
103AEGIS2D_NET_EXPORT void write_header(BinaryWriter& writer, const PacketHeader& header);
104
111[[nodiscard]] AEGIS2D_NET_EXPORT std::variant<PacketHeader, HeaderError>
112read_header(BinaryReader& reader) noexcept;
113
114} // namespace aegis2d::net
Little-endian binary serialization with bounds-checked reads, for network packets.
Reads values written by BinaryWriter from untrusted bytes, never past their end.
Definition binary.hpp:88
Writes values field by field into a growing byte buffer.
Definition binary.hpp:21
Binary serialization, UDP/TCP sockets and the network thread.
Definition binary.hpp:14
std::variant< PacketHeader, HeaderError > read_header(BinaryReader &reader) noexcept
Reads and checks the header at the start of a datagram.
PacketType
What a datagram carries; stored on one byte after the version.
Definition packet.hpp:22
@ Input
Client to server: the player's input state for one tick.
@ Disconnect
Both ways: the sender leaves the session.
@ Connect
Client to server: binds the client's UDP endpoint to its session.
@ Heartbeat
Both ways: keeps the session alive and carries acknowledgements when there is nothing else to send.
@ Event
Both ways: gameplay events that must arrive (spawn, death...), resent until acknowledged.
@ Accept
Server to client: the endpoint is bound, gameplay traffic may start.
@ Snapshot
Server to client: the state of the world.
void write_header(BinaryWriter &writer, const PacketHeader &header)
Writes the 18-byte header, magic and version included.
std::string_view to_string(PacketType type) noexcept
Name of a packet type, for logs.
constexpr std::size_t max_datagram_size
Largest UDP datagram the protocol sends or accepts, header included.
Definition packet.hpp:17
HeaderError
Why read_header() rejected a datagram.
Definition packet.hpp:84
@ Truncated
Fewer than PacketHeader::size bytes.
@ BadVersion
Another protocol version.
@ UnknownType
The type byte, its compressed flag aside, is not a PacketType.
@ BadMagic
The first four bytes are not PacketHeader::magic: not our protocol.
Header at the start of every UDP datagram.
Definition packet.hpp:52
static constexpr std::uint8_t compressed_flag
Bit of the type byte set when the payload is compressed.
Definition packet.hpp:62
bool operator==(const PacketHeader &) const =default
Field-by-field equality.
static constexpr std::size_t size
Size of the header on the wire, in bytes.
Definition packet.hpp:58
std::uint32_t session
Session token issued by the TCP login; 0 is never issued.
Definition packet.hpp:74
bool compressed
Whether the payload is compressed (see aegis2d/net/compression.hpp).
Definition packet.hpp:76
static constexpr std::uint32_t magic
First four bytes of every datagram: 52 54 59 50, "RTYP" in ASCII.
Definition packet.hpp:54
static constexpr std::uint8_t version
Protocol version; peers with another version are not understood.
Definition packet.hpp:56
std::uint32_t ack_bits
Bit n set: sequence ack - 1 - n was received too.
Definition packet.hpp:72
std::uint16_t ack
Most recent sequence number received from the peer.
Definition packet.hpp:70
std::uint16_t sequence
Number of this datagram, incremented by the sender for each one and wrapping from 65535 to 0.
Definition packet.hpp:68
static constexpr std::size_t max_payload_size
Largest payload after the header.
Definition packet.hpp:60
PacketType type
What the datagram carries.
Definition packet.hpp:65