Make stack gloabl, fix Dispatcher message processing

This commit is contained in:
flyingscorpio@clevo 2022-10-27 09:55:58 +02:00
parent a8c71c6abd
commit d447076265

View file

@ -17,10 +17,10 @@
* sender_thread * sender_thread
* */ * */
struct client_details clients[MAX_CLIENTS]; struct client_details clients[MAX_CLIENTS];
int stack[2];
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
int server_port, serv_sockid, client_sockid, client_id; int server_port, serv_sockid, client_sockid, client_id;
int stack[2];
int message_pipe[2]; int message_pipe[2];
struct sockaddr_in server_socket; struct sockaddr_in server_socket;
struct sockaddr_in client_socket; struct sockaddr_in client_socket;
@ -219,28 +219,27 @@ int disconnect_client(int client_id) {
/* Thread, parses and handles messages */ /* Thread, parses and handles messages */
/* Thread, Dispatcher : pop message from the stack, parse it and send it to the good ClientSender */ /* Thread, Dispatcher : pop message from the stack, parse it and send it to the good ClientSender */
void *Dispatcher(void *arg) { void *Dispatcher(void *arg) {
char buf = 0; char buf[BUF_LEN];
char *message[]; char message[1024];
int n = 0 ; int n = 0 ;
int k = 0 ; int k = 0 ;
while (1) { while (1) {
/* Read 1 bytes at a time, because we don't know how long the message will be */ /* 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) { while ((n = read(stack[0], buf, 1)) > 0) {
message[k++] = buf; message[k++] = buf[n - 1];
if (buf == '\n'){ if (buf[n - 1] == '\n'){
message[k++] = '\0';
k = 0; k = 0;
printf("Message received : %c", &message); printf("Message received : %s", message);
/* TODO: parse(*message); Do we do that in a separate thread ? */
/* TODO : parse(*message); Do we do that into a separate thread ? */ /* TODO: Writing Message into pipe */
/* TODO :Writing Message into pipe */
} }
if (n < 0) { if (n < 0) {
fprintf(stderr, "ERROR: Dispatcher failed to read from stack\n"); fprintf(stderr, "ERROR: Dispatcher failed to read from stack\n");
break; break;
} }
} }
} }
pthread_exit(0); pthread_exit(0);
} }