std1.0.0-beta-7 SafeC docs

Module async::net

#include <std/async/net.h> source

async: I/O driver, Runtime, async TCP

Futures that wait for sockets. They make the non-blocking call at once (std/sched/io_nb.h's sockets); when it would block, they register the task's waker with the IoDriver for that fd's readiness and are Pending. The IoDriver is a std::Reactor (std/sched/reactor.h: epoll on Linux, kqueue on macOS/BSD, WSAPoll on Windows) driven through Reactor::poll_events, and it is also the executor's Park source: when no task is runnable, the executor parks in the OS readiness queue until a socket is ready, the next timer is due, or another thread wakes a task.

Runtime bundles a LocalExecutor and an IoDriver (tokio's current-thread runtime):

static struct Runtime rt;           // must not move once initialized
rt = Runtime::new();
if (rt.init() != 0) { ... }          // opens the OS event queue
rt.spawn(&server_task);
int r = rt.block_on(&client);        // or rt.run(): until every task is done
rt.close_();

Inside tasks:

self.l = AsyncTcpListener::bind(8080);          // .fd < 0 on failure
self.acc = self.l.accept();
AWAIT_INTO(1, self.conn, self.acc);             // struct AsyncTcpStream; .fd < 0 on error
self.rd = self.conn.read(self.buf[0..512]);
AWAIT_INTO(2, self.n, self.rd);                 // bytes read, 0 at EOF, -1 on error
self.wr = self.conn.write_all(self.buf[0..self.n]);
AWAIT_INTO(3, self.ok, self.wr);                // 1 when all written
self.conn.close();

self.cn = AsyncTcpStream::connect(sched_ipv4(127, 0, 0, 1), 8080);
AWAIT_INTO(4, self.conn, self.cn);

Limits: ASYNC_IO_WAITERS concurrent waits per driver (a wait that finds the table full re-polls instead); on Linux one fd is awaited for one direction at a time (epoll registers an fd once — a read and a write waiting on the same socket at the same time make the second one re-poll). Wakes from other threads end a park at once on POSIX (a wake pipe); on Windows the driver parks at most ASYNC_IO_REMOTE_MS at a time so they are seen within that. (WSAPoll on Windows before 10 2004 never reports a refused connect, so a ConnectFut to a closed port waits there until dropped or timed out: give connects a timeout() on old Windows.) Hosted targets only.

Structs

IoWaiter
IoDriver
RuntimeA LocalExecutor parked in an IoDriver.
AsyncTcpStream
AsyncTcpListener
IoReadyReady once 'fd' is ready for 'filter' (SCHED_READ / SCHED_WRITE), for sockets and pipes these types don't cover.
AcceptFut
ConnectFut
ReadFut
WriteFut
WriteAllFut

Functions

io_driverThe IoDriver of the executor polling 'cx', or null outside a Runtime.
async_sock_local_portThe local port of a bound socket (0 on error).
async_sock_readSocket read/write/close that work on every platform (recv/send/ closesocket on Windows; no SIGPIPE).
async_sock_write
async_sock_close

Macros

ASYNC_IO_WAITERS
ASYNC_IO_REMOTE_MS