]> git.pond.sub.org Git - empserver/blobdiff - src/client/play.c
Fix client's command abort feature
[empserver] / src / client / play.c
index 11ea847e9fe6626bff9bcaa85575200179dcd3a9..c8948591cf01d65414d2a8ec8139a5be8e6d6b46 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2009, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2010, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                           Ken Stevens, Steve McClure
  *
  *  This program is free software; you can redistribute it and/or modify
@@ -28,7 +28,7 @@
  *  play.c: Playing the game
  *
  *  Known contributors to this file:
- *     Markus Armbruster, 2007
+ *     Markus Armbruster, 2007-2009
  *     Ron Koenderink, 2007-2009
  */
 
 #include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
-#ifndef _WIN32
-#include <sys/time.h>
-#include <sys/types.h>
-#include <unistd.h>
+#ifdef _WIN32
+#include <process.h>
+#include <sys/socket.h>
 #else
-#include <io.h>
+#include <sys/select.h>
 #endif
+#include <unistd.h>
 #include "linebuf.h"
 #include "misc.h"
 #include "proto.h"
@@ -67,10 +67,15 @@ static HANDLE bounce_empty;
  * stdin thread and is available for recv_input
  */
 static HANDLE bounce_full;
- /* Ctrl-C (SIGINT) was detected, generate EINTR for the w32_select() */
+/* Ctrl-C (SIGINT) was detected, generate EINTR for the w32_select() */
 static HANDLE ctrl_c_event;
 static int bounce_status, bounce_error;
 
+struct sigaction {
+    int sa_flags;
+    void (*sa_handler)(int sig);
+};
+
 #define SIGPIPE -1
 static void (*ctrl_handler)(int sig) = { SIG_DFL };
 
@@ -133,25 +138,26 @@ sigaction(int signal, struct sigaction *action, struct sigaction *oaction)
  * WIN32 does not support select type function on console input
  * so the client uses a separate thread to read input
  */
