qb  2.0.0.0
C++17 Actor Framework
qb Issue Watch Star Fork Follow @isndev
Loading...
Searching...
No Matches
qb::io::tcp::socket Class Reference

Class implementing TCP socket functionality for reliable, stream-oriented communication. More...

#include <socket.h>

Inheritance diagram for qb::io::tcp::socket:
Collaboration diagram for qb::io::tcp::socket:

Public Member Functions

 socket ()=default
 Default constructor.
 socket (socket const &)=delete
 Deleted copy constructor.
 socket (socket &&)=default
 Default move constructor.
socketoperator= (socket &&)=default
 Default move assignment operator.
 socket (io::socket &&sock) noexcept
 Constructor from a generic qb::io::socket (move semantics).
socketoperator= (io::socket &&sock) noexcept
 Move assignment operator from a generic qb::io::socket.
int init (int af=AF_INET) noexcept
 Initialize (open) the TCP socket.
int bind (qb::io::endpoint const &ep) noexcept
 Bind the socket to a specific local endpoint.
int bind (qb::io::uri const &u) noexcept
 Bind the socket to an endpoint specified by a URI.
int connect (qb::io::endpoint const &ep) noexcept
 Connect to a remote TCP endpoint.
int connect (uri const &u) noexcept
 Connect to a remote TCP endpoint specified by a URI.
int connect_v4 (std::string const &host, uint16_t port) noexcept
 Connect to an IPv4 TCP server.
int connect_v6 (std::string const &host, uint16_t port) noexcept
 Connect to an IPv6 TCP server.
int connect_un (std::string const &path) noexcept
 Connect to a Unix domain TCP-style socket.
int n_connect (qb::io::endpoint const &ep) noexcept
 Initiate a non-blocking connect to a remote TCP endpoint.
void connected () noexcept
 Called after a non-blocking connect attempt succeeds or needs to be finalized.
int n_connect (uri const &u) noexcept
 Initiate a non-blocking connect to a remote TCP endpoint specified by a URI.
int n_connect_v4 (std::string const &host, uint16_t port) noexcept
 Initiate a non-blocking connect to an IPv4 TCP server.
int n_connect_v6 (std::string const &host, uint16_t port) noexcept
 Initiate a non-blocking connect to an IPv6 TCP server.
int n_connect_un (std::string const &path) noexcept
 Initiate a non-blocking connect to a Unix domain TCP-style socket.
int read (void *dest, std::size_t len) const noexcept
 Read data from the connected TCP socket.
int write (const void *data, std::size_t size) const noexcept
 Write data to the connected TCP socket.
int disconnect () const noexcept
 Disconnect the TCP socket.

Static Public Member Functions

static constexpr bool is_secure () noexcept
 Indicates that this socket implementation is not secure.

Detailed Description

Class implementing TCP socket functionality for reliable, stream-oriented communication.

This class provides TCP socket functionality, inheriting protectedly from the base qb::io::socket and exposing a TCP-specific interface. It supports connecting to and communicating over TCP/IPv4, TCP/IPv6, and Unix Domain Sockets (if enabled). It is used as the underlying I/O primitive for qb::io::transport::tcp.

Constructor & Destructor Documentation

◆ socket() [1/3]

qb::io::tcp::socket::socket ( )
default

Default constructor.

Creates an uninitialized TCP socket. Call init() before use.

◆ socket() [2/3]

qb::io::tcp::socket::socket ( socket const & )
delete

Deleted copy constructor.

Sockets are not copyable.

◆ socket() [3/3]

qb::io::tcp::socket::socket ( io::socket && sock)
noexcept

Constructor from a generic qb::io::socket (move semantics).

Parameters
sockA generic qb::io::socket instance, typically already opened with SOCK_STREAM. The state of sock is moved into this tcp::socket.

Member Function Documentation

◆ operator=() [1/2]

socket & qb::io::tcp::socket::operator= ( socket && )
default

Default move assignment operator.

Returns
Reference to this socket.

◆ operator=() [2/2]

socket & qb::io::tcp::socket::operator= ( io::socket && sock)
noexcept

Move assignment operator from a generic qb::io::socket.

Parameters
sockA generic qb::io::socket to move from.
Returns
Reference to this tcp::socket.

◆ init()

int qb::io::tcp::socket::init ( int af = AF_INET)
noexcept

Initialize (open) the TCP socket.

Parameters
afAddress family (e.g., AF_INET, AF_INET6, AF_UNIX). Defaults to AF_INET.
Returns
0 on success, or a non-zero error code on failure.

This method calls the base qb::io::socket::open() with SOCK_STREAM type.

◆ bind() [1/2]

int qb::io::tcp::socket::bind ( qb::io::endpoint const & ep)
noexcept

Bind the socket to a specific local endpoint.

Parameters
epThe qb::io::endpoint to bind to.
Returns
0 on success, or a non-zero error code on failure.
See also
qb::io::socket::bind(const endpoint &)

◆ bind() [2/2]

int qb::io::tcp::socket::bind ( qb::io::uri const & u)
noexcept

Bind the socket to an endpoint specified by a URI.

