Wed, 11 Jun 2014 15:38:01 +0200
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
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 | 31 | #include "game.h" |
5
f7dfef88947d
separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
32 | #include <ncurses.h> |
f7dfef88947d
separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
33 | |
6 | 34 | static int server_open(Server *server, char *port) { |
5
f7dfef88947d
separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
35 | printw("\nListening for client...\n"); |
f7dfef88947d
separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
36 | refresh(); |
6 | 37 | 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
|
38 | addstr("Server creation failed"); |
6 | 39 | return 1; |
5
f7dfef88947d
separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
40 | } |
f7dfef88947d
separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
41 | |
6 | 42 | 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
|
43 | addstr("Listening for client failed"); |
6 | 44 | return 1; |
5
f7dfef88947d
separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
45 | } |
6 | 46 | |
47 | return 0; | |
48 | } | |
5
f7dfef88947d
separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
49 | |
6 | 50 | static int server_handshake(Client *client) { |
51 | net_send_code(client->fd, NETCODE_VERSION); | |
52 | 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
|
53 | addstr("Client uses an incompatible software version."); |
6 | 54 | return 1; |
5
f7dfef88947d
separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
55 | } |
f7dfef88947d
separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
56 | |
46
4dcfb4c58b6d
netcode is now aware of connection losses
Mike Becker <universe@uap-core.de>
parents:
34
diff
changeset
|
57 | addstr("Client connected - transmitting gameinfo..."); |
5
f7dfef88947d
separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
58 | refresh(); |
6 | 59 | |
60 | return 0; | |
61 | } | |
5
f7dfef88947d
separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
62 | |
6 | 63 | int server_run(Settings *settings) { |
64 | Server server; | |
65 | ||
66 | dump_gameinfo(&(settings->gameinfo)); | |
67 | ||
68 | if (server_open(&server, settings->port)) { | |
69 | net_destroy(&server); | |
70 | return EXIT_FAILURE; | |
71 | } | |
72 | ||
73 | if (server_handshake(server.client)) { | |
74 | net_destroy(&server); | |
75 | return EXIT_FAILURE; | |
76 | } | |
5
f7dfef88947d
separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
77 | |
6 | 78 | int fd = server.client->fd; |
22
41bbfd4d17a3
improved network performance
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
79 | net_send_data(fd, NETCODE_GAMEINFO, |
30
a285ee393860
experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents:
22
diff
changeset
|
80 | &(settings->gameinfo), sizeof(GameInfo)); |
46
4dcfb4c58b6d
netcode is now aware of connection losses
Mike Becker <universe@uap-core.de>
parents:
34
diff
changeset
|
81 | addstr("\rClient connected - awaiting challenge acceptance..."); |
5
f7dfef88947d
separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
82 | refresh(); |
f7dfef88947d
separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
83 | int code = net_recieve_code(fd); |
f7dfef88947d
separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
84 | if (code == NETCODE_ACCEPT) { |
46
4dcfb4c58b6d
netcode is now aware of connection losses
Mike Becker <universe@uap-core.de>
parents:
34
diff
changeset
|
85 | addstr("\rClient connected - challenge accepted."); |
5
f7dfef88947d
separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
86 | clrtoeol(); |
6 | 87 | |
88 | game_start(settings, fd); | |
5
f7dfef88947d
separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
89 | } else if (code == NETCODE_DECLINE) { |
46
4dcfb4c58b6d
netcode is now aware of connection losses
Mike Becker <universe@uap-core.de>
parents:
34
diff
changeset
|
90 | addstr("\rClient connected - challenge declined."); |
4dcfb4c58b6d
netcode is now aware of connection losses
Mike Becker <universe@uap-core.de>
parents:
34
diff
changeset
|
91 | clrtoeol(); |
4dcfb4c58b6d
netcode is now aware of connection losses
Mike Becker <universe@uap-core.de>
parents:
34
diff
changeset
|
92 | } else if (code == NETCODE_CONNLOST) { |
4dcfb4c58b6d
netcode is now aware of connection losses
Mike Becker <universe@uap-core.de>
parents:
34
diff
changeset
|
93 | addstr("\rClient connected - but gave no response."); |
5
f7dfef88947d
separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
94 | clrtoeol(); |
f7dfef88947d
separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
95 | } else { |
34
c4d4b8a8f902
added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents:
30
diff
changeset
|
96 | 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
|
97 | clrtoeol(); |
c4d4b8a8f902
added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents:
30
diff
changeset
|
98 | |
6 | 99 | net_destroy(&server); |
100 | return EXIT_FAILURE; | |
5
f7dfef88947d
separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
101 | } |
f7dfef88947d
separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
102 | |
f7dfef88947d
separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
103 | net_destroy(&server); |
6 | 104 | return EXIT_SUCCESS; |
5
f7dfef88947d
separated server and client module
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
105 | } |