# HG changeset patch # User Mike Becker # Date 1395220105 -3600 # Node ID daaf6e5b3501a0be1e73893026e42e445e0bc507 # Parent f7dfef88947db6f635a61049dcce2b94a61262af structured code diff -r f7dfef88947d -r daaf6e5b3501 src/Makefile --- a/src/Makefile Mon Mar 17 15:39:36 2014 +0100 +++ b/src/Makefile Wed Mar 19 10:08:25 2014 +0100 @@ -33,6 +33,7 @@ SRC += input.c SRC += server.c SRC += client.c +SRC += game.c OBJ = $(SRC:%.c=../build/%$(OBJ_EXT)) diff -r f7dfef88947d -r daaf6e5b3501 src/client.c --- a/src/client.c Mon Mar 17 15:39:36 2014 +0100 +++ b/src/client.c Wed Mar 19 10:08:25 2014 +0100 @@ -29,55 +29,67 @@ #include "terminal-chess.h" #include "input.h" +#include "game.h" #include -int client_run(Settings *settings) { - Server server; - int exit_code = EXIT_SUCCESS; - - if (net_find(&server, settings->serverhost, settings->port)) { +static int client_connect(Server *server, char *host, char *port) { + if (net_find(server, host, port)) { fprintf(stderr, "Can't find server\n"); - exit_code = EXIT_FAILURE; - goto quit; + return 1; } - if (net_connect(&server)) { + if (net_connect(server)) { perror("Can't connect to server"); - exit_code = EXIT_FAILURE; - goto quit; + return 1; } + + return 0; +} - /* net version handshake */ - int fd = server.fd; - if (net_recieve_code(fd) != NETCODE_VERSION) { +static int client_handshake(Server *server) { + if (net_recieve_code(server->fd) != NETCODE_VERSION) { fprintf(stderr, "Server uses an incompatible software version.\n"); - exit_code = EXIT_FAILURE; - goto quit; + return 1; } else { - net_send_code(fd, NETCODE_VERSION); + net_send_code(server->fd, NETCODE_VERSION); } printw("Connection established!\n\n"); refresh(); + + return 0; +} - if (net_recieve_code(fd) == NETCODE_GAMEINFO) { - net_recieve_data(fd, &(settings->gameinfo), +int client_run(Settings *settings) { + Server server; + + if (client_connect(&server, settings->serverhost, settings->port)) { + net_destroy(&server); + return EXIT_FAILURE; + } + + if (client_handshake(&server)) { + net_destroy(&server); + return EXIT_FAILURE; + } + + if (net_recieve_code(server.fd) == NETCODE_GAMEINFO) { + net_recieve_data(server.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 + net_send_code(server.fd, NETCODE_ACCEPT); + game_start(settings, server.fd); } else { - net_send_code(fd, NETCODE_DECLINE); + net_send_code(server.fd, NETCODE_DECLINE); } } else { fprintf(stderr, "Server sent invalid gameinfo.\n"); - exit_code = EXIT_FAILURE; - goto quit; + net_destroy(&server); + return EXIT_FAILURE; } -quit: net_destroy(&server); - return exit_code; + return EXIT_SUCCESS; } diff -r f7dfef88947d -r daaf6e5b3501 src/game.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/game.c Wed Mar 19 10:08:25 2014 +0100 @@ -0,0 +1,34 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2014 Mike Becker. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include "game.h" + +void game_start(Settings *settings, int opponent) { + +} diff -r f7dfef88947d -r daaf6e5b3501 src/game.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/game.h Wed Mar 19 10:08:25 2014 +0100 @@ -0,0 +1,46 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2014 Mike Becker. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef GAME_H +#define GAME_H + +#include "terminal-chess.h" + +#ifdef __cplusplus +extern "C" { +#endif + +void game_start(Settings *settings, int opponent); + +#ifdef __cplusplus +} +#endif + +#endif /* GAME_H */ + diff -r f7dfef88947d -r daaf6e5b3501 src/server.c --- a/src/server.c Mon Mar 17 15:39:36 2014 +0100 +++ b/src/server.c Wed Mar 19 10:08:25 2014 +0100 @@ -28,40 +28,54 @@ */ #include "terminal-chess.h" +#include "game.h" #include -int server_run(Settings *settings) { - Server server; - int exit_code = EXIT_SUCCESS; - - dump_gameinfo(&(settings->gameinfo)); +static int server_open(Server *server, char *port) { printw("\nListening for client...\n"); refresh(); - if (net_create(&server, settings->port)) { + if (net_create(server, port)) { perror("Server creation failed"); - exit_code = EXIT_FAILURE; - goto quit; + return 1; } - if (net_listen(&server)) { + if (net_listen(server)) { perror("Listening for client failed"); - exit_code = EXIT_FAILURE; - goto quit; + return 1; } + + return 0; +} - /* net version handshake */ - int fd = server.client->fd; - net_send_code(fd, NETCODE_VERSION); - if (net_recieve_code(fd) != NETCODE_VERSION) { +static int server_handshake(Client *client) { + net_send_code(client->fd, NETCODE_VERSION); + if (net_recieve_code(client->fd) != NETCODE_VERSION) { fprintf(stderr, "Client uses an incompatible software version.\n"); - exit_code = EXIT_FAILURE; - goto quit; + return 1; } printw("Client connected - transmitting gameinfo..."); refresh(); + + return 0; +} +int server_run(Settings *settings) { + Server server; + + dump_gameinfo(&(settings->gameinfo)); + + if (server_open(&server, settings->port)) { + net_destroy(&server); + return EXIT_FAILURE; + } + + if (server_handshake(server.client)) { + net_destroy(&server); + return EXIT_FAILURE; + } + int fd = server.client->fd; net_send_code(fd, NETCODE_GAMEINFO); net_send_data(fd, &(settings->gameinfo), sizeof(settings->gameinfo)); printw("\rClient connected - awaiting challenge acceptance..."); @@ -70,17 +84,17 @@ if (code == NETCODE_ACCEPT) { printw("\rClient connected - challenge accepted."); clrtoeol(); + + game_start(settings, fd); } else if (code == NETCODE_DECLINE) { printw("\rClient connected - challenge declined."); clrtoeol(); } else { fprintf(stderr, "Invalid client response\n"); - exit_code = EXIT_FAILURE; - goto quit; + net_destroy(&server); + return EXIT_FAILURE; } -quit: - net_destroy(&server); - return exit_code; + return EXIT_SUCCESS; }