Mon, 17 Mar 2014 15:39:36 +0100
separated server and client module
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@5 | 31 | #include <ncurses.h> |
universe@5 | 32 | |
universe@5 | 33 | int server_run(Settings *settings) { |
universe@5 | 34 | Server server; |
universe@5 | 35 | int exit_code = EXIT_SUCCESS; |
universe@5 | 36 | |
universe@5 | 37 | dump_gameinfo(&(settings->gameinfo)); |
universe@5 | 38 | printw("\nListening for client...\n"); |
universe@5 | 39 | refresh(); |
universe@5 | 40 | if (net_create(&server, settings->port)) { |
universe@5 | 41 | perror("Server creation failed"); |
universe@5 | 42 | exit_code = EXIT_FAILURE; |
universe@5 | 43 | goto quit; |
universe@5 | 44 | } |
universe@5 | 45 | |
universe@5 | 46 | if (net_listen(&server)) { |
universe@5 | 47 | perror("Listening for client failed"); |
universe@5 | 48 | exit_code = EXIT_FAILURE; |
universe@5 | 49 | goto quit; |
universe@5 | 50 | } |
universe@5 | 51 | |
universe@5 | 52 | /* net version handshake */ |
universe@5 | 53 | int fd = server.client->fd; |
universe@5 | 54 | net_send_code(fd, NETCODE_VERSION); |
universe@5 | 55 | if (net_recieve_code(fd) != NETCODE_VERSION) { |
universe@5 | 56 | fprintf(stderr, "Client uses an incompatible software version.\n"); |
universe@5 | 57 | exit_code = EXIT_FAILURE; |
universe@5 | 58 | goto quit; |
universe@5 | 59 | } |
universe@5 | 60 | |
universe@5 | 61 | printw("Client connected - transmitting gameinfo..."); |
universe@5 | 62 | refresh(); |
universe@5 | 63 | |
universe@5 | 64 | |
universe@5 | 65 | net_send_code(fd, NETCODE_GAMEINFO); |
universe@5 | 66 | net_send_data(fd, &(settings->gameinfo), sizeof(settings->gameinfo)); |
universe@5 | 67 | printw("\rClient connected - awaiting challenge acceptance..."); |
universe@5 | 68 | refresh(); |
universe@5 | 69 | int code = net_recieve_code(fd); |
universe@5 | 70 | if (code == NETCODE_ACCEPT) { |
universe@5 | 71 | printw("\rClient connected - challenge accepted."); |
universe@5 | 72 | clrtoeol(); |
universe@5 | 73 | } else if (code == NETCODE_DECLINE) { |
universe@5 | 74 | printw("\rClient connected - challenge declined."); |
universe@5 | 75 | clrtoeol(); |
universe@5 | 76 | } else { |
universe@5 | 77 | fprintf(stderr, "Invalid client response\n"); |
universe@5 | 78 | exit_code = EXIT_FAILURE; |
universe@5 | 79 | goto quit; |
universe@5 | 80 | } |
universe@5 | 81 | |
universe@5 | 82 | quit: |
universe@5 | 83 | |
universe@5 | 84 | net_destroy(&server); |
universe@5 | 85 | return exit_code; |
universe@5 | 86 | } |