|
Aegis2D
Modular C++26 2D game engine for networked games
|
Binary serialization, UDP/TCP sockets and the network thread. More...
Classes | |
| class | AckTracker |
| Receiver side of the acknowledgements: which datagrams of the peer arrived. More... | |
| class | BinaryReader |
| Reads values written by BinaryWriter from untrusted bytes, never past their end. More... | |
| class | BinaryWriter |
| Writes values field by field into a growing byte buffer. More... | |
| class | BitReader |
| Reads values written by BitWriter from untrusted bytes, never past their end. More... | |
| class | BitWriter |
| Writes values of any width from 0 to 64 bits into a growing byte buffer. More... | |
| class | Endpoint |
| IPv4 or IPv6 address and port of a peer. More... | |
| class | NetworkContext |
| Runs the completion of network operations: received datagrams, finished sends. More... | |
| class | NetworkThread |
| Runs a NetworkContext on a thread of its own, until stopped. More... | |
| struct | PacketHeader |
| Header at the start of every UDP datagram. More... | |
| struct | ReliabilitySettings |
| Settings of a ReliableChannel. More... | |
| class | ReliableChannel |
| Sequences, acknowledgements and reliable events of the datagrams exchanged with one peer. More... | |
| class | RttEstimator |
| Smoothed round trip time and its variation, from round trip samples. More... | |
| class | SocketError |
| Thrown when a socket cannot be opened or bound. More... | |
| class | TcpConnection |
| TCP connection exchanging whole messages over the byte stream. More... | |
| class | TcpListener |
| Listening TCP socket handing every accepted peer over as a TcpConnection. More... | |
| struct | TrafficCount |
| A number of datagrams and the bytes they hold. More... | |
| class | TrafficCounters |
| Counts the datagrams exchanged with one peer, by direction and packet type. More... | |
| class | UdpSocket |
| UDP socket bound to a local endpoint, receiving and sending whole datagrams. More... | |
Enumerations | |
| enum class | PacketType : std::uint8_t { Connect = 1 , Accept = 2 , Input = 3 , Snapshot = 4 , Event = 5 , Heartbeat = 6 , Disconnect = 7 } |
| What a datagram carries; stored on one byte after the version. More... | |
| enum class | HeaderError : std::uint8_t { Truncated , BadMagic , BadVersion , UnknownType } |
| Why read_header() rejected a datagram. More... | |
| enum class | TcpCloseReason : std::uint8_t { PeerClosed , Error , MessageTooLarge } |
| Why a TcpConnection closed by itself. More... | |
| enum class | Direction : std::uint8_t { Sent = 0 , Received = 1 } |
| Way a datagram went. More... | |
Functions | |
| std::optional< std::vector< std::byte > > | compress_payload (std::span< const std::byte > payload) |
| Compresses a payload with LZ4. | |
| std::optional< std::vector< std::byte > > | decompress_payload (std::span< const std::byte > compressed) |
| Decompresses a payload written by compress_payload(). | |
| void | write_datagram (BinaryWriter &writer, PacketHeader header, std::span< const std::byte > payload, std::optional< std::size_t > threshold=std::nullopt) |
| Writes a datagram: its header, then its payload, compressed when that pays. | |
| std::optional< std::vector< std::byte > > | read_payload (const PacketHeader &header, BinaryReader &reader) |
| Reads the payload of a datagram, decompressed if its header says it is compressed. | |
| std::string_view | name () noexcept |
| Name of the module, used to check that it is linked. | |
| std::string_view | to_string (PacketType type) noexcept |
| Name of a packet type, for logs. | |
| std::string_view | to_string (HeaderError error) noexcept |
| Short description of a header error, for logs. | |
| void | write_header (BinaryWriter &writer, const PacketHeader &header) |
| Writes the 18-byte header, magic and version included. | |
| std::variant< PacketHeader, HeaderError > | read_header (BinaryReader &reader) noexcept |
| Reads and checks the header at the start of a datagram. | |
| constexpr bool | sequence_greater_than (std::uint16_t a, std::uint16_t b) noexcept |
Whether sequence a was sent after sequence b, the counter having possibly wrapped in between. | |
| constexpr bool | sequence_less_than (std::uint16_t a, std::uint16_t b) noexcept |
Whether sequence a was sent before sequence b, the counter having possibly wrapped in between. | |
Variables | |
| constexpr std::size_t | default_compression_threshold = 128 |
| Default size from which write_datagram() tries to compress a payload, in bytes. | |
| constexpr std::size_t | max_datagram_size = 1200 |
| Largest UDP datagram the protocol sends or accepts, header included. | |
| constexpr std::size_t | max_events_in_flight = 256 |
| Largest number of events sent and not yet acknowledged at once. | |
| constexpr std::size_t | max_event_size = PacketHeader::max_payload_size - 1 - 2 - 2 |
Largest event, in bytes: one alone fills an Event datagram. | |
| constexpr std::uint16_t | sequence_half_range = 32768 |
| Half the range of a 16-bit sequence: the largest gap at which two sequences are still ordered is one less. | |
| constexpr std::size_t | max_message_size = 4096 |
| Largest message a TcpConnection sends or accepts, in bytes, prefix excluded. | |
| constexpr std::size_t | message_prefix_size = 2 |
Size of the length prefix in front of each message: a little-endian u16. | |
Binary serialization, UDP/TCP sockets and the network thread.
|
strong |
|
strong |
Why read_header() rejected a datagram.
| Enumerator | |
|---|---|
| Truncated | Fewer than PacketHeader::size bytes. |
| BadMagic | The first four bytes are not PacketHeader::magic: not our protocol. |
| BadVersion | Another protocol version. |
| UnknownType | The type byte, its compressed flag aside, is not a PacketType. |
|
strong |
What a datagram carries; stored on one byte after the version.
The values are part of the protocol: never renumber them, and keep them contiguous (read_header() checks the range).
|
strong |
Why a TcpConnection closed by itself.
| std::optional< std::vector< std::byte > > aegis2d::net::compress_payload | ( | std::span< const std::byte > | payload | ) |
Compresses a payload with LZ4.
| payload | Payload to compress, at most PacketHeader::max_payload_size bytes. |
payload (u16) then its LZ4 block, or std::nullopt if payload is empty, too large, or does not come out smaller. | std::optional< std::vector< std::byte > > aegis2d::net::decompress_payload | ( | std::span< const std::byte > | compressed | ) |
Decompresses a payload written by compress_payload().
| compressed | Bytes after the header of a datagram whose header is compressed. |
std::nullopt if compressed is malformed: shorter than 2 bytes, a size of 0 or above PacketHeader::max_payload_size, or an LZ4 block that is invalid or does not give exactly that size.
|
noexcept |
Name of the module, used to check that it is linked.
"net", a view of a static string.
|
noexcept |
Reads and checks the header at the start of a datagram.
Checks, in this order, that the 18 bytes are there, the magic, the version and the type. A rejected datagram must be dropped without reading further.
| reader | Reader at the start of the datagram; on success, it is left at the payload. |
| std::optional< std::vector< std::byte > > aegis2d::net::read_payload | ( | const PacketHeader & | header, |
| BinaryReader & | reader | ||
| ) |
Reads the payload of a datagram, decompressed if its header says it is compressed.
| header | Header of the datagram, from read_header(). |
| reader | Reader left at the payload by read_header(); on success, it is at the end. |
std::nullopt if it is compressed and malformed (see decompress_payload()); the reader then does not move.
|
constexprnoexcept |
Whether sequence a was sent after sequence b, the counter having possibly wrapped in between.
a is more recent when it is ahead of b by 1 to 32767 steps, counting from 65535 back to 0: 0 is more recent than 65535, and 10 than 40000. Two sequences exactly 32768 apart are not ordered: neither is more recent than the other.
| a | Sequence to test. |
| b | Sequence it is compared with. |
true if a is more recent than b; false if they are equal, if b is more recent, or if they are 32768 apart.
|
constexprnoexcept |
Whether sequence a was sent before sequence b, the counter having possibly wrapped in between.
| a | Sequence to test. |
| b | Sequence it is compared with. |
true if b is more recent than a, see sequence_greater_than().
|
noexcept |
Short description of a header error, for logs.
| error | Error to describe. |
"truncated header", "bad magic", "bad version" or "unknown type".
|
noexcept |
Name of a packet type, for logs.
| type | Type to name. |
"Connect", "Accept"... or "unknown" for a value outside the enum. | void aegis2d::net::write_datagram | ( | BinaryWriter & | writer, |
| PacketHeader | header, | ||
| std::span< const std::byte > | payload, | ||
| std::optional< std::size_t > | threshold = std::nullopt |
||
| ) |
Writes a datagram: its header, then its payload, compressed when that pays.
The payload is compressed when threshold is given, the payload holds at least that many bytes, and compress_payload() makes it smaller; the header written then has PacketHeader::compressed set, whatever header says.
| writer | Writer of the datagram, empty. |
| header | Header of the datagram. |
| payload | Payload of its type. |
| threshold | Size from which to try compressing; std::nullopt never compresses. |
| void aegis2d::net::write_header | ( | BinaryWriter & | writer, |
| const PacketHeader & | header | ||
| ) |
Writes the 18-byte header, magic and version included.
| writer | Writer of the datagram; the payload follows the header. |
| header | Fields to write. |
|
inlineconstexpr |
Default size from which write_datagram() tries to compress a payload, in bytes.
Below it, LZ4 rarely saves more than the 2 bytes of the size it adds.
|
inlineconstexpr |
Largest UDP datagram the protocol sends or accepts, header included.
Below common path MTUs, so datagrams are never fragmented by IP.
|
inlineconstexpr |
Largest event, in bytes: one alone fills an Event datagram.
The payload of a datagram minus the event count (u8) and the id and length of the event (u16 each).
|
inlineconstexpr |
Largest number of events sent and not yet acknowledged at once.
ReliableChannel::send() refuses more. Bounding them lets the receiver detect duplicates with a window of this many event ids (RFC section 5.3).
|
inlineconstexpr |
Largest message a TcpConnection sends or accepts, in bytes, prefix excluded.
TCP only carries the session (login), so messages stay small; a peer announcing a larger one is closed before anything is allocated.
|
inlineconstexpr |
Size of the length prefix in front of each message: a little-endian u16.
|
inlineconstexpr |
Half the range of a 16-bit sequence: the largest gap at which two sequences are still ordered is one less.