9#include <aegis2d/net/export.hpp>
49 bool record(std::uint16_t sequence)
noexcept;
53 [[nodiscard]] std::uint16_t
ack() const noexcept {
return ack_; }
57 [[nodiscard]] std::uint32_t
ack_bits() const noexcept {
return bits_; }
61 [[nodiscard]]
bool received_any() const noexcept {
return received_any_; }
64 std::uint16_t ack_ = 65535;
65 std::uint32_t bits_ = 0;
66 bool received_any_ =
false;
99 using Clock = std::chrono::steady_clock;
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;
131 std::optional<Clock::time_point> now = {})
noexcept;
138 [[nodiscard]] std::optional<std::uint16_t>
send(std::span<const std::byte> event);
177 std::vector<std::vector<std::byte>>& events,
178 std::optional<Clock::time_point> now = {});
182 [[nodiscard]] std::size_t
pending() const noexcept {
return pending_.size(); }
190 [[nodiscard]]
const Stats&
stats() const noexcept {
return stats_; }
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;
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;
215 static constexpr std::size_t sent_history = 256;
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;
221 ReliabilitySettings settings_;
222 std::uint16_t next_sequence_ = 0;
223 std::uint16_t next_event_ = 0;
224 bool sent_any_ =
false;
226 std::deque<PendingEvent> pending_;
227 std::array<SentDatagram, sent_history> sent_{};
229 std::bitset<max_events_in_flight> seen_events_;
230 std::uint16_t newest_event_ = 0;
231 bool received_event_ =
false;
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.
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