From 690a9d955e65608b96fad34a2b31e639223798aa Mon Sep 17 00:00:00 2001 From: "Luke T. Shumaker" Date: Wed, 18 Sep 2024 00:04:10 -0600 Subject: wip --- net9p.c | 53 +++++++++++++++++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 20 deletions(-) (limited to 'net9p.c') diff --git a/net9p.c b/net9p.c index 99d3d72..894b4fe 100644 --- a/net9p.c +++ b/net9p.c @@ -1,3 +1,7 @@ +#include +#include +#include +#include #include #include #include @@ -7,36 +11,45 @@ #include "net9p.h" #include "coroutine.h" -void net9p_listen_cr(void *_stack) { - static union { +void net9p_listen_cr(void *_arg) { + (void)_arg; + printf("listen initializng...\n"); + cr_begin(); + printf("listen running...\n"); + + union { struct sockaddr_in in; struct sockaddr gen; } addr = {0}; - if (!addr.in.sin_family) { - addr.in.sin_family = AF_INET; - addr.in.sin_port = 0x2823; - } - net9p_listen_stack_t *stack = _stack; - - cr_begin(); + addr.in.sin_family = AF_INET; + addr.in.sin_port = htons(9001); - stack->fd = socket(AF_INET, SOCK_STREAM|SOCK_NONBLOCK, 0); - bind(stack->fd, &addr.gen, sizeof addr); - listen(stack->fd, 5); + int fd = socket(AF_INET, SOCK_STREAM|SOCK_NONBLOCK, 0); + if (fd < 0) + error(1, errno, "socket"); + if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int)) < 0) + error(1, errno, "setsockopt"); + if (bind(fd, &addr.gen, sizeof addr) < 0) + error(1, errno, "bind"); + if (listen(fd, 5) < 0) + error(1, errno, "listen"); + int conn = 9; + if (!coroutine_add(net9p_worker_cr, &conn)) + error(1, 0, "coroutine_add(net9p_worker_cr, &%d)", conn); for (;;) { - int conn = accept(stack->fd, NULL, NULL); - if (conn >= 0) { - net9p_worker_stack_t *connstack = malloc(sizeof(net9p_worker_stack_t)); - connstack->fd = conn; - coroutine_add(net9p_worker_cr, connstack); - } cr_yield(); } cr_end(); } -void net9p_worker_cr(void *_stack) { - net9p_worker_stack_t *stack = _stack; +void net9p_worker_cr(void *_arg) { + int fd = *((int *)_arg); + cr_begin(); + + printf("worker %zu\n", cr_getcid()); + close(fd); + + cr_end(); } -- cgit v1.2.3-2-g168b