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