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

IOCP-based poller for asynchronous I/O on Windows. More...

#include <iocp.hpp>

Inheritance diagram for NNet::TIOCp:
NNet::TPollerBase

Public Types

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

Public Member Functions

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 all pending operations on the specified file descriptor.
 
void Register (int fd)
 Registers a file descriptor with the IOCP.
 
int Result ()
 Retrieves the result of the last completed IOCP operation.
 
void Poll ()
 Polls for IOCP events.
 
- 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

IOCP-based poller for asynchronous I/O on Windows.

TIOCp inherits from TPollerBase and implements an asynchronous poller using the Windows I/O Completion Ports (IOCP) API. It provides methods for posting asynchronous read, write, accept, connect, and cancellation operations. To optimize performance, TIOCp uses a custom arena allocator (TArenaAllocator) to preallocate IOCP event structures, avoiding per-operation dynamic memory allocations required by the API.

Type aliases provided:

Example usage:

TIOCp iocpPoller;
// Register a socket:
iocpPoller.Register(socketFd);
// Post asynchronous operations, then call:
iocpPoller.Poll();
void Register(int fd)
Registers a file descriptor with the IOCP.
void Poll()
Polls for IOCP events.
Note
This class is specific to Windows and uses IOCP for high-performance I/O.

Member Function Documentation

◆ Accept()

void NNet::TIOCp::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 to receive the client address.
lenPointer to a variable specifying the size of the address structure.
handleThe coroutine handle to resume when an incoming connection is accepted.

◆ Cancel()

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

Cancels all pending operations on the specified file descriptor.

Parameters
fdThe file descriptor.

◆ Connect()

void NNet::TIOCp::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.
handleThe coroutine handle to resume when the connection is established.

◆ Poll()

void NNet::TIOCp::Poll ( )

Polls for IOCP events.

This method waits for asynchronous I/O events and processes their completion.

◆ Read()

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

Posts an asynchronous read operation.

Parameters
fdThe file descriptor.
bufBuffer to store read data.
sizeNumber of bytes to read.
handleThe coroutine handle to resume when the operation completes.

◆ Recv()

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

Posts an asynchronous receive operation.

Parameters
fdThe file descriptor.
bufBuffer to store received data.
sizeMaximum number of bytes to receive.
handleThe coroutine handle to resume when the operation completes.

◆ Register()

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

Registers a file descriptor with the IOCP.

Parameters
fdThe file descriptor to register.

◆ Result()

int NNet::TIOCp::Result ( )

Retrieves the result of the last completed IOCP operation.

Returns
An integer representing the result of the operation.

◆ Send()

void NNet::TIOCp::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.
handleThe coroutine handle to resume when the operation completes.

◆ Write()

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

Posts an asynchronous write operation.

Parameters
fdThe file descriptor.
bufBuffer containing data to write.
sizeNumber of bytes to write.
handleThe coroutine handle to resume when the operation completes.

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