src/client.c

Wed, 19 Mar 2014 10:08:25 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 19 Mar 2014 10:08:25 +0100
changeset 6
daaf6e5b3501
parent 5
f7dfef88947d
child 7
41468077b5bb
permissions
-rw-r--r--

structured code

     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2014 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 "input.h"
    32 #include "game.h"
    33 #include <ncurses.h>
    35 static int client_connect(Server *server, char *host, char *port) {
    36     if (net_find(server, host, port)) {
    37         fprintf(stderr, "Can't find server\n");
    38         return 1;
    39     }
    41     if (net_connect(server)) {
    42         perror("Can't connect to server");
    43         return 1;
    44     }
    46     return 0;
    47 }
    49 static int client_handshake(Server *server) {
    50     if (net_recieve_code(server->fd) != NETCODE_VERSION) {
    51         fprintf(stderr, "Server uses an incompatible software version.\n");
    52         return 1;
    53     } else {
    54         net_send_code(server->fd, NETCODE_VERSION);
    55     }
    57     printw("Connection established!\n\n");
    58     refresh();
    60     return 0;
    61 }
    63 int client_run(Settings *settings) {
    64     Server server;
    66     if (client_connect(&server, settings->serverhost, settings->port)) {
    67         net_destroy(&server);
    68         return EXIT_FAILURE;
    69     }
    71     if (client_handshake(&server)) {
    72         net_destroy(&server);
    73         return EXIT_FAILURE;
    74     }
    76     if (net_recieve_code(server.fd) == NETCODE_GAMEINFO) {
    77         net_recieve_data(server.fd, &(settings->gameinfo),
    78             sizeof(settings->gameinfo));
    79         dump_gameinfo(&(settings->gameinfo));
    80         printw("Accept challenge (y/n)? ");
    81         if (prompt_yesno()) {
    82             net_send_code(server.fd, NETCODE_ACCEPT);
    83             game_start(settings, server.fd);
    84         } else {
    85             net_send_code(server.fd, NETCODE_DECLINE);
    86         }
    87     } else {
    88         fprintf(stderr, "Server sent invalid gameinfo.\n");
    89         net_destroy(&server);
    90         return EXIT_FAILURE;
    91     }
    93     net_destroy(&server);
    94     return EXIT_SUCCESS;
    95 }

mercurial