Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
aegis2d::net::ReliableChannel Class Reference

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.
 

Detailed Description

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.

Member Typedef Documentation

◆ Clock

using aegis2d::net::ReliableChannel::Clock = std::chrono::steady_clock

Clock of the resend timeouts.

Constructor & Destructor Documentation

◆ ReliableChannel()

aegis2d::net::ReliableChannel::ReliableChannel ( ReliabilitySettings  settings = {})
explicit

Creates a channel; its first datagram has sequence 0.

Parameters
settingsResend delay.

Member Function Documentation

◆ acks()

const AckTracker & aegis2d::net::ReliableChannel::acks ( ) const
inlinenoexcept

Datagrams received from the peer.

Returns
The receiver side of the acknowledgements.

◆ has_due_events()

bool aegis2d::net::ReliableChannel::has_due_events ( Clock::time_point  now) const
noexcept

Tells whether write_events() would write a datagram.

Parameters
nowCurrent time.
Returns
true if an event was never sent, or was sent at least resend_delay before now and not acknowledged since.

◆ next_header()

PacketHeader aegis2d::net::ReliableChannel::next_header ( PacketType  type,
std::uint32_t  session,
std::optional< Clock::time_point >  now = {} 
)
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.

Parameters
typeType of the datagram.
sessionSession token.
nowTime the datagram is sent, for rtt(); without it, the datagram gives no round trip sample.
Returns
The header, to write with write_header().

◆ pending()

std::size_t aegis2d::net::ReliableChannel::pending ( ) const
inlinenoexcept

Number of events sent and not acknowledged yet.

Returns
Between 0 and max_events_in_flight.

◆ read_events()

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.

Parameters
headerHeader of the datagram, from read_header(), of type Event.
payloadReader at the payload, left at its end.
eventsNew events are appended to it; the events seen before are not.
nowTime the datagram was received, as for receive().
Returns
false if the payload is malformed (no event, an event past its end, or bytes left after the last event).

◆ receive()

bool aegis2d::net::ReliableChannel::receive ( const PacketHeader &  header,
std::optional< Clock::time_point >  now = {} 
)
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).

Parameters
headerHeader of the datagram, from read_header().
nowTime the datagram was received, for rtt(); without it, its acknowledgement gives no round trip sample.
Returns
true for a datagram not seen before; false for a duplicate or a datagram too old to be acknowledged, whose payload should be ignored.

◆ rtt()

const RttEstimator & aegis2d::net::ReliableChannel::rtt ( ) const
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.

Returns
The estimate, without samples until the peer acknowledges a datagram sent with a time.

◆ send()

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.

Parameters
eventBytes of the event, copied; the payload of section 4 of the RFC.
Returns
The id of the event, or nothing if max_events_in_flight events are already waiting for their acknowledgement: the peer stopped acknowledging, it is gone.
Exceptions
std::length_errorif event is larger than max_event_size.

◆ stats()

const Stats & aegis2d::net::ReliableChannel::stats ( ) const
inlinenoexcept

Counters since the channel was created.

Returns
The counters.

◆ write_events()

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.

Parameters
writerWriter of the datagram, normally empty; send its data() to the peer.
sessionSession token.
nowCurrent time: the events written are due again at now + resend_delay.
Returns
The number of events written, 0 if nothing was written.

The documentation for this class was generated from the following file: