src/server.c

Tue, 28 Aug 2018 14:16:30 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 28 Aug 2018 14:16:30 +0200
changeset 59
3fa1de896666
parent 55
54ea19938d57
child 60
0c50aac49e55
permissions
-rw-r--r--

fixes inappropriate use of EXIT_ macros + adds a sample PGN file

     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             fclose(pgnfile);
    78             if (result) {
    79                 addstr("Invalid PGN file content.\n");
    80                 return 1;
    81             }
    82             if (!is_game_running(&continuegame)) {
    83                 addstr("Game has ended. Use -S to analyze it.\n");
    84                 return 1;
    85             }
    86             addch('\n');
    87             dump_moveinfo(&continuegame);
    88             addch('\n');
    89         } else {
    90             printw("Can't read PGN file (%s)\n", strerror(errno));
    91             return 1;
    92         }
    93     }
    95     if (server_open(&server, settings->port)) {
    96         net_destroy(&server);
    97         return 1;
    98     }
   100     if (server_handshake(server.client)) {
   101         net_destroy(&server);
   102         return 1;
   103     }
   105     int fd = server.client->fd;
   106     if (settings->continuepgn) {
   107         // Continue game, send PGN data
   108         uint16_t mc = 0;
   109         MoveList *movelist = continuegame.movelist;
   110         while (movelist) {
   111             mc++;
   112             movelist = movelist->next;
   113         }
   115         Move* moves = calloc(mc, sizeof(Move));
   117         movelist = continuegame.movelist;
   118         mc = 0;
   119         while (movelist) {
   120             moves[mc] = movelist->move;
   121             mc++;
   122             movelist = movelist->next;
   123         }
   125         size_t pgndata_size = sizeof(GameInfo)+sizeof(mc)+mc*sizeof(Move);
   126         char *pgndata = malloc(pgndata_size);
   127         memcpy(pgndata, &(settings->gameinfo), sizeof(GameInfo));
   128         memcpy(pgndata+sizeof(GameInfo), &mc, sizeof(mc));
   129         memcpy(pgndata+sizeof(GameInfo)+sizeof(mc), moves, mc*sizeof(Move));
   130         free(moves);
   131         net_send_data(fd, NETCODE_PGNDATA, pgndata, pgndata_size);
   132         free(pgndata);
   133     } else {
   134         // Start new game
   135         net_send_data(fd, NETCODE_GAMEINFO,
   136             &(settings->gameinfo), sizeof(GameInfo));
   137     }
   138     addstr("\rClient connected - awaiting challenge acceptance...");
   139     refresh();
   140     int code = net_recieve_code(fd);
   141     if (code == NETCODE_ACCEPT) {
   142         addstr("\rClient connected - challenge accepted.");
   143         clrtoeol();
   144         if (settings->continuepgn) {
   145             game_continue(settings, fd, &continuegame);
   146         } else {
   147             game_start(settings, fd);
   148         }
   149     } else if (code == NETCODE_DECLINE) {
   150         addstr("\rClient connected - challenge declined.");
   151         clrtoeol();
   152     } else if (code == NETCODE_CONNLOST) {
   153         addstr("\rClient connected - but gave no response.");
   154         clrtoeol();
   155     } else {
   156         addstr("\rInvalid client response");
   157         clrtoeol();
   159         net_destroy(&server);
   160         return 1;
   161     }
   163     net_destroy(&server);
   164     return 0;
   165 }

mercurial