group2 0.1.0
CSE 125 Group 2
Loading...
Searching...
No Matches
UdpSessionTransport.hpp
Go to the documentation of this file.
1
3
4#pragma once
5
7#include "UdpEndpoint.hpp"
9
10#include <SDL3/SDL_stdinc.h>
11
12#include <array>
13#include <cstdint>
14#include <deque>
15#include <map>
16#include <optional>
17#include <random>
18#include <string>
19#include <unordered_map>
20#include <vector>
21
22namespace net
23{
24
26{
27public:
28 enum class Mode
29 {
32 };
33
41
42 struct Event
43 {
45 std::uint64_t connectionId = 0;
47 std::vector<std::uint8_t> payload;
49 bool viaRelay = false;
50 };
51
52 struct Stats
53 {
54 std::uint64_t bytesSent = 0;
55 std::uint64_t bytesRecv = 0;
56 std::uint32_t packetsSent = 0;
57 std::uint32_t packetsRecv = 0;
58 std::uint32_t reliableRetransmits = 0;
59 std::uint32_t reliablePending = 0;
60 std::uint32_t activeRouteId = 0;
61 float rttMs = 0.0f;
62 bool relayActive = false;
63 bool directActive = false;
64 };
65
67 {
68 std::string host;
69 Uint16 port = 0;
70 std::uint32_t serverId = 0;
71 std::uint32_t clientNonce = 0;
73 bool enabled = false;
74 };
75
88 {
89 std::string directoryHost;
90 Uint16 directoryPort = 0;
91 std::vector<std::uint8_t> request;
92 bool enabled = false;
93 };
94
99
100 bool openServer(const char* bindAddr, Uint16 port);
101 bool connectClient(const char* host, Uint16 port, int timeoutMs);
102 void close();
103
104 void setRelayConfig(const RelayConfig& cfg);
105 void setPunchAssist(PunchAssist assist);
106 void preferRelay(bool enabled) { preferRelay_ = enabled; }
107
108 bool pollEvent(Event& out);
109 void pump();
110
111 bool send(std::uint64_t connectionId, ChannelId channel, const void* payload, int payloadLen, int redundancy = 1);
112 bool disconnect(std::uint64_t connectionId);
113
114 [[nodiscard]] bool isOpen() const noexcept { return endpoint_.isOpen(); }
115 [[nodiscard]] bool isClientConnected() const noexcept { return clientConnectionId_ != 0; }
116 [[nodiscard]] std::uint64_t clientConnectionId() const noexcept { return clientConnectionId_; }
117 [[nodiscard]] std::uint32_t clientNonce() const noexcept { return clientNonce_; }
119 [[nodiscard]] Uint16 localPort() const noexcept { return endpoint_.localPort(); }
120 [[nodiscard]] const Stats& stats() const noexcept { return stats_; }
121
123 bool sendDirectoryControl(const UdpEndpointAddr& dest, const void* payload, int payloadLen);
124
125private:
127 {
128 std::uint32_t sequence = 0;
130 std::vector<std::uint8_t> payload;
131 Uint64 lastSendMs = 0;
132 std::uint8_t attempts = 0;
133 };
134
136 {
137 std::uint32_t nextSequence = 1;
138 bool recvAny = false;
139 std::uint32_t recvHighest = 0;
140 std::uint32_t recvAckBits = 0;
141 bool orderedAny = false;
142 std::uint32_t orderedNext = 0;
143 std::map<std::uint32_t, std::vector<std::uint8_t>> orderedBuffer;
144 std::deque<PendingReliable> pending;
146 };
147
148 struct Peer
149 {
150 std::uint64_t connectionId = 0;
153 std::uint32_t relayServerId = 0;
154 std::uint32_t relayClientNonce = 0;
155 bool hasDirect = false;
156 bool hasRelay = false;
157 bool useRelay = false;
158 bool connectedEventSent = false;
159 Uint64 lastHeardMs = 0;
162 Uint64 lastKeepAliveMs = 0;
163 float rttMs = 0.0f;
164 std::array<ChannelState, static_cast<std::size_t>(ChannelId::Count)> channels;
165 };
166
167 static constexpr Uint64 k_keepAliveMs = 1000;
168 static constexpr Uint64 k_routeUnhealthyMs = 3000;
169 static constexpr Uint64 k_timeoutMs = 5000;
170 static constexpr Uint64 k_retransmitFloorMs = 80;
171 static constexpr std::size_t k_maxPeers = 128;
172 static constexpr std::size_t k_maxQueuedEvents = 1024;
173 static constexpr std::size_t k_maxReliablePending = 256;
174 static constexpr std::size_t k_maxReliableOrderedBuffer = 256;
175
176 [[nodiscard]] static std::size_t channelIndex(ChannelId channel) noexcept;
177 [[nodiscard]] static bool isKnownChannel(std::uint8_t channel) noexcept;
178 [[nodiscard]] static bool isReliable(ChannelId channel) noexcept;
179 [[nodiscard]] static bool seqMoreRecent(std::uint32_t s1, std::uint32_t s2) noexcept;
180 [[nodiscard]] static bool seqAcked(std::uint32_t seq, std::uint32_t ack, std::uint32_t ackBits) noexcept;
181
182 bool resolveAddress(const char* host, Uint16 port, UdpEndpointAddr& out, int timeoutMs);
183 void queueEvent(Event&& event);
184 Peer* findPeer(std::uint64_t connectionId);
185 Peer* createServerPeer(const UdpEndpointAddr& from, std::uint32_t clientNonce, bool viaRelay);
186
187 void sendConnectionRequest(bool viaRelay);
188 void sendConnectionAccepted(Peer& peer);
189 bool sendPacket(Peer& peer, PacketHeader hdr, const void* payload, int payloadLen, int redundancy);
190 bool sendViaRelay(Peer& peer, PacketHeader hdr, const void* payload, int payloadLen);
191 bool sendDirect(const UdpEndpointAddr& dest, PacketHeader hdr, const void* payload, int payloadLen, int redundancy);
192
193 void processDatagram(UdpReceivedMessage& msg, bool viaRelay);
195 void processPayload(Peer& peer, PacketHeader hdr, std::vector<std::uint8_t>&& payload, bool viaRelay);
196 void processAcks(Peer& peer, const PacketHeader& hdr);
197 void rememberReceived(ChannelState& state, std::uint32_t sequence);
198 bool acceptUnreliableSequenced(ChannelState& state, std::uint32_t sequence);
199 void deliverReliable(
200 Peer& peer, ChannelId channel, std::uint32_t sequence, std::vector<std::uint8_t>&& payload, bool viaRelay);
201 void retransmitReliable(Uint64 nowMs);
202 void sendKeepAlives(Uint64 nowMs);
203 void dropTimedOutPeers(Uint64 nowMs);
204
212 bool preferRelay_ = false;
213 std::uint32_t clientNonce_ = 0;
214 std::uint64_t clientConnectionId_ = 0;
216 std::mt19937_64 rng_;
217 std::unordered_map<std::uint64_t, Peer> peers_;
218 std::unordered_map<std::uint32_t, std::uint64_t> connectionByClientNonce_;
219 std::deque<Event> events_;
221};
222
223} // namespace net
Per-connection assembly buffer for fragmented UDP packets.
Opaque relay authorization token shared by discovery and transport.
Thin wrapper over SDL3_net's NET_DatagramSocket.
TCP stream client — sends input to the server and receives state updates.
Definition Client.hpp:69
A single gameplay event produced by network input processing.
Definition Event.hpp:47
TCP stream socket — receives client packets and echoes them back.
Definition Server.hpp:42
Definition FragmentReassembler.hpp:33
Wraps a NET_DatagramSocket with header-prefixed I/O.
Definition UdpEndpoint.hpp:109
UdpEndpointAddr relayAddr_
Definition UdpSessionTransport.hpp:208
void retransmitReliable(Uint64 nowMs)
Definition UdpSessionTransport.cpp:834
bool sendDirectoryControl(const UdpEndpointAddr &dest, const void *payload, int payloadLen)
Send a directory-control payload from the same UDP socket.
Definition UdpSessionTransport.cpp:393
bool sendDirect(const UdpEndpointAddr &dest, PacketHeader hdr, const void *payload, int payloadLen, int redundancy)
Definition UdpSessionTransport.cpp:437
void processPayload(Peer &peer, PacketHeader hdr, std::vector< std::uint8_t > &&payload, bool viaRelay)
Definition UdpSessionTransport.cpp:709
bool connectClient(const char *host, Uint16 port, int timeoutMs)
Definition UdpSessionTransport.cpp:126
UdpSessionTransport(const UdpSessionTransport &)=delete
bool isClientConnected() const noexcept
Definition UdpSessionTransport.hpp:115
std::deque< Event > events_
Definition UdpSessionTransport.hpp:219
bool acceptUnreliableSequenced(ChannelState &state, std::uint32_t sequence)
Definition UdpSessionTransport.cpp:780
bool isOpen() const noexcept
Definition UdpSessionTransport.hpp:114
std::mt19937_64 rng_
Definition UdpSessionTransport.hpp:216
UdpEndpoint endpoint_
Definition UdpSessionTransport.hpp:206
static constexpr Uint64 k_keepAliveMs
Definition UdpSessionTransport.hpp:167
void sendKeepAlives(Uint64 nowMs)
Definition UdpSessionTransport.cpp:856
~UdpSessionTransport()
Definition UdpSessionTransport.hpp:98
void processDatagram(UdpReceivedMessage &msg, bool viaRelay)
Definition UdpSessionTransport.cpp:548
void dropTimedOutPeers(Uint64 nowMs)
Definition UdpSessionTransport.cpp:893
void sendConnectionAccepted(Peer &peer)
Definition UdpSessionTransport.cpp:314
bool send(std::uint64_t connectionId, ChannelId channel, const void *payload, int payloadLen, int redundancy=1)
Definition UdpSessionTransport.cpp:327
std::unordered_map< std::uint32_t, std::uint64_t > connectionByClientNonce_
Definition UdpSessionTransport.hpp:218
RelayConfig relayConfig_
Definition UdpSessionTransport.hpp:209
void pump()
Definition UdpSessionTransport.cpp:219
const Stats & stats() const noexcept
Definition UdpSessionTransport.hpp:120
static bool seqMoreRecent(std::uint32_t s1, std::uint32_t s2) noexcept
Definition UdpSessionTransport.cpp:83
Stats stats_
Definition UdpSessionTransport.hpp:220
bool disconnect(std::uint64_t connectionId)
Definition UdpSessionTransport.cpp:370
void preferRelay(bool enabled)
Definition UdpSessionTransport.hpp:106
static bool isReliable(ChannelId channel) noexcept
Definition UdpSessionTransport.cpp:77
void processAcks(Peer &peer, const PacketHeader &hdr)
Definition UdpSessionTransport.cpp:739
bool sendViaRelay(Peer &peer, PacketHeader hdr, const void *payload, int payloadLen)
Definition UdpSessionTransport.cpp:454
Mode
Definition UdpSessionTransport.hpp:29
@ Client
Definition UdpSessionTransport.hpp:30
PunchAssist punchAssist_
Definition UdpSessionTransport.hpp:210
void close()
Definition UdpSessionTransport.cpp:174
Mode mode_
Definition UdpSessionTransport.hpp:205
Uint16 localPort() const noexcept
Get the local UDP port used by this session transport.
Definition UdpSessionTransport.hpp:119
void setPunchAssist(PunchAssist assist)
Definition UdpSessionTransport.cpp:196
Peer * findPeer(std::uint64_t connectionId)
Definition UdpSessionTransport.cpp:251
void queueEvent(Event &&event)
Definition UdpSessionTransport.cpp:202
static bool isKnownChannel(std::uint8_t channel) noexcept
Definition UdpSessionTransport.cpp:72
static bool seqAcked(std::uint32_t seq, std::uint32_t ack, std::uint32_t ackBits) noexcept
Definition UdpSessionTransport.cpp:89
bool pollEvent(Event &out)
Definition UdpSessionTransport.cpp:209
void processRelayPayload(UdpReceivedMessage &msg)
Definition UdpSessionTransport.cpp:519
UdpSessionTransport & operator=(const UdpSessionTransport &)=delete
bool resolveAddress(const char *host, Uint16 port, UdpEndpointAddr &out, int timeoutMs)
Definition UdpSessionTransport.cpp:99
static constexpr std::size_t k_maxPeers
Definition UdpSessionTransport.hpp:171
Uint64 lastConnectAttemptMs_
Definition UdpSessionTransport.hpp:215
UdpSessionTransport()
Definition UdpSessionTransport.cpp:61
static constexpr std::size_t k_maxQueuedEvents
Definition UdpSessionTransport.hpp:172
UdpEndpointAddr serverAddr_
Definition UdpSessionTransport.hpp:207
std::unordered_map< std::uint64_t, Peer > peers_
Definition UdpSessionTransport.hpp:217
std::uint32_t clientNonce() const noexcept
Definition UdpSessionTransport.hpp:117
static constexpr std::size_t k_maxReliablePending
Definition UdpSessionTransport.hpp:173
void sendConnectionRequest(bool viaRelay)
Definition UdpSessionTransport.cpp:287
bool preferRelay_
Definition UdpSessionTransport.hpp:212
bool sendPacket(Peer &peer, PacketHeader hdr, const void *payload, int payloadLen, int redundancy)
Definition UdpSessionTransport.cpp:401
bool openServer(const char *bindAddr, Uint16 port)
Definition UdpSessionTransport.cpp:115
Peer * createServerPeer(const UdpEndpointAddr &from, std::uint32_t clientNonce, bool viaRelay)
Definition UdpSessionTransport.cpp:258
void rememberReceived(ChannelState &state, std::uint32_t sequence)
Definition UdpSessionTransport.cpp:759
std::uint32_t clientNonce_
Definition UdpSessionTransport.hpp:213
static std::size_t channelIndex(ChannelId channel) noexcept
Definition UdpSessionTransport.cpp:66
EventType
Definition UdpSessionTransport.hpp:35
@ Payload
Definition UdpSessionTransport.hpp:37
@ DirectoryControl
Definition UdpSessionTransport.hpp:39
@ Connected
Definition UdpSessionTransport.hpp:36
@ Disconnected
Definition UdpSessionTransport.hpp:38
static constexpr Uint64 k_routeUnhealthyMs
Definition UdpSessionTransport.hpp:168
void setRelayConfig(const RelayConfig &cfg)
Definition UdpSessionTransport.cpp:187
std::uint64_t clientConnectionId() const noexcept
Definition UdpSessionTransport.hpp:116
UdpEndpointAddr punchDirectoryAddr_
Definition UdpSessionTransport.hpp:211
static constexpr Uint64 k_retransmitFloorMs
Definition UdpSessionTransport.hpp:170
void deliverReliable(Peer &peer, ChannelId channel, std::uint32_t sequence, std::vector< std::uint8_t > &&payload, bool viaRelay)
Definition UdpSessionTransport.cpp:794
static constexpr Uint64 k_timeoutMs
Definition UdpSessionTransport.hpp:169
std::uint64_t clientConnectionId_
Definition UdpSessionTransport.hpp:214
static constexpr std::size_t k_maxReliableOrderedBuffer
Definition UdpSessionTransport.hpp:174
Definition ChatProtocol.cpp:12
ChannelId
Per-channel reliability + ordering semantics.
Definition PacketHeader.hpp:48
@ Count
Definition PacketHeader.hpp:55
@ InputUnreliable
Definition PacketHeader.hpp:49
@ ControlReliableOrdered
Definition PacketHeader.hpp:51
36-byte header at the start of every UDP datagram.
Definition PacketHeader.hpp:71
Definition RelayToken.hpp:16
A UDP datagram address (server's view of a remote peer).
Definition UdpEndpoint.hpp:49
One framed UDP message ready to dispatch to upper layers.
Definition UdpEndpoint.hpp:97
Definition UdpSessionTransport.hpp:136
FragmentReassembler reassembler
Definition UdpSessionTransport.hpp:145
std::uint32_t recvAckBits
Definition UdpSessionTransport.hpp:140
bool recvAny
Definition UdpSessionTransport.hpp:138
std::uint32_t nextSequence
Definition UdpSessionTransport.hpp:137
std::uint32_t orderedNext
Definition UdpSessionTransport.hpp:142
bool orderedAny
Definition UdpSessionTransport.hpp:141
std::map< std::uint32_t, std::vector< std::uint8_t > > orderedBuffer
Definition UdpSessionTransport.hpp:143
std::deque< PendingReliable > pending
Definition UdpSessionTransport.hpp:144
std::uint32_t recvHighest
Definition UdpSessionTransport.hpp:139
Definition UdpSessionTransport.hpp:43
UdpEndpointAddr from
Definition UdpSessionTransport.hpp:48
EventType type
Definition UdpSessionTransport.hpp:44
std::uint64_t connectionId
Definition UdpSessionTransport.hpp:45
ChannelId channel
Definition UdpSessionTransport.hpp:46
bool viaRelay
Definition UdpSessionTransport.hpp:49
std::vector< std::uint8_t > payload
Definition UdpSessionTransport.hpp:47
Definition UdpSessionTransport.hpp:149
Uint64 lastKeepAliveMs
Definition UdpSessionTransport.hpp:162
std::array< ChannelState, static_cast< std::size_t >(ChannelId::Count)> channels
Definition UdpSessionTransport.hpp:164
bool useRelay
Definition UdpSessionTransport.hpp:157
std::uint64_t connectionId
Definition UdpSessionTransport.hpp:150
UdpEndpointAddr relayAddr
Definition UdpSessionTransport.hpp:152
bool connectedEventSent
Definition UdpSessionTransport.hpp:158
UdpEndpointAddr directAddr
Definition UdpSessionTransport.hpp:151
bool hasRelay
Definition UdpSessionTransport.hpp:156
float rttMs
Definition UdpSessionTransport.hpp:163
std::uint32_t relayServerId
Definition UdpSessionTransport.hpp:153
Uint64 lastHeardMs
Definition UdpSessionTransport.hpp:159
bool hasDirect
Definition UdpSessionTransport.hpp:155
Uint64 lastRelayHeardMs
Definition UdpSessionTransport.hpp:161
std::uint32_t relayClientNonce
Definition UdpSessionTransport.hpp:154
Uint64 lastDirectHeardMs
Definition UdpSessionTransport.hpp:160
Definition UdpSessionTransport.hpp:127
std::vector< std::uint8_t > payload
Definition UdpSessionTransport.hpp:130
std::uint32_t sequence
Definition UdpSessionTransport.hpp:128
ChannelId channel
Definition UdpSessionTransport.hpp:129
Uint64 lastSendMs
Definition UdpSessionTransport.hpp:131
std::uint8_t attempts
Definition UdpSessionTransport.hpp:132
Direct UDP hole-punch assist for global joins.
Definition UdpSessionTransport.hpp:88
std::string directoryHost
Directory host to re-punch through.
Definition UdpSessionTransport.hpp:89
std::vector< std::uint8_t > request
Pre-encoded PunchRequest envelope.
Definition UdpSessionTransport.hpp:91
bool enabled
Definition UdpSessionTransport.hpp:92
Uint16 directoryPort
Directory UDP port.
Definition UdpSessionTransport.hpp:90
Definition UdpSessionTransport.hpp:67
RelayToken relayToken
Definition UdpSessionTransport.hpp:72
std::uint32_t clientNonce
Definition UdpSessionTransport.hpp:71
bool enabled
Definition UdpSessionTransport.hpp:73
Uint16 port
Definition UdpSessionTransport.hpp:69
std::string host
Definition UdpSessionTransport.hpp:68
std::uint32_t serverId
Definition UdpSessionTransport.hpp:70
Definition UdpSessionTransport.hpp:53
std::uint64_t bytesSent
Definition UdpSessionTransport.hpp:54
std::uint64_t bytesRecv
Definition UdpSessionTransport.hpp:55
std::uint32_t packetsSent
Definition UdpSessionTransport.hpp:56
std::uint32_t activeRouteId
Definition UdpSessionTransport.hpp:60
bool relayActive
Definition UdpSessionTransport.hpp:62
std::uint32_t reliablePending
Definition UdpSessionTransport.hpp:59
bool directActive
Definition UdpSessionTransport.hpp:63
float rttMs
Definition UdpSessionTransport.hpp:61
std::uint32_t packetsRecv
Definition UdpSessionTransport.hpp:57
std::uint32_t reliableRetransmits
Definition UdpSessionTransport.hpp:58