-static DWORD WINAPI
-stdin_read_thread(LPVOID lpParam)
+static void
+stdin_read_thread(void *dummy)
 {
     for (;;) {
-       if (WaitForSingleObject(bounce_empty, INFINITE) != WAIT_OBJECT_0)
+       switch (WaitForSingleObject(bounce_empty, INFINITE)) {
+       case WAIT_FAILED:
+           bounce_status = -1;
+           bounce_error = GetLastError();
            break;
-       bounce_status = _read(0, bounce_buf, sizeof(bounce_buf));
-       bounce_error = errno;
-       if (bounce_status == 0) {
-           if (_isatty(0)) {
-               SetEvent(bounce_empty);
-               continue;
-           } else
-               break;
+       case WAIT_OBJECT_0:
+           bounce_status = _read(0, bounce_buf, sizeof(bounce_buf));
+           bounce_error = errno;
+           break;
+       case WAIT_ABANDONED:
+           return;
+       default:
+           assert(0);
        }
        SetEvent(bounce_full);
     }
-    SetEvent(bounce_full);
-    return 0;
 }
 
 /*
@@ -163,44 +169,51 @@ sysdep_stdin_init(void)
     bounce_empty = CreateEvent(NULL, FALSE, TRUE, NULL);
     bounce_full = CreateEvent(NULL, TRUE, FALSE, NULL);
     ctrl_c_event = CreateEvent(NULL, FALSE, FALSE, NULL);
-    CreateThread(NULL, 0, stdin_read_thread, NULL, 0, NULL);
+    _beginthread(stdin_read_thread, 0, NULL);
 }
 
 /*
  * This function uses to WaitForMultipleObjects to wait for both
  * stdin and socket reading or writing.
- * Stdin is treated special in WIN32. Waiting for stdin is done
+ * Stdin is treated special in WIN32.  Waiting for stdin is done
  * via a bounce_full event which is set in the stdin thread.
- * Execute command file reading is done via handle.  Execute
- * command is read via CreateFile/ReadFile instead open/read
- * because a file descriptor is not waitable.
- * WaitForMultipleObjects will only respond with one object
+ * Execute command file reading is done via handle.
+ * WaitForMultipleObjects will only respond with one object,
  * so an additonal select is also done to determine
- * which individual events are active for the sock.
+ * which individual events are active.
  */
 static int
 w32_select(int nfds, fd_set *rdfd, fd_set *wrfd, fd_set *errfd, struct timeval* time)
 {
     HANDLE handles[3];
     SOCKET sock;
-    int result, s_result, num_handles = 0;
+    int inp, sockfd, result, s_result, num_handles;
     struct timeval tv_time = {0, 0};
-    fd_set rdfd2;
-
-    if (rdfd->fd_count > 1) {
-       sock = rdfd->fd_array[1];
-       if (rdfd->fd_array[0])
-           handles[num_handles++] = (HANDLE)rdfd->fd_array[0];
-       else {
-           handles[num_handles++] = ctrl_c_event;
-           handles[num_handles++] = bounce_full;
-       }
-    } else {
-       assert(rdfd->fd_count == 1);
-       sock = rdfd->fd_array[0];
+    fd_set rdsock, wrsock;
+
+    switch (rdfd->fd_count) {
+    case 1:
+       inp = -1;
+       sockfd = rdfd->fd_array[0];
+       break;
+    case 2:
+       inp = rdfd->fd_array[0];
+       sockfd = rdfd->fd_array[1];
+       break;
+    default:
+       assert(0);
     }
-    assert(wrfd->fd_count == 0 ||
-          (wrfd->fd_count == 1 && wrfd->fd_array[0] == sock));
+    sock = w32_fd2socket(sockfd);
+
+    assert(wrfd->fd_count == 0
+          || (wrfd->fd_count == 1 && wrfd->fd_array[0] == (SOCKET)sockfd));
+    assert(inp < 0 || inp == input_fd);
+
+    num_handles = 0;
+    handles[num_handles++] = ctrl_c_event;
+    if (inp >= 0)
+       handles[num_handles++]
+           = inp ? (HANDLE)_get_osfhandle(inp) : bounce_full;
     /* always wait on the socket */
     handles[num_handles++] = WSACreateEvent();
 
@@ -219,29 +232,32 @@ w32_select(int nfds, fd_set *rdfd, fd_set *wrfd, fd_set *errfd, struct timeval*
     }
     WSACloseEvent(handles[num_handles - 1]);
 
-    if (num_handles == 3 && result == WAIT_OBJECT_0) {
+    if (result == WAIT_OBJECT_0) {
        errno = EINTR;
        return -1;
     }
 
-    FD_ZERO(&rdfd2);
-    FD_SET(sock, &rdfd2);
-    s_result = select(sock + 1, &rdfd2, wrfd, NULL, &tv_time);
+    FD_ZERO(&rdsock);
+    FD_ZERO(&wrsock);
+    FD_SET(sock, &rdsock);
+    if (wrfd->fd_count)
+       FD_SET(sock, &wrsock);
+    s_result = select(sock + 1, &rdsock, &wrsock, NULL, &tv_time);
 
     if (s_result < 0) {
-       errno = WSAGetLastError();
+       w32_set_winsock_errno();
        return s_result;
     }
 
-    *rdfd = rdfd2;
-    if (num_handles == 3 && result == WAIT_OBJECT_0 + 1) {
-       FD_SET((SOCKET)0, rdfd);
-       s_result++;
-    }
-    if (num_handles == 2 && result == WAIT_OBJECT_0) {
-       FD_SET((SOCKET)handles[0], rdfd);
+    if (!FD_ISSET(sock, &rdsock))
+       FD_CLR((SOCKET)sockfd, rdfd);
+    if (!FD_ISSET(sock, &wrsock))
+       FD_CLR((SOCKET)sockfd, wrfd);
+    if (inp >= 0 && result == WAIT_OBJECT_0 + 1)
        s_result++;
-    }
+    else
+       FD_CLR((SOCKET)inp, rdfd);
+
     return s_result;
 }
 
@@ -278,9 +294,6 @@ w32_ring_from_file_or_bounce_buf(struct ring *r, int fd)
     return res;
 }
 #define ring_from_file w32_ring_from_file_or_bounce_buf
-#define close(fd) w32_close_handle((fd))
-#define read(sock, buffer, buf_size) \
-       w32_recv((sock), (buffer), (buf_size), 0)
 #define select(nfds, rd, wr, error, time) \
        w32_select((nfds), (rd), (wr), (error), (time))
 #define sigemptyset(mask) ((void)0)
@@ -289,7 +302,7 @@ w32_ring_from_file_or_bounce_buf(struct ring *r, int fd)
 #endif
 
 #define EOF_COOKIE "ctld\n"
-#define INTR_COOKIE "\naborted\n"
+#define INTR_COOKIE "aborted\n"
 
 int input_fd;
 int send_eof;                          /* need to send EOF_COOKIE */
@@ -452,6 +465,7 @@ play(int sock)
     struct sigaction sa;
     struct ring inbuf;         /* input buffer, draining to SOCK */
     int eof_fd0;               /* read fd 0 hit EOF? */
+    int input_eol;             /* input ends with '\n'? */
     fd_set rdfd, wrfd;
     int n;
 
@@ -463,7 +477,7 @@ play(int sock)
     sigaction(SIGPIPE, &sa, NULL);
 
     ring_init(&inbuf);
-    eof_fd0 = send_eof = send_intr = 0;
+    eof_fd0 = input_eol = send_eof = send_intr = 0;
     input_fd = 0;
     sysdep_stdin_init();
 
@@ -473,10 +487,10 @@ play(int sock)
 
        /*
         * Want to read player input only when we don't need to send
-        * cookies, and we haven't hit EOF on fd 0, and INBUF can
-        * accept some.
+        * cookies, and INPUT_FD is still open, and INBUF can accept
+        * some.
         */
-       if (!send_intr && !send_eof && !eof_fd0 && ring_space(&inbuf))
+       if (!send_intr && !send_eof && input_fd >= 0 && ring_space(&inbuf))
            FD_SET(input_fd, &rdfd);
        /* Want to send player input only when we have something */
        if (send_intr || send_eof || ring_len(&inbuf))
@@ -492,18 +506,27 @@ play(int sock)
            }
        }
 
