diff -r 0a08f79c320d -r 3693fd2eb0e9 src/main.c --- a/src/main.c Sun Feb 23 21:03:35 2014 +0100 +++ b/src/main.c Thu Mar 06 15:03:06 2014 +0100 @@ -30,6 +30,8 @@ #include "terminal-chess.h" #include #include +#include +#include "input.h" int get_settings(int argc, char **argv, Settings *settings) { char *valid; @@ -104,25 +106,28 @@ void dump_gameinfo(Gameinfo *gameinfo) { int serverwhite = gameinfo->servercolor == WHITE; - printf( - "Game details:\n" - " Server plays %s - Client plays %s\n", + attron(A_UNDERLINE); + printw("Game details\n"); + attroff(A_UNDERLINE); + printw(" Server: %s\n Client: %s\n", serverwhite?"white":"black", serverwhite?"black":"White" ); if (gameinfo->time > 0) { if (gameinfo->time % 60) { - printf(" Time limit: %ds + %ds\n", + printw(" Time limit: %ds + %ds\n", gameinfo->time, gameinfo->addtime); } else { - printf(" Time limit: %dm + %ds\n", + printw(" Time limit: %dm + %ds\n", gameinfo->time/60, gameinfo->addtime); } } else { - printf(" No time limit\n"); + printw(" No time limit\n"); } + refresh(); } int cleanup(Settings *settings, int exitcode) { + if (settings->server) { if (net_destroy(settings->server)) { perror("Server shutdown failed"); @@ -162,12 +167,17 @@ return EXIT_SUCCESS; } + initscr(); + cbreak(); + atexit((void(*)(void)) endwin); + Server server; settings.server = &server; if (is_server(&settings)) { dump_gameinfo(&(settings.gameinfo)); - printf("\nListening for client...\n"); + printw("\nListening for client...\n"); + refresh(); if (net_create(&server, settings.port)) { perror("Server creation failed"); return cleanup(&settings, EXIT_FAILURE); @@ -178,12 +188,36 @@ return cleanup(&settings, EXIT_FAILURE); } - printf("Client connected - transmitting gameinfo...\n"); - net_send(server.client->fd, NETCODE_GAMEINFO, - &(settings.gameinfo), sizeof(settings.gameinfo)); + /* net version handshake */ + int fd = server.client->fd; + net_send_code(fd, NETCODE_VERSION); + if (net_recieve_code(fd) != NETCODE_VERSION) { + fprintf(stderr, "Client uses an incompatible software version.\n"); + return cleanup(&settings, EXIT_FAILURE); + } + + printw("Client connected - transmitting gameinfo..."); + refresh(); + + + net_send_code(fd, NETCODE_GAMEINFO); + net_send_data(fd, &(settings.gameinfo), sizeof(settings.gameinfo)); + printw("\rClient connected - awaiting challenge acceptance..."); + refresh(); + int code = net_recieve_code(fd); + if (code == NETCODE_ACCEPT) { + printw("\rClient connected - challenge accepted."); + clrtoeol(); + } else if (code == NETCODE_DECLINE) { + printw("\rClient connected - challenge declined."); + clrtoeol(); + } else { + fprintf(stderr, "Invalid client response\n"); + return cleanup(&settings, EXIT_FAILURE); + } } else { if (net_find(&server, settings.serverhost, settings.port)) { - perror("Can't find server"); + fprintf(stderr, "Can't find server\n"); return cleanup(&settings, EXIT_FAILURE); } @@ -191,17 +225,35 @@ perror("Can't connect to server"); return cleanup(&settings, EXIT_FAILURE); } + + int fd = server.fd; + if (net_recieve_code(fd) != NETCODE_VERSION) { + fprintf(stderr, "Server uses an incompatible software version.\n"); + return cleanup(&settings, EXIT_FAILURE); + } else { + net_send_code(fd, NETCODE_VERSION); + } - printf("Connection established!\n\n"); - if (net_recieve_code(server.fd) == NETCODE_GAMEINFO) { - net_recieve_data(server.fd, &(settings.gameinfo), + printw("Connection established!\n\n"); + refresh(); + + if (net_recieve_code(fd) == NETCODE_GAMEINFO) { + net_recieve_data(fd, &(settings.gameinfo), sizeof(settings.gameinfo)); dump_gameinfo(&(settings.gameinfo)); + printw("Accept challenge (y/n)? "); + if (prompt_yesno()) { + net_send_code(fd, NETCODE_ACCEPT); + // TODO: start game + } else { + net_send_code(fd, NETCODE_DECLINE); + } } else { fprintf(stderr, "Server sent invalid gameinfo.\n"); } } + getch(); /* TODO: remove */ return cleanup(&settings, EXIT_SUCCESS); }