]> git.pond.sub.org Git - empserver/blob - src/client/play.c
8fa48fd49622a8930467654a4af189e2b3c138aa
[empserver] / src / client / play.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2009, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  play.c: Playing the game
29  *
30  *  Known contributors to this file:
31  *     Markus Armbruster, 2007
32  *     Ron Koenderink, 2007-2009
33  */
34
35 #include <config.h>
36
37 #include <assert.h>
38 #include <errno.h>
39 #include <signal.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #ifndef _WIN32
43 #include <sys/select.h>
44 #include <unistd.h>
45 #else
46 #include <process.h>
47 #include <io.h>
48 #endif
49 #include "linebuf.h"
50 #include "misc.h"
51 #include "proto.h"
52 #include "ringbuf.h"
53 #include "secure.h"
54
55 #ifdef _WIN32
56 static CRITICAL_SECTION signal_critical_section;
57 static LPCRITICAL_SECTION signal_critical_section_ptr = NULL;
58
59 static unsigned char bounce_buf[RING_SIZE];
60 /*
61  * Set bounce_empty to indicate bounce_buf is available for the stdin thread
62  * to use.
63  */
64 static HANDLE bounce_empty;
65 /*
66  * Set bounce_full to indicate bounce_buf is contains data from the
67  * stdin thread and is available for recv_input
68  */
69 static HANDLE bounce_full;
70 /* Ctrl-C (SIGINT) was detected, generate EINTR for the w32_select() */
71 static HANDLE ctrl_c_event;
72 static int bounce_status, bounce_error;
73
74 #define SIGPIPE -1
75 static void (*ctrl_handler)(int sig) = { SIG_DFL };
76
77 /*
78  * Ctrl-C handler for emulating the SIGINT in WIN32
79  */
80 static BOOL WINAPI
81 w32_signal_handler(DWORD ctrl_type)
82 {
83     if (ctrl_type == CTRL_C_EVENT) {
84         EnterCriticalSection(signal_critical_section_ptr);
85         if (ctrl_handler != SIG_DFL) {
86             ctrl_handler(SIGINT);
87             LeaveCriticalSection(signal_critical_section_ptr);
88             SetEvent(ctrl_c_event);
89             return TRUE;
90         } else
91             LeaveCriticalSection(signal_critical_section_ptr);
92     }
93     return FALSE;
94 }
95
96 /*
97  * WIN32 equivalent for sigaction supports the following:
98  * set handler for SIGINT using WIN32 Ctrl-C handler
99  * reset handler SIGINT to SIG_DFL
100  * ignore SIGPIPE
101  */
102 static int
103 sigaction(int signal, struct sigaction *action, struct sigaction *oaction)
104 {
105     assert(!oaction);
106     assert(action);
107
108     if (signal == SIGPIPE)
109         assert(action->sa_handler == SIG_IGN);
110     else {
111         assert(signal == SIGINT && action->sa_handler != SIG_IGN);
112         if (ctrl_handler == action->sa_handler)
113             return 0;
114         if (signal_critical_section_ptr == NULL) {
115             signal_critical_section_ptr = &signal_critical_section;
116             InitializeCriticalSection(signal_critical_section_ptr);
117         }
118         EnterCriticalSection(signal_critical_section_ptr);
119         if (!SetConsoleCtrlHandler(w32_signal_handler,
120                                    action->sa_handler != SIG_DFL)) {
121             errno = GetLastError();
122             LeaveCriticalSection(signal_critical_section_ptr);
123             return -1;
124         }
125         ctrl_handler = action->sa_handler;
126         LeaveCriticalSection(signal_critical_section_ptr);
127     }
128     return 0;
129 }
130
131 /*
132  * Read the stdin in WIN32 environment
133  * WIN32 does not support select type function on console input
134  * so the client uses a separate thread to read input
135  */
136 static void
137 stdin_read_thread(void *dummy)
138 {
139     for (;;) {
140         switch (WaitForSingleObject(bounce_empty, INFINITE)) {
141         case WAIT_FAILED:
142             bounce_status = -1;
143             bounce_error = GetLastError();
144             break;
145         case WAIT_OBJECT_0:
146             bounce_status = _read(0, bounce_buf, sizeof(bounce_buf));
147             bounce_error = errno;
148             break;
149         case WAIT_ABANDONED:
150             return;
151         default:
152             assert(0);
153         }
154         SetEvent(bounce_full);
155     }
156 }
157
158 /*
159  * Initialize and start the stdin reading thread for WIN32
160  */
161 static void
162 sysdep_stdin_init(void)
163 {
164     bounce_empty = CreateEvent(NULL, FALSE, TRUE, NULL);
165     bounce_full = CreateEvent(NULL, TRUE, FALSE, NULL);
166     ctrl_c_event = CreateEvent(NULL, FALSE, FALSE, NULL);
167     _beginthread(stdin_read_thread, 0, NULL);
168 }
169
170 /*
171  * This function uses to WaitForMultipleObjects to wait for both
172  * stdin and socket reading or writing.
173  * Stdin is treated special in WIN32. Waiting for stdin is done
174  * via a bounce_full event which is set in the stdin thread.
175  * Execute command file reading is done via handle.  Execute
176  * command is read via CreateFile/ReadFile instead open/read
177  * because a file descriptor is not waitable.
178  * WaitForMultipleObjects will only respond with one object
179  * so an additonal select is also done to determine
180  * which individual events are active for the sock.
181  */
182 static int
183 w32_select(int nfds, fd_set *rdfd, fd_set *wrfd, fd_set *errfd, struct timeval* time)
184 {
185     HANDLE handles[3];
186     SOCKET sock;
187     int inp, result, s_result, num_handles;
188     struct timeval tv_time = {0, 0};
189     fd_set rdfd2;
190
191     switch (rdfd->fd_count) {
192     case 1:
193         inp = -1;
194         sock = rdfd->fd_array[0];
195         break;
196     case 2:
197         inp = rdfd->fd_array[0];
198         sock = rdfd->fd_array[1];
199         break;
200     default:
201         assert(0);
202     }
203
204     assert(wrfd->fd_count == 0
205            || (wrfd->fd_count == 1 && wrfd->fd_array[0] == sock));
206     assert(inp < 0 || inp == input_fd);
207
208     num_handles = 0;
209     handles[num_handles++] = ctrl_c_event;
210     if (inp >= 0)
211         handles[num_handles++] = inp ? (HANDLE)inp : bounce_full;
212     /* always wait on the socket */
213     handles[num_handles++] = WSACreateEvent();
214
215     if (wrfd->fd_count > 0)
216         WSAEventSelect(sock, handles[num_handles - 1],
217                        FD_READ | FD_WRITE | FD_CLOSE);
218     else
219         WSAEventSelect(sock, handles[num_handles - 1],
220                        FD_READ | FD_CLOSE);
221
222     result = WaitForMultipleObjects(num_handles, handles, 0, INFINITE);
223     if (result < 0) {
224         errno = GetLastError();
225         WSACloseEvent(handles[num_handles - 1]);
226         return -1;
227     }
228     WSACloseEvent(handles[num_handles - 1]);
229
230     if (result == WAIT_OBJECT_0) {
231         errno = EINTR;
232         return -1;
233     }
234
235     FD_ZERO(&rdfd2);
236     FD_SET(sock, &rdfd2);
237     s_result = select(sock + 1, &rdfd2, wrfd, NULL, &tv_time);
238
239     if (s_result < 0) {
240         errno = WSAGetLastError();
241         return s_result;
242     }
243
244     *rdfd = rdfd2;
245     if (inp >= 0 && result == WAIT_OBJECT_0 + 1) {
246         FD_SET((SOCKET)inp, rdfd);
247         s_result++;
248     }
249     return s_result;
250 }
251
252 /*
253  * Read input from the user either stdin or from file.
254  * For stdin, read from bounce_buf which filled by the stdin thread
255  * otherwise use the regular ring_from_file.
256  */
257 static int
258 w32_ring_from_file_or_bounce_buf(struct ring *r, int fd)
259 {
260     int i, res;
261
262     if (fd)
263         return ring_from_file(r, fd);
264
265     if (bounce_status < 0) {
266         errno = bounce_error;
267         res = bounce_status;
268     } else {
269         for (i = 0; i < bounce_status; i++) {
270             if (ring_putc(r, bounce_buf[i]) == EOF) {
271                 /* more work to do, hold on to bounce_buf */
272                 memmove(bounce_buf, bounce_buf + i, bounce_status - i);
273                 bounce_status -= i;
274                 return i;
275             }
276         }
277         res = i;
278     }
279
280     ResetEvent(bounce_full);
281     SetEvent(bounce_empty);
282     return res;
283 }
284 #define ring_from_file w32_ring_from_file_or_bounce_buf
285 #define close(fd) w32_close_handle((fd))
286 #define read(sock, buffer, buf_size) \
287         w32_recv((sock), (buffer), (buf_size), 0)
288 #define select(nfds, rd, wr, error, time) \
289         w32_select((nfds), (rd), (wr), (error), (time))
290 #define sigemptyset(mask) ((void)0)
291 #else
292 #define sysdep_stdin_init() ((void)0)
293 #endif
294
295 #define EOF_COOKIE "ctld\n"
296 #define INTR_COOKIE "\naborted\n"
297
298 int input_fd;
299 int send_eof;                           /* need to send EOF_COOKIE */
300 static volatile sig_atomic_t send_intr; /* need to send INTR_COOKIE */
301
302 /*
303  * Receive and process server output from SOCK.
304  * Return number of characters received on success, -1 on error.
305  */
306 static int
307 recv_output(int sock)
308 {
309     /*
310      * Read a chunk of server output and feed its characters into a
311      * simple state machine.
312      * Initial state is SCANNING_ID.
313      * In state SCANNING_ID, buffer the character.  If it's a space,
314      * decode the id that has been buffered, and enter state BUFFERING
315      * or COPYING depending on its value.
316      * In state BUFFERING, buffer the character.  If it's newline,
317      * pass id and buffered text to servercmd(), then enter state
318      * SCANNING_ID.
319      * In state COPYING, pass the character to outch().  If it's
320      * newline, enter state SCANNING_ID.
321      */
322     static enum {
323         SCANNING_ID, BUFFERING, COPYING
324     } state = SCANNING_ID;
325     static int id;
326     static struct lbuf lbuf;
327     char buf[4096];
328     ssize_t n;
329     int i, ch, len;
330     char *line;
331
332     n = read(sock, buf, sizeof(buf));
333     if (n < 0)
334         return -1;
335
336     for (i = 0; i < n; i++) {
337         ch = buf[i];
338         switch (state) {
339         case SCANNING_ID:
340             if (ch == '\n') {
341                 /* FIXME gripe unexpected! */
342                 lbuf_init(&lbuf);
343                 break;
344             }
345             lbuf_putc(&lbuf, ch);
346             if (ch != ' ')
347                 break;
348             line = lbuf_line(&lbuf);
349             id = parseid(line);
350             lbuf_init(&lbuf);
351
352             switch (id) {
353             case C_PROMPT:
354             case C_FLUSH:
355             case C_EXECUTE:
356             case C_EXIT:
357             case C_FLASH:
358             case C_INFORM:
359             case C_PIPE:
360             case C_REDIR:
361                 state = BUFFERING;
362                 break;
363             default:
364                 /* unknown or unexpected id, treat like C_DATA */
365             case C_DATA:
366                 state = COPYING;
367                 break;
368             }
369             break;
370
371         case BUFFERING:
372             len = lbuf_putc(&lbuf, ch);
373             if (len) {
374                 line = lbuf_line(&lbuf);
375                 servercmd(id, line, len);
376                 lbuf_init(&lbuf);
377                 state = SCANNING_ID;
378             }
379             break;
380
381         case COPYING:
382             outch(ch);
383             if (ch == '\n')
384                 state = SCANNING_ID;
385         }
386     }
387
388     return n;
389 }
390
391 /*
392  * Receive command input from FD into INBUF.
393  * Return 1 on receipt of input, zero on EOF, -1 on error.
394  */
395 static int
396 recv_input(int fd, struct ring *inbuf)
397 {
398     static struct lbuf cmdbuf;
399     int n, i, ch;
400     char *line;
401     int res = 1;
402
403     n = ring_from_file(inbuf, fd);
404     if (n < 0)
405         return -1;
406     if (n == 0) {
407         /* EOF on input */
408         if (lbuf_len(&cmdbuf)) {
409             /* incomplete line */
410             ring_putc(inbuf, '\n');
411             n++;
412         }
413         /*
414          * Can't put EOF cookie into INBUF here, it may not fit.
415          * Leave it to caller.
416          */
417         res = 0;
418     }
419
420     /* copy input to AUXFP etc. */
421     for (i = -n; i < 0; i++) {
422         ch = ring_peek(inbuf, i);
423         assert(ch != EOF);
424         if (ch != '\r' && lbuf_putc(&cmdbuf, ch)) {
425             line = lbuf_line(&cmdbuf);
426             if (auxfp)
427                 fputs(line, auxfp);
428             save_input(line);
429             lbuf_init(&cmdbuf);
430         }
431     }
432
433     return res;
434 }
435
436 static void
437 intr(int sig)
438 {
439     send_intr = 1;
440 }
441
442 /*
443  * Play on SOCK.
444  * The session must be in the playing phase.
445  * Return 0 when the session ended, -1 on error.
446  */
447 int
448 play(int sock)
449 {
450     /*
451      * Player input flows from INPUT_FD through recv_input() into ring
452      * buffer INBUF, which drains into SOCK.  This must not block.
453      * Server output flows from SOCK into recv_output().  Reading SOCK
454      * must not block.
455      */
456     struct sigaction sa;
457     struct ring inbuf;          /* input buffer, draining to SOCK */
458     int eof_fd0;                /* read fd 0 hit EOF? */
459     fd_set rdfd, wrfd;
460     int n;
461
462     sa.sa_flags = 0;
463     sigemptyset(&sa.sa_mask);
464     sa.sa_handler = intr;
465     sigaction(SIGINT, &sa, NULL);
466     sa.sa_handler = SIG_IGN;
467     sigaction(SIGPIPE, &sa, NULL);
468
469     ring_init(&inbuf);
470     eof_fd0 = send_eof = send_intr = 0;
471     input_fd = 0;
472     sysdep_stdin_init();
473
474     for (;;) {
475         FD_ZERO(&rdfd);
476         FD_ZERO(&wrfd);
477
478         /*
479          * Want to read player input only when we don't need to send
480          * cookies, and INPUT_FD is still open, and INBUF can accept
481          * some.
482          */
483         if (!send_intr && !send_eof && input_fd >= 0 && ring_space(&inbuf))
484             FD_SET(input_fd, &rdfd);
485         /* Want to send player input only when we have something */
486         if (send_intr || send_eof || ring_len(&inbuf))
487             FD_SET(sock, &wrfd);
488         /* Always want to read server output */
489         FD_SET(sock, &rdfd);
490
491         n = select(MAX(input_fd, sock) + 1, &rdfd, &wrfd, NULL, NULL);
492         if (n < 0) {
493             if (errno != EINTR) {
494                 perror("select");
495                 return -1;
496             }
497         }
498
499         if (send_eof
500             && ring_putm(&inbuf, EOF_COOKIE, sizeof(EOF_COOKIE) - 1) >= 0)
501             send_eof--;
502         if (send_intr
503             && ring_putm(&inbuf, INTR_COOKIE, sizeof(INTR_COOKIE) - 1) >= 0) {
504             send_intr = 0;
505             if (input_fd) {
506                 /* execute aborted, switch back to fd 0 */
507                 close(input_fd);
508                 input_fd = eof_fd0 ? -1 : 0;
509             }
510         }
511
512         if (n < 0)
513             continue;
514
515         /* read player input */
516         if (input_fd >= 0 && FD_ISSET(input_fd, &rdfd)) {
517             n = recv_input(input_fd, &inbuf);
518             if (n < 0) {
519                 perror("read stdin"); /* FIXME stdin misleading, could be execing */
520                 n = 0;
521             }
522             if (n == 0) {
523                 /* EOF on input */
524                 send_eof++;
525                 if (input_fd) {
526                     /* execute done, switch back to fd 0 */
527                     close(input_fd);
528                     input_fd = eof_fd0 ? -1 : 0;
529                 } else {
530                     /* stop reading input, drain socket ring buffers */
531                     eof_fd0 = 1;
532                     input_fd = -1;
533                     sa.sa_handler = SIG_DFL;
534                     sigaction(SIGINT, &sa, NULL);
535                 }
536             }
537         }
538
539         /* send it to the server */
540         if (FD_ISSET(sock, &wrfd)) {
541             n = ring_to_file(&inbuf, sock);
542             if (n < 0) {
543                 perror("write socket");
544                 return -1;
545             }
546         }
547
548         /* read server output and print it */
549         if (FD_ISSET(sock, &rdfd)) {
550             n = recv_output(sock);
551             if (n < 0) {
552                 perror("read socket");
553                 return -1;
554             }
555             if (n == 0)
556                 return 0;
557         }
558     }
559 }