src/server.c

Wed, 29 Aug 2018 13:55:18 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 29 Aug 2018 13:55:18 +0200
changeset 64
4eda5df55f86
parent 60
0c50aac49e55
permissions
-rw-r--r--

fixes castling not printed correctly to PGN

     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2016 Mike Becker. All rights reserved.
     5  *
     6  * Redistribution and use in source and binary forms, with or without
     7  * modification, are permitted provided that the following conditions are met:
     8  *
     9  *   1. Redistributions of source code must retain the above copyright
    10  *      notice, this list of conditions and the following disclaimer.
    11  *
    12  *   2. Redistributions in binary form must reproduce the above copyright
    13  *      notice, this list of conditions and the following disclaimer in the
    14  *      documentation and/or other materials provided with the distribution.
    15  *
    16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    26  * POSSIBILITY OF SUCH DAMAGE.
    27  *
    28  */
    30 #include "terminal-chess.h"
    31 #include "game.h"
    32 #include <ncurses.h>
    33 #include <errno.h>
    34 #include <string.h>
    36 static int server_open(Server *server, char *port) {
    37     printw("\nListening for client...\n");
    38     refresh();
    39     if (net_create(server, port)) {
    40         addstr("Server creation failed");
    41         return 1;
    42     }
    44     if (net_listen(server)) {
    45         addstr("Listening for client failed");
    46         return 1;
    47     }
    49     return 0;
    50 }
    52 static int server_handshake(Client *client) {
    53     net_send_code(client->fd, NETCODE_VERSION);
    54     if (net_recieve_code(client->fd) != NETCODE_VERSION) {
    55         addstr("Client uses an incompatible software version.");
    56         return 1;
    57     }
    59     addstr("Client connected - transmitting gameinfo...");
    60     refresh();
    62     return 0;
    63 }
    65 int server_run(Settings *settings) {
    66     Server server;
    68     dump_gameinfo(&(settings->gameinfo));
    69     GameState continuegame;
    70     gamestate_init(&continuegame);
    71     if (settings->continuepgn) {
    72         /* preload PGN data before handshake */
    73         FILE *pgnfile = fopen(settings->continuepgn, "r");
    74         if (pgnfile) {
    75             int result = read_pgn(pgnfile, &continuegame,
    76                 &(settings->gameinfo));
    77             long position = ftell(pgnfile);
    78             fclose(pgnfile);
    79             if (result) {
    80                 printw("Invalid PGN file content at position %ld:\n%s\n",
    81                         position, pgn_error_str(result));
    82                 return 1;
    83             }
    84             if (!is_game_running(&continuegame)) {
    85                 addstr("Game has ended. Use -S to analyze it.\n");
    86                 return 1;
    87             }
    88             addch('\n');
    89             dump_moveinfo(&continuegame);
    90             addch('\n');
    91         } else {
    92             printw("Can't read PGN file (%s)\n", strerror(errno));
    93             return 1;
    94         }
    95     }
    97     if (server_open(&server, settings->port)) {
    98         net_destroy(&server);
    99         return 1;
   100     }
   102     if (server_handshake(server.client)) {
   103         net_destroy(&server);
   104         return 1;
   105     }
   107     int fd = server.client->fd;
   108     if (settings->continuepgn) {
   109         /* Continue game, send PGN data */
   110         uint16_t mc = 0;
   111         MoveList *movelist = continuegame.movelist;
   112         while (movelist) {
   113             mc++;
   114             movelist = movelist->next;
   115         }
   117         Move* moves = calloc(mc, sizeof(Move));
   119         movelist = continuegame.movelist;
   120         mc = 0;
   121         while (movelist) {
   122             moves[mc] = movelist->move;
   123             mc++;
   124             movelist = movelist->next;
   125         }
   127         size_t pgndata_size = sizeof(GameInfo)+sizeof(mc)+mc*sizeof(Move);
   128         char *pgndata = malloc(pgndata_size);
   129         memcpy(pgndata, &(settings->gameinfo), sizeof(GameInfo));
   130         memcpy(pgndata+sizeof(GameInfo), &mc, sizeof(mc));
   131         memcpy(pgndata+sizeof(GameInfo)+sizeof(mc), moves, mc*sizeof(Move));
   132         free(moves);
   133         net_send_data(fd, NETCODE_PGNDATA, pgndata, pgndata_size);
   134         free(pgndata);
   135     } else {
   136         /* Start new game */
   137         net_send_data(fd, NETCODE_GAMEINFO,
   138             &(settings->gameinfo), sizeof(GameInfo));
   139     }
   140     addstr("\rClient connected - awaiting challenge acceptance...");
   141     refresh();
   142     int code = net_recieve_code(fd);
   143     if (code == NETCODE_ACCEPT) {
   144         addstr("\rClient connected - challenge accepted.");
   145         clrtoeol();
   146         if (settings->continuepgn) {
   147             game_continue(settings, fd, &continuegame);
   148         } else {
   149             game_start(settings, fd);
   150         }
   151     } else if (code == NETCODE_DECLINE) {
   152         addstr("\rClient connected - challenge declined.");
   153         clrtoeol();
   154     } else if (code == NETCODE_CONNLOST) {
   155         addstr("\rClient connected - but gave no response.");
   156         clrtoeol();
   157     } else {
   158         addstr("\rInvalid client response");
   159         clrtoeol();
   161         net_destroy(&server);
   162         return 1;
   163     }
   165     net_destroy(&server);
   166     return 0;
   167 }

mercurial