vkmEngine 1.0.15
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
50 static constexpr float MAX_SILENCE_A_FRAME = 0.25f;
51
57 static constexpr uint16_t NO_SEQUENCE = 0;
58
59 public:
60 NetConnection() = default;
61 ~NetConnection() = default;
62
63 NetConnection(const NetConnection& other) = delete;
64 NetConnection& operator=(const NetConnection& other) = delete;
65
66 NetConnection(NetConnection && other) = delete;
67 NetConnection& operator=(NetConnection && other) = delete;
68
69 public:
71 void open(const NetAddress& peer);
72
82 void frame(const uint8_t* payload, size_t size, std::vector<uint8_t>& out);
83
94 bool accept(
95 const uint8_t* bytes,
96 size_t size,
97 std::vector<uint8_t>& payload,
98 std::vector<uint16_t>& acknowledged
99 );
100
108 void refuse();
109
112 void advance(float seconds);
113
114 bool timedOut() const { return m_silent > TIMEOUT_SECONDS; }
115
123 uint16_t nextSequence() const { return m_outgoing; }
124
130 float roundTrip() const { return m_roundTrip; }
131
139 float lossFraction() const {
140 const uint64_t seen = m_arrived + m_missed;
141 return seen == 0 ? 0.0f : static_cast<float>(m_missed) / static_cast<float>(seen);
142 }
143
145 float roundTripVariation() const { return m_variation; }
146
147 const NetAddress& peer() const { return m_peer; }
148
149 private:
153 struct Sent {
154 uint16_t sequence = 0;
155 float age = 0.0f;
156 };
157
158 private:
159 void recordTrip(uint16_t acknowledged, std::vector<uint16_t>& reported);
160
161 private:
162 NetAddress m_peer;
163
164 uint16_t m_outgoing = NO_SEQUENCE + 1;
165 uint16_t m_newest = 0;
166 uint32_t m_seen = 0;
167 bool m_heard = false;
168
169 uint16_t m_wasNewest = 0;
170 uint32_t m_wasSeen = 0;
171 bool m_wasHeard = false;
172
173 float m_silent = 0.0f;
174
175 uint64_t m_arrived = 0;
176 uint64_t m_missed = 0;
177
178 float m_roundTrip = 0.0f;
179 float m_variation = 0.0f;
180
181 std::vector<Sent> m_sent;
182};
183
184} // namespace Vkm::Engine
static constexpr uint16_t NO_SEQUENCE
The sequence number nothing ever sends, so a header can say "nothing".
Definition connection.h:57
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:123
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.
static constexpr float MAX_SILENCE_A_FRAME
Definition connection.h:50
float roundTrip() const
Round trip, smoothed.
Definition connection.h:130
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:139
void advance(float seconds)
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:145
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