From 657434187016b8f47e5da60ce437fa2538a089e5 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Sun, 13 Aug 2023 13:31:04 +0900 Subject: [PATCH] Minor fixes --- src/socket.c | 19 +++++++++++++++---- src/socket.h | 2 +- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/socket.c b/src/socket.c index 83117a7..09a7df4 100644 --- a/src/socket.c +++ b/src/socket.c @@ -24,6 +24,7 @@ int unix_socket_handler_thread_function(void *ud) { int data_socket; char buffer[8]; char ret_buffer[8]; + unsigned int ticks; while(1) { mtx_lock(handler->mutex); @@ -43,6 +44,7 @@ int unix_socket_handler_thread_function(void *ud) { break; } + ticks = 0; memset(ret_buffer, 0, sizeof(ret_buffer)); while (1) { mtx_lock(handler->mutex); @@ -56,6 +58,11 @@ int unix_socket_handler_thread_function(void *ud) { ret = read(data_socket, buffer, sizeof(buffer)); if (ret == -1) { if (errno == EAGAIN || errno == EWOULDBLOCK) { + if (++ticks > 200) { + // Timed out. + ret = -1; + break; + } thrd_sleep(&duration, 0); continue; } @@ -84,13 +91,17 @@ int unix_socket_handler_thread_function(void *ud) { ret_buffer[1] |= 2; } } + ret = 0; break; } - ret = write(data_socket, ret_buffer, sizeof(ret_buffer)); - if (ret == -1) { - // Error. TODO handle this. - break; + if (ret == 0) { + ret = write(data_socket, ret_buffer, sizeof(ret_buffer)); + if (ret == -1) { + // Error. TODO handle this. + close(data_socket); + continue; + } } close(data_socket); diff --git a/src/socket.h b/src/socket.h index e00e278..b19a4c7 100644 --- a/src/socket.h +++ b/src/socket.h @@ -23,7 +23,7 @@ typedef struct UnixSocketHandler { /* * ???? 0001 - thread should stop */ - volatile uint64_t ccflags; + uint64_t ccflags; int socket_descriptor; } UnixSocketHandler;