UDPSocket
UDPSocket
#include <icy/net/udpsocket.h>Inherits:
Handle< uv_udp_t >,Socket
UDP socket implementation.
Public Methods
| Return | Name | Description |
|---|---|---|
UDPSocket | Constructs the UDPSocket and initializes the underlying libuv handle. | |
UDPSocket | Deleted constructor. | |
UDPSocket | Deleted constructor. | |
void | connect virtual | Records the peer address and fires the Connect signal to mimic TCP socket behaviour. UDP is connectionless; this call does not send any data. |
void | connect virtual | Resolves host via DNS (or maps "localhost"), then calls connect(Address). |
void | close virtual | Stops receiving and closes the underlying UDP handle. |
void | bind virtual | Binds the socket to address and starts the receive loop. |
ssize_t | send virtual | Sends len bytes to the previously connected peer address. Returns -1 if no peer address has been set. |
ssize_t | sendOwned virtual | Sends an owned payload buffer to the connected peer. |
ssize_t | send virtual | Sends len bytes to peerAddress. Returns -1 if the socket is uninitialized or the address is not authorized. |
ssize_t | sendOwned virtual | |
bool | setBroadcast | Enables or disables UDP broadcast. |
bool | setMulticastLoop | Enables or disables IP multicast loopback. |
bool | setMulticastTTL | Sets the IP multicast time-to-live (hop limit). |
net::Address | address virtual const | Returns the locally bound address, or a wildcard address if unbound. |
net::Address | peerAddress virtual const | Returns the connected peer address set by connect(), or a wildcard address if unconnected. |
net::TransportType | transport virtual const | Returns the UDP transport protocol. |
void | setError virtual | Sets the socket error and triggers close. |
const icy::Error & | error virtual const | Returns the current socket error, if any. |
bool | closed virtual const | Returns true if the native socket handle is closed. |
uv::Loop * | loop virtual const | Returns the event loop associated with this socket. |
void | onRecv virtual | Dispatches a received datagram to all socket adapters via onSocketRecv. |
UDPSocket
UDPSocket(uv::Loop * loop)Constructs the UDPSocket and initializes the underlying libuv handle.
Parameters
loopEvent loop to use; defaults to the default loop.
UDPSocket
UDPSocket(const UDPSocket &) = deleteDeleted constructor.
UDPSocket
UDPSocket(UDPSocket &&) = deleteDeleted 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
peerAddressThe 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
hostHostname or IP address string.portDestination 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
addressLocal address to bind to.flagsOptional 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
dataPointer to the data to send.lenNumber of bytes to send.flagsReserved; 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
dataPointer to the data to send.lenNumber of bytes to send.peerAddressDestination address; must match the connected peer if one is set.flagsReserved; 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
flagtrue to enable broadcast.
Returns
true if the option was set successfully.
setMulticastLoop
bool setMulticastLoop(bool flag)Enables or disables IP multicast loopback.
Parameters
flagtrue 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
ttlValue in the range [1, 255].
Returns
true if the option was set successfully.
Exceptions
std::invalid_argumentifttlis out of range.
address
virtual const
virtual net::Address address() constReturns the locally bound address, or a wildcard address if unbound.
peerAddress
virtual const
virtual net::Address peerAddress() constReturns the connected peer address set by connect(), or a wildcard address if unconnected.
transport
virtual const
virtual net::TransportType transport() constReturns the UDP transport protocol.
setError
virtual
virtual void setError(const icy::Error & err)Sets the socket error and triggers close.
Parameters
errThe error to record.
error
virtual const
virtual const icy::Error & error() constReturns the current socket error, if any.
closed
virtual const
virtual bool closed() constReturns true if the native socket handle is closed.
loop
virtual const
virtual uv::Loop * loop() constReturns 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
bufBuffer containing the received datagram payload.addressAddress of the sender.
Protected Attributes
_peer
net::Address _peer_buffer
Buffer _bufferProtected Methods
| Return | Name | Description |
|---|---|---|
void | init virtual | Initializes the underlying socket context. |
void | reset virtual | Resets the socket context for reuse. |
void | onError virtual | Called by [setError()](#seterror-4) after the error state has been updated. |
void | onClose virtual | Called by [close()](#close-23) after the context has been released. |
bool | recvStart virtual | |
bool | recvStop 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
errorThe 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
Ptr
std::shared_ptr< UDPSocket > Ptr()Vec
std::vector< Ptr > Vec()