]> git.lizzy.rs Git - dragonblocks_alpha.git/blobdiff - src/network.c
Tweak feature info in README
[dragonblocks_alpha.git] / src / network.c
index 7c585b797bd1d91bf11bb2a1134adcde66f5dd50..f4fb0a50896d7b528c9dad2e815e7ea7874667f2 100644 (file)
@@ -2,14 +2,14 @@
 
 bool send_command(Client *client, RemoteCommand cmd)
 {
-       pthread_mutex_lock(client->write_mtx);
+       pthread_mutex_lock(&client->mtx);
        bool ret = write_u32(client->fd, cmd);
-       pthread_mutex_unlock(client->write_mtx);
+       pthread_mutex_unlock(&client->mtx);
        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;
 
@@ -44,11 +43,13 @@ static void handle_packets(Client *client) {
                if (handler && handler->func) {
                        bool good = client->state & handler->state_flags;
                        if (! good)
-                               printf("Recieved %s command, but client is in invalid state: %d\n", handler->name, client->state);
+                               printf("Received %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);
+                       printf("Received invalid command %d\n", command);
                }
        }
+
+       return client->state == CS_DISCONNECTED || errno == EINTR;
 }