src/client/play.c

Go to the documentation of this file.
00001 /*
00002  *  Empire - A multi-player, client/server Internet based war game.
00003  *  Copyright (C) 1986-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
00004  *                           Ken Stevens, Steve McClure
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  *  ---
00021  *
00022  *  See files README, COPYING and CREDITS in the root of the source
00023  *  tree for related information and legal notices.  It is expected
00024  *  that future projects/authors will amend these files as needed.
00025  *
00026  *  ---
00027  *
00028  *  play.c: Playing the game
00029  * 
00030  *  Known contributors to this file:
00031  *     Markus Armbruster, 2007
00032  *     Ron Koenderink, 2007
00033  */
00034 
00035 #include <config.h>
00036 
00037 #include <assert.h>
00038 #include <errno.h>
00039 #include <signal.h>
00040 #include <stdio.h>
00041 #include <stdlib.h>
00042 #ifndef _WIN32
00043 #include <sys/time.h>
00044 #include <sys/types.h>
00045 #include <unistd.h>
00046 #else
00047 #include <io.h>
00048 #endif
00049 #include "linebuf.h"
00050 #include "misc.h"
00051 #include "proto.h"
00052 #include "ringbuf.h"
00053 #include "secure.h"
00054 
00055 #ifdef _WIN32
00056 static CRITICAL_SECTION signal_critical_section;
00057 static LPCRITICAL_SECTION signal_critical_section_ptr = NULL;
00058 
00059 static unsigned char bounce_buf[RING_SIZE];
00060 /*
00061  * Set bounce_empty to indicate bounce_buf is available for the stdin thread
00062  * to use.
00063  */
00064 static HANDLE bounce_empty;
00065 /*
00066  * Set bounce_full to indicate bounce_buf is contains data from the
00067  * stdin thread and is available for recv_input
00068  */
00069 static HANDLE bounce_full;
00070  /* Ctrl-C (SIGINT) was detected, generate EINTR for the w32_select() */
00071 static HANDLE ctrl_c_event;
00072 static int bounce_status, bounce_error;
00073 
00074 #define SIGPIPE -1
00075 static void (*ctrl_handler)(int sig) = { SIG_DFL };
00076 
00077 /*
00078  * Ctrl-C handler for emulating the SIGINT in WIN32
00079  */
00080 static BOOL WINAPI
00081 w32_signal_handler(DWORD ctrl_type)
00082 {
00083     if (ctrl_type == CTRL_C_EVENT) {
00084         EnterCriticalSection(signal_critical_section_ptr);
00085         if (ctrl_handler != SIG_DFL) {
00086             ctrl_handler(SIGINT);
00087             LeaveCriticalSection(signal_critical_section_ptr);
00088             SetEvent(ctrl_c_event);
00089             return TRUE;
00090         } else
00091             LeaveCriticalSection(signal_critical_section_ptr);
00092     }
00093     return FALSE;
00094 }
00095 
00096 /*
00097  * WIN32 equivalent for sigaction supports the following:
00098  * set handler for SIGINT using WIN32 Ctrl-C handler
00099  * reset handler SIGINT to SIG_DFL
00100  * ignore SIGPIPE
00101  */
00102 static int
00103 sigaction(int signal, struct sigaction *action, struct sigaction *oaction)
00104 {
00105     assert(!oaction);
00106     assert(action);
00107     
00108     if (signal == SIGPIPE)
00109         assert(action->sa_handler == SIG_IGN);
00110     else {
00111         assert(signal == SIGINT && action->sa_handler != SIG_IGN);
00112         if (ctrl_handler == action->sa_handler)
00113             return 0;
00114         if (signal_critical_section_ptr == NULL) {
00115             signal_critical_section_ptr = &signal_critical_section;
00116             InitializeCriticalSection(signal_critical_section_ptr);
00117         }
00118         EnterCriticalSection(signal_critical_section_ptr);
00119         if (!SetConsoleCtrlHandler(w32_signal_handler,
00120                                    action->sa_handler != SIG_DFL)) {
00121             errno = GetLastError();
00122             LeaveCriticalSection(signal_critical_section_ptr);
00123             return -1;
00124         }
00125         ctrl_handler = action->sa_handler;
00126         LeaveCriticalSection(signal_critical_section_ptr);
00127     }
00128     return 0;
00129 }
00130 
00131 /*
00132  * Read the stdin in WIN32 environment
00133  * WIN32 does not support select type function on console input
00134  * so the client uses a separate thread to read input
00135  */
00136 static DWORD WINAPI
00137 stdin_read_thread(LPVOID lpParam)
00138 {
00139     for (;;) {
00140         if (WaitForSingleObject(bounce_empty, INFINITE) != WAIT_OBJECT_0)
00141             break;
00142         bounce_status = _read(0, bounce_buf, sizeof(bounce_buf));
00143         bounce_error = errno;
00144         if (bounce_status == 0) {
00145             if (_isatty(0)) {
00146                 SetEvent(bounce_empty);
00147                 continue;
00148             } else
00149                 break;
00150         }
00151         SetEvent(bounce_full);
00152     }
00153     SetEvent(bounce_full);
00154     return 0;
00155 }
00156 
00157 /*
00158  * Initialize and start the stdin reading thread for WIN32
00159  */
00160 static void
00161 sysdep_stdin_init(void)
00162 {
00163     bounce_empty = CreateEvent(NULL, FALSE, TRUE, "bounce_empty");
00164     bounce_full = CreateEvent(NULL, TRUE, FALSE, "bounce_full");
00165     ctrl_c_event = CreateEvent(NULL, FALSE, FALSE, "Ctrl_C");
00166     CreateThread(NULL, 0, stdin_read_thread, NULL, 0, NULL);
00167 }
00168 
00169 /*
00170  * This function uses to WaitForMultipleObjects to wait for both
00171  * stdin and socket reading or writing.
00172  * Stdin is treated special in WIN32. Waiting for stdin is done
00173  * via a bounce_full event which is set in the stdin thread.
00174  * Execute command file reading is done via handle.  Execute
00175  * command is read via CreateFile/ReadFile instead open/read
00176  * because a file descriptor is not waitable.
00177  * WaitForMultipleObjects will only respond with one object
00178  * so an additonal select is also done to determine
00179  * which individual events are active for the sock.
00180  */
00181 static int
00182 w32_select(int nfds, fd_set *rdfd, fd_set *wrfd, fd_set *errfd, struct timeval* time)
00183 {
00184     HANDLE handles[3];
00185     SOCKET sock;
00186     int result, s_result, num_handles = 0;
00187     struct timeval tv_time = {0, 0};
00188     fd_set rdfd2;
00189 
00190     if (rdfd->fd_count > 1) {
00191         sock = rdfd->fd_array[1];
00192         if (rdfd->fd_array[0])
00193             handles[num_handles++] = (HANDLE)rdfd->fd_array[0];
00194         else {
00195             handles[num_handles++] = ctrl_c_event;
00196             handles[num_handles++] = bounce_full;
00197         }
00198     } else {
00199         assert(rdfd->fd_count == 1);
00200         sock = rdfd->fd_array[0];
00201     }
00202     assert(wrfd->fd_count == 0 ||
00203            (wrfd->fd_count == 1 && wrfd->fd_array[0] == sock));
00204     /* always wait on the socket */
00205     handles[num_handles++] = WSACreateEvent();
00206 
00207     if (wrfd->fd_count > 0)
00208         WSAEventSelect(sock, handles[num_handles - 1],
00209                        FD_READ | FD_WRITE | FD_CLOSE);
00210     else
00211         WSAEventSelect(sock, handles[num_handles - 1],
00212                        FD_READ | FD_CLOSE);
00213 
00214     result = WaitForMultipleObjects(num_handles, handles, 0, INFINITE);
00215     if (result < 0) {
00216         errno = GetLastError();
00217         WSACloseEvent(handles[num_handles - 1]);
00218         return -1;
00219     }
00220     WSACloseEvent(handles[num_handles - 1]);
00221     
00222     if (num_handles == 3 && result == WAIT_OBJECT_0) {
00223         errno = EINTR;
00224         return -1;
00225     }
00226 
00227     FD_ZERO(&rdfd2);
00228     FD_SET(sock, &rdfd2);
00229     s_result = select(sock + 1, &rdfd2, wrfd, NULL, &tv_time);
00230 
00231     if (s_result < 0) {
00232         errno = WSAGetLastError();
00233         return s_result;
00234     }
00235 
00236     *rdfd = rdfd2;
00237     if (num_handles == 3 && result == WAIT_OBJECT_0 + 1) {
00238         FD_SET((SOCKET)0, rdfd);
00239         s_result++;
00240     }
00241     if (num_handles == 2 && result == WAIT_OBJECT_0) {
00242         FD_SET((SOCKET)handles[0], rdfd);
00243         s_result++;
00244     }
00245     return s_result;
00246 }
00247 
00248 /*
00249  * Read input from the user either stdin or from file.
00250  * For stdin, read from bounce_buf which filled by the stdin thread
00251  * otherwise use the regular ring_from_file.
00252  */
00253 static int
00254 w32_ring_from_file_or_bounce_buf(struct ring *r, int fd)
00255 {
00256     int i, res;
00257 
00258     if (fd)
00259         return ring_from_file(r, fd);
00260 
00261     if (bounce_status < 0) {
00262         errno = bounce_error;
00263         res = bounce_status;
00264     } else {
00265         for (i = 0; i < bounce_status; i++) {
00266             if (ring_putc(r, bounce_buf[i]) == EOF) {
00267                 /* more work to do, hold on to bounce_buf */
00268                 memmove(bounce_buf, bounce_buf + i, bounce_status - i);
00269                 bounce_status -= i;
00270                 return i;
00271             }
00272         }
00273         res = i;
00274     }
00275 
00276     ResetEvent(bounce_full);
00277     SetEvent(bounce_empty);
00278     return res;
00279 }
00280 #define ring_from_file w32_ring_from_file_or_bounce_buf
00281 #define close(fd) w32_close_handle((fd))
00282 #define read(sock, buffer, buf_size) \
00283         w32_recv((sock), (buffer), (buf_size), 0)
00284 #define select(nfds, rd, wr, error, time) \
00285         w32_select((nfds), (rd), (wr), (error), (time))
00286 #define sigemptyset(mask) ((void)0)
00287 #else
00288 #define sysdep_stdin_init() ((void)0)
00289 #endif
00290 
00291 #define EOF_COOKIE "ctld\n"
00292 #define INTR_COOKIE "\naborted\n"
00293 
00294 int input_fd;
00295 int send_eof;                           /* need to send EOF_COOKIE */
00296 static volatile sig_atomic_t send_intr; /* need to send INTR_COOKIE */
00297 
00298 /*
00299  * Receive and process server output from SOCK.
00300  * Return number of characters received on success, -1 on error.
00301  */
00302 static int
00303 recv_output(int sock)
00304 {
00305     /*
00306      * Read a chunk of server output and feed its characters into a
00307      * simple state machine.
00308      * Initial state is SCANNING_ID.
00309      * In state SCANNING_ID, buffer the character.  If it's a space,
00310      * decode the id that has been buffered, and enter state BUFFERING
00311      * or COPYING depending on its value.
00312      * In state BUFFERING, buffer the character.  If it's newline,
00313      * pass id and buffered text to servercmd(), then enter state
00314      * SCANNING_ID.
00315      * In state COPYING, pass the character to outch().  If it's
00316      * newline, enter state SCANNING_ID.
00317      */
00318     static enum {
00319         SCANNING_ID, BUFFERING, COPYING
00320     } state = SCANNING_ID;
00321     static int id;
00322     static struct lbuf lbuf;
00323     char buf[4096];
00324     ssize_t n;
00325     int i, ch, len;
00326     char *line;
00327 
00328     n = read(sock, buf, sizeof(buf));
00329     if (n < 0)
00330         return -1;
00331 
00332     for (i = 0; i < n; i++) {
00333         ch = buf[i];
00334         switch (state) {
00335         case SCANNING_ID:
00336             if (ch == '\n') {
00337                 /* FIXME gripe unexpected! */
00338                 lbuf_init(&lbuf);
00339                 break;
00340             }
00341             lbuf_putc(&lbuf, ch);
00342             if (ch != ' ')
00343                 break;
00344             line = lbuf_line(&lbuf);
00345             id = parseid(line);
00346             lbuf_init(&lbuf);
00347 
00348             switch (id) {
00349             case C_PROMPT:
00350             case C_FLUSH:
00351             case C_EXECUTE:
00352             case C_EXIT:
00353             case C_FLASH:
00354             case C_INFORM:
00355             case C_PIPE:
00356             case C_REDIR:
00357                 state = BUFFERING;
00358                 break;
00359             default:
00360                 /* unknown or unexpected id, treat like C_DATA */
00361             case C_DATA:
00362                 state = COPYING;
00363                 break;
00364             }
00365             break;
00366 
00367         case BUFFERING:
00368             len = lbuf_putc(&lbuf, ch);
00369             if (len) {
00370                 line = lbuf_line(&lbuf);
00371                 servercmd(id, line, len);
00372                 lbuf_init(&lbuf);
00373                 state = SCANNING_ID;
00374             }
00375             break;
00376 
00377         case COPYING:
00378             outch(ch);
00379             if (ch == '\n')
00380                 state = SCANNING_ID;
00381         }
00382     }
00383 
00384     return n;
00385 }
00386 
00387 /*
00388  * Receive command input from FD into INBUF.
00389  * Return 1 on receipt of input, zero on EOF, -1 on error.
00390  */
00391 static int
00392 recv_input(int fd, struct ring *inbuf)
00393 {
00394     static struct lbuf cmdbuf;
00395     int n, i, ch;
00396     char *line;
00397     int res = 1;
00398 
00399     n = ring_from_file(inbuf, fd);
00400     if (n < 0)
00401         return -1;
00402     if (n == 0) {
00403         /* EOF on input */
00404         if (lbuf_len(&cmdbuf)) {
00405             /* incomplete line */
00406             ring_putc(inbuf, '\n');
00407             n++;
00408         }
00409         /*
00410          * Can't put EOF cookie into INBUF here, it may not fit.
00411          * Leave it to caller.
00412          */
00413         res = 0;
00414     }
00415 
00416     /* copy input to AUXFP etc. */
00417     for (i = -n; i < 0; i++) {
00418         ch = ring_peek(inbuf, i);
00419         assert(ch != EOF);
00420         if (ch != '\r' && lbuf_putc(&cmdbuf, ch)) {
00421             line = lbuf_line(&cmdbuf);
00422             if (auxfp)
00423                 fputs(line, auxfp);
00424             save_input(line);
00425             lbuf_init(&cmdbuf);
00426         }
00427     }
00428 
00429     return res;
00430 }
00431 
00432 static void
00433 intr(int sig)
00434 {
00435     send_intr = 1;
00436 }
00437 
00438 /*
00439  * Play on SOCK.
00440  * The session must be in the playing phase.
00441  * Return 0 when the session ended, -1 on error.
00442  */
00443 int
00444 play(int sock)
00445 {
00446     /*
00447      * Player input flows from INPUT_FD through recv_input() into ring
00448      * buffer INBUF, which drains into SOCK.  This must not block.
00449      * Server output flows from SOCK into recv_output().  Reading SOCK
00450      * must not block.
00451      */
00452     struct sigaction sa;
00453     struct ring inbuf;          /* input buffer, draining to SOCK */
00454     int eof_fd0;                /* read fd 0 hit EOF? */
00455     fd_set rdfd, wrfd;
00456     int n;
00457 
00458     sa.sa_flags = 0;
00459     sigemptyset(&sa.sa_mask);
00460     sa.sa_handler = intr;
00461     sigaction(SIGINT, &sa, NULL);
00462     sa.sa_handler = SIG_IGN;
00463     sigaction(SIGPIPE, &sa, NULL);
00464 
00465     ring_init(&inbuf);
00466     eof_fd0 = send_eof = send_intr = 0;
00467     input_fd = 0;
00468     sysdep_stdin_init();
00469 
00470     for (;;) {
00471         FD_ZERO(&rdfd);
00472         FD_ZERO(&wrfd);
00473 
00474         /*
00475          * Want to read player input only when we don't need to send
00476          * cookies, and we haven't hit EOF on fd 0, and INBUF can
00477          * accept some.
00478          */
00479         if (!send_intr && !send_eof && !eof_fd0 && ring_space(&inbuf))
00480             FD_SET(input_fd, &rdfd);
00481         /* Want to send player input only when we have something */
00482         if (send_intr || send_eof || ring_len(&inbuf))
00483             FD_SET(sock, &wrfd);
00484         /* Always want to read server output */
00485         FD_SET(sock, &rdfd);
00486 
00487         n = select(MAX(input_fd, sock) + 1, &rdfd, &wrfd, NULL, NULL);
00488         if (n < 0) {
00489             if (errno != EINTR) {
00490                 perror("select");
00491                 return -1;
00492             }
00493         }
00494 
00495         if (send_eof
00496             && ring_putm(&inbuf, EOF_COOKIE, sizeof(EOF_COOKIE) - 1) >= 0)
00497             send_eof--;
00498         if (send_intr
00499             && ring_putm(&inbuf, INTR_COOKIE, sizeof(INTR_COOKIE) - 1) >= 0)
00500             send_intr = 0;
00501 
00502         if (n < 0)
00503             continue;
00504 
00505         /* read player input */
00506         if (FD_ISSET(input_fd, &rdfd)) {
00507             n = recv_input(input_fd, &inbuf);
00508             if (n < 0) {
00509                 perror("read stdin"); /* FIXME stdin misleading, could be execing */
00510                 n = 0;
00511             }
00512             if (n == 0) {
00513                 /* EOF on input */
00514                 send_eof++;
00515                 if (input_fd) {
00516                     /* execute done, switch back to fd 0 */
00517                     close(input_fd);
00518                     input_fd = 0;
00519                 } else {
00520                     /* stop reading input, drain socket ring buffers */
00521                     eof_fd0 = 1;
00522                     sa.sa_handler = SIG_DFL;
00523                     sigaction(SIGINT, &sa, NULL);
00524                 }
00525             }
00526         }
00527 
00528         /* send it to the server */
00529         if (FD_ISSET(sock, &wrfd)) {
00530             n = ring_to_file(&inbuf, sock);
00531             if (n < 0) {
00532                 perror("write socket");
00533                 return -1;
00534             }
00535         }
00536 
00537         /* read server output and print it */
00538         if (FD_ISSET(sock, &rdfd)) {
00539             n = recv_output(sock);
00540             if (n < 0) {
00541                 perror("read socket");
00542                 return -1;
00543             }
00544             if (n == 0)
00545                 return 0;
00546         }
00547     }
00548 }

Generated on Fri Mar 28 11:01:11 2008 for empserver by  doxygen 1.5.2