10#include <SDL3/SDL_stdinc.h>
19#include <unordered_map>
100 bool openServer(
const char* bindAddr, Uint16 port);
101 bool connectClient(
const char* host, Uint16 port,
int timeoutMs);
111 bool send(std::uint64_t connectionId,
ChannelId channel,
const void* payload,
int payloadLen,
int redundancy = 1);
177 [[nodiscard]]
static bool isKnownChannel(std::uint8_t 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;
200 Peer& peer,
ChannelId channel, std::uint32_t sequence, std::vector<std::uint8_t>&& payload,
bool viaRelay);
217 std::unordered_map<std::uint64_t, Peer>
peers_;
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
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