summary refs log tree commit diff homepage
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--client.c45
-rw-r--r--help.c18
-rw-r--r--server.c121
3 files changed, 91 insertions, 93 deletions
diff --git a/client.c b/client.c
index a216a63..c421ba7 100644
--- a/client.c
+++ b/client.c
@@ -36,9 +36,9 @@
 
 static int client;
 
-static void clientMessage(const struct ClientMessage *msg) {
-    ssize_t len = send(client, msg, sizeof(*msg), 0);
-    if (len < 0) err(EX_IOERR, "send");
+static void clientMessage(struct ClientMessage msg) {
+    ssize_t size = send(client, &msg, sizeof(msg), 0);
+    if (size < 0) err(EX_IOERR, "send");
 }
 
 static void clientMove(int8_t dx, int8_t dy) {
@@ -46,7 +46,7 @@ static void clientMove(int8_t dx, int8_t dy) {
         .type = CLIENT_MOVE,
         .data.m = { .dx = dx, .dy = dy },
     };
-    clientMessage(&msg);
+    clientMessage(msg);
 }
 
 static void clientPut(uint8_t color, char cell) {
@@ -54,7 +54,7 @@ static void clientPut(uint8_t color, char cell) {
         .type = CLIENT_PUT,
         .data.p = { .color = color, .cell = cell },
     };
-    clientMessage(&msg);
+    clientMessage(msg);
 }
 
 static void clientSpawn(uint8_t spawn) {
@@ -62,7 +62,7 @@ static void clientSpawn(uint8_t spawn) {
         .type = CLIENT_SPAWN,
         .data.s.spawn = spawn,
     };
-    clientMessage(&msg);
+    clientMessage(msg);
 }
 
 static uint8_t inputColor = COLOR_WHITE;
@@ -265,9 +265,9 @@ static void serverPut(uint8_t x, uint8_t y, uint8_t color, char cell) {
 
 static void serverTile(void) {
     struct Tile tile;
-    ssize_t len = recv(client, &tile, sizeof(tile), 0);
-    if (len < 0) err(EX_IOERR, "recv");
-    if (len < (ssize_t)sizeof(tile)) {
+    ssize_t size = recv(client, &tile, sizeof(tile), 0);
+    if (size < 0) err(EX_IOERR, "recv");
+    if ((size_t)size < sizeof(tile)) {
         errx(EX_PROTOCOL, "This tile isn't big enough...");
     }
 
@@ -291,43 +291,42 @@ static void serverCursor(uint8_t oldX, uint8_t oldY, uint8_t newX, uint8_t newY)
 
 static void readMessage(void) {
     struct ServerMessage msg;
-    ssize_t len = recv(client, &msg, sizeof(msg), 0);
-    if (len < 0) err(EX_IOERR, "recv");
-    if (len < (ssize_t)sizeof(msg)) errx(EX_PROTOCOL, "A message was cut short.");
+    ssize_t size = recv(client, &msg, sizeof(msg), 0);
+    if (size < 0) err(EX_IOERR, "recv");
+    if ((size_t)size < sizeof(msg)) errx(EX_PROTOCOL, "A message was cut short.");
 
     int sy, sx;
     getyx(stdscr, sy, sx);
 
     switch (msg.type) {
-        case SERVER_TILE:
+        case SERVER_TILE: {
             serverTile();
-            break;
+        } break;
 
-        case SERVER_MOVE:
+        case SERVER_MOVE: {
             move(msg.data.m.cellY, msg.data.m.cellX);
             refresh();
-            return;
+        } return;
 
-        case SERVER_PUT:
+        case SERVER_PUT: {
             serverPut(
                 msg.data.p.cellX,
                 msg.data.p.cellY,
                 msg.data.p.color,
                 msg.data.p.cell
             );
-            break;
+        } break;
 
-        case SERVER_CURSOR:
+        case SERVER_CURSOR: {
             serverCursor(
                 msg.data.c.oldCellX,
                 msg.data.c.oldCellY,
                 msg.data.c.newCellX,
                 msg.data.c.newCellY
             );
-            break;
+        } break;
 
-        default:
-            errx(EX_PROTOCOL, "I don't know what %d means!", msg.type);
+        default: errx(EX_PROTOCOL, "I don't know what %d means!", msg.type);
     }
 
     move(sy, sx);
@@ -382,7 +381,7 @@ int main() {
         .sun_path = "torus.sock",
     };
     int error = connect(client, (struct sockaddr *)&addr, sizeof(addr));
-    if (error) err(EX_IOERR, "torus.sock");
+    if (error) err(EX_NOINPUT, "torus.sock");
 
     initscr();
     cbreak();
diff --git a/help.c b/help.c
index 7fa7e9b..4415065 100644
--- a/help.c
+++ b/help.c
@@ -25,9 +25,9 @@
 
 static int client;
 
-static void clientMessage(const struct ClientMessage *msg) {
-    ssize_t len = send(client, msg, sizeof(*msg), 0);
-    if (len < 0) err(EX_IOERR, "send");
+static void clientMessage(struct ClientMessage msg) {
+    ssize_t size = send(client, &msg, sizeof(msg), 0);
+    if (size < 0) err(EX_IOERR, "send");
 }
 
 static void clientMove(int8_t dx, int8_t dy) {
@@ -35,7 +35,7 @@ static void clientMove(int8_t dx, int8_t dy) {
         .type = CLIENT_MOVE,
         .data.m = { .dx = dx, .dy = dy },
     };
-    clientMessage(&msg);
+    clientMessage(msg);
 }
 
 static void clientPut(uint8_t color, char cell) {
@@ -43,7 +43,7 @@ static void clientPut(uint8_t color, char cell) {
         .type = CLIENT_PUT,
         .data.p = { .color = color, .cell = cell },
     };
-    clientMessage(&msg);
+    clientMessage(msg);
 }
 
 #define DELAY (50000)
@@ -96,7 +96,7 @@ int main() {
         .sun_path = "torus.sock",
     };
     int error = connect(client, (struct sockaddr *)&addr, sizeof(addr));
-    if (error) err(EX_IOERR, "torus.sock");
+    if (error) err(EX_NOINPUT, "torus.sock");
 
     pid_t pid = fork();
     if (pid < 0) err(EX_OSERR, "fork");
@@ -104,9 +104,9 @@ int main() {
     if (!pid) {
         for (;;) {
             char buf[4096];
-            ssize_t len = recv(client, buf, sizeof(buf), 0);
-            if (len < 0) err(EX_IOERR, "recv");
-            if (!len) return EX_OK;
+            ssize_t size = recv(client, buf, sizeof(buf), 0);
+            if (size < 0) err(EX_IOERR, "recv");
+            if (!size) return EX_OK;
         }
     }
 
diff --git a/server.c b/server.c
index 84ed24f..cc1bdc3 100644
--- a/server.c
+++ b/server.c
@@ -39,7 +39,7 @@ static struct Tile *tiles;
 
 static void tilesMap(void) {
     int fd = open("torus.dat", O_CREAT | O_RDWR, 0644);
-    if (fd < 0) err(EX_IOERR, "torus.dat");
+    if (fd < 0) err(EX_CANTCREAT, "torus.dat");
 
     int error = ftruncate(fd, TILES_SIZE);
     if (error) err(EX_IOERR, "ftruncate");
@@ -114,20 +114,20 @@ static struct Client *clientAdd(int fd) {
     return client;
 }
 
-static bool clientSend(const struct Client *client, const struct ServerMessage *msg) {
-    ssize_t len = send(client->fd, msg, sizeof(*msg), 0);
-    if (len < 0) return false;
+static bool clientSend(const struct Client *client, struct ServerMessage msg) {
+    ssize_t size = send(client->fd, &msg, sizeof(msg), 0);
+    if (size < 0) return false;
 
-    if (msg->type == SERVER_TILE) {
+    if (msg.type == SERVER_TILE) {
         struct Tile *tile = tileAccess(client->tileX, client->tileY);
-        len = send(client->fd, tile, sizeof(*tile), 0);
-        if (len < 0) return false;
+        size = send(client->fd, tile, sizeof(*tile), 0);
+        if (size < 0) return false;
     }
 
     return true;
 }
 
-static void clientCast(const struct Client *origin, const struct ServerMessage *msg) {
+static void clientCast(const struct Client *origin, struct ServerMessage msg) {
     for (struct Client *client = clientHead; client; client = client->next) {
         if (client == origin) continue;
         if (client->tileX != origin->tileX) continue;
@@ -148,7 +148,7 @@ static void clientRemove(struct Client *client) {
             .newCellX = CURSOR_NONE,   .newCellY = CURSOR_NONE,
         },
     };
-    clientCast(client, &msg);
+    clientCast(client, msg);
 
     close(client->fd);
     free(client);
@@ -167,21 +167,21 @@ static bool clientCursors(const struct Client *client) {
 
         msg.data.c.newCellX = friend->cellX;
         msg.data.c.newCellY = friend->cellY;
-        if (!clientSend(client, &msg)) return false;
+        if (!clientSend(client, msg)) return false;
     }
     return true;
 }
 
-static bool clientUpdate(struct Client *client, struct Client *old) {
+static bool clientUpdate(const struct Client *client, const struct Client *old) {
     struct ServerMessage msg = {
         .type = SERVER_MOVE,
         .data.m = { .cellX = client->cellX, .cellY = client->cellY },
     };
-    if (!clientSend(client, &msg)) return false;
+    if (!clientSend(client, msg)) return false;
 
     if (client->tileX != old->tileX || client->tileY != old->tileY) {
         msg.type = SERVER_TILE;
-        if (!clientSend(client, &msg)) return false;
+        if (!clientSend(client, msg)) return false;
 
         if (!clientCursors(client)) return false;
 
@@ -192,7 +192,7 @@ static bool clientUpdate(struct Client *client, struct Client *old) {
                 .newCellX = CURSOR_NONE, .newCellY = CURSOR_NONE,
             },
         };
-        clientCast(old, &msg);
+        clientCast(old, msg);
 
         msg = (struct ServerMessage) {
             .type = SERVER_CURSOR,
@@ -201,7 +201,7 @@ static bool clientUpdate(struct Client *client, struct Client *old) {
                 .newCellX = client->cellX, .newCellY = client->cellY,
             },
         };
-        clientCast(client, &msg);
+        clientCast(client, msg);
 
     } else {
         msg = (struct ServerMessage) {
@@ -270,8 +270,8 @@ static bool clientPut(const struct Client *client, uint8_t color, char cell) {
             .cell = cell,
         },
     };
-    bool success = clientSend(client, &msg);
-    clientCast(client, &msg);
+    bool success = clientSend(client, msg);
+    clientCast(client, msg);
     return success;
 }
 
@@ -291,7 +291,7 @@ int main() {
         .sun_path = "torus.sock",
     };
     error = bind(server, (struct sockaddr *)&addr, sizeof(addr));
-    if (error) err(EX_IOERR, "torus.sock");
+    if (error) err(EX_CANTCREAT, "torus.sock");
 
     error = listen(server, 0);
     if (error) err(EX_OSERR, "listen");
@@ -310,54 +310,53 @@ int main() {
     for (;;) {
         nevents = kevent(kq, NULL, 0, &event, 1, NULL);
         if (nevents < 0) err(EX_IOERR, "kevent");
-        if (!nevents) continue;
-
-        if (event.udata) {
-            struct Client *client = event.udata;
-            if (event.flags & EV_EOF) {
-                clientRemove(client);
-                continue;
-            }
-
-            struct ClientMessage msg;
-            ssize_t len = recv(client->fd, &msg, sizeof(msg), 0);
-            if (len != sizeof(msg)) {
-                clientRemove(client);
-                continue;
-            }
-
-            bool success = false;
-            if (msg.type == CLIENT_MOVE) {
-                success = clientMove(client, msg.data.m.dx, msg.data.m.dy);
-            } else if (msg.type == CLIENT_PUT) {
-                success = clientPut(client, msg.data.p.color, msg.data.p.cell);
-            } else if (msg.type == CLIENT_SPAWN) {
-                success = clientSpawn(client, msg.data.s.spawn);
-            }
-            if (!success) clientRemove(client);
 
-            continue;
-        }
+        if (!event.udata) {
+            int fd = accept(server, NULL, NULL);
+            if (fd < 0) err(EX_IOERR, "accept");
+            fcntl(fd, F_SETFL, O_NONBLOCK);
 
-        int fd = accept(server, NULL, NULL);
-        if (fd < 0) err(EX_IOERR, "accept");
-        fcntl(fd, F_SETFL, O_NONBLOCK);
+            int on = 1;
+            error = setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &on, sizeof(on));
+            if (error) err(EX_IOERR, "setsockopt");
 
-        int on = 1;
-        error = setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &on, sizeof(on));
-        if (error) err(EX_IOERR, "setsockopt");
+            struct Client *client = clientAdd(fd);
 
-        struct Client *client = clientAdd(fd);
+            struct kevent event = {
+                .ident = fd,
+                .filter = EVFILT_READ,
+                .flags = EV_ADD,
+                .udata = client,
+            };
+            nevents = kevent(kq, &event, 1, NULL, 0, NULL);
+            if (nevents < 0) err(EX_IOERR, "kevent");
 
-        struct kevent event = {
-            .ident = fd,
-            .filter = EVFILT_READ,
-            .flags = EV_ADD,
-            .udata = client,
-        };
-        nevents = kevent(kq, &event, 1, NULL, 0, NULL);
-        if (nevents < 0) err(EX_OSERR, "kevent");
+            if (!clientSpawn(client, 0)) clientRemove(client);
+
+            continue;
+        }
 
-        if (!clientSpawn(client, 0)) clientRemove(client);
+        struct Client *client = event.udata;
+        if (event.flags & EV_EOF) {
+            clientRemove(client);
+            continue;
+        }
+
+        struct ClientMessage msg;
+        ssize_t size = recv(client->fd, &msg, sizeof(msg), 0);
+        if (size != sizeof(msg)) {
+            clientRemove(client);
+            continue;
+        }
+
+        bool success = false;
+        if (msg.type == CLIENT_MOVE) {
+            success = clientMove(client, msg.data.m.dx, msg.data.m.dy);
+        } else if (msg.type == CLIENT_PUT) {
+            success = clientPut(client, msg.data.p.color, msg.data.p.cell);
+        } else if (msg.type == CLIENT_SPAWN) {
+            success = clientSpawn(client, msg.data.s.spawn);
+        }
+        if (!success) clientRemove(client);
     }
 }