From 8ae41297e21c96793ab1f2c8d1abcc5f274501e2 Mon Sep 17 00:00:00 2001 From: "Luke T. Shumaker" Date: Sun, 22 Sep 2024 00:56:20 -0600 Subject: wip netio --- netio_posix.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 netio_posix.c (limited to 'netio_posix.c') diff --git a/netio_posix.c b/netio_posix.c new file mode 100644 index 0000000..0aa8277 --- /dev/null +++ b/netio_posix.c @@ -0,0 +1,49 @@ +#include +#include + +#include "coroutine.h" + +/* http://davmac.org/davpage/linux/async-io.html */ +void netio_init(void) { + sigaction(TODO) +} + +int netio_accept(int socket) { + /* AFAICT there is no good POSIX way to do this without busy-polling. */ + for (;;) { + int conn = accept(socket, NULL, NULL); + if (conn < 0) { + if (errno == EAGAIN || errno == EWOULDBLOCK) { + cr_yield(); + continue; + } + return -errno; + } + return conn; + } +} + +int netio_read(int conn, void *buf, size_t count) { + int r; + struct aiocb ctl_block = { + .aio_filedes = conn, + .aio_buf = buff, + .aio_nbytes = count, + .aio_sigevent = { + .sigev_notify = SIGEV_SIGNAL, + .sigev_signo = SIGIO, + .sigev_value = { + .sigval_int = (int)cr_getcid(), + }, + }, + }; + + if (aio_read(&ctl_block) < 0) + return -errno; + + while ((r = aio_error(&ctl_block)) == EINPROGRESS) + cr_pause_and_yield(); + + + +} -- cgit v1.2.3-2-g168b