diff options
author | Luke T. Shumaker <lukeshu@lukeshu.com> | 2024-09-18 00:04:10 -0600 |
---|---|---|
committer | Luke T. Shumaker <lukeshu@lukeshu.com> | 2024-09-18 00:04:10 -0600 |
commit | 690a9d955e65608b96fad34a2b31e639223798aa (patch) | |
tree | 60bf87a326b6433297ee86e8d039199e4d4becdb /net9p.c | |
parent | 4412b908755c7b9899cf8cce4e0985aa6816121a (diff) |
wip
Diffstat (limited to 'net9p.c')
-rw-r--r-- | net9p.c | 53 |
1 files changed, 33 insertions, 20 deletions
@@ -1,3 +1,7 @@ +#include <errno.h> +#include <stdio.h> +#include <unistd.h> +#include <error.h> #include <stdlib.h> #include <sys/types.h> #include <sys/socket.h> @@ -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(); } |