src/server.c

Wed, 09 Apr 2014 12:07:47 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 09 Apr 2014 12:07:47 +0200
changeset 34
c4d4b8a8f902
parent 30
a285ee393860
child 46
4dcfb4c58b6d
permissions
-rw-r--r--

added nonblocking read for network games + minor build system fixes

universe@5 1 /*
universe@5 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@5 3 *
universe@5 4 * Copyright 2014 Mike Becker. All rights reserved.
universe@5 5 *
universe@5 6 * Redistribution and use in source and binary forms, with or without
universe@5 7 * modification, are permitted provided that the following conditions are met:
universe@5 8 *
universe@5 9 * 1. Redistributions of source code must retain the above copyright
universe@5 10 * notice, this list of conditions and the following disclaimer.
universe@5 11 *
universe@5 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@5 13 * notice, this list of conditions and the following disclaimer in the
universe@5 14 * documentation and/or other materials provided with the distribution.
universe@5 15 *
universe@5 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@5 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@5 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@5 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@5 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@5 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@5 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@5 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@5 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@5 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@5 26 * POSSIBILITY OF SUCH DAMAGE.
universe@5 27 *
universe@5 28 */
universe@5 29
universe@5 30 #include "terminal-chess.h"
universe@6 31 #include "game.h"
universe@5 32 #include <ncurses.h>
universe@5 33
universe@6 34 static int server_open(Server *server, char *port) {
universe@5 35 printw("\nListening for client...\n");
universe@5 36 refresh();
universe@6 37 if (net_create(server, port)) {
universe@34 38 addstr("Server creation failed");
universe@6 39 return 1;
universe@5 40 }
universe@5 41
universe@6 42 if (net_listen(server)) {
universe@34 43 addstr("Listening for client failed");
universe@6 44 return 1;
universe@5 45 }
universe@6 46
universe@6 47 return 0;
universe@6 48 }
universe@5 49
universe@6 50 static int server_handshake(Client *client) {
universe@6 51 net_send_code(client->fd, NETCODE_VERSION);
universe@6 52 if (net_recieve_code(client->fd) != NETCODE_VERSION) {
universe@34 53 addstr("Client uses an incompatible software version.");
universe@6 54 return 1;
universe@5 55 }
universe@5 56
universe@5 57 printw("Client connected - transmitting gameinfo...");
universe@5 58 refresh();
universe@6 59
universe@6 60 return 0;
universe@6 61 }
universe@5 62
universe@6 63 int server_run(Settings *settings) {
universe@6 64 Server server;
universe@6 65
universe@6 66 dump_gameinfo(&(settings->gameinfo));
universe@6 67
universe@6 68 if (server_open(&server, settings->port)) {
universe@6 69 net_destroy(&server);
universe@6 70 return EXIT_FAILURE;
universe@6 71 }
universe@6 72
universe@6 73 if (server_handshake(server.client)) {
universe@6 74 net_destroy(&server);
universe@6 75 return EXIT_FAILURE;
universe@6 76 }
universe@5 77
universe@6 78 int fd = server.client->fd;
universe@22 79 net_send_data(fd, NETCODE_GAMEINFO,
universe@30 80 &(settings->gameinfo), sizeof(GameInfo));
universe@5 81 printw("\rClient connected - awaiting challenge acceptance...");
universe@5 82 refresh();
universe@5 83 int code = net_recieve_code(fd);
universe@5 84 if (code == NETCODE_ACCEPT) {
universe@5 85 printw("\rClient connected - challenge accepted.");
universe@5 86 clrtoeol();
universe@6 87
universe@6 88 game_start(settings, fd);
universe@5 89 } else if (code == NETCODE_DECLINE) {
universe@5 90 printw("\rClient connected - challenge declined.");
universe@5 91 clrtoeol();
universe@5 92 } else {
universe@34 93 addstr("\rInvalid client response");
universe@34 94 clrtoeol();
universe@34 95
universe@6 96 net_destroy(&server);
universe@6 97 return EXIT_FAILURE;
universe@5 98 }
universe@5 99
universe@5 100 net_destroy(&server);
universe@6 101 return EXIT_SUCCESS;
universe@5 102 }

mercurial