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