aboutsummaryrefslogtreecommitdiff
path: root/src/socket.c
diff options
context:
space:
mode:
authorkartofen <mladenovnasko0@gmail.com>2022-10-16 22:55:45 +0300
committerkartofen <mladenovnasko0@gmail.com>2022-10-16 22:55:45 +0300
commitb1b7c92aa96af5502f276819b628b05d54f4655c (patch)
tree6a04459dc7128fd4259356a88785defeab678b30 /src/socket.c
parent675ded0d66b9fd60777d3037ded1446a3f9ef986 (diff)
nearly works
Diffstat (limited to 'src/socket.c')
-rw-r--r--src/socket.c36
1 files changed, 35 insertions, 1 deletions
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 <string.h>
#include <arpa/inet.h>
#include <netdb.h>
+#include <signal.h>
+#include <wait.h>
+#include <time.h>
+
#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 <time.h>
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);