Net module

TCPSocket

TCP socket implementation.

TCPSocket

#include <icy/net/tcpsocket.h>

Inherits: Stream< uv_tcp_t >, Socket Subclassed by: SSLSocket

TCP socket implementation.

Public Attributes

ReturnNameDescription
LocalSignal< void(const net::TCPSocket::Ptr &)>AcceptConnectionFired when a new client connection is accepted; carries a shared_ptr to the new socket.

AcceptConnection

LocalSignal< void(const net::TCPSocket::Ptr &)> AcceptConnection

Fired when a new client connection is accepted; carries a shared_ptr to the new socket.

Public Methods

ReturnNameDescription
TCPSocketConstructs the TCPSocket and initializes the underlying libuv handle.
TCPSocketDeleted constructor.
TCPSocketDeleted constructor.
boolshutdown virtualSends a TCP shutdown request; the socket closes after the peer acknowledges.
voidclose virtualCloses the socket immediately, releasing all associated resources.
voidconnect virtualConnects to peerAddress using a libuv connect request. On success, calls onConnect(); on failure, calls setUVError().
voidconnect virtualResolves host via DNS (or maps "localhost"), then connects.
ssize_tsend virtualWrites len bytes to the connected peer.
ssize_tsendOwned virtualSends an owned payload buffer to the connected peer.
ssize_tsend virtualWrites len bytes; peerAddress is ignored for TCP (connected stream).
ssize_tsendOwned virtual
voidbind virtualBinds the socket to address. Resets and reinitializes the handle if the address family changes.
voidlisten virtualStarts listening for incoming connections with the given backlog.
voidacceptConnection virtualAccepts the next pending client connection and fires AcceptConnection.
boolsetReusePortEnables SO_REUSEPORT on Linux kernel >= 3.9 for multi-thread load balancing. Must be called after bind(). No-op and returns false on unsupported platforms.
boolsetNoDelayEnables or disables TCP_NODELAY (Nagle's algorithm).
boolsetKeepAliveEnables or disables TCP keep-alive probes.
boolsetSimultaneousAcceptsEnables or disables simultaneous accepts on Windows. No-op and returns false on non-Windows platforms.
voidsetModeSets the socket mode (ServerSide or ClientSide).
SocketModemode constReturns the current socket mode.
voidsetError virtualSets the socket error; ignores the call if an error is already recorded. Setting an error causes the socket to close.
const icy::Error &error virtual constReturns the current socket error, if any.
boolclosed virtual constReturns true if the native socket handle is closed.
net::Addressaddress virtual constReturns the IP address and port number of the socket. A wildcard address is returned if the socket is not connected.
net::AddresspeerAddress virtual constReturns the IP address and port number of the peer socket. A wildcard address is returned if the socket is not connected.
net::TransportTypetransport virtual constReturns the TCP transport protocol.
uv::Loop *loop virtual constReturns the event loop associated with this socket.
voidonConnect virtualCalled by the stream layer when the TCP connection is established.
voidonRead virtualCalled by the stream layer with raw received bytes; wraps them in a MutableBuffer.
voidonRecv virtualDispatches a received buffer to all socket adapters via onSocketRecv.
voidonError virtualDispatches the error to adapters and closes the socket.
voidonClose virtualDispatches the close event to all socket adapters.

TCPSocket

TCPSocket(uv::Loop * loop)

Constructs the TCPSocket and initializes the underlying libuv handle.

Parameters

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

TCPSocket

TCPSocket(const TCPSocket &) = delete

Deleted constructor.


TCPSocket

TCPSocket(TCPSocket &&) = delete

Deleted constructor.


shutdown

virtual

virtual bool shutdown()

Sends a TCP shutdown request; the socket closes after the peer acknowledges.

Returns

true if the shutdown request was queued successfully.


close

virtual

virtual void close()

Closes the socket immediately, releasing all associated resources.


connect

virtual

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

Connects to peerAddress using a libuv connect request. On success, calls onConnect(); on failure, calls setUVError().

Parameters

  • peerAddress The remote address to connect to.

connect

virtual

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

Resolves host via DNS (or maps "localhost"), then connects.

Parameters

  • host Hostname or IP address string.

  • port Destination port.


send

virtual

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

Writes len bytes to the connected peer.

Parameters

  • data Pointer to the data to send.

  • len Number of bytes to send.

  • flags Reserved; currently unused.

Returns

Number of bytes sent, 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)

