universe@5: /* universe@5: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@5: * universe@55: * Copyright 2016 Mike Becker. All rights reserved. universe@5: * universe@5: * Redistribution and use in source and binary forms, with or without universe@5: * modification, are permitted provided that the following conditions are met: universe@5: * universe@5: * 1. Redistributions of source code must retain the above copyright universe@5: * notice, this list of conditions and the following disclaimer. universe@5: * universe@5: * 2. Redistributions in binary form must reproduce the above copyright universe@5: * notice, this list of conditions and the following disclaimer in the universe@5: * documentation and/or other materials provided with the distribution. universe@5: * universe@5: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@5: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@5: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@5: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@5: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@5: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@5: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@5: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@5: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@5: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@5: * POSSIBILITY OF SUCH DAMAGE. universe@5: * universe@5: */ universe@5: universe@5: #include "terminal-chess.h" universe@6: #include "game.h" universe@5: #include universe@51: #include universe@51: #include universe@5: universe@6: static int server_open(Server *server, char *port) { universe@5: printw("\nListening for client...\n"); universe@5: refresh(); universe@6: if (net_create(server, port)) { universe@34: addstr("Server creation failed"); universe@6: return 1; universe@5: } universe@5: universe@6: if (net_listen(server)) { universe@34: addstr("Listening for client failed"); universe@6: return 1; universe@5: } universe@6: universe@6: return 0; universe@6: } universe@5: universe@6: static int server_handshake(Client *client) { universe@6: net_send_code(client->fd, NETCODE_VERSION); universe@6: if (net_recieve_code(client->fd) != NETCODE_VERSION) { universe@34: addstr("Client uses an incompatible software version."); universe@6: return 1; universe@5: } universe@5: universe@46: addstr("Client connected - transmitting gameinfo..."); universe@5: refresh(); universe@6: universe@6: return 0; universe@6: } universe@5: universe@6: int server_run(Settings *settings) { universe@6: Server server; universe@6: universe@6: dump_gameinfo(&(settings->gameinfo)); universe@51: GameState continuegame; universe@51: gamestate_init(&continuegame); universe@51: if (settings->continuepgn) { universe@64: /* preload PGN data before handshake */ universe@51: FILE *pgnfile = fopen(settings->continuepgn, "r"); universe@51: if (pgnfile) { universe@51: int result = read_pgn(pgnfile, &continuegame, universe@51: &(settings->gameinfo)); universe@60: long position = ftell(pgnfile); universe@51: fclose(pgnfile); universe@59: if (result) { universe@60: printw("Invalid PGN file content at position %ld:\n%s\n", universe@60: position, pgn_error_str(result)); universe@59: return 1; universe@51: } universe@51: if (!is_game_running(&continuegame)) { universe@51: addstr("Game has ended. Use -S to analyze it.\n"); universe@59: return 1; universe@51: } universe@51: addch('\n'); universe@51: dump_moveinfo(&continuegame); universe@51: addch('\n'); universe@51: } else { universe@51: printw("Can't read PGN file (%s)\n", strerror(errno)); universe@59: return 1; universe@51: } universe@51: } universe@6: universe@6: if (server_open(&server, settings->port)) { universe@6: net_destroy(&server); universe@59: return 1; universe@6: } universe@6: universe@6: if (server_handshake(server.client)) { universe@6: net_destroy(&server); universe@59: return 1; universe@6: } universe@5: universe@6: int fd = server.client->fd; universe@51: if (settings->continuepgn) { universe@64: /* Continue game, send PGN data */ universe@51: uint16_t mc = 0; universe@51: MoveList *movelist = continuegame.movelist; universe@51: while (movelist) { universe@51: mc++; universe@51: movelist = movelist->next; universe@51: } universe@51: universe@51: Move* moves = calloc(mc, sizeof(Move)); universe@51: universe@51: movelist = continuegame.movelist; universe@51: mc = 0; universe@51: while (movelist) { universe@51: moves[mc] = movelist->move; universe@51: mc++; universe@51: movelist = movelist->next; universe@51: } universe@51: universe@51: size_t pgndata_size = sizeof(GameInfo)+sizeof(mc)+mc*sizeof(Move); universe@51: char *pgndata = malloc(pgndata_size); universe@51: memcpy(pgndata, &(settings->gameinfo), sizeof(GameInfo)); universe@51: memcpy(pgndata+sizeof(GameInfo), &mc, sizeof(mc)); universe@51: memcpy(pgndata+sizeof(GameInfo)+sizeof(mc), moves, mc*sizeof(Move)); universe@51: free(moves); universe@51: net_send_data(fd, NETCODE_PGNDATA, pgndata, pgndata_size); universe@51: free(pgndata); universe@51: } else { universe@64: /* Start new game */ universe@51: net_send_data(fd, NETCODE_GAMEINFO, universe@51: &(settings->gameinfo), sizeof(GameInfo)); universe@51: } universe@46: addstr("\rClient connected - awaiting challenge acceptance..."); universe@5: refresh(); universe@5: int code = net_recieve_code(fd); universe@5: if (code == NETCODE_ACCEPT) { universe@46: addstr("\rClient connected - challenge accepted."); universe@5: clrtoeol(); universe@51: if (settings->continuepgn) { universe@51: game_continue(settings, fd, &continuegame); universe@51: } else { universe@51: game_start(settings, fd); universe@51: } universe@5: } else if (code == NETCODE_DECLINE) { universe@46: addstr("\rClient connected - challenge declined."); universe@46: clrtoeol(); universe@46: } else if (code == NETCODE_CONNLOST) { universe@46: addstr("\rClient connected - but gave no response."); universe@5: clrtoeol(); universe@5: } else { universe@34: addstr("\rInvalid client response"); universe@34: clrtoeol(); universe@34: universe@6: net_destroy(&server); universe@59: return 1; universe@5: } universe@5: universe@5: net_destroy(&server); universe@59: return 0; universe@5: }