vkmEngine 1.0.0
A C++ game engine · vkmengine.com
Loading...
Searching...
No Matches
Vkm::Engine::UdpSocket Class Reference

A non-blocking UDP socket, the same on Windows and Linux. More...

#include <udp_socket.h>

Public Member Functions

bool open (uint16_t port)
 Take a port and start listening on every interface.
void close ()
 Close it. Safe to call on one that was never opened.
bool send (const NetAddress &to, const uint8_t *bytes, size_t size)
 Send size bytes to to.
bool receive (std::vector< uint8_t > &out, NetAddress &from)
 Take the next datagram, if one is waiting.
NetAddress localAddress () const
 Loopback and the port this is bound to, including one the system picked for zero.

Static Public Attributes

static constexpr size_t MAX_DATAGRAM = 1200
 The largest datagram this will send.

Detailed Description

A non-blocking UDP socket, the same on Windows and Linux.

The whole Winsock/BSD difference sits behind this type. Non-blocking, so a frame never stalls on the network.

Member Function Documentation

◆ open()

bool Vkm::Engine::UdpSocket::open ( uint16_t port)
nodiscard

Take a port and start listening on every interface.

Parameters
portPort to bind, or 0 to let the system choose a free one.
Returns
Whether the socket is open.

◆ send()

bool Vkm::Engine::UdpSocket::send ( const NetAddress & to,
const uint8_t * bytes,
size_t size )

Send size bytes to to.

Refused past MAX_DATAGRAM.

Parameters
toWhere it goes.
bytesThe payload.
sizePayload length in bytes.
Returns
False when there is no socket, nowhere to send, it will not fit, or the send failed.

◆ receive()

bool Vkm::Engine::UdpSocket::receive ( std::vector< uint8_t > & out,
NetAddress & from )
nodiscard

Take the next datagram, if one is waiting.

One datagram per call. An empty or oversized one answers true with out empty, so a caller's per-frame read bound counts it.

Parameters
outFilled with the payload; cleared first.
fromFilled with where it came from.
Returns
False when nothing is waiting, which is not an error.

◆ localAddress()

NetAddress Vkm::Engine::UdpSocket::localAddress ( ) const
inline

Loopback and the port this is bound to, including one the system picked for zero.

127.0.0.1 rather than the bound 0.0.0.0, which nothing can send to.

Returns
The local address; null when the socket is closed.

Member Data Documentation

◆ MAX_DATAGRAM

size_t Vkm::Engine::UdpSocket::MAX_DATAGRAM = 1200
staticconstexpr

The largest datagram this will send.

Inside the smallest path MTU worth designing for, with room for the IP and UDP headers. Past it IP fragments, and one lost fragment loses the whole datagram.


The documentation for this class was generated from the following file: