4#include "SDL3pp_properties.h"
5#include "SDL3pp_version.h"
7#ifdef SDL3PP_ENABLE_NET
9#include <SDL3_net/SDL_net.h>
196#define SDL_NET_MAJOR_VERSION
205#define SDL_NET_MINOR_VERSION
214#define SDL_NET_MICRO_VERSION
223#define SDL_NET_VERSION \
225 SDL_NET_MAJOR_VERSION, SDL_NET_MINOR_VERSION, SDL_NET_MICRO_VERSION)
232#define SDL_NET_VERSION_ATLEAST(X, Y, Z) \
233 ((SDL_NET_MAJOR_VERSION >= X) && \
234 (SDL_NET_MAJOR_VERSION > X || SDL_NET_MINOR_VERSION >= Y) && \
235 (SDL_NET_MAJOR_VERSION > X || SDL_NET_MINOR_VERSION > Y || \
236 SDL_NET_MICRO_VERSION >= Z))
251inline int Version() {
return NET_Version(); }
292inline void Quit() { NET_Quit(); }
497 const void*
GetBytes(
int* num_bytes);
757 using AddressBase::AddressBase;
863 NET_RefAddress(resource);
882 if (
get() != other.
get()) {
934 :
Address(address ? NET_RefAddress(address.
get()) : nullptr)
980 return NET_WaitUntilResolved(address, timeout);
1017 return NET_GetAddressStatus(address);
1052 return CheckError(NET_GetAddressString(address));
1103 return CheckError(NET_GetAddressBytes(address, num_bytes));
1205 NET_SimulateAddressResolutionLoss(percent_loss);
1225 return NET_CompareAddresses(a, b);
1244struct LocalAddressesArrayDeleter
1247 static void operator()(
AddressRef* addresses);
1286 auto* addrs =
CheckError(NET_GetLocalAddresses(&count));
1313 NET_FreeLocalAddresses(
reinterpret_cast<AddressRaw*
>(addresses));
1316inline void LocalAddressesArrayDeleter::operator()(
AddressRef* addresses)
1499 bool WriteTo(
const void* buf,
int buflen);
1618 int ReadFrom(
void* buf,
int buflen);
1679 using StreamSocketBase::StreamSocketBase;
1885 return NET_WaitUntilConnected(sock, timeout);
1981 using ServerBase::ServerBase;
1997 :
Server(other.release())
2140 return Server(addr, port, props);
2153namespace prop::Server {
2155constexpr auto REUSEADDR_BOOLEAN =
2156 NET_PROP_SERVER_REUSEADDR_BOOLEAN;
2201 CheckError(NET_AcceptClient(server, client_stream));
2296 return NET_GetConnectionStatus(sock);
2353 return NET_WriteToStreamSocket(sock, buf, buflen);
2393 return NET_GetStreamSocketPendingWrites(sock);
2441 return NET_WaitUntilStreamSocketDrained(sock, timeout);
2499 return NET_ReadFromStreamSocket(sock, buf, buflen);
2546 NET_SimulateStreamPacketLoss(sock, percent_loss);
2583 NET_DestroyStreamSocket(sock);
2858 using DatagramSocketBase::DatagramSocketBase;
3068 using DatagramBase::DatagramBase;
3251namespace prop::DatagramSocket {
3253constexpr auto REUSEADDR_BOOLEAN =
3254 NET_PROP_DATAGRAM_SOCKET_REUSEADDR_BOOLEAN;
3256constexpr auto ALLOW_BROADCAST_BOOLEAN =
3257 NET_PROP_DATAGRAM_SOCKET_ALLOW_BROADCAST_BOOLEAN;
3346 return NET_SendDatagram(sock, address, port, buf, buflen);
3476 if (!NET_ReceiveDatagram(sock, &dgram))
return false;
3537 NET_SimulateDatagramPacketLoss(sock, percent_loss);
3572 NET_DestroyDatagramSocket(sock);
3631 return NET_WaitUntilInputAvailable(vsockets, numsockets, timeout);
Base class for SDL memory allocated array wrap.
Definition SDL3pp_ownPtr.h:44
constexpr RawPointer release() noexcept
Definition SDL3pp_resource.h:57
friend constexpr void swap(ResourceBaseT &lhs, ResourceBaseT &rhs) noexcept
Definition SDL3pp_resource.h:65
constexpr RawPointer get() const noexcept
Definition SDL3pp_resource.h:54
constexpr ResourceBaseT()=default
Default constructor, creates null/invalid resource.
Const reference wrapper for a given resource,.
Definition SDL3pp_resource.h:115
Helpers to use C++ strings parameters.
Definition SDL3pp_strings.h:58
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition SDL3pp_error.h:199
void Quit()
Clean up all initialized subsystems.
Definition SDL3pp_init.h:329
ResourceRefT< PropertiesBase > PropertiesRef
Reference for Properties.
Definition SDL3pp_properties.h:55
int WaitUntilDrained(Sint32 timeout)
Block until all of a stream socket's pending data is sent.
Definition SDL3pp_net.h:2444
int WaitUntilInputAvailable(void **vsockets, int numsockets, Sint32 timeout)
Block on multiple sockets until at least one has data available.
Definition SDL3pp_net.h:3627
const char * GetAddressString(AddressRef address)
Get a human-readable string from a resolved address.
Definition SDL3pp_net.h:1050
constexpr Status WAITING
Async operation is still in progress, check again later.
Definition SDL3pp_net.h:320
void DestroyDatagramSocket(DatagramSocketRaw sock)
Dispose of a previously-created datagram socket.
Definition SDL3pp_net.h:3570
NET_StreamSocket * StreamSocketRaw
Alias to raw representation for StreamSocket.
Definition SDL3pp_net.h:124
bool SendDatagram(DatagramSocketRef sock, AddressRef address, Uint16 port, const void *buf, int buflen)
Send a new packet over a datagram socket to a remote system.
Definition SDL3pp_net.h:3340
int ReadFromStreamSocket(StreamSocketRef sock, void *buf, int buflen)
Receive bytes that a remote system sent to a stream socket.
Definition SDL3pp_net.h:2497
void DestroyServer(ServerRaw server)
Dispose of a previously-created server.
Definition SDL3pp_net.h:2228
Address GetStreamSocketAddress(StreamSocketRef sock)
Get the remote address of a stream socket.
Definition SDL3pp_net.h:2249
Status WaitUntilResolved(AddressRef address, Sint32 timeout)
Block until an address is resolved.
Definition SDL3pp_net.h:978
ResourceRefT< StreamSocketBase > StreamSocketRef
Reference for StreamSocket.
Definition SDL3pp_net.h:131
int CompareAddresses(AddressRef a, AddressRef b)
Compare two Address objects.
Definition SDL3pp_net.h:1223
ResourceRefT< AddressBase > AddressRef
Reference for Address.
Definition SDL3pp_net.h:115
void Destroy()
Dispose of a datagram packet previously received.
Definition SDL3pp_net.h:3503
constexpr Status SUCCESS
Async operation complete, result was success.
Definition SDL3pp_net.h:322
void SimulateStreamPacketLoss(StreamSocketRef sock, int percent_loss)
Enable simulated stream socket failures.
Definition SDL3pp_net.h:2544
NET_Address * AddressRaw
Alias to raw representation for Address.
Definition SDL3pp_net.h:108
NET_DatagramSocket * DatagramSocketRaw
Alias to raw representation for DatagramSocket.
Definition SDL3pp_net.h:156
Status GetAddressStatus(AddressRef address)
Check if an address is resolved, without blocking.
Definition SDL3pp_net.h:1015
Status GetConnectionStatus()
Check if a stream socket is connected, without blocking.
Definition SDL3pp_net.h:2299
void SimulateDatagramPacketLoss(int percent_loss)
Enable simulated datagram socket failures.
Definition SDL3pp_net.h:3540
bool operator==(const AddressBase &other) const
Compares two addresses for equality.
Definition SDL3pp_net.h:1233
void DestroyStreamSocket(StreamSocketRaw sock)
Dispose of a previously-created stream socket.
Definition SDL3pp_net.h:2581
bool SendDatagram(AddressRef address, Uint16 port, const void *buf, int buflen)
Send a new packet over a datagram socket to a remote system.
Definition SDL3pp_net.h:3349
OwnArray< AddressRef, LocalAddressesArrayDeleter > LocalAddressesArray
Array of addresses returned by GetLocalAddresses.
Definition SDL3pp_net.h:1254
ResourceRefT< ServerBase > ServerRef
Reference for Server.
Definition SDL3pp_net.h:147
bool WriteTo(const void *buf, int buflen)
Send bytes over a stream socket to a remote system.
Definition SDL3pp_net.h:2356
void FreeLocalAddresses(AddressRef *addresses)
Free the results from GetLocalAddresses.
Definition SDL3pp_net.h:1311
const void * GetAddressBytes(AddressRef address, int *num_bytes)
Get the protocol-level bytes of a network address from a resolved address.
Definition SDL3pp_net.h:1101
Status GetConnectionStatus(StreamSocketRef sock)
Check if a stream socket is connected, without blocking.
Definition SDL3pp_net.h:2294
ResourceRefT< DatagramBase > DatagramRef
Reference for Datagram.
Definition SDL3pp_net.h:182
bool WriteToStreamSocket(StreamSocketRef sock, const void *buf, int buflen)
Send bytes over a stream socket to a remote system.
Definition SDL3pp_net.h:2349
Status WaitUntilConnected(StreamSocketRef sock, Sint32 timeout)
Block until a stream socket has connected to a server.
Definition SDL3pp_net.h:1883
Address GetAddress()
Get the remote address of a stream socket.
Definition SDL3pp_net.h:2254
void SimulateAddressResolutionLoss(int percent_loss)
Enable simulated address resolution failures.
Definition SDL3pp_net.h:1203
void DestroyDatagram(DatagramRaw dgram)
Dispose of a datagram packet previously received.
Definition SDL3pp_net.h:3501
const NET_Datagram * DatagramRawConst
Alias to const raw representation for Datagram.
Definition SDL3pp_net.h:175
int GetPendingWrites()
Query bytes still pending transmission on a stream socket.
Definition SDL3pp_net.h:2396
Address RefAddress(AddressRef address)
Add a reference to an Address.
Definition SDL3pp_net.h:1146
int WaitUntilStreamSocketDrained(StreamSocketRef sock, Sint32 timeout)
Block until all of a stream socket's pending data is sent.
Definition SDL3pp_net.h:2439
StreamSocket CreateClient(AddressRef address, Uint16 port, PropertiesRef props)
Begin connecting a socket as a client to a remote server.
Definition SDL3pp_net.h:1822
void AcceptClient(ServerRef server, NET_StreamSocket **client_stream)
Create a stream socket for the next pending client connection.
Definition SDL3pp_net.h:2199
LocalAddressesArray GetLocalAddresses()
Obtain a list of local addresses on the system.
Definition SDL3pp_net.h:1283
void Destroy()
Dispose of a previously-created server.
Definition SDL3pp_net.h:2230
Server CreateServer(Uint16 port, PropertiesRef props)
Create a server, which listens for connections to accept.
Definition SDL3pp_net.h:2143
const void * GetBytes(int *num_bytes)
Get the protocol-level bytes of a network address from a resolved address.
Definition SDL3pp_net.h:1106
NET_Server * ServerRaw
Alias to raw representation for Server.
Definition SDL3pp_net.h:140
bool Receive(DatagramSocketRef sock)
Receive a new packet that a remote system sent to a datagram socket.
Definition SDL3pp_net.h:3473
const char * GetString()
Get a human-readable string from a resolved address.
Definition SDL3pp_net.h:1055
Status WaitUntilResolved(Sint32 timeout)
Block until an address is resolved.
Definition SDL3pp_net.h:983
int Compare(AddressRef b) const
Compare two Address objects.
Definition SDL3pp_net.h:1228
Server CreateServer(AddressRef addr, Uint16 port, PropertiesRef props)
Create a server, which listens for connections to accept.
Definition SDL3pp_net.h:2138
auto operator<=>(const AddressBase &other) const
Compares two addresses.
Definition SDL3pp_net.h:1238
Address ResolveHostname(StringParam host)
Resolve a human-readable hostname.
Definition SDL3pp_net.h:926
void AcceptClient(NET_StreamSocket **client_stream)
Create a stream socket for the next pending client connection.
Definition SDL3pp_net.h:2204
Status WaitUntilConnected(Sint32 timeout)
Block until a stream socket has connected to a server.
Definition SDL3pp_net.h:1888
void UnrefAddress(AddressRaw address)
Drop a reference to an Address.
Definition SDL3pp_net.h:1170
void Destroy()
Dispose of a previously-created stream socket.
Definition SDL3pp_net.h:2586
int GetStreamSocketPendingWrites(StreamSocketRef sock)
Query bytes still pending transmission on a stream socket.
Definition SDL3pp_net.h:2391
void SimulateStreamPacketLoss(int percent_loss)
Enable simulated stream socket failures.
Definition SDL3pp_net.h:2549
ResourceConstRef< DatagramRaw, DatagramRawConst > DatagramConstRef
Safely wrap Datagram for non owning const parameters.
Definition SDL3pp_net.h:185
Status GetStatus()
Check if an address is resolved, without blocking.
Definition SDL3pp_net.h:1020
void Unref()
Drop a reference to an Address.
Definition SDL3pp_net.h:1172
constexpr Status FAILURE
Async operation complete, result was failure.
Definition SDL3pp_net.h:316
ResourceRefT< DatagramSocketBase > DatagramSocketRef
Reference for DatagramSocket.
Definition SDL3pp_net.h:163
void Destroy()
Dispose of a previously-created datagram socket.
Definition SDL3pp_net.h:3575
Datagram ReceiveDatagram()
Receive a new packet that a remote system sent to a datagram socket.
Definition SDL3pp_net.h:3463
int ReadFrom(void *buf, int buflen)
Receive bytes that a remote system sent to a stream socket.
Definition SDL3pp_net.h:2502
void SimulateDatagramPacketLoss(DatagramSocketRef sock, int percent_loss)
Enable simulated datagram socket failures.
Definition SDL3pp_net.h:3535
DatagramSocket CreateDatagramSocket(AddressRef addr, Uint16 port, PropertiesRef props)
Create and bind a new datagram socket.
Definition SDL3pp_net.h:3231
StreamSocket CreateClient(Uint16 port, PropertiesRef props)
Begin connecting a socket as a client to a remote server.
Definition SDL3pp_net.h:1829
NET_Datagram * DatagramRaw
Alias to raw representation for Datagram.
Definition SDL3pp_net.h:172
bool ReceiveDatagram(DatagramSocketRef sock, Datagram &dgram)
Receive a new packet that a remote system sent to a datagram socket.
Definition SDL3pp_net.h:3402
NET_Status Status
A tri-state for asynchronous operations.
Definition SDL3pp_net.h:314
DatagramSocket CreateDatagramSocket(Uint16 port, PropertiesRef props)
Create and bind a new datagram socket.
Definition SDL3pp_net.h:3238
::Sint32 Sint32
A signed 32-bit integer type.
Definition SDL3pp_stdinc.h:283
::Uint16 Uint16
An unsigned 16-bit integer type.
Definition SDL3pp_stdinc.h:270
Main include header for the SDL3pp library.
Base class to Address.
Definition SDL3pp_net.h:331
constexpr ResourceBaseT()=default
Default constructor, creates null/invalid resource.
Opaque representation of a computer-readable network address.
Definition SDL3pp_net.h:756
constexpr Address & operator=(Address &&other) noexcept
Assignment operator.
Definition SDL3pp_net.h:873
static Address borrow(AddressRaw resource)
Safely borrows the from AddressRaw.
Definition SDL3pp_net.h:860
~Address()
Destructor.
Definition SDL3pp_net.h:870
constexpr Address(AddressRaw resource) noexcept
Constructs from raw Address.
Definition SDL3pp_net.h:766
constexpr Address(Address &&other) noexcept
Move constructor.
Definition SDL3pp_net.h:772
Address & operator=(const Address &other)
Assignment operator.
Definition SDL3pp_net.h:880
Base class to Datagram.
Definition SDL3pp_net.h:2982
constexpr ResourceBaseT()=default
Default constructor, creates null/invalid resource.
Base class to DatagramSocket.
Definition SDL3pp_net.h:2594
constexpr ResourceBaseT()=default
Default constructor, creates null/invalid resource.
An object that represents a datagram connection to another system.
Definition SDL3pp_net.h:2857
constexpr DatagramSocket(DatagramSocket &&other) noexcept
Move constructor.
Definition SDL3pp_net.h:2873
constexpr DatagramSocket & operator=(DatagramSocket &&other) noexcept
Assignment operator.
Definition SDL3pp_net.h:2969
~DatagramSocket()
Destructor.
Definition SDL3pp_net.h:2966
constexpr DatagramSocket(DatagramSocketRaw resource) noexcept
Constructs from raw DatagramSocket.
Definition SDL3pp_net.h:2867
The data provided for new incoming packets from ReceiveDatagram().
Definition SDL3pp_net.h:3067
constexpr Datagram & operator=(Datagram &&other) noexcept
Assignment operator.
Definition SDL3pp_net.h:3139
constexpr Datagram(Datagram &&other) noexcept
Move constructor.
Definition SDL3pp_net.h:3083
~Datagram()
Destructor.
Definition SDL3pp_net.h:3136
constexpr Datagram(DatagramRaw resource) noexcept
Constructs from raw Datagram.
Definition SDL3pp_net.h:3077
A non-owning reference wrapper for a given resource.
Definition SDL3pp_resource.h:93
Base class to Server.
Definition SDL3pp_net.h:1899
constexpr ResourceBaseT()=default
Default constructor, creates null/invalid resource.
The receiving end of a stream connection.
Definition SDL3pp_net.h:1980
constexpr Server & operator=(Server &&other) noexcept
Assignment operator.
Definition SDL3pp_net.h:2069
constexpr Server(Server &&other) noexcept
Move constructor.
Definition SDL3pp_net.h:1996
constexpr Server(ServerRaw resource) noexcept
Constructs from raw Server.
Definition SDL3pp_net.h:1990
~Server()
Destructor.
Definition SDL3pp_net.h:2066
Base class to StreamSocket.
Definition SDL3pp_net.h:1327
constexpr ResourceBaseT()=default
Default constructor, creates null/invalid resource.
An object that represents a streaming connection to another system.
Definition SDL3pp_net.h:1678
~StreamSocket()
Destructor.
Definition SDL3pp_net.h:1758
constexpr StreamSocket(StreamSocketRaw resource) noexcept
Constructs from raw StreamSocket.
Definition SDL3pp_net.h:1688
constexpr StreamSocket(StreamSocket &&other) noexcept
Move constructor.
Definition SDL3pp_net.h:1694
constexpr StreamSocket & operator=(StreamSocket &&other) noexcept
Assignment operator.
Definition SDL3pp_net.h:1761