From d447076265a50b89c221e7dbc1fdc156384e5515 Mon Sep 17 00:00:00 2001 From: "flyingscorpio@clevo" Date: Thu, 27 Oct 2022 09:55:58 +0200 Subject: [PATCH] Make stack gloabl, fix Dispatcher message processing --- src/server.c | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/src/server.c b/src/server.c index f7ac68e..d76e064 100644 --- a/src/server.c +++ b/src/server.c @@ -17,10 +17,10 @@ * sender_thread * */ struct client_details clients[MAX_CLIENTS]; +int stack[2]; int main(int argc, char *argv[]) { int server_port, serv_sockid, client_sockid, client_id; - int stack[2]; int message_pipe[2]; struct sockaddr_in server_socket; struct sockaddr_in client_socket; @@ -219,28 +219,27 @@ int disconnect_client(int client_id) { /* Thread, parses and handles messages */ /* Thread, Dispatcher : pop message from the stack, parse it and send it to the good ClientSender */ void *Dispatcher(void *arg) { - char buf = 0; - char *message[]; + char buf[BUF_LEN]; + char message[1024]; int n = 0 ; int k = 0 ; while (1) { /* Read 1 bytes at a time, because we don't know how long the message will be */ - while ((n = read(stack_fd, buf, 1)) > 0) { - message[k++] = buf; - if (buf == '\n'){ + while ((n = read(stack[0], buf, 1)) > 0) { + message[k++] = buf[n - 1]; + if (buf[n - 1] == '\n'){ + message[k++] = '\0'; k = 0; - printf("Message received : %c", &message); - - /* TODO : parse(*message); Do we do that into a separate thread ? */ - - /* TODO :Writing Message into pipe */ + printf("Message received : %s", message); + /* TODO: parse(*message); Do we do that in a separate thread ? */ + /* TODO: Writing Message into pipe */ } - if (n < 0) { - fprintf(stderr, "ERROR: Dispatcher failed to read from stack\n"); - break; - } - } + if (n < 0) { + fprintf(stderr, "ERROR: Dispatcher failed to read from stack\n"); + break; + } + } } pthread_exit(0); }