-       if (send_eof
+       if ((send_eof || send_intr) && !input_eol
+           && ring_putc(&inbuf, '\n') != EOF)
+           input_eol = 1;
+       if (send_eof && input_eol
            && ring_putm(&inbuf, EOF_COOKIE, sizeof(EOF_COOKIE) - 1) >= 0)
            send_eof--;
-       if (send_intr
-           && ring_putm(&inbuf, INTR_COOKIE, sizeof(INTR_COOKIE) - 1) >= 0)
+       if (send_intr && input_eol
+           && ring_putm(&inbuf, INTR_COOKIE, sizeof(INTR_COOKIE) - 1) >= 0) {
            send_intr = 0;
+           if (input_fd) {
+               /* execute aborted, switch back to fd 0 */
+               close(input_fd);
+               input_fd = eof_fd0 ? -1 : 0;
+           }
+       }
 
        if (n < 0)
            continue;
 
        /* read player input */
-       if (FD_ISSET(input_fd, &rdfd)) {
+       if (input_fd >= 0 && FD_ISSET(input_fd, &rdfd)) {
            n = recv_input(input_fd, &inbuf);
            if (n < 0) {
                perror("read stdin"); /* FIXME stdin misleading, could be execing */
@@ -515,14 +538,16 @@ play(int sock)
                if (input_fd) {
                    /* execute done, switch back to fd 0 */
                    close(input_fd);
-                   input_fd = 0;
+                   input_fd = eof_fd0 ? -1 : 0;
                } else {
                    /* stop reading input, drain socket ring buffers */
                    eof_fd0 = 1;
+                   input_fd = -1;
                    sa.sa_handler = SIG_DFL;
                    sigaction(SIGINT, &sa, NULL);
                }
-           }
+           } else
+               input_eol = ring_peek(&inbuf, -1) == '\n';
        }
 
        /* send it to the server */