|
Aegis2D
Modular C++26 2D game engine for networked games
|
Sequences, acknowledgements and reliable events of the datagrams exchanged with one peer. More...
#include <aegis2d/net/reliability.hpp>
Classes | |
| struct | Stats |
| Counters since the channel was created. More... | |
Public Types | |
| using | Clock = std::chrono::steady_clock |
| Clock of the resend timeouts. | |
Public Member Functions | |
| ReliableChannel (ReliabilitySettings settings={}) | |
| Creates a channel; its first datagram has sequence 0. | |
| PacketHeader | next_header (PacketType type, std::uint32_t session, std::optional< Clock::time_point > now={}) noexcept |
| Header of the next datagram sent to the peer. | |
| std::optional< std::uint16_t > | send (std::span< const std::byte > event) |
| Queues an event, to be sent by write_events() until it is acknowledged. | |
| bool | has_due_events (Clock::time_point now) const noexcept |
| Tells whether write_events() would write a datagram. | |
| std::size_t | write_events (BinaryWriter &writer, std::uint32_t session, Clock::time_point now) |
Writes an Event datagram carrying the events due, if any. | |
| bool | receive (const PacketHeader &header, std::optional< Clock::time_point > now={}) noexcept |
| Records a valid datagram received from the peer. | |
| bool | read_events (const PacketHeader &header, BinaryReader &payload, std::vector< std::vector< std::byte > > &events, std::optional< Clock::time_point > now={}) |
Reads the payload of an Event datagram and hands over the new events. | |
| std::size_t | pending () const noexcept |
| Number of events sent and not acknowledged yet. | |
| const AckTracker & | acks () const noexcept |
| Datagrams received from the peer. | |
| const Stats & | stats () const noexcept |
| Counters since the channel was created. | |
| const RttEstimator & | rtt () const noexcept |
| Round trip time to the peer. | |
Sequences, acknowledgements and reliable events of the datagrams exchanged with one peer.
One per peer: a client has one for the server, the server one per session. Every datagram sent to the peer takes its header from next_header() or write_events(), so its sequence follows the previous one and it acknowledges the peer's datagrams; every valid datagram received from it goes through receive() or read_events(), which record its sequence and the acknowledgements it carries.
Events are byte strings that must arrive (spawns, deaths...): send() queues one, and write_events() puts the events due in an Event datagram, oldest first, those never sent and those sent more than ReliabilitySettings::resend_delay ago without being acknowledged. An event is delivered once the peer acknowledges a datagram that carried it. On the other side, read_events() hands each event over once, however many copies arrive. Events may be handed over in another order than they were sent.
Given the time each datagram is sent and received, the channel also estimates the round trip time to the peer, see rtt().
Not thread-safe: use it from the game thread only.
| using aegis2d::net::ReliableChannel::Clock = std::chrono::steady_clock |
Clock of the resend timeouts.
|
explicit |
Creates a channel; its first datagram has sequence 0.
| settings | Resend delay. |
|
inlinenoexcept |
Datagrams received from the peer.
|
noexcept |
Tells whether write_events() would write a datagram.
| now | Current time. |
true if an event was never sent, or was sent at least resend_delay before now and not acknowledged since.
|
noexcept |
Header of the next datagram sent to the peer.
Takes the next sequence, so every datagram sent must use its own header, and fills ack and ack_bits with the datagrams received so far. For Event datagrams, use write_events(), which records which events the datagram carries.
| type | Type of the datagram. |
| session | Session token. |
| now | Time the datagram is sent, for rtt(); without it, the datagram gives no round trip sample. |
|
inlinenoexcept |
Number of events sent and not acknowledged yet.
| bool aegis2d::net::ReliableChannel::read_events | ( | const PacketHeader & | header, |
| BinaryReader & | payload, | ||
| std::vector< std::vector< std::byte > > & | events, | ||
| std::optional< Clock::time_point > | now = {} |
||
| ) |
Reads the payload of an Event datagram and hands over the new events.
The payload is checked whole first: if it is malformed, nothing changes and the datagram must be dropped. Otherwise the datagram is recorded as by receive(), and the events not received before are appended to events.
| header | Header of the datagram, from read_header(), of type Event. |
| payload | Reader at the payload, left at its end. |
| events | New events are appended to it; the events seen before are not. |
| now | Time the datagram was received, as for receive(). |
false if the payload is malformed (no event, an event past its end, or bytes left after the last event).
|
noexcept |
Records a valid datagram received from the peer.
Records its sequence for the next acknowledgements, and marks as delivered the events of the datagrams it acknowledges. Call it once the payload was read without error, for every type but Event (read_events() calls it).
| header | Header of the datagram, from read_header(). |
| now | Time the datagram was received, for rtt(); without it, its acknowledgement gives no round trip sample. |
true for a datagram not seen before; false for a duplicate or a datagram too old to be acknowledged, whose payload should be ignored.
|
inlinenoexcept |
Round trip time to the peer.
A sample is taken when a datagram received with a time acknowledges, in its ack field, a datagram sent with a time and not acknowledged before: the time between the two. Sequences acknowledged only through ack_bits give no sample, as the datagram that acknowledged them first may have been lost. A sample includes the time the peer waited before sending its next datagram, at most its sending period.
| std::optional< std::uint16_t > aegis2d::net::ReliableChannel::send | ( | std::span< const std::byte > | event | ) |
Queues an event, to be sent by write_events() until it is acknowledged.
| event | Bytes of the event, copied; the payload of section 4 of the RFC. |
| std::length_error | if event is larger than max_event_size. |
|
inlinenoexcept |
Counters since the channel was created.
| std::size_t aegis2d::net::ReliableChannel::write_events | ( | BinaryWriter & | writer, |
| std::uint32_t | session, | ||
| Clock::time_point | now | ||
| ) |
Writes an Event datagram carrying the events due, if any.
Writes the header (from next_header()), then the events due, oldest first, as many as fit in max_datagram_size. Nothing is written when no event is due.
| writer | Writer of the datagram, normally empty; send its data() to the peer. |
| session | Session token. |
| now | Current time: the events written are due again at now + resend_delay. |