]> git.lizzy.rs Git - dragonblocks_alpha.git/blobdiff - src/network.c
Make server thread-safe
[dragonblocks_alpha.git] / src / network.c
index 27bc5ed591dd3dc3c7c7abc771306a6d680d3655..0d29637d3f3796ecbcf6120ab50e382fc3c53bb7 100644 (file)
@@ -8,8 +8,8 @@ bool send_command(Client *client, RemoteCommand cmd)
        return ret;
 }
 
-static void handle_packets(Client *client) {
-       while (client->state != CS_DISCONNECTED) {
+static bool handle_packets(Client *client) {
+       while (client->state != CS_DISCONNECTED || ! interrupted) {
                struct pollfd pfd = {
                        .fd = client->fd,
                        .events = POLLIN,
@@ -20,21 +20,20 @@ static void handle_packets(Client *client) {
 
                if (pstate == -1) {
                        perror("poll");
-                       return;
+                       break;
                }
 
-               if (client->state == CS_DISCONNECTED)
-                       return;
-
-               if (pstate == 0)
+               if (pstate == 0) {
+                       sched_yield();
                        continue;
+               }
 
                if (! (pfd.revents & POLLIN))
-                       return;
+                       return false;
 
                HostCommand command;
                if (! read_u32(client->fd, &command))
-                       return;
+                       break;
 
                CommandHandler *handler = NULL;
 
@@ -46,9 +45,11 @@ static void handle_packets(Client *client) {
                        if (! good)
                                printf("Recieved %s command, but client is in invalid state: %d\n", handler->name, client->state);
                        if (! handler->func(client, good))
-                               return;
+                               break;
                } else {
                        printf("Recieved invalid command %d\n", command);
                }
        }
+
+       return client->state == CS_DISCONNECTED || errno == EINTR;
 }