Home
Net module

UDPSocket

UDP socket implementation.

UDPSocket

#include <icy/net/udpsocket.h>
class UDPSocket

Defined in src/net/include/icy/net/udpsocket.h:28

Inherits: Handle< uv_udp_t >, Socket

UDP socket implementation.

List of all members

NameKindOwner
UDPSocketfunctionDeclared here
~UDPSocketfunctionDeclared here
UDPSocketfunctionDeclared here
operator=functionDeclared here
UDPSocketfunctionDeclared here
operator=functionDeclared here
connectfunctionDeclared here
connectfunctionDeclared here
closefunctionDeclared here
bindfunctionDeclared here
sendfunctionDeclared here
sendOwnedfunctionDeclared here
sendfunctionDeclared here
sendOwnedfunctionDeclared here
setBroadcastfunctionDeclared here
setMulticastLoopfunctionDeclared here
setMulticastTTLfunctionDeclared here
addressfunctionDeclared here
peerAddressfunctionDeclared here
transportfunctionDeclared here
setErrorfunctionDeclared here
errorfunctionDeclared here
closedfunctionDeclared here
loopfunctionDeclared here
onRecvfunctionDeclared here
_peervariableDeclared here
_buffervariableDeclared here
initfunctionDeclared here
resetfunctionDeclared here
onErrorfunctionDeclared here
onClosefunctionDeclared here
recvStartfunctionDeclared here
recvStopfunctionDeclared here
PtrtypedefDeclared here
VectypedefDeclared here
HandlefunctionInherited from Handle
~HandlefunctionInherited from Handle
initfunctionInherited from Handle
invokefunctionInherited from Handle
invokeOrThrowfunctionInherited from Handle
closefunctionInherited from Handle
reffunctionInherited from Handle
unreffunctionInherited from Handle
initializedfunctionInherited from Handle
activefunctionInherited from Handle
closingfunctionInherited from Handle
closedfunctionInherited from Handle
errorfunctionInherited from Handle
setErrorfunctionInherited from Handle
setUVErrorfunctionInherited from Handle
setAndThrowErrorfunctionInherited from Handle
throwLastErrorfunctionInherited from Handle
loopfunctionInherited from Handle
resetfunctionInherited from Handle
getfunctionInherited from Handle
tidfunctionInherited from Handle
contextfunctionInherited from Handle
setCloseCleanupfunctionInherited from Handle
clearCloseCleanupfunctionInherited from Handle
assertThreadfunctionInherited from Handle
_loopvariableInherited from Handle
_contextvariableInherited from Handle
_tidvariableInherited from Handle
_errorvariableInherited from Handle
onErrorfunctionInherited from Handle
onClosefunctionInherited from Handle
HandlefunctionInherited from Handle
operator=functionInherited from Handle
HandlefunctionInherited from Handle
operator=functionInherited from Handle
TypetypedefInherited from Handle
opaquevariableInherited from Socket
SocketfunctionInherited from Socket
~SocketfunctionInherited from Socket
SocketfunctionInherited from Socket
operator=functionInherited from Socket
SocketfunctionInherited from Socket
operator=functionInherited from Socket
connectfunctionInherited from Socket
connectfunctionInherited from Socket
bindfunctionInherited from Socket
listenfunctionInherited from Socket
shutdownfunctionInherited from Socket
sendOwnedfunctionInherited from Socket
sendOwnedfunctionInherited from Socket
closefunctionInherited from Socket
addressfunctionInherited from Socket
peerAddressfunctionInherited from Socket
transportfunctionInherited from Socket
setErrorfunctionInherited from Socket
errorfunctionInherited from Socket
closedfunctionInherited from Socket
loopfunctionInherited from Socket
_afvariableInherited from Socket
initfunctionInherited from Socket
resetfunctionInherited from Socket
PtrtypedefInherited from Socket
VectypedefInherited from Socket
priorityvariableInherited from SocketAdapter
SocketAdapterfunctionInherited from SocketAdapter
~SocketAdapterfunctionInherited from SocketAdapter
sendfunctionInherited from SocketAdapter
sendfunctionInherited from SocketAdapter
sendOwnedfunctionInherited from SocketAdapter
sendOwnedfunctionInherited from SocketAdapter
sendPacketfunctionInherited from SocketAdapter
sendPacketfunctionInherited from SocketAdapter
sendPacketfunctionInherited from SocketAdapter
setSenderfunctionInherited from SocketAdapter
senderfunctionInherited from SocketAdapter
addReceiverfunctionInherited from SocketAdapter
removeReceiverfunctionInherited from SocketAdapter
hasReceiverfunctionInherited from SocketAdapter
receiversfunctionInherited from SocketAdapter
onSocketConnectfunctionInherited from SocketAdapter
onSocketRecvfunctionInherited from SocketAdapter
onSocketErrorfunctionInherited from SocketAdapter
onSocketClosefunctionInherited from SocketAdapter
_sendervariableInherited from SocketAdapter
_receiversvariableInherited from SocketAdapter
_dirtyvariableInherited from SocketAdapter
cleanupReceiversfunctionInherited from SocketAdapter

