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

Poller implementation based on io_uring. More...

#include <uring.hpp>

Inheritance diagram for NNet::TUring:
NNet::TPollerBase

Public Types

using TSocket = NNet::TPollerDrivenSocket<TUring>
 Alias for the poller-driven socket type.
 
using TFileHandle = NNet::TPollerDrivenFileHandle<TUring>
 Alias for the poller-driven file handle type.
 

Public Member Functions

 TUring (int queueSize=256)
 Constructs a TUring instance.
 
 ~TUring ()
 Destructor cleans up the io_uring and related resources.
 
void Read (int fd, void *buf, int size, std::coroutine_handle<> handle)
 Posts an asynchronous read operation.
 
void Write (int fd, const void *buf, int size, std::coroutine_handle<> handle)
 Posts an asynchronous write operation.
 
void Recv (int fd, void *buf, int size, std::coroutine_handle<> handle)
 Posts an asynchronous receive operation.
 
void Send (int fd, const void *buf, int size, std::coroutine_handle<> handle)
 Posts an asynchronous send operation.
 
void Accept (int fd, struct sockaddr *addr, socklen_t *len, std::coroutine_handle<> handle)
 Posts an asynchronous accept operation.
 
void Connect (int fd, const sockaddr *addr, socklen_t len, std::coroutine_handle<> handle)
 Posts an asynchronous connect operation.
 
void Cancel (int fd)
 Cancels pending operations on the specified file descriptor.
 
void Cancel (std::coroutine_handle<> h)
 Cancels pending operations associated with a specific coroutine handle.
 
void Register (int fd)
 Registers a file descriptor with the IO_uring poller.
 
int Wait (timespec ts={10, 0})
 Waits for I/O completions.
 
void Poll ()
 Polls for I/O completions.
 
int Result ()
 Retrieves the result of the last completed I/O completion.
 
void Submit ()
 Submits queued I/O requests to the kernel.
 
std::tuple< int, int, int > Kernel () const
 For testing: retrieves kernel version.
 
const std::string & KernelStr () const
 For testing: retrieves a string describing kernel.
 
- Public Member Functions inherited from NNet::TPollerBase
 TPollerBase ()=default
 Default constructor.
 
 TPollerBase (const TPollerBase &)=delete
 Copying is disabled.
 
TPollerBaseoperator= (const TPollerBase &)=delete
 
unsigned AddTimer (TTime deadline, THandle h)
 Schedules a timer.
 
bool RemoveTimer (unsigned timerId, TTime deadline)
 Removes or cancels a timer.
 
void AddRead (int fd, THandle h)
 Registers a read event on a file descriptor.
 
void AddWrite (int fd, THandle h)
 Registers a write event on a file descriptor.
 
void AddRemoteHup (int fd, THandle h)
 Registers a remote hang-up (RHUP) event.
 
void RemoveEvent (int fd)
 Removes registered events for a specific file descriptor.
 
void RemoveEvent (THandle)
 Removes events associated with a given coroutine handle.
 
auto Sleep (TTime until)
 Suspends execution until the specified time.
 
template<typename Rep, typename Period>
auto Sleep (std::chrono::duration< Rep, Period > duration)
 Overload of Sleep() accepting a duration.
 
auto Yield ()
 Yields execution to the next event loop iteration.
 
void Wakeup (TEvent &&change)
 Wakes up a coroutine waiting on an event.
 
void WakeupReadyHandles ()
 Wakes up all coroutines waiting on ready events.
 
void SetMaxDuration (std::chrono::milliseconds maxDuration)
 Sets the maximum polling duration.
 
auto TimersSize () const
 Returns the number of scheduled timers.
 

Additional Inherited Members

- Protected Member Functions inherited from NNet::TPollerBase
timespec GetTimeout () const
 Computes the poll timeout based on scheduled timers.
 
void Reset ()
 Clears the lists of ready events and pending changes.
 
void ProcessTimers ()
 Processes scheduled timers.
 
- Static Protected Member Functions inherited from NNet::TPollerBase
static constexpr timespec GetMaxDuration (std::chrono::milliseconds duration)
 Computes a timespec from a duration.
 
- Protected Attributes inherited from NNet::TPollerBase
int MaxFd_ = -1
 Highest file descriptor in use.
 
std::vector< TEventChanges_
 Pending changes (registered events).
 
std::vector< TEventReadyEvents_
 Events ready to wake up their coroutines.
 
unsigned TimerId_ = 0
 Counter for generating unique timer IDs.
 
std::priority_queue< TTimerTimers_
 Priority queue for scheduled timers.
 
TTime LastTimersProcessTime_
 Last time timers were processed.
 
unsigned LastFiredTimer_ = (unsigned)(-1)
 ID of the last fired timer.
 
std::chrono::milliseconds MaxDuration_ = std::chrono::milliseconds(100)
 Maximum poll duration.
 
timespec MaxDurationTs_ = GetMaxDuration(MaxDuration_)
 Max duration represented as timespec.
 

