26 * POSSIBILITY OF SUCH DAMAGE. |
26 * POSSIBILITY OF SUCH DAMAGE. |
27 * |
27 * |
28 */ |
28 */ |
29 |
29 |
30 #include "terminal-chess.h" |
30 #include "terminal-chess.h" |
|
31 #include "game.h" |
31 #include <ncurses.h> |
32 #include <ncurses.h> |
32 |
33 |
33 int server_run(Settings *settings) { |
34 static int server_open(Server *server, char *port) { |
34 Server server; |
|
35 int exit_code = EXIT_SUCCESS; |
|
36 |
|
37 dump_gameinfo(&(settings->gameinfo)); |
|
38 printw("\nListening for client...\n"); |
35 printw("\nListening for client...\n"); |
39 refresh(); |
36 refresh(); |
40 if (net_create(&server, settings->port)) { |
37 if (net_create(server, port)) { |
41 perror("Server creation failed"); |
38 perror("Server creation failed"); |
42 exit_code = EXIT_FAILURE; |
39 return 1; |
43 goto quit; |
|
44 } |
40 } |
45 |
41 |
46 if (net_listen(&server)) { |
42 if (net_listen(server)) { |
47 perror("Listening for client failed"); |
43 perror("Listening for client failed"); |
48 exit_code = EXIT_FAILURE; |
44 return 1; |
49 goto quit; |
|
50 } |
45 } |
|
46 |
|
47 return 0; |
|
48 } |
51 |
49 |
52 /* net version handshake */ |
50 static int server_handshake(Client *client) { |
53 int fd = server.client->fd; |
51 net_send_code(client->fd, NETCODE_VERSION); |
54 net_send_code(fd, NETCODE_VERSION); |
52 if (net_recieve_code(client->fd) != NETCODE_VERSION) { |
55 if (net_recieve_code(fd) != NETCODE_VERSION) { |
|
56 fprintf(stderr, "Client uses an incompatible software version.\n"); |
53 fprintf(stderr, "Client uses an incompatible software version.\n"); |
57 exit_code = EXIT_FAILURE; |
54 return 1; |
58 goto quit; |
|
59 } |
55 } |
60 |
56 |
61 printw("Client connected - transmitting gameinfo..."); |
57 printw("Client connected - transmitting gameinfo..."); |
62 refresh(); |
58 refresh(); |
|
59 |
|
60 return 0; |
|
61 } |
63 |
62 |
|
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 } |
64 |
77 |
|
78 int fd = server.client->fd; |
65 net_send_code(fd, NETCODE_GAMEINFO); |
79 net_send_code(fd, NETCODE_GAMEINFO); |
66 net_send_data(fd, &(settings->gameinfo), sizeof(settings->gameinfo)); |
80 net_send_data(fd, &(settings->gameinfo), sizeof(settings->gameinfo)); |
67 printw("\rClient connected - awaiting challenge acceptance..."); |
81 printw("\rClient connected - awaiting challenge acceptance..."); |
68 refresh(); |
82 refresh(); |
69 int code = net_recieve_code(fd); |
83 int code = net_recieve_code(fd); |
70 if (code == NETCODE_ACCEPT) { |
84 if (code == NETCODE_ACCEPT) { |
71 printw("\rClient connected - challenge accepted."); |
85 printw("\rClient connected - challenge accepted."); |
72 clrtoeol(); |
86 clrtoeol(); |
|
87 |
|
88 game_start(settings, fd); |
73 } else if (code == NETCODE_DECLINE) { |
89 } else if (code == NETCODE_DECLINE) { |
74 printw("\rClient connected - challenge declined."); |
90 printw("\rClient connected - challenge declined."); |
75 clrtoeol(); |
91 clrtoeol(); |
76 } else { |
92 } else { |
77 fprintf(stderr, "Invalid client response\n"); |
93 fprintf(stderr, "Invalid client response\n"); |
78 exit_code = EXIT_FAILURE; |
94 net_destroy(&server); |
79 goto quit; |
95 return EXIT_FAILURE; |
80 } |
96 } |
81 |
97 |
82 quit: |
|
83 |
|
84 net_destroy(&server); |
98 net_destroy(&server); |
85 return exit_code; |
99 return EXIT_SUCCESS; |
86 } |
100 } |