Mon, 17 Mar 2014 15:39:36 +0100
separated server and client module
src/Makefile | file | annotate | diff | comparison | revisions | |
src/client.c | file | annotate | diff | comparison | revisions | |
src/main.c | file | annotate | diff | comparison | revisions | |
src/server.c | file | annotate | diff | comparison | revisions | |
src/terminal-chess.h | file | annotate | diff | comparison | revisions |
1.1 --- a/src/Makefile Mon Mar 17 14:35:53 2014 +0100 1.2 +++ b/src/Makefile Mon Mar 17 15:39:36 2014 +0100 1.3 @@ -31,6 +31,8 @@ 1.4 SRC = main.c 1.5 SRC += network.c 1.6 SRC += input.c 1.7 +SRC += server.c 1.8 +SRC += client.c 1.9 1.10 OBJ = $(SRC:%.c=../build/%$(OBJ_EXT)) 1.11
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/src/client.c Mon Mar 17 15:39:36 2014 +0100 2.3 @@ -0,0 +1,83 @@ 2.4 +/* 2.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 2.6 + * 2.7 + * Copyright 2014 Mike Becker. All rights reserved. 2.8 + * 2.9 + * Redistribution and use in source and binary forms, with or without 2.10 + * modification, are permitted provided that the following conditions are met: 2.11 + * 2.12 + * 1. Redistributions of source code must retain the above copyright 2.13 + * notice, this list of conditions and the following disclaimer. 2.14 + * 2.15 + * 2. Redistributions in binary form must reproduce the above copyright 2.16 + * notice, this list of conditions and the following disclaimer in the 2.17 + * documentation and/or other materials provided with the distribution. 2.18 + * 2.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 2.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 2.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 2.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 2.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 2.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 2.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 2.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 2.29 + * POSSIBILITY OF SUCH DAMAGE. 2.30 + * 2.31 + */ 2.32 + 2.33 +#include "terminal-chess.h" 2.34 +#include "input.h" 2.35 +#include <ncurses.h> 2.36 + 2.37 +int client_run(Settings *settings) { 2.38 + Server server; 2.39 + int exit_code = EXIT_SUCCESS; 2.40 + 2.41 + if (net_find(&server, settings->serverhost, settings->port)) { 2.42 + fprintf(stderr, "Can't find server\n"); 2.43 + exit_code = EXIT_FAILURE; 2.44 + goto quit; 2.45 + } 2.46 + 2.47 + if (net_connect(&server)) { 2.48 + perror("Can't connect to server"); 2.49 + exit_code = EXIT_FAILURE; 2.50 + goto quit; 2.51 + } 2.52 + 2.53 + /* net version handshake */ 2.54 + int fd = server.fd; 2.55 + if (net_recieve_code(fd) != NETCODE_VERSION) { 2.56 + fprintf(stderr, "Server uses an incompatible software version.\n"); 2.57 + exit_code = EXIT_FAILURE; 2.58 + goto quit; 2.59 + } else { 2.60 + net_send_code(fd, NETCODE_VERSION); 2.61 + } 2.62 + 2.63 + printw("Connection established!\n\n"); 2.64 + refresh(); 2.65 + 2.66 + if (net_recieve_code(fd) == NETCODE_GAMEINFO) { 2.67 + net_recieve_data(fd, &(settings->gameinfo), 2.68 + sizeof(settings->gameinfo)); 2.69 + dump_gameinfo(&(settings->gameinfo)); 2.70 + printw("Accept challenge (y/n)? "); 2.71 + if (prompt_yesno()) { 2.72 + net_send_code(fd, NETCODE_ACCEPT); 2.73 + // TODO: start game 2.74 + } else { 2.75 + net_send_code(fd, NETCODE_DECLINE); 2.76 + } 2.77 + } else { 2.78 + fprintf(stderr, "Server sent invalid gameinfo.\n"); 2.79 + exit_code = EXIT_FAILURE; 2.80 + goto quit; 2.81 + } 2.82 + 2.83 +quit: 2.84 + net_destroy(&server); 2.85 + return exit_code; 2.86 +}
3.1 --- a/src/main.c Mon Mar 17 14:35:53 2014 +0100 3.2 +++ b/src/main.c Mon Mar 17 15:39:36 2014 +0100 3.3 @@ -31,7 +31,6 @@ 3.4 #include <string.h> 3.5 #include <time.h> 3.6 #include <ncurses.h> 3.7 -#include "input.h" 3.8 3.9 int get_settings(int argc, char **argv, Settings *settings) { 3.10 char *valid; 3.11 @@ -126,17 +125,6 @@ 3.12 refresh(); 3.13 } 3.14 3.15 -int cleanup(Settings *settings, int exitcode) { 3.16 - 3.17 - if (settings->server) { 3.18 - if (net_destroy(settings->server)) { 3.19 - perror("Server shutdown failed"); 3.20 - } 3.21 - } 3.22 - 3.23 - return exitcode; 3.24 -} 3.25 - 3.26 static WINDOW* window; 3.27 3.28 void leavescr() { 3.29 @@ -179,88 +167,6 @@ 3.30 cbreak(); 3.31 atexit(leavescr); 3.32 3.33 - Server server; 3.34 - settings.server = &server; 3.35 - 3.36 - if (is_server(&settings)) { 3.37 - dump_gameinfo(&(settings.gameinfo)); 3.38 - printw("\nListening for client...\n"); 3.39 - refresh(); 3.40 - if (net_create(&server, settings.port)) { 3.41 - perror("Server creation failed"); 3.42 - return cleanup(&settings, EXIT_FAILURE); 3.43 - } 3.44 - 3.45 - if (net_listen(&server)) { 3.46 - perror("Listening for client failed"); 3.47 - return cleanup(&settings, EXIT_FAILURE); 3.48 - } 3.49 - 3.50 - /* net version handshake */ 3.51 - int fd = server.client->fd; 3.52 - net_send_code(fd, NETCODE_VERSION); 3.53 - if (net_recieve_code(fd) != NETCODE_VERSION) { 3.54 - fprintf(stderr, "Client uses an incompatible software version.\n"); 3.55 - return cleanup(&settings, EXIT_FAILURE); 3.56 - } 3.57 - 3.58 - printw("Client connected - transmitting gameinfo..."); 3.59 - refresh(); 3.60 - 3.61 - 3.62 - net_send_code(fd, NETCODE_GAMEINFO); 3.63 - net_send_data(fd, &(settings.gameinfo), sizeof(settings.gameinfo)); 3.64 - printw("\rClient connected - awaiting challenge acceptance..."); 3.65 - refresh(); 3.66 - int code = net_recieve_code(fd); 3.67 - if (code == NETCODE_ACCEPT) { 3.68 - printw("\rClient connected - challenge accepted."); 3.69 - clrtoeol(); 3.70 - } else if (code == NETCODE_DECLINE) { 3.71 - printw("\rClient connected - challenge declined."); 3.72 - clrtoeol(); 3.73 - } else { 3.74 - fprintf(stderr, "Invalid client response\n"); 3.75 - return cleanup(&settings, EXIT_FAILURE); 3.76 - } 3.77 - } else { 3.78 - if (net_find(&server, settings.serverhost, settings.port)) { 3.79 - fprintf(stderr, "Can't find server\n"); 3.80 - return cleanup(&settings, EXIT_FAILURE); 3.81 - } 3.82 - 3.83 - if (net_connect(&server)) { 3.84 - perror("Can't connect to server"); 3.85 - return cleanup(&settings, EXIT_FAILURE); 3.86 - } 3.87 - 3.88 - int fd = server.fd; 3.89 - if (net_recieve_code(fd) != NETCODE_VERSION) { 3.90 - fprintf(stderr, "Server uses an incompatible software version.\n"); 3.91 - return cleanup(&settings, EXIT_FAILURE); 3.92 - } else { 3.93 - net_send_code(fd, NETCODE_VERSION); 3.94 - } 3.95 - 3.96 - printw("Connection established!\n\n"); 3.97 - refresh(); 3.98 - 3.99 - if (net_recieve_code(fd) == NETCODE_GAMEINFO) { 3.100 - net_recieve_data(fd, &(settings.gameinfo), 3.101 - sizeof(settings.gameinfo)); 3.102 - dump_gameinfo(&(settings.gameinfo)); 3.103 - printw("Accept challenge (y/n)? "); 3.104 - if (prompt_yesno()) { 3.105 - net_send_code(fd, NETCODE_ACCEPT); 3.106 - // TODO: start game 3.107 - } else { 3.108 - net_send_code(fd, NETCODE_DECLINE); 3.109 - } 3.110 - } else { 3.111 - fprintf(stderr, "Server sent invalid gameinfo.\n"); 3.112 - } 3.113 - } 3.114 - 3.115 - return cleanup(&settings, EXIT_SUCCESS); 3.116 + return is_server(&settings) ? server_run(&settings) : client_run(&settings); 3.117 } 3.118
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/src/server.c Mon Mar 17 15:39:36 2014 +0100 4.3 @@ -0,0 +1,86 @@ 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 +#include "terminal-chess.h" 4.34 +#include <ncurses.h> 4.35 + 4.36 +int server_run(Settings *settings) { 4.37 + Server server; 4.38 + int exit_code = EXIT_SUCCESS; 4.39 + 4.40 + dump_gameinfo(&(settings->gameinfo)); 4.41 + printw("\nListening for client...\n"); 4.42 + refresh(); 4.43 + if (net_create(&server, settings->port)) { 4.44 + perror("Server creation failed"); 4.45 + exit_code = EXIT_FAILURE; 4.46 + goto quit; 4.47 + } 4.48 + 4.49 + if (net_listen(&server)) { 4.50 + perror("Listening for client failed"); 4.51 + exit_code = EXIT_FAILURE; 4.52 + goto quit; 4.53 + } 4.54 + 4.55 + /* net version handshake */ 4.56 + int fd = server.client->fd; 4.57 + net_send_code(fd, NETCODE_VERSION); 4.58 + if (net_recieve_code(fd) != NETCODE_VERSION) { 4.59 + fprintf(stderr, "Client uses an incompatible software version.\n"); 4.60 + exit_code = EXIT_FAILURE; 4.61 + goto quit; 4.62 + } 4.63 + 4.64 + printw("Client connected - transmitting gameinfo..."); 4.65 + refresh(); 4.66 + 4.67 + 4.68 + net_send_code(fd, NETCODE_GAMEINFO); 4.69 + net_send_data(fd, &(settings->gameinfo), sizeof(settings->gameinfo)); 4.70 + printw("\rClient connected - awaiting challenge acceptance..."); 4.71 + refresh(); 4.72 + int code = net_recieve_code(fd); 4.73 + if (code == NETCODE_ACCEPT) { 4.74 + printw("\rClient connected - challenge accepted."); 4.75 + clrtoeol(); 4.76 + } else if (code == NETCODE_DECLINE) { 4.77 + printw("\rClient connected - challenge declined."); 4.78 + clrtoeol(); 4.79 + } else { 4.80 + fprintf(stderr, "Invalid client response\n"); 4.81 + exit_code = EXIT_FAILURE; 4.82 + goto quit; 4.83 + } 4.84 + 4.85 +quit: 4.86 + 4.87 + net_destroy(&server); 4.88 + return exit_code; 4.89 +}
5.1 --- a/src/terminal-chess.h Mon Mar 17 14:35:53 2014 +0100 5.2 +++ b/src/terminal-chess.h Mon Mar 17 15:39:36 2014 +0100 5.3 @@ -54,11 +54,15 @@ 5.4 Gameinfo gameinfo; 5.5 char* port; 5.6 char* serverhost; /* NULL, if we are about to start a server */ 5.7 - Server *server; 5.8 } Settings; 5.9 5.10 #define is_server(settings) !((settings)->serverhost) 5.11 5.12 +void dump_gameinfo(Gameinfo *gameinfo); 5.13 + 5.14 +int server_run(Settings* settings); 5.15 +int client_run(Settings* settings); 5.16 + 5.17 #ifdef __cplusplus 5.18 } 5.19 #endif