Detailed Description

Poller implementation based on io_uring.

TUring inherits from TPollerBase and implements asynchronous I/O operations using the Linux io_uring API. It provides methods for performing asynchronous read, write, receive, send, accept, and connect operations.

Key features:

  • Uses io_uring to queue and submit asynchronous I/O operations.
  • Provides operations such as Read(), Write(), Recv(), Send(), Accept() and Connect().
  • Offers additional methods for cancelling pending operations, registering file descriptors, waiting for completions, and submitting queued requests.

Type aliases:

Example usage:

// Create an io_uring poller with a queue size of 256.
TUring uringPoller(256);
// Register a socket or file descriptor.
uringPoller.Register(socketFd);
// Post an asynchronous read.
uringPoller.Read(socketFd, buffer, bufferSize, coroutineHandle);
// Submit queued operations.
uringPoller.Submit();
// Poll for I/O completions.
int ret = uringPoller.Wait();
// Process the result of the completed operations.
int result = uringPoller.Result();
TUring(int queueSize=256)
Constructs a TUring instance.

Additional methods for testing:

  • Kernel() returns a tuple of kernel-provided statistics.
  • KernelStr() returns a descriptive string with kernel statistics.

Constructor & Destructor Documentation

◆ TUring()

NNet::TUring::TUring ( int queueSize = 256)

Constructs a TUring instance.

Parameters
queueSizeThe desired size of the io_uring submission queue (default is 256).

Member Function Documentation

◆ Accept()

void NNet::TUring::Accept ( int fd,
struct sockaddr * addr,
socklen_t * len,
std::coroutine_handle<> handle )

Posts an asynchronous accept operation.

Parameters
fdThe listening socket descriptor.
addrPointer to a sockaddr structure where the client address is stored.
lenPointer to the length of the address structure.
handleCoroutine handle to resume upon acceptance.

◆ Cancel() [1/2]

void NNet::TUring::Cancel ( int fd)

Cancels pending operations on the specified file descriptor.

Parameters
fdThe file descriptor.

◆ Cancel() [2/2]

void NNet::TUring::Cancel ( std::coroutine_handle<> h)

Cancels pending operations associated with a specific coroutine handle.

Parameters
hThe coroutine handle.

◆ Connect()

void NNet::TUring::Connect ( int fd,
const sockaddr * addr,
socklen_t len,
std::coroutine_handle<> handle )

Posts an asynchronous connect operation.

Parameters
fdThe socket descriptor.
addrPointer to the destination address.
lenSize of the destination address structure.
handleCoroutine handle to resume upon connection.

◆ Kernel()

std::tuple< int, int, int > NNet::TUring::Kernel ( ) const

For testing: retrieves kernel version.

Returns
A tuple of integers representing kernel version.

◆ KernelStr()

const std::string & NNet::TUring::KernelStr ( ) const

For testing: retrieves a string describing kernel.

Returns
A descriptive string with kernel version.

◆ Poll()

void NNet::TUring::Poll ( )
inline

Polls for I/O completions.

This method simply calls Wait() with a computed timeout.

◆ Read()

void NNet::TUring::Read ( int fd,
void * buf,
int size,
std::coroutine_handle<> handle )

Posts an asynchronous read operation.

Parameters
fdThe file descriptor.
bufBuffer where data is to be stored.
sizeNumber of bytes to read.
handleCoroutine handle to resume upon completion.

◆ Recv()

void NNet::TUring::Recv ( int fd,
void * buf,
int size,
std::coroutine_handle<> handle )

Posts an asynchronous receive operation.

Parameters
fdThe file descriptor.
bufBuffer where received data will be stored.
sizeMaximum number of bytes to receive.
handleCoroutine handle to resume upon completion.

◆ Register()

void NNet::TUring::Register ( int fd)

Registers a file descriptor with the IO_uring poller.

Parameters
fdThe file descriptor to register.

◆ Result()

int NNet::TUring::Result ( )

Retrieves the result of the last completed I/O completion.

Returns
An integer result, such as the number of bytes transferred.

◆ Send()

void NNet::TUring::Send ( int fd,
const void * buf,
int size,
std::coroutine_handle<> handle )

Posts an asynchronous send operation.

Parameters
fdThe file descriptor.
bufBuffer containing data to send.
sizeNumber of bytes to send.
handleCoroutine handle to resume upon completion.

◆ Wait()

int NNet::TUring::Wait ( timespec ts = {10, 0})

Waits for I/O completions.

Parameters
tsOptional timeout (default is 10 seconds, expressed as {10, 0}).
Returns
The number of completed events (or a negative value on error).

◆ Write()

void NNet::TUring::Write ( int fd,
const void * buf,
int size,
std::coroutine_handle<> handle )

Posts an asynchronous write operation.

Parameters
fdThe file descriptor.
bufBuffer with data to write.
sizeNumber of bytes to write.
handleCoroutine handle to resume upon completion.

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