From: Michael D. Lowis Date: Fri, 17 Dec 2021 19:38:56 +0000 (-0500) Subject: added Net_Serve to make it easier to write servers X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=0728b42b2dd53dd25848c49bf54b211ad3a750b4;p=proto%2Faos.git added Net_Serve to make it easier to write servers --- diff --git a/bin/dial.c b/bin/dial.c index 6b5589d..507241b 100644 --- a/bin/dial.c +++ b/bin/dial.c @@ -1,5 +1,4 @@ #include -#include #include char* Usage = "dial [OPTIONS] DIALSTR [CMD [ARG...]]"; diff --git a/bin/listen.c b/bin/listen.c index 3c87533..d205875 100644 --- a/bin/listen.c +++ b/bin/listen.c @@ -1,11 +1,10 @@ #include -#include #include -#include char* Usage = "listen DIALSTR CMD [ARG...]"; +char** Command = NULL; -void Serve(int cfd, char** argv) +void OnNewClient(int cfd) { int pid = fork(); if (pid < 0) @@ -17,7 +16,7 @@ void Serve(int cfd, char** argv) dup2(cfd, 0); dup2(cfd, 1); dup2(cfd, 2); - exit(execvp(argv[0], argv)); + exit(execvp(Command[0], Command)); } close(cfd); } @@ -30,23 +29,8 @@ int main(int argc, char** argv) return 1; } - int sfd = Net_Announce(argv[0]); - if (sfd < 0) - { - fatal("Net_Announce():"); - } - - while (!Net_Listen(sfd, 5)) - { - int cfd; - if ((cfd = Net_Accept(sfd)) >= 0) - { - Serve(cfd, argv+1); - } - while (waitpid(-1, 0, WNOHANG) > 0); - } - perror("listen"); - close(sfd); + Command = argv+1; + Net_Serve(argv[0], OnNewClient); return 0; } diff --git a/bin/rules.mk b/bin/rules.mk index 7b944d4..4952daf 100644 --- a/bin/rules.mk +++ b/bin/rules.mk @@ -1,2 +1,2 @@ -$(BINDIR)/screenlock: LIBS += -lnet -lui -lX11 -lXft -lfontconfig +$(BINDIR)/screenlock: LIBS += -lui -lX11 -lXft -lfontconfig $(BINDIR)/winmgr: LIBS += -lX11 -lXft -lfontconfig -lXinerama diff --git a/inc/liba.h b/inc/liba.h index 705451f..b8e4e73 100644 --- a/inc/liba.h +++ b/inc/liba.h @@ -33,9 +33,9 @@ /* Garbage Collector Interface */ -void* gc_alloc(size_t sz); -void gc_addref(void* p); -void gc_delref(void* p); +void* GC_Allocate(size_t sz); +void GC_AddRef(void* p); +void GC_DelRef(void* p); /* Option Parsing @@ -74,7 +74,7 @@ int Net_Announce(char* dialstr); int Net_Listen(int fd, int backlog); int Net_Accept(int fd); int Net_Dial(char* dialstr); - +void Net_Serve(char* dialstr, void (*on_client)(int cfd)); /* Basic Runtime Facilities diff --git a/lib/a/Net.c b/lib/a/Net.c index 65baf5e..0eea143 100644 --- a/lib/a/Net.c +++ b/lib/a/Net.c @@ -4,6 +4,7 @@ #include #include #include +#include typedef struct { char* network; @@ -62,6 +63,7 @@ int MakeSocket(char* dialstr, struct socket_t* sock) sock->addr.in.sin_port = htons(strtol(conn.service, NULL, 0)); sock->addr.in.sin_addr = ResolveAddress(conn.address); sock->fd = socket(AF_INET, SOCK_STREAM, 0); + setsockopt(sock->fd, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int)); } else if (!strcmp("udp", conn.network)) { @@ -69,6 +71,7 @@ int MakeSocket(char* dialstr, struct socket_t* sock) sock->addr.in.sin_port = htons(strtol(conn.service, NULL, 0)); sock->addr.in.sin_addr = ResolveAddress(conn.address); sock->fd = socket(AF_INET, SOCK_DGRAM, 0); + setsockopt(sock->fd, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int)); } else if (!strcmp("unix", conn.network)) { @@ -144,3 +147,26 @@ int Net_Dial(char* dialstr) } return (rv == 0 ? sock.fd : rv); } + +void Net_Serve(char* dialstr, void (*on_client)(int cfd)) +{ + int sfd = Net_Announce(dialstr); + if (sfd >= 0) + { + while (!Net_Listen(sfd, 5)) + { + int cfd; + if ((cfd = Net_Accept(sfd)) >= 0) + { + on_client(cfd); + } + while (waitpid(-1, 0, WNOHANG) > 0); + } + perror("Net_Listen():"); + close(sfd); + } + else + { + fatal("Net_Announce():"); + } +} diff --git a/lib/a/gc.c b/lib/a/gc.c index 2a5c644..7ca71da 100644 --- a/lib/a/gc.c +++ b/lib/a/gc.c @@ -60,7 +60,7 @@ static unsigned int Primes[] = { /* Public Routines ***************************************/ -void* gc_alloc(size_t sz) +void* GC_Allocate(size_t sz) { sz = (sz / sizeof(intptr_t)) + ((sz % sizeof(intptr_t)) ? 1 : 0); object_t* obj = ecalloc(1, sizeof(object_t) + sz); @@ -69,12 +69,12 @@ void* gc_alloc(size_t sz) return obj->data; } -void gc_addref(void* p) +void GC_AddRef(void* p) { log_add((object_t*)p-1, INCREMENT); } -void gc_delref(void* p) +void GC_DelRef(void* p) { log_add((object_t*)p-1, DECREMENT); }