7#include "net/wire/protocol.h"
8#include "platform/net/net_address.h"
9#include "platform/net/udp_socket.h"
11namespace Vkm::Engine {
22bool isNewerSequence(uint16_t a, uint16_t b);
56 NetConnection() =
default;
57 ~NetConnection() =
default;
59 NetConnection(
const NetConnection& other) =
delete;
60 NetConnection& operator=(
const NetConnection& other) =
delete;
62 NetConnection(NetConnection && other) =
delete;
63 NetConnection& operator=(NetConnection && other) =
delete;
78 void frame(
const uint8_t* payload,
size_t size, std::vector<uint8_t>& out);
93 std::vector<uint8_t>& payload,
94 std::vector<uint16_t>& acknowledged
135 const uint64_t seen = m_arrived + m_missed;
136 return seen == 0 ? 0.0f :
static_cast<float>(m_missed) /
static_cast<float>(seen);
142 const NetAddress& peer()
const {
return m_peer; }
149 uint16_t sequence = 0;
154 void recordTrip(uint16_t acknowledged, std::vector<uint16_t>& reported);
160 uint16_t m_newest = 0;
162 bool m_heard =
false;
164 uint16_t m_wasNewest = 0;
165 uint32_t m_wasSeen = 0;
166 bool m_wasHeard =
false;
168 float m_silent = 0.0f;
170 uint64_t m_arrived = 0;
171 uint64_t m_missed = 0;
173 float m_roundTrip = 0.0f;
174 float m_variation = 0.0f;
176 std::vector<Sent> m_sent;
static constexpr uint16_t NO_SEQUENCE
The sequence number nothing ever sends, so a header can say "nothing".
Definition connection.h:53
static constexpr size_t HEADER_BYTES
Bytes of header on every packet: sequence, acknowledgement, and the bitfield of the thirty-two before...
Definition connection.h:40
uint16_t nextSequence() const
What frame() will stamp on the next packet.
Definition connection.h:118
void frame(const uint8_t *payload, size_t size, std::vector< uint8_t > &out)
Wrap payload in a header and hand it back for sending.
bool accept(const uint8_t *bytes, size_t size, std::vector< uint8_t > &payload, std::vector< uint16_t > &acknowledged)
Take a datagram that arrived from this peer.
float roundTrip() const
Round trip, smoothed.
Definition connection.h:125
void refuse()
Take back the acknowledgement for the packet just accepted.
float lossFraction() const
What fraction of the peer's packets never arrived, so far.
Definition connection.h:134
void advance(float seconds)
Age the silence timer and every unacknowledged packet by seconds of wall clock.
static constexpr float TIMEOUT_SECONDS
How long silence lasts before a peer is given up on.
Definition connection.h:46
static constexpr size_t MAX_PAYLOAD
The most one packet carries past its header: what a datagram leaves.
Definition connection.h:43
void open(const NetAddress &peer)
Point this at peer. Resets everything the last one left.
float roundTripVariation() const
How much that trip varies, smoothed the same way.
Definition connection.h:140
static constexpr size_t MAX_DATAGRAM
The largest datagram this will send.
Definition udp_socket.h:26
Where a datagram came from, or is going to: an IPv4 host and a port.
Definition net_address.h:13