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