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
     1.1 --- a/src/game.c	Thu Apr 17 11:55:36 2014 +0200
     1.2 +++ b/src/game.c	Thu Apr 17 12:16:14 2014 +0200
     1.3 @@ -275,11 +275,17 @@
     1.4                      net_send_code(opponent, NETCODE_REMIS);
     1.5                      printw("Remis offer sent - waiting for acceptance...");
     1.6                      refresh();
     1.7 -                    if (net_recieve_code(opponent) == NETCODE_ACCEPT) {
     1.8 +                    code = net_recieve_code(opponent);
     1.9 +                    if (code == NETCODE_ACCEPT) {
    1.10                          printw("\rRemis accepted!");
    1.11                          clrtoeol();
    1.12                          refresh();
    1.13                          return 1;
    1.14 +                    } else if (code == NETCODE_CONNLOST) {
    1.15 +                        printw("\rYour opponent left the game.");
    1.16 +                        clrtoeol();
    1.17 +                        refresh();
    1.18 +                        return 1;
    1.19                      } else {
    1.20                          remisrejected = TRUE;
    1.21                      }
    1.22 @@ -312,6 +318,9 @@
    1.23                          } else {
    1.24                              return 0;
    1.25                          }
    1.26 +                    } else if (code == NETCODE_CONNLOST) {
    1.27 +                        printw("Your opponent left the game.");
    1.28 +                        return 1;
    1.29                      } else {
    1.30                          printw("Invalid network response.");
    1.31                      }
    1.32 @@ -362,6 +371,10 @@
    1.33                  printw("\rYour opponent resigned!");
    1.34                  clrtoeol();
    1.35                  return 1;
    1.36 +            case NETCODE_CONNLOST:
    1.37 +                printw("\rYour opponent has left the game.");
    1.38 +                clrtoeol();
    1.39 +                return 1;
    1.40              case NETCODE_REMIS:
    1.41                  if (prompt_yesno(
    1.42                      "\rYour opponent offers remis - do you accept")) {
    1.43 @@ -437,13 +450,6 @@
    1.44      draw_board(&gamestate);
    1.45      
    1.46      gamestate_cleanup(&gamestate);
    1.47 -    
    1.48 -    mvaddstr(getmaxy(stdscr)-1, 0,
    1.49 -        "Game has ended. Press any key to leave...");
    1.50 -    refresh();
    1.51 -    cbreak();
    1.52 -    flushinp();
    1.53 -    getch();
    1.54  }
    1.55  
    1.56  void game_start(Settings *settings, int opponent) {
    1.57 @@ -473,11 +479,4 @@
    1.58      draw_board(&gamestate);
    1.59      
    1.60      gamestate_cleanup(&gamestate);
    1.61 -    
    1.62 -    mvaddstr(getmaxy(stdscr)-1, 0,
    1.63 -        "Game has ended. Press any key to leave...");
    1.64 -    refresh();
    1.65 -    cbreak();
    1.66 -    flushinp();
    1.67 -    getch();
    1.68  }
     2.1 --- a/src/main.c	Thu Apr 17 11:55:36 2014 +0200
     2.2 +++ b/src/main.c	Thu Apr 17 12:16:14 2014 +0200
     2.3 @@ -179,15 +179,22 @@
     2.4      }
     2.5      atexit(leavescr);
     2.6      
     2.7 +    int exitcode;
     2.8      if (settings.singlemachine) {
     2.9          game_start_singlemachine(&settings);
    2.10 +        exitcode = EXIT_SUCCESS;
    2.11      } else {
    2.12 -        int exitcode = is_server(&settings) ?
    2.13 +        exitcode = is_server(&settings) ?
    2.14              server_run(&settings) : client_run(&settings);
    2.15 -        
    2.16 -        if (exitcode != EXIT_SUCCESS) {
    2.17 -            cbreak(); getch();
    2.18 -        }
    2.19      }
    2.20 +    
    2.21 +    mvaddstr(getmaxy(stdscr)-1, 0,
    2.22 +        "Game has ended. Press any key to leave...");
    2.23 +    refresh();
    2.24 +    cbreak();
    2.25 +    flushinp();
    2.26 +    getch();
    2.27 +    
    2.28 +    return exitcode;
    2.29  }
    2.30  
     3.1 --- a/src/network.c	Thu Apr 17 11:55:36 2014 +0200
     3.2 +++ b/src/network.c	Thu Apr 17 12:16:14 2014 +0200
     3.3 @@ -126,8 +126,11 @@
     3.4  
     3.5  uint8_t net_recieve_code(int socket) {
     3.6      uint8_t code;
     3.7 -    recv(socket, &code, sizeof(uint8_t), 0);
     3.8 -    return code;
     3.9 +    if (recv(socket, &code, sizeof(code), 0) == sizeof(code)) {
    3.10 +        return code;
    3.11 +    } else {
    3.12 +        return NETCODE_CONNLOST;
    3.13 +    }
    3.14  }
    3.15  
    3.16  void net_recieve_data(int socket, void *data, size_t len) {
     4.1 --- a/src/network.h	Thu Apr 17 11:55:36 2014 +0200
     4.2 +++ b/src/network.h	Thu Apr 17 12:16:14 2014 +0200
     4.3 @@ -47,6 +47,7 @@
     4.4  #define NETCODE_RESIGN 0x41
     4.5  #define NETCODE_REMIS 0x42
     4.6  #define NETCODE_TIMEOVER 0x44
     4.7 +#define NETCODE_CONNLOST 0x80
     4.8  
     4.9  #define NETCODE_VERSION 13
    4.10  
     5.1 --- a/src/server.c	Thu Apr 17 11:55:36 2014 +0200
     5.2 +++ b/src/server.c	Thu Apr 17 12:16:14 2014 +0200
     5.3 @@ -54,7 +54,7 @@
     5.4          return 1;
     5.5      }
     5.6  
     5.7 -    printw("Client connected - transmitting gameinfo...");
     5.8 +    addstr("Client connected - transmitting gameinfo...");
     5.9      refresh();
    5.10      
    5.11      return 0;
    5.12 @@ -78,16 +78,19 @@
    5.13      int fd = server.client->fd;
    5.14      net_send_data(fd, NETCODE_GAMEINFO,
    5.15          &(settings->gameinfo), sizeof(GameInfo));
    5.16 -    printw("\rClient connected - awaiting challenge acceptance...");
    5.17 +    addstr("\rClient connected - awaiting challenge acceptance...");
    5.18      refresh();
    5.19      int code = net_recieve_code(fd);
    5.20      if (code == NETCODE_ACCEPT) {
    5.21 -        printw("\rClient connected - challenge accepted.");
    5.22 +        addstr("\rClient connected - challenge accepted.");
    5.23          clrtoeol();
    5.24          
    5.25          game_start(settings, fd);
    5.26      } else if (code == NETCODE_DECLINE) {
    5.27 -        printw("\rClient connected - challenge declined.");
    5.28 +        addstr("\rClient connected - challenge declined.");
    5.29 +        clrtoeol();
    5.30 +    } else if (code == NETCODE_CONNLOST) {
    5.31 +        addstr("\rClient connected - but gave no response.");
    5.32          clrtoeol();
    5.33      } else {
    5.34          addstr("\rInvalid client response");

mercurial