High-level asynchronous socket for network communication. More...
#include <socket.hpp>
Public Types | |
using | TPoller = TPollerBase |
Public Member Functions | |
TSocket (TPollerBase &poller, int domain, int type=SOCK_STREAM) | |
Constructs a TSocket using the given poller, address family, and socket type. | |
TSocket (const TAddress &addr, int fd, TPollerBase &poller) | |
Constructs a TSocket for an already-connected socket. | |
TSocket (TSocket &&other) | |
TSocket & | operator= (TSocket &&other) |
auto | Connect (const TAddress &addr, TTime deadline=TTime::max()) |
Asynchronously connects to the specified address. | |
auto | Accept () |
Asynchronously accepts an incoming connection. | |
void | Bind (const TAddress &addr) |
Binds the socket to the specified local address. | |
void | Listen (int backlog=128) |
Puts the socket in a listening state with an optional backlog (default is 128). | |
const std::optional< TAddress > & | RemoteAddr () const |
Returns the remote address of the connected peer. | |
const std::optional< TAddress > & | LocalAddr () const |
Returns the local address to which the socket is bound. | |
int | Fd () const |
Returns the underlying socket descriptor. | |
![]() | |
auto | ReadSome (void *buf, size_t size) |
Asynchronously reads data from the socket into the provided buffer. | |
auto | ReadSomeYield (void *buf, size_t size) |
Forces a read operation on the next event loop iteration. | |
auto | WriteSome (const void *buf, size_t size) |
Asynchronously writes data from the provided buffer to the socket. | |
auto | WriteSomeYield (const void *buf, size_t size) |
Forces a write operation on the next event loop iteration. | |
auto | Monitor () |
Monitors the socket for remote hang-up (closure). | |
void | Close () |
Closes the socket. | |
Protected Attributes | |
std::optional< TAddress > | LocalAddr_ |
std::optional< TAddress > | RemoteAddr_ |
Additional Inherited Members | |
![]() | |
TSocketBase (TPollerBase &poller, int domain, int type) | |
Constructs a TSocketBase with a new socket descriptor. | |
TSocketBase (int fd, TPollerBase &poller) | |
Constructs a TSocketBase from an existing socket descriptor. | |
TSocketBase (const TSocketBase &other)=delete | |
TSocketBase & | operator= (TSocketBase &other) const =delete |
High-level asynchronous socket for network communication.
TSocket provides an easy-to-use interface for common network operations such as connecting, accepting, binding, and listening. It also stores the local and remote addresses.
NNet::TSocket::TSocket | ( | TPollerBase & | poller, |
int | domain, | ||
int | type = SOCK_STREAM ) |
Constructs a TSocket using the given poller, address family, and socket type.
poller | Reference to the poller for asynchronous operations. |
domain | The address family (e.g., AF_INET, AF_INET6). |
type | The socket type; defaults to SOCK_STREAM. |
NNet::TSocket::TSocket | ( | const TAddress & | addr, |
int | fd, | ||
TPollerBase & | poller ) |
Constructs a TSocket for an already-connected socket.
addr | The remote address. |
fd | The existing socket descriptor. |
poller | Reference to the poller. |
|
inline |
Asynchronously accepts an incoming connection.
This awaitable suspends until a new connection is ready on the socket and then returns a new TSocket object representing the client connection.
std::system_error | if accept() fails. |
|
inline |
Asynchronously connects to the specified address.
Initiates a connection to addr
. If the socket is already connected, an error is thrown. A deadline may be specified to time out the connection.
addr | The destination address. |
deadline | (Optional) Timeout deadline; defaults to TTime::max(). |
const std::optional< TAddress > & NNet::TSocket::LocalAddr | ( | ) | const |
Returns the local address to which the socket is bound.
const std::optional< TAddress > & NNet::TSocket::RemoteAddr | ( | ) | const |
Returns the remote address of the connected peer.