Net module

UDPSocket

UDP socket implementation.

UDPSocket

#include <icy/net/udpsocket.h>

Inherits: Handle< uv_udp_t >, Socket

UDP socket implementation.

Public Methods

ReturnNameDescription
UDPSocketConstructs the UDPSocket and initializes the underlying libuv handle.
UDPSocketDeleted constructor.
UDPSocketDeleted constructor.
voidconnect virtualRecords the peer address and fires the Connect signal to mimic TCP socket behaviour. UDP is connectionless; this call does not send any data.
voidconnect virtualResolves host via DNS (or maps "localhost"), then calls connect(Address).
voidclose virtualStops receiving and closes the underlying UDP handle.
voidbind virtualBinds the socket to address and starts the receive loop.
ssize_tsend virtualSends len bytes to the previously connected peer address. Returns -1 if no peer address has been set.
ssize_tsendOwned virtualSends an owned payload buffer to the connected peer.
ssize_tsend virtualSends len bytes to peerAddress. Returns -1 if the socket is uninitialized or the address is not authorized.
ssize_tsendOwned virtual
boolsetBroadcastEnables or disables UDP broadcast.
boolsetMulticastLoopEnables or disables IP multicast loopback.
boolsetMulticastTTLSets the IP multicast time-to-live (hop limit).
net::Addressaddress virtual constReturns the locally bound address, or a wildcard address if unbound.
net::AddresspeerAddress virtual constReturns the connected peer address set by connect(), or a wildcard address if unconnected.
net::TransportTypetransport virtual constReturns the UDP transport protocol.
voidsetError virtualSets the socket error and triggers close.
const icy::Error &error virtual constReturns the current socket error, if any.
boolclosed virtual constReturns true if the native socket handle is closed.
uv::Loop *loop virtual constReturns the event loop associated with this socket.
voidonRecv virtualDispatches a received datagram to all socket adapters via onSocketRecv.

UDPSocket

UDPSocket(uv::Loop * loop)

Constructs the UDPSocket and initializes the underlying libuv handle.

Parameters

  • loop Event loop to use; defaults to the default loop.

UDPSocket

UDPSocket(const UDPSocket &) = delete

Deleted constructor.


UDPSocket

UDPSocket(UDPSocket &&) = delete

Deleted constructor.


connect

virtual

virtual void connect(const net::Address & peerAddress)

Records the peer address and fires the Connect signal to mimic TCP socket behaviour. UDP is connectionless; this call does not send any data.

Parameters

  • peerAddress The remote address to associate with this socket.

connect

virtual

virtual void connect(std::string_view host, uint16_t port)

Resolves host via DNS (or maps "localhost"), then calls connect(Address).

Parameters

  • host Hostname or IP address string.

  • port Destination port.


close

virtual

virtual void close()

Stops receiving and closes the underlying UDP handle.


bind

virtual

virtual void bind(const net::Address & address, unsigned flags)

Binds the socket to address and starts the receive loop.

Parameters

  • address Local address to bind to.

  • flags Optional bind flags (UV_UDP_IPV6ONLY is added automatically for IPv6).


send

virtual

virtual ssize_t send(const char * data, size_t len, int flags)

Sends len bytes to the previously connected peer address. Returns -1 if no peer address has been set.

Parameters

  • data Pointer to the data to send.

  • len Number of bytes to send.

  • flags Reserved; currently unused.

Returns

Number of bytes accepted for sending, or -1 on error.


sendOwned

virtual

virtual ssize_t sendOwned(Buffer && buffer, int flags)

Sends an owned payload buffer to the connected peer.


send

virtual

virtual ssize_t send(const char * data, size_t len, const net::Address & peerAddress, int flags)

Sends len bytes to peerAddress. Returns -1 if the socket is uninitialized or the address is not authorized.

Parameters

  • data Pointer to the data to send.

  • len Number of bytes to send.

  • peerAddress Destination address; must match the connected peer if one is set.

  • flags Reserved; currently unused.

Returns

Number of bytes accepted for sending, or -1 on error.


sendOwned

virtual

virtual ssize_t sendOwned(Buffer && buffer, const net::Address & peerAddress, int flags)

setBroadcast

bool setBroadcast(bool flag)

Enables or disables UDP broadcast.

Parameters

  • flag true to enable broadcast.

Returns

true if the option was set successfully.


setMulticastLoop

bool setMulticastLoop(bool flag)

Enables or disables IP multicast loopback.

Parameters

  • flag true to enable multicast loopback.

Returns

true if the option was set successfully.


setMulticastTTL

bool setMulticastTTL(int ttl)

Sets the IP multicast time-to-live (hop limit).

Parameters

  • ttl Value in the range [1, 255].

Returns

true if the option was set successfully.

Exceptions

  • std::invalid_argument if ttl is out of range.

address

virtual const

virtual net::Address address() const

Returns the locally bound address, or a wildcard address if unbound.


peerAddress

virtual const

virtual net::Address peerAddress() const

Returns the connected peer address set by connect(), or a wildcard address if unconnected.


transport

virtual const

virtual net::TransportType transport() const

Returns the UDP transport protocol.


setError

virtual

virtual void setError(const icy::Error & err)

Sets the socket error and triggers close.

Parameters

  • err The error to record.

error

virtual const

virtual const icy::Error & error() const

Returns the current socket error, if any.


closed

virtual const

virtual bool closed() const

Returns true if the native socket handle is closed.


loop

virtual const

virtual uv::Loop * loop() const

Returns the event loop associated with this socket.


onRecv

virtual

virtual void onRecv(const MutableBuffer & buf, const net::Address & address)

Dispatches a received datagram to all socket adapters via onSocketRecv.

Parameters

  • buf Buffer containing the received datagram payload.

  • address Address of the sender.

Protected Attributes

ReturnNameDescription
net::Address_peer
Buffer_buffer

_peer

net::Address _peer

_buffer

Buffer _buffer

Protected Methods

ReturnNameDescription
voidinit virtualInitializes the underlying socket context.
voidreset virtualResets the socket context for reuse.
voidonError virtualCalled by [setError()](#seterror-4) after the error state has been updated.
voidonClose virtualCalled by [close()](#close-23) after the context has been released.
boolrecvStart virtual
boolrecvStop virtual

init

virtual

virtual void init()

Initializes the underlying socket context.


reset

virtual

virtual void reset()

Resets the socket context for reuse.


onError

virtual

virtual void onError(const icy::Error & error)

Called by [setError()](#seterror-4) after the error state has been updated.

Override to react to errors. The default implementation is a no-op.

Parameters

  • error The error that was set.

onClose

virtual

virtual void onClose()

Called by [close()](#close-23) after the context has been released.

Override to perform cleanup on handle closure. The default implementation is a no-op.


recvStart

virtual

virtual bool recvStart()

recvStop

virtual

virtual bool recvStop()

Public Types

NameDescription
Ptr
Vec

Ptr

std::shared_ptr< UDPSocket > Ptr()

Vec

std::vector< Ptr > Vec()