TCPSocket
TCPSocket
#include <icy/net/tcpsocket.h>Inherits:
Stream< uv_tcp_t >,SocketSubclassed by:SSLSocket
TCP socket implementation.
Public Attributes
| Return | Name | Description |
|---|---|---|
LocalSignal< void(const net::TCPSocket::Ptr &)> | AcceptConnection | Fired when a new client connection is accepted; carries a shared_ptr to the new socket. |
AcceptConnection
LocalSignal< void(const net::TCPSocket::Ptr &)> AcceptConnectionFired when a new client connection is accepted; carries a shared_ptr to the new socket.
Public Methods
| Return | Name | Description |
|---|---|---|
TCPSocket | Constructs the TCPSocket and initializes the underlying libuv handle. | |
TCPSocket | Deleted constructor. | |
TCPSocket | Deleted constructor. | |
bool | shutdown virtual | Sends a TCP shutdown request; the socket closes after the peer acknowledges. |
void | close virtual | Closes the socket immediately, releasing all associated resources. |
void | connect virtual | Connects to peerAddress using a libuv connect request. On success, calls onConnect(); on failure, calls setUVError(). |
void | connect virtual | Resolves host via DNS (or maps "localhost"), then connects. |
ssize_t | send virtual | Writes len bytes to the connected peer. |
ssize_t | sendOwned virtual | Sends an owned payload buffer to the connected peer. |
ssize_t | send virtual | Writes len bytes; peerAddress is ignored for TCP (connected stream). |
ssize_t | sendOwned virtual | |
void | bind virtual | Binds the socket to address. Resets and reinitializes the handle if the address family changes. |
void | listen virtual | Starts listening for incoming connections with the given backlog. |
void | acceptConnection virtual | Accepts the next pending client connection and fires AcceptConnection. |
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. |
bool | setNoDelay | Enables or disables TCP_NODELAY (Nagle's algorithm). |
bool | setKeepAlive | Enables or disables TCP keep-alive probes. |
bool | setSimultaneousAccepts | Enables or disables simultaneous accepts on Windows. No-op and returns false on non-Windows platforms. |
void | setMode | Sets the socket mode (ServerSide or ClientSide). |
SocketMode | mode const | Returns the current socket mode. |
void | setError virtual | Sets 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 const | Returns the current socket error, if any. |
bool | closed virtual const | Returns true if the native socket handle is closed. |
net::Address | address virtual const | Returns the IP address and port number of the socket. A wildcard address is returned if the socket is not connected. |
net::Address | peerAddress virtual const | Returns the IP address and port number of the peer socket. A wildcard address is returned if the socket is not connected. |
net::TransportType | transport virtual const | Returns the TCP transport protocol. |
uv::Loop * | loop virtual const | Returns the event loop associated with this socket. |
void | onConnect virtual | Called by the stream layer when the TCP connection is established. |
void | onRead virtual | Called by the stream layer with raw received bytes; wraps them in a MutableBuffer. |
void | onRecv virtual | Dispatches a received buffer to all socket adapters via onSocketRecv. |
void | onError virtual | Dispatches the error to adapters and closes the socket. |
void | onClose virtual | Dispatches the close event to all socket adapters. |
TCPSocket
TCPSocket(uv::Loop * loop)Constructs the TCPSocket and initializes the underlying libuv handle.
Parameters
loopEvent loop to use; defaults to the default loop.
TCPSocket
TCPSocket(const TCPSocket &) = deleteDeleted constructor.
TCPSocket
TCPSocket(TCPSocket &&) = deleteDeleted 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
peerAddressThe 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
hostHostname or IP address string.portDestination port.
send
virtual
virtual ssize_t send(const char * data, size_t len, int flags)Writes len bytes to the connected peer.
Parameters
dataPointer to the data to send.lenNumber of bytes to send.flagsReserved; 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
dataPointer to the data to send.lenNumber of bytes to send.peerAddressIgnored; present for interface conformance.flagsReserved; 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
addressLocal address to bind to.flagsOptional 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
backlogMaximum 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
enabletrue 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
enabletrue to enable keep-alive.delayInitial 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
enabletrue 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
modeThe mode to assign.
mode
const
SocketMode mode() constReturns 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
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.
address
virtual const
virtual net::Address address() constReturns 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() constReturns 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() constReturns the TCP transport protocol.
loop
virtual const
virtual uv::Loop * loop() constReturns 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
dataPointer to received bytes.lenNumber of bytes received.
onRecv
virtual
virtual void onRecv(const MutableBuffer & buf)Dispatches a received buffer to all socket adapters via onSocketRecv.
Parameters
bufThe buffer containing the received data.
onError
virtual
virtual void onError(const icy::Error & error)Dispatches the error to adapters and closes the socket.
Parameters
errorThe error that occurred.
onClose
virtual
virtual void onClose()Dispatches the close event to all socket adapters.
Protected Attributes
| Return | Name | Description |
|---|---|---|
SocketMode | _mode | |
net::Address | _peerAddress | Cached peer address (avoids syscall per recv) |
_mode
SocketMode _mode_peerAddress
net::Address _peerAddressCached peer address (avoids syscall per recv)
Protected Methods
| Return | Name | Description |
|---|---|---|
void | init virtual | Initializes the underlying socket context. |
void | reset virtual | Resets 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
Ptr
std::shared_ptr< TCPSocket > Ptr()Vec
std::vector< Ptr > Vec()