COROIO: NNet::TSocket Class Reference
COROIO
 
Loading...
Searching...
No Matches
NNet::TSocket Class Reference

High-level asynchronous socket for network communication. More...

#include <socket.hpp>

Inheritance diagram for NNet::TSocket:
NNet::TSocketBase< TSockOps > NNet::TPollerDrivenSocket< TIOCp > NNet::TPollerDrivenSocket< TUring > NNet::TPollerDrivenSocket< T >

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)
 
TSocketoperator= (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.
 
- Public Member Functions inherited from NNet::TSocketBase< TSockOps >
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< TAddressLocalAddr_
 
std::optional< TAddressRemoteAddr_
 

Additional Inherited Members

- Protected Member Functions inherited from NNet::TSocketBase< TSockOps >
 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
 
TSocketBaseoperator= (TSocketBase &other) const =delete
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ TSocket() [1/2]

NNet::TSocket::TSocket ( TPollerBase & poller,
int domain,
int type = SOCK_STREAM )

Constructs a TSocket using the given poller, address family, and socket type.

Parameters
pollerReference to the poller for asynchronous operations.
domainThe address family (e.g., AF_INET, AF_INET6).
typeThe socket type; defaults to SOCK_STREAM.

◆ TSocket() [2/2]

NNet::TSocket::TSocket ( const TAddress & addr,
int fd,
TPollerBase & poller )

Constructs a TSocket for an already-connected socket.

Parameters
addrThe remote address.
fdThe existing socket descriptor.
pollerReference to the poller.

Member Function Documentation

◆ Accept()

auto NNet::TSocket::Accept ( )
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.

Returns
An awaitable object that yields a TSocket for the new connection.
Exceptions
std::system_errorif accept() fails.

◆ Connect()

auto NNet::TSocket::Connect ( const TAddress & addr,
TTime deadline = TTime::max() )
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.

Parameters
addrThe destination address.
deadline(Optional) Timeout deadline; defaults to TTime::max().
Returns
An awaitable object that suspends until the connection is established.
Note
If a deadline is specified and exceeded, the awaitable will throw a timed-out error.

◆ LocalAddr()

const std::optional< TAddress > & NNet::TSocket::LocalAddr ( ) const

Returns the local address to which the socket is bound.

Returns
An optional TAddress holding the local address.

◆ RemoteAddr()

const std::optional< TAddress > & NNet::TSocket::RemoteAddr ( ) const

Returns the remote address of the connected peer.

Returns
An optional TAddress holding the remote peer's address.

The documentation for this class was generated from the following file: