group2 0.1.0
CSE 125 Group 2
Loading...
Searching...
No Matches
UdpEndpoint.hpp
Go to the documentation of this file.
1
28
29#pragma once
30
31#include "PacketHeader.hpp"
32
33#include <SDL3/SDL_stdinc.h>
34
35#include <SDL3_net/SDL_net.h>
36#include <cstdint>
37#include <vector>
38
39namespace net
40{
41
49{
50 NET_Address* addr = nullptr;
51 Uint16 port = 0;
52
53 UdpEndpointAddr() = default;
54 UdpEndpointAddr(NET_Address* address, Uint16 p) : addr(address ? NET_RefAddress(address) : nullptr), port(p) {}
56 : addr(other.addr ? NET_RefAddress(other.addr) : nullptr), port(other.port)
57 {}
59 {
60 if (this == &other)
61 return *this;
62 release();
63 addr = other.addr ? NET_RefAddress(other.addr) : nullptr;
64 port = other.port;
65 return *this;
66 }
67 UdpEndpointAddr(UdpEndpointAddr&& other) noexcept : addr(other.addr), port(other.port)
68 {
69 other.addr = nullptr;
70 other.port = 0;
71 }
73 {
74 if (this == &other)
75 return *this;
76 release();
77 addr = other.addr;
78 port = other.port;
79 other.addr = nullptr;
80 other.port = 0;
81 return *this;
82 }
84
86 void release() noexcept
87 {
88 if (addr) {
89 NET_UnrefAddress(addr);
90 addr = nullptr;
91 }
92 }
93};
94
102
109{
110public:
111 UdpEndpoint() = default;
112 UdpEndpoint(const UdpEndpoint&) = delete;
117
121 bool open(const char* bindAddr, Uint16 port);
122
124 void close() noexcept;
125
133 bool send(const UdpEndpointAddr& dest, PacketHeader hdr, const void* payload, int payloadLen);
134
139 bool sendDatagramBytes(const UdpEndpointAddr& dest, const void* bytes, int len);
140
177 bool
178 sendFragmented(const UdpEndpointAddr& dest, PacketHeader hdr, const void* data, int dataLen, int redundancy = 1);
179
186
188 [[nodiscard]] bool isOpen() const noexcept { return socket_ != nullptr; }
189
191 [[nodiscard]] uint16_t localPort() const noexcept;
192
193private:
194 NET_DatagramSocket* socket_ = nullptr;
195};
196
197} // namespace net
Wire format helpers for the UDP-first transport layer.
UdpEndpoint()=default
uint16_t localPort() const noexcept
Get the local port this socket is bound to.
Definition UdpEndpoint.cpp:219
NET_DatagramSocket * socket_
Definition UdpEndpoint.hpp:194
void close() noexcept
Close the socket. Idempotent.
Definition UdpEndpoint.cpp:44
UdpEndpoint & operator=(UdpEndpoint &&)=delete
bool tryReceive(UdpReceivedMessage &out)
Try to receive one datagram (non-blocking).
Definition UdpEndpoint.cpp:168
UdpEndpoint(const UdpEndpoint &)=delete
UdpEndpoint & operator=(const UdpEndpoint &)=delete
bool open(const char *bindAddr, Uint16 port)
Bind a UDP socket to bindAddr:port (server) or to any free port (client, with bindAddr = nullptr and ...
Definition UdpEndpoint.cpp:13
bool sendFragmented(const UdpEndpointAddr &dest, PacketHeader hdr, const void *data, int dataLen, int redundancy=1)
Send a payload by splitting it into MTU-safe fragments.
Definition UdpEndpoint.cpp:98
~UdpEndpoint()
Definition UdpEndpoint.hpp:116
bool sendDatagramBytes(const UdpEndpointAddr &dest, const void *bytes, int len)
Send an already encoded datagram to dest.
Definition UdpEndpoint.cpp:80
bool isOpen() const noexcept
True after a successful open.
Definition UdpEndpoint.hpp:188
UdpEndpoint(UdpEndpoint &&)=delete
bool send(const UdpEndpointAddr &dest, PacketHeader hdr, const void *payload, int payloadLen)
Send a payload with our header prefixed, to dest.
Definition UdpEndpoint.cpp:52
Definition ChatProtocol.cpp:12
36-byte header at the start of every UDP datagram.
Definition PacketHeader.hpp:71
A UDP datagram address (server's view of a remote peer).
Definition UdpEndpoint.hpp:49
void release() noexcept
Drop the refcount (idempotent).
Definition UdpEndpoint.hpp:86
UdpEndpointAddr(const UdpEndpointAddr &other)
Definition UdpEndpoint.hpp:55
UdpEndpointAddr & operator=(const UdpEndpointAddr &other)
Definition UdpEndpoint.hpp:58
NET_Address * addr
Refcounted SDL_net address.
Definition UdpEndpoint.hpp:50
~UdpEndpointAddr()
Definition UdpEndpoint.hpp:83
UdpEndpointAddr(UdpEndpointAddr &&other) noexcept
Definition UdpEndpoint.hpp:67
UdpEndpointAddr(NET_Address *address, Uint16 p)
Definition UdpEndpoint.hpp:54
Uint16 port
Source/destination port.
Definition UdpEndpoint.hpp:51
UdpEndpointAddr & operator=(UdpEndpointAddr &&other) noexcept
Definition UdpEndpoint.hpp:72
UdpEndpointAddr()=default
One framed UDP message ready to dispatch to upper layers.
Definition UdpEndpoint.hpp:97
UdpEndpointAddr from
Source address. Owns the NET_Address ref.
Definition UdpEndpoint.hpp:98
PacketHeader header
Parsed header.
Definition UdpEndpoint.hpp:99
std::vector< uint8_t > payload
Bytes after the header.
Definition UdpEndpoint.hpp:100