vkmEngine 1.0.0
A C++ game engine · vkmengine.com
Loading...
Searching...
No Matches
connection.h
1#pragma once
2
3#include <cstddef>
4#include <cstdint>
5#include <vector>
6
7#include "net/wire/protocol.h"
8#include "platform/net/net_address.h"
9#include "platform/net/udp_socket.h"
10
11namespace Vkm::Engine {
12
22bool isNewerSequence(uint16_t a, uint16_t b);
23
34class NetConnection {
35 public:
40 static constexpr size_t HEADER_BYTES = 8;
41
44
46 static constexpr float TIMEOUT_SECONDS = 5.0f;
47
53 static constexpr uint16_t NO_SEQUENCE = 0;
54
55 public:
56 NetConnection() = default;
57 ~NetConnection() = default;
58
59 NetConnection(const NetConnection& other) = delete;
60 NetConnection& operator=(const NetConnection& other) = delete;
61
62 NetConnection(NetConnection && other) = delete;
63 NetConnection& operator=(NetConnection && other) = delete;
64
65 public:
67 void open(const NetAddress& peer);
68
78 void frame(const uint8_t* payload, size_t size, std::vector<uint8_t>& out);
79
90 bool accept(
91 const uint8_t* bytes,
92 size_t size,
93 std::vector<uint8_t>& payload,
94 std::vector<uint16_t>& acknowledged
95 );
96
104 void refuse();
105
107 void advance(float seconds);
108
109 bool timedOut() const { return m_silent > TIMEOUT_SECONDS; }
110
118 uint16_t nextSequence() const { return m_outgoing; }
119
125 float roundTrip() const { return m_roundTrip; }
126
134 float lossFraction() const {
135 const uint64_t seen = m_arrived + m_missed;
136 return seen == 0 ? 0.0f : static_cast<float>(m_missed) / static_cast<float>(seen);
137 }
138
140 float roundTripVariation() const { return m_variation; }
141
142 const NetAddress& peer() const { return m_peer; }
143
144 private:
148 struct Sent {
149 uint16_t sequence = 0;
150 float age = 0.0f;
151 };
152
153 private:
154 void recordTrip(uint16_t acknowledged, std::vector<uint16_t>& reported);
155
156 private:
157 NetAddress m_peer;
158
159 uint16_t m_outgoing = NO_SEQUENCE + 1;
160 uint16_t m_newest = 0;
161 uint32_t m_seen = 0;
162 bool m_heard = false;
163
164 uint16_t m_wasNewest = 0;
165 uint32_t m_wasSeen = 0;
166 bool m_wasHeard = false;
167
168 float m_silent = 0.0f;
169
170 uint64_t m_arrived = 0;
171 uint64_t m_missed = 0;
172
173 float m_roundTrip = 0.0f;
174 float m_variation = 0.0f;
175
176 std::vector<Sent> m_sent;
177};
178
179} // namespace Vkm::Engine
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