Make client_sockid non blocking and handle the reading loops
This commit is contained in:
parent
9500e59f57
commit
b28f9e46e5
1 changed files with 9 additions and 10 deletions
19
src/server.c
19
src/server.c
|
@ -2,6 +2,8 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <pthread.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
|
@ -99,6 +101,8 @@ int main(int argc, char *argv[]) {
|
|||
} else {
|
||||
printf("received connection from %s\n", ip4tostring(ntohl(client_socket.sin_addr.s_addr)));
|
||||
|
||||
fcntl(client_sockid, F_SETFL, O_NONBLOCK);
|
||||
|
||||
/* Get client id to make sure we can handle him */
|
||||
if ((client_id = get_next_free_client_id()) == -1) {
|
||||
/* Client array is full, refuse connection */
|
||||
|
@ -292,13 +296,11 @@ void *ClientListener(void *arg) {
|
|||
int n = 0;
|
||||
|
||||
while (1) {
|
||||
/* Loop for as long as there is nothing to read */
|
||||
while ((n = read(details.sockid, buf, BUF_LEN)) == 0);
|
||||
if (n == -1) {
|
||||
perror("read");
|
||||
/* Loop for as long as there is nothing to read (or there is an error) */
|
||||
while ((n = read(details.sockid, buf, BUF_LEN)) == -1) {
|
||||
if (errno != EAGAIN) perror("read");
|
||||
continue;
|
||||
}
|
||||
|
||||
/* TODO: Get mutex */
|
||||
|
||||
/* Write client_id for Dispatcher */
|
||||
|
@ -308,11 +310,8 @@ void *ClientListener(void *arg) {
|
|||
/* Write what we've read as long as there is something to read */
|
||||
do {
|
||||
write(message_buffer[1], buf, n);
|
||||
} 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) {
|
||||
perror("read");
|
||||
continue;
|
||||
}
|
||||
} while ((n = read(details.sockid, buf, BUF_LEN)) > 0);
|
||||
if ((n == -1) && (errno != EAGAIN)) perror("read");
|
||||
|
||||
/* TODO: Free mutex */
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue