From 145775818375bafcf191a7b4986e57a00b41ac68 Mon Sep 17 00:00:00 2001 From: kartofen Date: Tue, 18 Oct 2022 23:25:46 +0300 Subject: use glfw and some minor refactoring --- src/socket.c | 50 +++++++++++--------------------------------------- 1 file changed, 11 insertions(+), 39 deletions(-) (limited to 'src/socket.c') diff --git a/src/socket.c b/src/socket.c index 838df53..2ce4900 100644 --- a/src/socket.c +++ b/src/socket.c @@ -5,8 +5,6 @@ #include #include #include -#include -#include #include #include "socket.h" @@ -23,16 +21,6 @@ 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; @@ -46,7 +34,7 @@ int listener(char *port, void (*on_recv)(char *, int)) int rv; if((rv = getaddrinfo(NULL, port, &hints, &servinfo)) != 0) { - fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv)); + err("getaddrinfo: %s\n", gai_strerror(rv)); return 1; } @@ -69,8 +57,8 @@ int listener(char *port, void (*on_recv)(char *, int)) } if(p == NULL) { - fprintf(stderr, "listener: failed to bind socket\n"); - return 2; + err("listener: failed to bind socket\n"); + return 1; } freeaddrinfo(servinfo); @@ -81,17 +69,8 @@ int listener(char *port, void (*on_recv)(char *, int)) { if(now < time(NULL)) { now = time(NULL); - printf("FPS: %d\n", i); + info("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}; @@ -107,20 +86,13 @@ int listener(char *port, void (*on_recv)(char *, int)) #ifdef LOG_INFO_STDOUT char s[INET6_ADDRSTRLEN]; - printf("listener: got packet from %s, %d bytes long\n", + info("listener: got packet from %s, %d bytes long\n", inet_ntop(their_addr.ss_family, get_in_addr((struct sockaddr *)&their_addr), s, sizeof(s)), numbytes); #endif - // #include - - // pid_t p = fork(); - // if(p < -1) { return 69; } - // else if(p == 0) { - on_recv(buf, numbytes); - // exit(0); - // } + on_recv(buf, numbytes); i++; } @@ -139,7 +111,7 @@ int talker(char *port, char *address, void (*on_send)(char *, int *)) int rv; if((rv = getaddrinfo(address, port, &hints, &servinfo)) != 0) { - fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv)); + err("getaddrinfo: %s\n", gai_strerror(rv)); return 1; } @@ -156,8 +128,8 @@ int talker(char *port, char *address, void (*on_send)(char *, int *)) } if(p == NULL) { - fprintf(stderr, "talker: failed to create socket\n"); - return 2; + err("talker: failed to create socket\n"); + return 1; } int i = 0; @@ -166,7 +138,7 @@ int talker(char *port, char *address, void (*on_send)(char *, int *)) { if(now < time(NULL)) { now = time(NULL); - printf("FPS %d\n", i); + info("FPS %d\n", i); i = 0; } @@ -181,7 +153,7 @@ int talker(char *port, char *address, void (*on_send)(char *, int *)) return 1; } #ifdef LOG_INFO_STDOUT - printf("talker: sent %d bytes to %s\n", numbytes, address); + info("talker: sent %d bytes to %s\n", numbytes, address); #endif i++; -- cgit v1.2.3