Writes len bytes; peerAddress is ignored for TCP (connected stream).

Parameters

  • data Pointer to the data to send.

  • len Number of bytes to send.

  • peerAddress Ignored; present for interface conformance.

  • flags Reserved; currently unused.

Returns

Number of bytes sent, or -1 on error.


sendOwned

virtual

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

bind

virtual

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

Binds the socket to address. Resets and reinitializes the handle if the address family changes.

Parameters

  • address Local address to bind to.

  • flags Optional bind flags (e.g. UV_TCP_IPV6ONLY is added automatically for IPv6).


listen

virtual

virtual void listen(int backlog)

Starts listening for incoming connections with the given backlog.

Parameters

  • backlog Maximum length of the pending connection queue.

acceptConnection

virtual

virtual void acceptConnection()

Accepts the next pending client connection and fires AcceptConnection.


setReusePort

bool setReusePort()

Enables SO_REUSEPORT on Linux kernel >= 3.9 for multi-thread load balancing. Must be called after bind(). No-op and returns false on unsupported platforms.

Returns

true if the socket option was set successfully.


setNoDelay

bool setNoDelay(bool enable)

Enables or disables TCP_NODELAY (Nagle's algorithm).

Parameters

  • enable true to disable Nagle's algorithm (lower latency).

Returns

true if the option was set successfully.


setKeepAlive

bool setKeepAlive(bool enable, int delay)

Enables or disables TCP keep-alive probes.

Parameters

  • enable true to enable keep-alive.

  • delay Initial delay in seconds before the first keep-alive probe.

Returns

true if the option was set successfully.


setSimultaneousAccepts

bool setSimultaneousAccepts(bool enable)

Enables or disables simultaneous accepts on Windows. No-op and returns false on non-Windows platforms.

Parameters

  • enable true to enable simultaneous accepts.

Returns

true if the option was set successfully.


setMode

void setMode(SocketMode mode)

Sets the socket mode (ServerSide or ClientSide).

Parameters

  • mode The mode to assign.

mode

const

SocketMode mode() const

Returns the current socket mode.


setError

virtual

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

Sets the socket error; ignores the call if an error is already recorded. Setting an error causes the socket to 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.


address

virtual const

virtual net::Address address() const

Returns the IP address and port number of the socket. A wildcard address is returned if the socket is not connected.


peerAddress

virtual const

virtual net::Address peerAddress() const

Returns the IP address and port number of the peer socket. A wildcard address is returned if the socket is not connected.


transport

virtual const

virtual net::TransportType transport() const

Returns the TCP transport protocol.


loop

virtual const

virtual uv::Loop * loop() const

Returns the event loop associated with this socket.


onConnect

virtual

virtual void onConnect()

Called by the stream layer when the TCP connection is established.


onRead

virtual

virtual void onRead(const char * data, size_t len)

Called by the stream layer with raw received bytes; wraps them in a MutableBuffer.

Parameters

  • data Pointer to received bytes.

  • len Number of bytes received.


onRecv

virtual

virtual void onRecv(const MutableBuffer & buf)

Dispatches a received buffer to all socket adapters via onSocketRecv.

Parameters

  • buf The buffer containing the received data.

onError

virtual

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

Dispatches the error to adapters and closes the socket.

Parameters

  • error The error that occurred.

onClose

virtual

virtual void onClose()

Dispatches the close event to all socket adapters.

Protected Attributes

ReturnNameDescription
SocketMode_mode
net::Address_peerAddressCached peer address (avoids syscall per recv)

_mode

SocketMode _mode

_peerAddress

net::Address _peerAddress

Cached peer address (avoids syscall per recv)

Protected Methods

ReturnNameDescription
voidinit virtualInitializes the underlying socket context.
voidreset virtualResets the socket context for reuse.

init

virtual

virtual void init()

Initializes the underlying socket context.


reset

virtual

virtual void reset()

Resets the socket context for reuse.

Public Types

NameDescription
Ptr
Vec

Ptr

std::shared_ptr< TCPSocket > Ptr()

Vec

std::vector< Ptr > Vec()