Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
reliability.hpp
Go to the documentation of this file.
1
7#pragma once
9#include <aegis2d/net/export.hpp>
11#include <aegis2d/net/rtt.hpp>
12
13#include <array>
14#include <bitset>
15#include <chrono>
16#include <cstddef>
17#include <cstdint>
18#include <deque>
19#include <optional>
20#include <span>
21#include <vector>
22
23namespace aegis2d::net {
24
28inline constexpr std::size_t max_events_in_flight = 256;
29
33inline constexpr std::size_t max_event_size = PacketHeader::max_payload_size - 1 - 2 - 2;
34
40class AEGIS2D_NET_EXPORT AckTracker {
41public:
49 bool record(std::uint16_t sequence) noexcept;
50
53 [[nodiscard]] std::uint16_t ack() const noexcept { return ack_; }
54
57 [[nodiscard]] std::uint32_t ack_bits() const noexcept { return bits_; }
58
61 [[nodiscard]] bool received_any() const noexcept { return received_any_; }
62
63private:
64 std::uint16_t ack_ = 65535;
65 std::uint32_t bits_ = 0;
66 bool received_any_ = false;
67};
68
74 std::chrono::milliseconds resend_delay{100};
75};
76
96class AEGIS2D_NET_EXPORT ReliableChannel {
97public:
99 using Clock = std::chrono::steady_clock;
100
102 struct Stats {
104 std::uint64_t queued = 0;
106 std::uint64_t transmissions = 0;
108 std::uint64_t resends = 0;
110 std::uint64_t acknowledged = 0;
112 std::uint64_t received = 0;
114 std::uint64_t duplicates = 0;
115 };
116
119 explicit ReliableChannel(ReliabilitySettings settings = {});
120
130 [[nodiscard]] PacketHeader next_header(PacketType type, std::uint32_t session,
131 std::optional<Clock::time_point> now = {}) noexcept;
132
138 [[nodiscard]] std::optional<std::uint16_t> send(std::span<const std::byte> event);
139
144 [[nodiscard]] bool has_due_events(Clock::time_point now) const noexcept;
145
153 std::size_t write_events(BinaryWriter& writer, std::uint32_t session, Clock::time_point now);
154
164 bool receive(const PacketHeader& header, std::optional<Clock::time_point> now = {}) noexcept;
165
176 bool read_events(const PacketHeader& header, BinaryReader& payload,
177 std::vector<std::vector<std::byte>>& events,
178 std::optional<Clock::time_point> now = {});
179
182 [[nodiscard]] std::size_t pending() const noexcept { return pending_.size(); }
183
186 [[nodiscard]] const AckTracker& acks() const noexcept { return acks_; }
187
190 [[nodiscard]] const Stats& stats() const noexcept { return stats_; }
191
200 [[nodiscard]] const RttEstimator& rtt() const noexcept { return rtt_; }
201
202private:
203 struct PendingEvent {
204 std::uint16_t id = 0;
205 std::vector<std::byte> data;
206 std::optional<Clock::time_point> last_sent;
207 bool acknowledged = false;
208 };
209 struct SentDatagram {
210 std::uint16_t sequence = 0;
211 bool waiting = false;
212 std::optional<Clock::time_point> sent_at;
213 std::vector<std::uint16_t> events;
214 };
215 static constexpr std::size_t sent_history = 256;
216
217 [[nodiscard]] bool due(const PendingEvent& event, Clock::time_point now) const noexcept;
218 void acknowledge(std::uint16_t sequence) noexcept;
219 bool first_receipt(std::uint16_t id) noexcept;
220
221 ReliabilitySettings settings_;
222 std::uint16_t next_sequence_ = 0;
223 std::uint16_t next_event_ = 0;
224 bool sent_any_ = false;
225 AckTracker acks_;
226 std::deque<PendingEvent> pending_;
227 std::array<SentDatagram, sent_history> sent_{};
228 // Event ids received: bit n is newest_event_ - n.
229 std::bitset<max_events_in_flight> seen_events_;
230 std::uint16_t newest_event_ = 0;
231 bool received_event_ = false;
232 Stats stats_;
233 RttEstimator rtt_;
234};
235
236} // namespace aegis2d::net
Little-endian binary serialization with bounds-checked reads, for network packets.
Receiver side of the acknowledgements: which datagrams of the peer arrived.
Definition reliability.hpp:40
bool record(std::uint16_t sequence) noexcept
Records the sequence of a datagram received from the peer.
bool received_any() const noexcept
Tells whether any datagram was recorded.
Definition reliability.hpp:61
std::uint16_t ack() const noexcept
The ack field to send.
Definition reliability.hpp:53
std::uint32_t ack_bits() const noexcept
The ack_bits field to send.
Definition reliability.hpp:57
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
Sequences, acknowledgements and reliable events of the datagrams exchanged with one peer.
Definition reliability.hpp:96
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.
bool has_due_events(Clock::time_point now) const noexcept
Tells whether write_events() would write a datagram.
ReliableChannel(ReliabilitySettings settings={})
Creates a channel; its first datagram has sequence 0.
bool receive(const PacketHeader &header, std::optional< Clock::time_point > now={}) noexcept
Records a valid datagram received from the peer.
const Stats & stats() const noexcept
Counters since the channel was created.
Definition reliability.hpp:190
const AckTracker & acks() const noexcept
Datagrams received from the peer.
Definition reliability.hpp:186
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.
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.
std::chrono::steady_clock Clock
Clock of the resend timeouts.
Definition reliability.hpp:99
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.
const RttEstimator & rtt() const noexcept
Round trip time to the peer.
Definition reliability.hpp:200
std::size_t pending() const noexcept
Number of events sent and not acknowledged yet.
Definition reliability.hpp:182
Smoothed round trip time and its variation, from round trip samples.
Definition rtt.hpp:21
Binary serialization, UDP/TCP sockets and the network thread.
Definition binary.hpp:14
constexpr std::size_t max_events_in_flight
Largest number of events sent and not yet acknowledged at once.
Definition reliability.hpp:28
PacketType
What a datagram carries; stored on one byte after the version.
Definition packet.hpp:22
constexpr std::size_t max_event_size
Largest event, in bytes: one alone fills an Event datagram.
Definition reliability.hpp:33
Header carried by every UDP datagram, and the packet types.
Round trip time of a session, smoothed like TCP's, with its jitter.
Header at the start of every UDP datagram.
Definition packet.hpp:52
static constexpr std::size_t max_payload_size
Largest payload after the header.
Definition packet.hpp:60
Settings of a ReliableChannel.
Definition reliability.hpp:70
std::chrono::milliseconds resend_delay
Time after which an event sent and not acknowledged is sent again.
Definition reliability.hpp:74
Counters since the channel was created.
Definition reliability.hpp:102