src/server.c

Wed, 26 Aug 2015 12:59:30 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 26 Aug 2015 12:59:30 +0200
changeset 53
78b580bfde33
parent 51
84f2e380a434
child 55
54ea19938d57
permissions
-rw-r--r--

fixed new network games starting with BLACK

5
f7dfef88947d separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff changeset
1 /*
f7dfef88947d separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
f7dfef88947d separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff changeset
3 *
f7dfef88947d separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff changeset
4 * Copyright 2014 Mike Becker. All rights reserved.
f7dfef88947d separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff changeset
5 *
f7dfef88947d separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
f7dfef88947d separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
f7dfef88947d separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff changeset
8 *
f7dfef88947d separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
f7dfef88947d separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
f7dfef88947d separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff changeset
11 *
f7dfef88947d separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
f7dfef88947d separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
f7dfef88947d separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
f7dfef88947d separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff changeset
15 *
f7dfef88947d separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
f7dfef88947d separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
f7dfef88947d separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
f7dfef88947d separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
f7dfef88947d separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
f7dfef88947d separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
f7dfef88947d separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
f7dfef88947d separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
f7dfef88947d separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
f7dfef88947d separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
f7dfef88947d separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
f7dfef88947d separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27 *
f7dfef88947d separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28 */
f7dfef88947d separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff changeset
29
f7dfef88947d separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff changeset
30 #include "terminal-chess.h"
6
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
31 #include "game.h"
5
f7dfef88947d separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff changeset
32 #include <ncurses.h>
51
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
33 #include <errno.h>
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
34 #include <string.h>
5
f7dfef88947d separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff changeset
35
6
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
36 static int server_open(Server *server, char *port) {
5
f7dfef88947d separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff changeset
37 printw("\nListening for client...\n");
f7dfef88947d separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff changeset
38 refresh();
6
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
39 if (net_create(server, port)) {
34
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 30
diff changeset
40 addstr("Server creation failed");
6
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
41 return 1;
5
f7dfef88947d separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff changeset
42 }
f7dfef88947d separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff changeset
43
6
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
44 if (net_listen(server)) {
34
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 30
diff changeset
45 addstr("Listening for client failed");
6
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
46 return 1;
5
f7dfef88947d separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff changeset
47 }
6
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
48
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
49 return 0;
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
50 }
5
f7dfef88947d separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff changeset
51
6
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
52 static int server_handshake(Client *client) {
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
53 net_send_code(client->fd, NETCODE_VERSION);
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
54 if (net_recieve_code(client->fd) != NETCODE_VERSION) {
34
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 30
diff changeset
55 addstr("Client uses an incompatible software version.");
6
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
56 return 1;
5
f7dfef88947d separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff changeset
57 }
f7dfef88947d separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff changeset
58
46
4dcfb4c58b6d netcode is now aware of connection losses
Mike Becker <universe@uap-core.de>
parents: 34
diff changeset
59 addstr("Client connected - transmitting gameinfo...");
5
f7dfef88947d separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff changeset
60 refresh();
6
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
61
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
62 return 0;
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
63 }
5
f7dfef88947d separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff changeset
64
6
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
65 int server_run(Settings *settings) {
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
66 Server server;
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
67
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
68 dump_gameinfo(&(settings->gameinfo));
51
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
69 GameState continuegame;
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
70 gamestate_init(&continuegame);
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
71 if (settings->continuepgn) {
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
72 // preload PGN data before handshake
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
73 FILE *pgnfile = fopen(settings->continuepgn, "r");
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
74 if (pgnfile) {
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
75 int result = read_pgn(pgnfile, &continuegame,
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
76 &(settings->gameinfo));
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
77 fclose(pgnfile);
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
78 if (result != EXIT_SUCCESS) {
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
79 addstr("Invalid PGN file content.\n");
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
80 return EXIT_FAILURE;
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
81 }
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
82 if (!is_game_running(&continuegame)) {
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
83 addstr("Game has ended. Use -S to analyze it.\n");
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
84 return EXIT_FAILURE;
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
85 }
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
86 addch('\n');
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
87 dump_moveinfo(&continuegame);
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
88 addch('\n');
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
89 } else {
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
90 printw("Can't read PGN file (%s)\n", strerror(errno));
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
91 return EXIT_FAILURE;
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
92 }
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
93 }
6
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
94
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
95 if (server_open(&server, settings->port)) {
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
96 net_destroy(&server);
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
97 return EXIT_FAILURE;
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
98 }
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
99
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
100 if (server_handshake(server.client)) {
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
101 net_destroy(&server);
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
102 return EXIT_FAILURE;
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
103 }
5
f7dfef88947d separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff changeset
104
6
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
105 int fd = server.client->fd;
51
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
106 if (settings->continuepgn) {
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
107 // Continue game, send PGN data
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
108 uint16_t mc = 0;
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
109 MoveList *movelist = continuegame.movelist;
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
110 while (movelist) {
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
111 mc++;
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
112 movelist = movelist->next;
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
113 }
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
114
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
115 Move* moves = calloc(mc, sizeof(Move));
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
116
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
117 movelist = continuegame.movelist;
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
118 mc = 0;
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
119 while (movelist) {
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
120 moves[mc] = movelist->move;
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
121 mc++;
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
122 movelist = movelist->next;
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
123 }
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
124
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
125 size_t pgndata_size = sizeof(GameInfo)+sizeof(mc)+mc*sizeof(Move);
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
126 char *pgndata = malloc(pgndata_size);
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
127 memcpy(pgndata, &(settings->gameinfo), sizeof(GameInfo));
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
128 memcpy(pgndata+sizeof(GameInfo), &mc, sizeof(mc));
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
129 memcpy(pgndata+sizeof(GameInfo)+sizeof(mc), moves, mc*sizeof(Move));
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
130 free(moves);
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
131 net_send_data(fd, NETCODE_PGNDATA, pgndata, pgndata_size);
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
132 free(pgndata);
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
133 } else {
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
134 // Start new game
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
135 net_send_data(fd, NETCODE_GAMEINFO,
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
136 &(settings->gameinfo), sizeof(GameInfo));
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
137 }
46
4dcfb4c58b6d netcode is now aware of connection losses
Mike Becker <universe@uap-core.de>
parents: 34
diff changeset
138 addstr("\rClient connected - awaiting challenge acceptance...");
5
f7dfef88947d separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff changeset
139 refresh();
f7dfef88947d separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff changeset
140 int code = net_recieve_code(fd);
f7dfef88947d separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff changeset
141 if (code == NETCODE_ACCEPT) {
46
4dcfb4c58b6d netcode is now aware of connection losses
Mike Becker <universe@uap-core.de>
parents: 34
diff changeset
142 addstr("\rClient connected - challenge accepted.");
5
f7dfef88947d separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff changeset
143 clrtoeol();
51
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
144 if (settings->continuepgn) {
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
145 game_continue(settings, fd, &continuegame);
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
146 } else {
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
147 game_start(settings, fd);
84f2e380a434 added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
148 }
5
f7dfef88947d separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff changeset
149 } else if (code == NETCODE_DECLINE) {
46
4dcfb4c58b6d netcode is now aware of connection losses
Mike Becker <universe@uap-core.de>
parents: 34
diff changeset
150 addstr("\rClient connected - challenge declined.");
4dcfb4c58b6d netcode is now aware of connection losses
Mike Becker <universe@uap-core.de>
parents: 34
diff changeset
151 clrtoeol();
4dcfb4c58b6d netcode is now aware of connection losses
Mike Becker <universe@uap-core.de>
parents: 34
diff changeset
152 } else if (code == NETCODE_CONNLOST) {
4dcfb4c58b6d netcode is now aware of connection losses
Mike Becker <universe@uap-core.de>
parents: 34
diff changeset
153 addstr("\rClient connected - but gave no response.");
5
f7dfef88947d separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff changeset
154 clrtoeol();
f7dfef88947d separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff changeset
155 } else {
34
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 30
diff changeset
156 addstr("\rInvalid client response");
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 30
diff changeset
157 clrtoeol();
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 30
diff changeset
158
6
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
159 net_destroy(&server);
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
160 return EXIT_FAILURE;
5
f7dfef88947d separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff changeset
161 }
f7dfef88947d separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff changeset
162
f7dfef88947d separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff changeset
163 net_destroy(&server);
6
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
164 return EXIT_SUCCESS;
5
f7dfef88947d separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff changeset
165 }

mercurial