Let ClientListener send client_id to Dispatcher

This commit is contained in:
flyingscorpio@clevo 2022-10-27 23:28:05 +02:00
parent 69d0b56607
commit e693ad35c5

View file

@ -289,20 +289,33 @@ void *Dispatcher(void *arg) {
/* Thread, listens for incoming messages */ /* Thread, listens for incoming messages */
void *ClientListener(void *arg) { void *ClientListener(void *arg) {
struct client_details details = *((struct client_details*) arg); struct client_details details = *((struct client_details*) arg);
char buf[BUF_LEN]; char buf[BUF_LEN], client_id_buf[BUF_LEN];
int n = 0; int n = 0;
while (1) { while (1) {
/* Read n bytes at a time, because we don't know how long the message will be */ /* Loop for as long as there is nothing to read */
while ((n = read(details.sockid, buf, BUF_LEN)) > 0) { while ((n = read(details.sockid, buf, BUF_LEN)) == 0);
/* TODO: Get mutex */ if (n == -1) {
perror("read");
continue;
}
/* TODO: Get mutex */
/* Write client_id for Dispatcher */
sprintf(client_id_buf, "%d", details.id);
write(message_buffer[1], client_id_buf, strlen(buf));
/* Write what we've read as long as there is something to read */
do {
write(message_buffer[1], buf, n); write(message_buffer[1], buf, n);
/* TODO: Free mutex */ } while ((n = read(details.sockid, buf, BUF_LEN)) > 0); // TODO: we are blocking here, change socket details to make it non blocking?
} if (n == -1) {
if (n < 0) { perror("read");
fprintf(stderr, "ERROR: read\n"); continue;
break;
} }
/* TODO: Free mutex */
} }
pthread_exit(0); pthread_exit(0);