src/server.c

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

mercurial