diff --git a/groundStation/src/microcart_cli.c b/groundStation/src/microcart_cli.c index 0e8d5cc8b732e946e0cc225e1dd8ee37f84cf301..76e59968ab7d4182978a2c0d466a527b9f6d24be 100644 --- a/groundStation/src/microcart_cli.c +++ b/groundStation/src/microcart_cli.c @@ -50,6 +50,8 @@ static int new_client(int fd); static ssize_t get_client_index(int fd); /* Returns pointer to client buffer, or -1 */ static char * get_client_buffer(int fd); +/* Returns -1 on error */ +static int remove_client(fd); /* Thread-safe wrappers */ pthread_mutex_t quadSocketMutex; @@ -246,6 +248,15 @@ int main(int argc, char **argv) } } } + } else { + for(int i = 0; i < MAX_CLIENTS; ++i) { + if(client_fds[i] != -1) { + char buff; + if(recv(client_fds[i],&buff, 1, MSG_PEEK | MSG_DONTWAIT) < 0) { + remove_client(client_fds[i]); + } + } + } } } @@ -467,3 +478,15 @@ static char * get_client_buffer(int fd) return client_buffers[slot]; } } + +static int remove_client(fd) { + ssize_t slot = get_client_index(fd); + if(slot == -1) + return slot; + char *clientBuffer = get_client_buffer(fd); + if(clientBuffer == NULL) + return -1; + clientBuffer = '\0'; + client_fds[clientIndex] = -1; + return 0; +} \ No newline at end of file