Inherited from Handle

KindNameDescription
functionHandle inlineConstruct the handle bound to the given event loop.
function~Handle virtual inline
functioninit inlineInitialize the underlying libuv handle by calling f with the loop, the raw handle pointer, and any additional args.
functioninvoke inlineInvoke a libuv function f with args on the initialized handle.
functioninvokeOrThrow inlineInvoke a libuv function f with args, throwing on failure.
functionclose virtual inlineClose and destroy the handle.
functionref inlineRe-reference the handle with the event loop after a previous [unref()](icy-uv-Handle.html#unref).
functionunref inlineUnreference the handle from the event loop.
functioninitialized const inlineReturn true if the handle has been successfully initialized via [init()](icy-uv-Handle.html#init-8).
functionactive virtual const inlineReturn true when the handle is active (libuv uv_is_active).
functionclosing virtual const inlineReturn true if uv_close has been called and the handle is awaiting its close callback (libuv uv_is_closing).
functionclosed virtual const inlineReturn true if the handle has been fully closed (context released).
functionerror const inlineReturn the last error set on this handle, or a default-constructed [Error](icy-Error.html#error) if no error has occurred.
functionsetError virtual inlineSet the error state and invoke [onError()](icy-uv-Handle.html#onerror).
functionsetUVError inlineTranslate a libuv error code into an [Error](icy-Error.html#error) and call [setError()](icy-uv-Handle.html#seterror-1).
functionsetAndThrowError inlineSet the error state from a libuv error code and throw a std::runtime_error.
functionthrowLastError inlineThrow a std::runtime_error if the handle currently holds an error.
functionloop const inlineReturn the event loop this handle is bound to.
functionreset inlineClose the current handle (if open) and allocate a fresh [Context](icy-uv-Context.html#context-2), leaving the handle ready to be re-initialized via [init()](icy-uv-Handle.html#init-8).
functionget const inlineReturn the raw libuv handle pointer cast to [Handle](icy-uv-Handle.html#handle-6).
functiontid const inlineReturn the ID of the thread that constructed this handle.
functioncontext const inlineReturn the raw [Context](icy-uv-Context.html#context-2) that owns the libuv handle memory.
functionsetCloseCleanup inline
functionclearCloseCleanup inline
functionassertThread const inlineThrow std::logic_error if called from any thread other than the thread that constructed this handle.
variable_loop
variable_context
variable_tid
variable_error
functiononError virtual inlineCalled by [setError()](icy-uv-Handle.html#seterror-1) after the error state has been updated.
functiononClose virtual inlineCalled by [close()](icy-uv-Handle.html#close-18) after the context has been released.
functionHandleNonCopyable and NonMovable.
functionoperator=Deleted assignment operator.
functionHandleDeleted constructor.
functionoperator=Deleted assignment operator.
typedefTypeDefine the native handle type.

Inherited from Socket

KindNameDescription
variableopaqueOptional client data.
functionSocketDefaulted constructor.
function~Socket virtual noexceptDefaulted destructor.
functionSocketDeleted constructor.
functionoperator=Deleted assignment operator.
functionSocketDeleted constructor.
functionoperator=Deleted assignment operator.
functionconnect virtualConnects to the given peer IP address.
functionconnect virtualResolves and connects to the given host address.
functionbind virtualBind a local address to the socket. The address may be IPv4 or IPv6 (if supported).
functionlisten virtual inlineListens the socket on the given address.
functionshutdown virtual inlineSends the shutdown packet which should result is socket closure via callback.
functionsendOwned virtualSends an owned payload buffer to the connected peer.
functionsendOwned virtual
functionclose virtualCloses the underlying socket.
functionaddress virtual constThe locally bound address.
functionpeerAddress virtual constThe connected peer address.
functiontransport virtual constThe transport protocol: TCP, UDP or SSLTCP.
functionsetError virtualSets the socket error.
functionerror virtual constReturn the socket error if any.
functionclosed virtual constReturns true if the native socket handle is closed.
functionloop virtual constReturns the socket event loop.
variable_af
functioninit virtualInitializes the underlying socket context.
functionreset virtualResets the socket context for reuse.
typedefPtr
typedefVec

Inherited from SocketAdapter

KindNameDescription
variablepriorityThe priority of this adapter for STL sort operations.
functionSocketAdapterCreates the SocketAdapter.
function~SocketAdapter virtual noexceptDestroys the SocketAdapter.
functionsend virtualSends the given data buffer to the connected peer. Returns the number of bytes sent or -1 on error. No exception will be thrown. For TCP sockets the given peer address must match the connected peer address.
functionsend virtual
functionsendOwned virtualSends an owned payload buffer to the connected peer.
functionsendOwned virtual
functionsendPacket virtualSends the given packet to the connected peer. Returns the number of bytes sent or -1 on error. No exception will be thrown. For TCP sockets the given peer address must match the connected peer address.
functionsendPacket virtual
functionsendPacket virtualSends the given packet to the connected peer. This method provides delegate compatibility, and unlike other send methods throws an exception if the underlying socket is closed.
functionsetSender virtualSets the pointer to the outgoing data adapter. Send methods proxy data to this adapter by default.
functionsenderReturns the output SocketAdapter pointer.
functionaddReceiver virtualSets the pointer to the incoming data adapter. Events proxy data to this adapter by default.
functionremoveReceiver virtualRemove the given receiver.
functionhasReceiver virtualReturns true if the given receiver is connected.
functionreceiversReturns all currently registered input SocketAdapter pointers. Dead (removed) entries are excluded from the returned list.
functiononSocketConnect virtualCalled when the socket establishes a connection. Forwards the event to all registered receivers in priority order. Override to intercept before the application sees the event.
functiononSocketRecv virtualCalled when data is received from the socket. Forwards the event to all registered receivers in priority order.
functiononSocketError virtualCalled when the socket encounters an error. Forwards the event to all registered receivers in priority order.
functiononSocketClose virtualCalled when the socket is closed. Forwards the event to all registered receivers in priority order.
variable_sender
variable_receivers
variable_dirty
functioncleanupReceivers virtual

Public Methods

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

UDPSocket

UDPSocket(uv::Loop * loop = uv::defaultLoop())

Defined in src/net/include/icy/net/udpsocket.h:37

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

Defined in src/net/include/icy/net/udpsocket.h:40

Deleted constructor.


UDPSocket

UDPSocket(UDPSocket &&) = delete

Defined in src/net/include/icy/net/udpsocket.h:42

Deleted constructor.


connect

virtual override

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

Defined in src/net/include/icy/net/udpsocket.h:48

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.
Reimplements

connect

virtual override

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

Defined in src/net/include/icy/net/udpsocket.h:53

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

Parameters

  • host Hostname or IP address string.

  • port Destination port.

Reimplements

close

virtual override

virtual void close() override

Defined in src/net/include/icy/net/udpsocket.h:56

Stops receiving and closes the underlying UDP handle.

Reimplements

bind

virtual override

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

Defined in src/net/include/icy/net/udpsocket.h:61

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).

Reimplements

send

virtual override

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

Defined in src/net/include/icy/net/udpsocket.h:69

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.

Reimplements

sendOwned

virtual override

virtual ssize_t sendOwned(Buffer && buffer, int flags = 0) override

Defined in src/net/include/icy/net/udpsocket.h:70

Sends an owned payload buffer to the connected peer.

Reimplements

send

virtual override

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

Defined in src/net/include/icy/net/udpsocket.h:79

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.

Reimplements

sendOwned

virtual override

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

Defined in src/net/include/icy/net/udpsocket.h:81

Reimplements

setBroadcast

bool setBroadcast(bool flag)

Defined in src/net/include/icy/net/udpsocket.h:87

Enables or disables UDP broadcast.

Parameters

  • flag true to enable broadcast.

Returns

true if the option was set successfully.


setMulticastLoop

bool setMulticastLoop(bool flag)

Defined in src/net/include/icy/net/udpsocket.h:92

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)

Defined in src/net/include/icy/net/udpsocket.h:98

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 override

virtual net::Address address() const override

Defined in src/net/include/icy/net/udpsocket.h:101

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

Reimplements

peerAddress

virtual const override

virtual net::Address peerAddress() const override

Defined in src/net/include/icy/net/udpsocket.h:104

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

Reimplements

transport

virtual const override

virtual net::TransportType transport() const override

Defined in src/net/include/icy/net/udpsocket.h:107

Returns the UDP transport protocol.

Reimplements

setError

virtual override

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

Defined in src/net/include/icy/net/udpsocket.h:111

Sets the socket error and triggers close.

Parameters

  • err The error to record.
Reimplements

error

virtual const override

virtual const icy::Error & error() const override

Defined in src/net/include/icy/net/udpsocket.h:114

Returns the current socket error, if any.

Reimplements

closed

virtual const override

virtual bool closed() const override

Defined in src/net/include/icy/net/udpsocket.h:117

Returns true if the native socket handle is closed.

Reimplements

loop

virtual const override

virtual uv::Loop * loop() const override

Defined in src/net/include/icy/net/udpsocket.h:120

Returns the event loop associated with this socket.

Reimplements

onRecv

virtual

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

Defined in src/net/include/icy/net/udpsocket.h:125

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

Defined in src/net/include/icy/net/udpsocket.h:141


_buffer

Buffer _buffer

Defined in src/net/include/icy/net/udpsocket.h:142

Protected Methods

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

init

virtual override

virtual void init() override

Defined in src/net/include/icy/net/udpsocket.h:128

Initializes the underlying socket context.

Reimplements

reset

virtual override

virtual void reset() override

Defined in src/net/include/icy/net/udpsocket.h:129

Resets the socket context for reuse.

Reimplements

onError

virtual override

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

Defined in src/net/include/icy/net/udpsocket.h:131

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.
Reimplements

onClose

virtual override

virtual void onClose() override

Defined in src/net/include/icy/net/udpsocket.h:132

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.

Reimplements

recvStart

virtual

virtual bool recvStart()

Defined in src/net/include/icy/net/udpsocket.h:134


recvStop

virtual

virtual bool recvStop()

Defined in src/net/include/icy/net/udpsocket.h:135

Public Types

NameDescription
Ptr
Vec

Ptr

using Ptr = std::shared_ptr< UDPSocket >

Defined in src/net/include/icy/net/udpsocket.h:32


Vec

using Vec = std::vector< Ptr >

Defined in src/net/include/icy/net/udpsocket.h:33