Parameters
uThe qb::io::uri specifying the local address and port to bind to.
Returns
0 on success, or a non-zero error code on failure.

◆ connect() [1/2]

int qb::io::tcp::socket::connect ( qb::io::endpoint const & ep)
noexcept

Connect to a remote TCP endpoint.

Parameters
epThe qb::io::endpoint of the remote server.
Returns
0 on success (blocking connect), or a non-zero error code on failure.
See also
qb::io::socket::connect(const endpoint &)

◆ connect() [2/2]

int qb::io::tcp::socket::connect ( uri const & u)
noexcept

Connect to a remote TCP endpoint specified by a URI.

Parameters
uThe qb::io::uri of the remote server.
Returns
0 on success, or a non-zero error code on failure.

◆ connect_v4()

int qb::io::tcp::socket::connect_v4 ( std::string const & host,
uint16_t port )
noexcept

Connect to an IPv4 TCP server.

Parameters
hostThe hostname or IP address string of the server.
portThe port number of the server.
Returns
0 on success, or a non-zero error code on failure.

◆ connect_v6()

int qb::io::tcp::socket::connect_v6 ( std::string const & host,
uint16_t port )
noexcept

Connect to an IPv6 TCP server.

Parameters
hostThe hostname or IP address string of the server.
portThe port number of the server.
Returns
0 on success, or a non-zero error code on failure.

◆ connect_un()

int qb::io::tcp::socket::connect_un ( std::string const & path)
noexcept

Connect to a Unix domain TCP-style socket.

Parameters
pathThe file system path of the Unix domain socket.
Returns
0 on success, or a non-zero error code on failure.
Note
This is only effective if QB_ENABLE_UDS is active and the system supports AF_UNIX.

◆ n_connect() [1/2]

int qb::io::tcp::socket::n_connect ( qb::io::endpoint const & ep)
noexcept

Initiate a non-blocking connect to a remote TCP endpoint.

Parameters
epThe qb::io::endpoint of the remote server.
Returns
0 if connection is in progress (use select or an event loop to check for completion) or succeeded immediately. Returns a non-zero error code on immediate failure.

Sets the socket to non-blocking before attempting to connect. The connected() method (or handle_write_ready) should be checked later to confirm connection.

See also
qb::io::socket::connect_n(const endpoint &)

◆ connected()

void qb::io::tcp::socket::connected ( )
inlinenoexcept

Called after a non-blocking connect attempt succeeds or needs to be finalized.

For TCP, this method typically checks for socket errors to confirm successful connection after n_connect indicated progress. In this base tcp::socket, it's a no-op but can be overridden by derived classes (like ssl::socket for handshake).

◆ n_connect() [2/2]

int qb::io::tcp::socket::n_connect ( uri const & u)
noexcept

Initiate a non-blocking connect to a remote TCP endpoint specified by a URI.

Parameters
uThe qb::io::uri of the remote server.
Returns
0 if connection is in progress or succeeded immediately, non-zero error code on immediate failure.

◆ n_connect_v4()

int qb::io::tcp::socket::n_connect_v4 ( std::string const & host,
uint16_t port )
noexcept

Initiate a non-blocking connect to an IPv4 TCP server.

Parameters
hostThe hostname or IP address string of the server.
portThe port number of the server.
Returns
0 if connection is in progress or succeeded immediately, non-zero error code on immediate failure.

◆ n_connect_v6()

int qb::io::tcp::socket::n_connect_v6 ( std::string const & host,
uint16_t port )
noexcept

Initiate a non-blocking connect to an IPv6 TCP server.

Parameters
hostThe hostname or IP address string of the server.
portThe port number of the server.
Returns
0 if connection is in progress or succeeded immediately, non-zero error code on immediate failure.

◆ n_connect_un()

int qb::io::tcp::socket::n_connect_un ( std::string const & path)
noexcept

Initiate a non-blocking connect to a Unix domain TCP-style socket.

Parameters
pathThe file system path of the Unix domain socket.
Returns
0 if connection is in progress or succeeded immediately, non-zero error code on immediate failure.

◆ read()

int qb::io::tcp::socket::read ( void * dest,
std::size_t len ) const
noexcept

Read data from the connected TCP socket.

Parameters
destPointer to the buffer where received data will be stored.
lenMaximum number of bytes to read into the buffer.
Returns
Number of bytes actually read. 0 indicates graceful shutdown by the peer. A negative value indicates an error (e.g., WSAEWOULDBLOCK or EAGAIN if non-blocking and no data).
See also
qb::io::socket::recv(void*, int, int)

◆ write()

int qb::io::tcp::socket::write ( const void * data,
std::size_t size ) const
noexcept

Write data to the connected TCP socket.

Parameters
dataPointer to the data to be sent.
sizeNumber of bytes to send from the data buffer.
Returns
Number of bytes actually written. This can be less than size if the send buffer is full. A negative value indicates an error.
See also
qb::io::socket::send(const void*, int, int)

◆ disconnect()

int qb::io::tcp::socket::disconnect ( ) const
noexcept

Disconnect the TCP socket.

Returns
0 on success, or a non-zero error code on failure.

This typically calls qb::io::socket::shutdown() with SD_BOTH and then close().