From b1b7c92aa96af5502f276819b628b05d54f4655c Mon Sep 17 00:00:00 2001 From: kartofen Date: Sun, 16 Oct 2022 22:55:45 +0300 Subject: nearly works --- src/socket.c | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) (limited to 'src/socket.c') diff --git a/src/socket.c b/src/socket.c index e388e45..f435812 100644 --- a/src/socket.c +++ b/src/socket.c @@ -5,9 +5,15 @@ #include #include #include +#include +#include +#include + #include "socket.h" #include "typedef.h" +// #define LOG_INFO_STDOUT + void *get_in_addr(struct sockaddr *sa) { if (sa->sa_family == AF_INET) { @@ -17,6 +23,16 @@ void *get_in_addr(struct sockaddr *sa) return &(((struct sockaddr_in6*)sa)->sin6_addr); } +void sigchld_handler(int s) +{ + // waitpid() might overwrite errno, so we save and restore it: + int saved_errno = errno; + + while(waitpid(-1, NULL, WNOHANG) > 0); + + errno = saved_errno; +} + int listener(char *port, void (*on_recv)(char *, int)) { int sockfd; @@ -59,7 +75,6 @@ int listener(char *port, void (*on_recv)(char *, int)) freeaddrinfo(servinfo); - #include int i = 0; time_t now = 0; while(1) @@ -68,6 +83,15 @@ int listener(char *port, void (*on_recv)(char *, int)) now = time(NULL); printf("FPS: %d\n", i); i = 0; + + struct sigaction sa; + sa.sa_handler = sigchld_handler; // reap all dead processes + sigemptyset(&sa.sa_mask); + sa.sa_flags = SA_RESTART; + if (sigaction(SIGCHLD, &sa, NULL) == -1) { + perror("sigaction"); + return 1; + } } char buf[BUF_CAP] = {0}; @@ -129,8 +153,16 @@ int talker(char *port, char *address, void (*on_send)(char *, int *)) return 2; } + int i = 0; + time_t now = 0; while(1) { + if(now < time(NULL)) { + now = time(NULL); + printf("FPS %d\n", i); + i = 0; + } + char buf[BUF_CAP] = {0}; int bytes; on_send(buf, &bytes); @@ -144,6 +176,8 @@ int talker(char *port, char *address, void (*on_send)(char *, int *)) #ifdef LOG_INFO_STDOUT printf("talker: sent %d bytes to %s\n", numbytes, address); #endif + + i++; } freeaddrinfo(servinfo); -- cgit v1.2.3