aboutsummaryrefslogtreecommitdiff
path: root/src/socket.c
diff options
context:
space:
mode:
authorkartofen <mladenovnasko0@gmail.com>2022-10-18 23:25:46 +0300
committerkartofen <mladenovnasko0@gmail.com>2022-10-18 23:25:46 +0300
commit145775818375bafcf191a7b4986e57a00b41ac68 (patch)
treeb539525328515401be6bc2a6c9fb17841b8a16d1 /src/socket.c
parent7e64d0c78e8a21f2ede7eab1303758391001c0dc (diff)
use glfw and some minor refactoring
Diffstat (limited to 'src/socket.c')
-rw-r--r--src/socket.c50
1 files changed, 11 insertions, 39 deletions
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 <string.h>
#include <arpa/inet.h>
#include <netdb.h>
-#include <signal.h>
-#include <wait.h>
#include <time.h>
#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 <stdlib.h>
-
- // 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++;