Wed, 19 Mar 2014 10:08:25 +0100
structured code
src/Makefile | file | annotate | diff | comparison | revisions | |
src/client.c | file | annotate | diff | comparison | revisions | |
src/game.c | file | annotate | diff | comparison | revisions | |
src/game.h | file | annotate | diff | comparison | revisions | |
src/server.c | file | annotate | diff | comparison | revisions |
1.1 --- a/src/Makefile Mon Mar 17 15:39:36 2014 +0100 1.2 +++ b/src/Makefile Wed Mar 19 10:08:25 2014 +0100 1.3 @@ -33,6 +33,7 @@ 1.4 SRC += input.c 1.5 SRC += server.c 1.6 SRC += client.c 1.7 +SRC += game.c 1.8 1.9 OBJ = $(SRC:%.c=../build/%$(OBJ_EXT)) 1.10
2.1 --- a/src/client.c Mon Mar 17 15:39:36 2014 +0100 2.2 +++ b/src/client.c Wed Mar 19 10:08:25 2014 +0100 2.3 @@ -29,55 +29,67 @@ 2.4 2.5 #include "terminal-chess.h" 2.6 #include "input.h" 2.7 +#include "game.h" 2.8 #include <ncurses.h> 2.9 2.10 -int client_run(Settings *settings) { 2.11 - Server server; 2.12 - int exit_code = EXIT_SUCCESS; 2.13 - 2.14 - if (net_find(&server, settings->serverhost, settings->port)) { 2.15 +static int client_connect(Server *server, char *host, char *port) { 2.16 + if (net_find(server, host, port)) { 2.17 fprintf(stderr, "Can't find server\n"); 2.18 - exit_code = EXIT_FAILURE; 2.19 - goto quit; 2.20 + return 1; 2.21 } 2.22 2.23 - if (net_connect(&server)) { 2.24 + if (net_connect(server)) { 2.25 perror("Can't connect to server"); 2.26 - exit_code = EXIT_FAILURE; 2.27 - goto quit; 2.28 + return 1; 2.29 } 2.30 + 2.31 + return 0; 2.32 +} 2.33 2.34 - /* net version handshake */ 2.35 - int fd = server.fd; 2.36 - if (net_recieve_code(fd) != NETCODE_VERSION) { 2.37 +static int client_handshake(Server *server) { 2.38 + if (net_recieve_code(server->fd) != NETCODE_VERSION) { 2.39 fprintf(stderr, "Server uses an incompatible software version.\n"); 2.40 - exit_code = EXIT_FAILURE; 2.41 - goto quit; 2.42 + return 1; 2.43 } else { 2.44 - net_send_code(fd, NETCODE_VERSION); 2.45 + net_send_code(server->fd, NETCODE_VERSION); 2.46 } 2.47 2.48 printw("Connection established!\n\n"); 2.49 refresh(); 2.50 + 2.51 + return 0; 2.52 +} 2.53 2.54 - if (net_recieve_code(fd) == NETCODE_GAMEINFO) { 2.55 - net_recieve_data(fd, &(settings->gameinfo), 2.56 +int client_run(Settings *settings) { 2.57 + Server server; 2.58 + 2.59 + if (client_connect(&server, settings->serverhost, settings->port)) { 2.60 + net_destroy(&server); 2.61 + return EXIT_FAILURE; 2.62 + } 2.63 + 2.64 + if (client_handshake(&server)) { 2.65 + net_destroy(&server); 2.66 + return EXIT_FAILURE; 2.67 + } 2.68 + 2.69 + if (net_recieve_code(server.fd) == NETCODE_GAMEINFO) { 2.70 + net_recieve_data(server.fd, &(settings->gameinfo), 2.71 sizeof(settings->gameinfo)); 2.72 dump_gameinfo(&(settings->gameinfo)); 2.73 printw("Accept challenge (y/n)? "); 2.74 if (prompt_yesno()) { 2.75 - net_send_code(fd, NETCODE_ACCEPT); 2.76 - // TODO: start game 2.77 + net_send_code(server.fd, NETCODE_ACCEPT); 2.78 + game_start(settings, server.fd); 2.79 } else { 2.80 - net_send_code(fd, NETCODE_DECLINE); 2.81 + net_send_code(server.fd, NETCODE_DECLINE); 2.82 } 2.83 } else { 2.84 fprintf(stderr, "Server sent invalid gameinfo.\n"); 2.85 - exit_code = EXIT_FAILURE; 2.86 - goto quit; 2.87 + net_destroy(&server); 2.88 + return EXIT_FAILURE; 2.89 } 2.90 2.91 -quit: 2.92 net_destroy(&server); 2.93 - return exit_code; 2.94 + return EXIT_SUCCESS; 2.95 }
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/src/game.c Wed Mar 19 10:08:25 2014 +0100 3.3 @@ -0,0 +1,34 @@ 3.4 +/* 3.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3.6 + * 3.7 + * Copyright 2014 Mike Becker. All rights reserved. 3.8 + * 3.9 + * Redistribution and use in source and binary forms, with or without 3.10 + * modification, are permitted provided that the following conditions are met: 3.11 + * 3.12 + * 1. Redistributions of source code must retain the above copyright 3.13 + * notice, this list of conditions and the following disclaimer. 3.14 + * 3.15 + * 2. Redistributions in binary form must reproduce the above copyright 3.16 + * notice, this list of conditions and the following disclaimer in the 3.17 + * documentation and/or other materials provided with the distribution. 3.18 + * 3.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 3.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 3.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 3.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 3.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 3.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 3.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 3.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 3.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 3.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 3.29 + * POSSIBILITY OF SUCH DAMAGE. 3.30 + * 3.31 + */ 3.32 + 3.33 +#include "game.h" 3.34 + 3.35 +void game_start(Settings *settings, int opponent) { 3.36 + 3.37 +}
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/src/game.h Wed Mar 19 10:08:25 2014 +0100 4.3 @@ -0,0 +1,46 @@ 4.4 +/* 4.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 4.6 + * 4.7 + * Copyright 2014 Mike Becker. All rights reserved. 4.8 + * 4.9 + * Redistribution and use in source and binary forms, with or without 4.10 + * modification, are permitted provided that the following conditions are met: 4.11 + * 4.12 + * 1. Redistributions of source code must retain the above copyright 4.13 + * notice, this list of conditions and the following disclaimer. 4.14 + * 4.15 + * 2. Redistributions in binary form must reproduce the above copyright 4.16 + * notice, this list of conditions and the following disclaimer in the 4.17 + * documentation and/or other materials provided with the distribution. 4.18 + * 4.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 4.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 4.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 4.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 4.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 4.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 4.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 4.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 4.29 + * POSSIBILITY OF SUCH DAMAGE. 4.30 + * 4.31 + */ 4.32 + 4.33 +#ifndef GAME_H 4.34 +#define GAME_H 4.35 + 4.36 +#include "terminal-chess.h" 4.37 + 4.38 +#ifdef __cplusplus 4.39 +extern "C" { 4.40 +#endif 4.41 + 4.42 +void game_start(Settings *settings, int opponent); 4.43 + 4.44 +#ifdef __cplusplus 4.45 +} 4.46 +#endif 4.47 + 4.48 +#endif /* GAME_H */ 4.49 +
5.1 --- a/src/server.c Mon Mar 17 15:39:36 2014 +0100 5.2 +++ b/src/server.c Wed Mar 19 10:08:25 2014 +0100 5.3 @@ -28,40 +28,54 @@ 5.4 */ 5.5 5.6 #include "terminal-chess.h" 5.7 +#include "game.h" 5.8 #include <ncurses.h> 5.9 5.10 -int server_run(Settings *settings) { 5.11 - Server server; 5.12 - int exit_code = EXIT_SUCCESS; 5.13 - 5.14 - dump_gameinfo(&(settings->gameinfo)); 5.15 +static int server_open(Server *server, char *port) { 5.16 printw("\nListening for client...\n"); 5.17 refresh(); 5.18 - if (net_create(&server, settings->port)) { 5.19 + if (net_create(server, port)) { 5.20 perror("Server creation failed"); 5.21 - exit_code = EXIT_FAILURE; 5.22 - goto quit; 5.23 + return 1; 5.24 } 5.25 5.26 - if (net_listen(&server)) { 5.27 + if (net_listen(server)) { 5.28 perror("Listening for client failed"); 5.29 - exit_code = EXIT_FAILURE; 5.30 - goto quit; 5.31 + return 1; 5.32 } 5.33 + 5.34 + return 0; 5.35 +} 5.36 5.37 - /* net version handshake */ 5.38 - int fd = server.client->fd; 5.39 - net_send_code(fd, NETCODE_VERSION); 5.40 - if (net_recieve_code(fd) != NETCODE_VERSION) { 5.41 +static int server_handshake(Client *client) { 5.42 + net_send_code(client->fd, NETCODE_VERSION); 5.43 + if (net_recieve_code(client->fd) != NETCODE_VERSION) { 5.44 fprintf(stderr, "Client uses an incompatible software version.\n"); 5.45 - exit_code = EXIT_FAILURE; 5.46 - goto quit; 5.47 + return 1; 5.48 } 5.49 5.50 printw("Client connected - transmitting gameinfo..."); 5.51 refresh(); 5.52 + 5.53 + return 0; 5.54 +} 5.55 5.56 +int server_run(Settings *settings) { 5.57 + Server server; 5.58 + 5.59 + dump_gameinfo(&(settings->gameinfo)); 5.60 + 5.61 + if (server_open(&server, settings->port)) { 5.62 + net_destroy(&server); 5.63 + return EXIT_FAILURE; 5.64 + } 5.65 + 5.66 + if (server_handshake(server.client)) { 5.67 + net_destroy(&server); 5.68 + return EXIT_FAILURE; 5.69 + } 5.70 5.71 + int fd = server.client->fd; 5.72 net_send_code(fd, NETCODE_GAMEINFO); 5.73 net_send_data(fd, &(settings->gameinfo), sizeof(settings->gameinfo)); 5.74 printw("\rClient connected - awaiting challenge acceptance..."); 5.75 @@ -70,17 +84,17 @@ 5.76 if (code == NETCODE_ACCEPT) { 5.77 printw("\rClient connected - challenge accepted."); 5.78 clrtoeol(); 5.79 + 5.80 + game_start(settings, fd); 5.81 } else if (code == NETCODE_DECLINE) { 5.82 printw("\rClient connected - challenge declined."); 5.83 clrtoeol(); 5.84 } else { 5.85 fprintf(stderr, "Invalid client response\n"); 5.86 - exit_code = EXIT_FAILURE; 5.87 - goto quit; 5.88 + net_destroy(&server); 5.89 + return EXIT_FAILURE; 5.90 } 5.91 5.92 -quit: 5.93 - 5.94 net_destroy(&server); 5.95 - return exit_code; 5.96 + return EXIT_SUCCESS; 5.97 }