summaryrefslogtreecommitdiff
path: root/netio_posix.c
diff options
context:
space:
mode:
authorLuke T. Shumaker <lukeshu@lukeshu.com>2024-09-22 00:56:20 -0600
committerLuke T. Shumaker <lukeshu@lukeshu.com>2024-09-22 00:56:20 -0600
commit8ae41297e21c96793ab1f2c8d1abcc5f274501e2 (patch)
treec2d8a94ed875c3739a2f730a2ea16dd802948615 /netio_posix.c
parentb13be233849bfec6277638f6bf7261a4b709cedf (diff)
wip netio
Diffstat (limited to 'netio_posix.c')
-rw-r--r--netio_posix.c49
1 files changed, 49 insertions, 0 deletions
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 <aio.h>
+#include <sys/socket.h>
+
+#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();
+
+
+
+}