COROIO: NNet::TSocketBase< TSockOps > Class Template Reference
COROIO
 
Loading...
Searching...
No Matches
NNet::TSocketBase< TSockOps > Class Template Reference

Template base class implementing asynchronous socket I/O operations. More...

#include <socket.hpp>

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

Classes

struct  TAwaitable
 

Public Member Functions

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 Member Functions

 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

template<typename TSockOps>
class NNet::TSocketBase< TSockOps >

Template base class implementing asynchronous socket I/O operations.

This class extends the internal TSocketBase<void> by implementing asynchronous operations based on a type-specific socket operations trait, TSockOps. The TSockOps type must implement the following static methods:

  • static int TSockOps::read(int fd, void* buf, size_t size)
  • static int TSockOps::write(int fd, const void* buf, size_t size)
  • static int TSockOps::close(int fd)

It provides awaitable methods for reading and writing:

Both variants such as TSocket and TFileHandle (or similarly named higher-level wrappers) inherit from this class and expose the more user-friendly API. End users should use these derived classes, rather than interacting with TSocketBase<TSockOps> directly.

Template Parameters
TSockOpsThe type that provides low-level socket operations.

Constructor & Destructor Documentation

◆ TSocketBase() [1/2]

template<typename TSockOps>
NNet::TSocketBase< TSockOps >::TSocketBase ( TPollerBase & poller,
int domain,
int type )
inlineprotected

Constructs a TSocketBase with a new socket descriptor.

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

◆ TSocketBase() [2/2]

template<typename TSockOps>
NNet::TSocketBase< TSockOps >::TSocketBase ( int fd,
TPollerBase & poller )
inlineprotected

Constructs a TSocketBase from an existing socket descriptor.

Parameters
fdAn already created socket descriptor.
pollerReference to the poller managing asynchronous I/O.

Member Function Documentation

◆ Close()

template<typename TSockOps>
void NNet::TSocketBase< TSockOps >::Close ( )
inline

Closes the socket.

Closes the socket using the low-level operation provided by TSockOps, removes it from the poller, and marks the descriptor as invalid.

◆ Monitor()

template<typename TSockOps>
auto NNet::TSocketBase< TSockOps >::Monitor ( )
inline

Monitors the socket for remote hang-up (closure).

This awaitable suspends the coroutine until a remote hang-up is detected.

Returns
An awaitable object that yields a boolean value (true) once the remote hang-up is detected.

◆ ReadSome()

template<typename TSockOps>
auto NNet::TSocketBase< TSockOps >::ReadSome ( void * buf,
size_t size )
inline

Asynchronously reads data from the socket into the provided buffer.

When awaited, this method suspends the current coroutine until the read operation completes and then returns the number of bytes read.

Parameters
bufPointer to the destination buffer.
sizeThe number of bytes to read.
Returns
An awaitable object that yields the number of bytes read.

◆ ReadSomeYield()

template<typename TSockOps>
auto NNet::TSocketBase< TSockOps >::ReadSomeYield ( void * buf,
size_t size )
inline

Forces a read operation on the next event loop iteration.

Similar to ReadSome(), but this variant ensures that the read is deferred to the next iteration of the event loop.

Parameters
bufPointer to the buffer where data will be stored.
sizeNumber of bytes to read.
Returns
An awaitable object yielding the number of bytes read.

◆ WriteSome()

template<typename TSockOps>
auto NNet::TSocketBase< TSockOps >::WriteSome ( const void * buf,
size_t size )
inline

Asynchronously writes data from the provided buffer to the socket.

This method suspends the current coroutine until the data is written, and then returns the number of bytes successfully written.

Parameters
bufPointer to the data to be written.
sizeThe number of bytes to write.
Returns
An awaitable object that yields the number of bytes written.

◆ WriteSomeYield()

template<typename TSockOps>
auto NNet::TSocketBase< TSockOps >::WriteSomeYield ( const void * buf,
size_t size )
inline

Forces a write operation on the next event loop iteration.

This variant behaves similarly to WriteSome() but defers execution until the next loop iteration.

Parameters
bufPointer to the data to be written.
sizeNumber of bytes to write.
Returns
An awaitable object that yields the number of bytes written.

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