R-Type 0.1
Loading...
Searching...
No Matches
UdpTransport Class Reference

Asynchronous UDP socket wrapper used by the server. More...

#include <transport/UdpTransport.hpp>

Public Member Functions

 UdpTransport (asio::io_context &context, std::uint16_t port, NetworkMetrics &metrics)
 Opens an IPv4 UDP socket bound to a port.
 
void startReceive ()
 Starts the receive loop that forwards datagrams to the callback.
 
void send (const asio::ip::udp::endpoint &destination, const std::vector< std::uint8_t > &data)
 Queues a datagram for sending, ignored if larger than the limit.
 
void close ()
 Cancels pending operations and closes the socket.
 
bool isOpen () const
 Tells whether the socket is still open.
 
void setReceiveCallback (std::function< void(const asio::ip::udp::endpoint &Endpoint, const std::vector< std::uint8_t > &)> callback)
 Sets the function called for every received datagram.
 

Detailed Description

Asynchronous UDP socket wrapper used by the server.

Constructor & Destructor Documentation

◆ UdpTransport()

UdpTransport::UdpTransport ( asio::io_context & context,
std::uint16_t port,
NetworkMetrics & metrics )

Opens an IPv4 UDP socket bound to a port.

Parameters
contextAsio context that runs the socket handlers.
portLocal port to listen on.
metricsCounters updated on every send and receive.

Member Function Documentation

◆ send()

void UdpTransport::send ( const asio::ip::udp::endpoint & destination,
const std::vector< std::uint8_t > & data )

Queues a datagram for sending, ignored if larger than the limit.

Parameters
destinationAddress and port of the receiver.
dataBytes to send.

◆ isOpen()

bool UdpTransport::isOpen ( ) const

Tells whether the socket is still open.

Returns
True if the socket is open.

◆ setReceiveCallback()

void UdpTransport::setReceiveCallback ( std::function< void(const asio::ip::udp::endpoint &Endpoint, const std::vector< std::uint8_t > &)> callback)

Sets the function called for every received datagram.

Parameters
callbackReceives the sender address and the datagram bytes.