src/main.c

changeset 5
f7dfef88947d
parent 4
560e07f7a6a1
child 7
41468077b5bb
equal deleted inserted replaced
4:560e07f7a6a1 5:f7dfef88947d
29 29
30 #include "terminal-chess.h" 30 #include "terminal-chess.h"
31 #include <string.h> 31 #include <string.h>
32 #include <time.h> 32 #include <time.h>
33 #include <ncurses.h> 33 #include <ncurses.h>
34 #include "input.h"
35 34
36 int get_settings(int argc, char **argv, Settings *settings) { 35 int get_settings(int argc, char **argv, Settings *settings) {
37 char *valid; 36 char *valid;
38 unsigned long int time, port; 37 unsigned long int time, port;
39 uint8_t timeunit = 60; 38 uint8_t timeunit = 60;
124 printw(" No time limit\n"); 123 printw(" No time limit\n");
125 } 124 }
126 refresh(); 125 refresh();
127 } 126 }
128 127
129 int cleanup(Settings *settings, int exitcode) {
130
131 if (settings->server) {
132 if (net_destroy(settings->server)) {
133 perror("Server shutdown failed");
134 }
135 }
136
137 return exitcode;
138 }
139
140 static WINDOW* window; 128 static WINDOW* window;
141 129
142 void leavescr() { 130 void leavescr() {
143 mvprintw(getmaxy(window)-1, 0, "Leaving terminal-chess. Press any key..."); 131 mvprintw(getmaxy(window)-1, 0, "Leaving terminal-chess. Press any key...");
144 getch(); 132 getch();
177 165
178 window = initscr(); 166 window = initscr();
179 cbreak(); 167 cbreak();
180 atexit(leavescr); 168 atexit(leavescr);
181 169
182 Server server; 170 return is_server(&settings) ? server_run(&settings) : client_run(&settings);
183 settings.server = &server;
184
185 if (is_server(&settings)) {
186 dump_gameinfo(&(settings.gameinfo));
187 printw("\nListening for client...\n");
188 refresh();
189 if (net_create(&server, settings.port)) {
190 perror("Server creation failed");
191 return cleanup(&settings, EXIT_FAILURE);
192 }
193
194 if (net_listen(&server)) {
195 perror("Listening for client failed");
196 return cleanup(&settings, EXIT_FAILURE);
197 }
198
199 /* net version handshake */
200 int fd = server.client->fd;
201 net_send_code(fd, NETCODE_VERSION);
202 if (net_recieve_code(fd) != NETCODE_VERSION) {
203 fprintf(stderr, "Client uses an incompatible software version.\n");
204 return cleanup(&settings, EXIT_FAILURE);
205 }
206
207 printw("Client connected - transmitting gameinfo...");
208 refresh();
209
210
211 net_send_code(fd, NETCODE_GAMEINFO);
212 net_send_data(fd, &(settings.gameinfo), sizeof(settings.gameinfo));
213 printw("\rClient connected - awaiting challenge acceptance...");
214 refresh();
215 int code = net_recieve_code(fd);
216 if (code == NETCODE_ACCEPT) {
217 printw("\rClient connected - challenge accepted.");
218 clrtoeol();
219 } else if (code == NETCODE_DECLINE) {
220 printw("\rClient connected - challenge declined.");
221 clrtoeol();
222 } else {
223 fprintf(stderr, "Invalid client response\n");
224 return cleanup(&settings, EXIT_FAILURE);
225 }
226 } else {
227 if (net_find(&server, settings.serverhost, settings.port)) {
228 fprintf(stderr, "Can't find server\n");
229 return cleanup(&settings, EXIT_FAILURE);
230 }
231
232 if (net_connect(&server)) {
233 perror("Can't connect to server");
234 return cleanup(&settings, EXIT_FAILURE);
235 }
236
237 int fd = server.fd;
238 if (net_recieve_code(fd) != NETCODE_VERSION) {
239 fprintf(stderr, "Server uses an incompatible software version.\n");
240 return cleanup(&settings, EXIT_FAILURE);
241 } else {
242 net_send_code(fd, NETCODE_VERSION);
243 }
244
245 printw("Connection established!\n\n");
246 refresh();
247
248 if (net_recieve_code(fd) == NETCODE_GAMEINFO) {
249 net_recieve_data(fd, &(settings.gameinfo),
250 sizeof(settings.gameinfo));
251 dump_gameinfo(&(settings.gameinfo));
252 printw("Accept challenge (y/n)? ");
253 if (prompt_yesno()) {
254 net_send_code(fd, NETCODE_ACCEPT);
255 // TODO: start game
256 } else {
257 net_send_code(fd, NETCODE_DECLINE);
258 }
259 } else {
260 fprintf(stderr, "Server sent invalid gameinfo.\n");
261 }
262 }
263
264 return cleanup(&settings, EXIT_SUCCESS);
265 } 171 }
266 172

mercurial