Template base class implementing asynchronous socket I/O operations. More...
#include <socket.hpp>
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 | |
TSocketBase & | operator= (TSocketBase &other) const =delete |
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.
TSockOps | The type that provides low-level socket operations. |
|
inlineprotected |
Constructs a TSocketBase with a new socket descriptor.
poller | Reference to the poller responsible for asynchronous operations. |
domain | The address family (e.g., AF_INET, AF_INET6). |
type | The socket type (e.g., SOCK_STREAM, SOCK_DGRAM). |
|
inlineprotected |
Constructs a TSocketBase from an existing socket descriptor.
fd | An already created socket descriptor. |
poller | Reference to the poller managing asynchronous I/O. |
|
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.
|
inline |
Monitors the socket for remote hang-up (closure).
This awaitable suspends the coroutine until a remote hang-up is detected.
|
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.
buf | Pointer to the destination buffer. |
size | The number of bytes to read. |
|
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.
buf | Pointer to the buffer where data will be stored. |
size | Number of bytes to read. |
|
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.
buf | Pointer to the data to be written. |
size | The number of bytes to write. |
|
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.
buf | Pointer to the data to be written. |
size | Number of bytes to write. |