netcode is now aware of connection losses

Thu, 17 Apr 2014 12:16:14 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 17 Apr 2014 12:16:14 +0200
changeset 46
4dcfb4c58b6d
parent 45
e14a1d9aa91d
child 47
d726e4b46c33

netcode is now aware of connection losses

src/game.c file | annotate | diff | comparison | revisions
src/main.c file | annotate | diff | comparison | revisions
src/network.c file | annotate | diff | comparison | revisions
src/network.h file | annotate | diff | comparison | revisions
src/server.c file | annotate | diff | comparison | revisions
--- a/src/game.c	Thu Apr 17 11:55:36 2014 +0200
+++ b/src/game.c	Thu Apr 17 12:16:14 2014 +0200
@@ -275,11 +275,17 @@
                     net_send_code(opponent, NETCODE_REMIS);
                     printw("Remis offer sent - waiting for acceptance...");
                     refresh();
-                    if (net_recieve_code(opponent) == NETCODE_ACCEPT) {
+                    code = net_recieve_code(opponent);
+                    if (code == NETCODE_ACCEPT) {
                         printw("\rRemis accepted!");
                         clrtoeol();
                         refresh();
                         return 1;
+                    } else if (code == NETCODE_CONNLOST) {
+                        printw("\rYour opponent left the game.");
+                        clrtoeol();
+                        refresh();
+                        return 1;
                     } else {
                         remisrejected = TRUE;
                     }
@@ -312,6 +318,9 @@
                         } else {
                             return 0;
                         }
+                    } else if (code == NETCODE_CONNLOST) {
+                        printw("Your opponent left the game.");
+                        return 1;
                     } else {
                         printw("Invalid network response.");
                     }
@@ -362,6 +371,10 @@
                 printw("\rYour opponent resigned!");
                 clrtoeol();
                 return 1;
+            case NETCODE_CONNLOST:
+                printw("\rYour opponent has left the game.");
+                clrtoeol();
+                return 1;
             case NETCODE_REMIS:
                 if (prompt_yesno(
                     "\rYour opponent offers remis - do you accept")) {
@@ -437,13 +450,6 @@
     draw_board(&gamestate);
     
     gamestate_cleanup(&gamestate);
-    
-    mvaddstr(getmaxy(stdscr)-1, 0,
-        "Game has ended. Press any key to leave...");
-    refresh();
-    cbreak();
-    flushinp();
-    getch();
 }
 
 void game_start(Settings *settings, int opponent) {
@@ -473,11 +479,4 @@
     draw_board(&gamestate);
     
     gamestate_cleanup(&gamestate);
-    
-    mvaddstr(getmaxy(stdscr)-1, 0,
-        "Game has ended. Press any key to leave...");
-    refresh();
-    cbreak();
-    flushinp();
-    getch();
 }
--- a/src/main.c	Thu Apr 17 11:55:36 2014 +0200
+++ b/src/main.c	Thu Apr 17 12:16:14 2014 +0200
@@ -179,15 +179,22 @@
     }
     atexit(leavescr);
     
+    int exitcode;
     if (settings.singlemachine) {
         game_start_singlemachine(&settings);
+        exitcode = EXIT_SUCCESS;
     } else {
-        int exitcode = is_server(&settings) ?
+        exitcode = is_server(&settings) ?
             server_run(&settings) : client_run(&settings);
-        
-        if (exitcode != EXIT_SUCCESS) {
-            cbreak(); getch();
-        }
     }
+    
+    mvaddstr(getmaxy(stdscr)-1, 0,
+        "Game has ended. Press any key to leave...");
+    refresh();
+    cbreak();
+    flushinp();
+    getch();
+    
+    return exitcode;
 }
 
--- a/src/network.c	Thu Apr 17 11:55:36 2014 +0200
+++ b/src/network.c	Thu Apr 17 12:16:14 2014 +0200
@@ -126,8 +126,11 @@
 
 uint8_t net_recieve_code(int socket) {
     uint8_t code;
-    recv(socket, &code, sizeof(uint8_t), 0);
-    return code;
+    if (recv(socket, &code, sizeof(code), 0) == sizeof(code)) {
+        return code;
+    } else {
+        return NETCODE_CONNLOST;
+    }
 }
 
 void net_recieve_data(int socket, void *data, size_t len) {
--- a/src/network.h	Thu Apr 17 11:55:36 2014 +0200
+++ b/src/network.h	Thu Apr 17 12:16:14 2014 +0200
@@ -47,6 +47,7 @@
 #define NETCODE_RESIGN 0x41
 #define NETCODE_REMIS 0x42
 #define NETCODE_TIMEOVER 0x44
+#define NETCODE_CONNLOST 0x80
 
 #define NETCODE_VERSION 13
 
--- a/src/server.c	Thu Apr 17 11:55:36 2014 +0200
+++ b/src/server.c	Thu Apr 17 12:16:14 2014 +0200
@@ -54,7 +54,7 @@
         return 1;
     }
 
-    printw("Client connected - transmitting gameinfo...");
+    addstr("Client connected - transmitting gameinfo...");
     refresh();
     
     return 0;
@@ -78,16 +78,19 @@
     int fd = server.client->fd;
     net_send_data(fd, NETCODE_GAMEINFO,
         &(settings->gameinfo), sizeof(GameInfo));
-    printw("\rClient connected - awaiting challenge acceptance...");
+    addstr("\rClient connected - awaiting challenge acceptance...");
     refresh();
     int code = net_recieve_code(fd);
     if (code == NETCODE_ACCEPT) {
-        printw("\rClient connected - challenge accepted.");
+        addstr("\rClient connected - challenge accepted.");
         clrtoeol();
         
         game_start(settings, fd);
     } else if (code == NETCODE_DECLINE) {
-        printw("\rClient connected - challenge declined.");
+        addstr("\rClient connected - challenge declined.");
+        clrtoeol();
+    } else if (code == NETCODE_CONNLOST) {
+        addstr("\rClient connected - but gave no response.");
         clrtoeol();
     } else {
         addstr("\rInvalid client response");

mercurial