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
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.
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.
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/>.
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.
27 * play.c: Playing the game
29 * Known contributors to this file:
30 * Markus Armbruster, 2007-2017
31 * Ron Koenderink, 2007-2009
32 * Martin Haukeli, 2015
44 #include <sys/socket.h>
46 #include <sys/select.h>
55 #ifdef HAVE_LIBREADLINE
56 # if defined(HAVE_READLINE_READLINE_H)
57 # include <readline/readline.h>
58 # elif defined(HAVE_READLINE_H)
59 # include <readline.h>
60 # else /* !defined(HAVE_READLINE_H) */
61 extern char *readline ();
62 # endif /* !defined(HAVE_READLINE_H) */
63 #endif /* HAVE_LIBREADLINE */
65 #ifdef HAVE_READLINE_HISTORY
66 # if defined(HAVE_READLINE_HISTORY_H)
67 # include <readline/history.h>
68 # elif defined(HAVE_HISTORY_H)
70 # else /* !defined(HAVE_HISTORY_H) */
71 extern void add_history ();
72 extern int write_history ();
73 extern int read_history ();
74 # endif /* !defined(HAVE_HISTORY_H) */
75 #endif /* HAVE_READLINE_HISTORY */
77 #define EOF_COOKIE "ctld\n"
78 #define INTR_COOKIE "aborted\n"
81 * Player input file descriptor
82 * 0 while reading interactive input
83 * >0 while reading a batch file
84 * <0 during error handling
88 static volatile sig_atomic_t send_intr; /* need to send INTR_COOKIE */
91 static CRITICAL_SECTION signal_critical_section;
92 static LPCRITICAL_SECTION signal_critical_section_ptr = NULL;
94 static unsigned char bounce_buf[RING_SIZE];
96 * Set bounce_empty to indicate bounce_buf is available for the stdin thread
99 static HANDLE bounce_empty;
101 * Set bounce_full to indicate bounce_buf is contains data from the
102 * stdin thread and is available for recv_input
104 static HANDLE bounce_full;
105 /* Ctrl-C (SIGINT) was detected, generate EINTR for the w32_select() */
106 static HANDLE ctrl_c_event;
107 static int bounce_status, bounce_error;
111 void (*sa_handler)(int sig);
115 static void (*ctrl_handler)(int sig) = { SIG_DFL };
118 * Ctrl-C handler for emulating the SIGINT in WIN32
121 w32_signal_handler(DWORD ctrl_type)
123 if (ctrl_type == CTRL_C_EVENT) {
124 EnterCriticalSection(signal_critical_section_ptr);
125 if (ctrl_handler != SIG_DFL) {
126 ctrl_handler(SIGINT);
127 LeaveCriticalSection(signal_critical_section_ptr);
128 SetEvent(ctrl_c_event);
131 LeaveCriticalSection(signal_critical_section_ptr);
137 * WIN32 equivalent for sigaction supports the following:
138 * set handler for SIGINT using WIN32 Ctrl-C handler
139 * reset handler SIGINT to SIG_DFL
143 sigaction(int signal, struct sigaction *action, struct sigaction *oaction)
148 if (signal == SIGPIPE)
149 assert(action->sa_handler == SIG_IGN);
151 assert(signal == SIGINT && action->sa_handler != SIG_IGN);
152 if (ctrl_handler == action->sa_handler)
154 if (signal_critical_section_ptr == NULL) {
155 signal_critical_section_ptr = &signal_critical_section;
156 InitializeCriticalSection(signal_critical_section_ptr);
158 EnterCriticalSection(signal_critical_section_ptr);
159 if (!SetConsoleCtrlHandler(w32_signal_handler,
160 action->sa_handler != SIG_DFL)) {
161 errno = GetLastError();
162 LeaveCriticalSection(signal_critical_section_ptr);
165 ctrl_handler = action->sa_handler;
166 LeaveCriticalSection(signal_critical_section_ptr);
172 * Read the stdin in WIN32 environment
173 * WIN32 does not support select type function on console input
174 * so the client uses a separate thread to read input
177 stdin_read_thread(void *dummy)
180 switch (WaitForSingleObject(bounce_empty, INFINITE)) {
183 bounce_error = GetLastError();
186 bounce_status = _read(0, bounce_buf, sizeof(bounce_buf));
187 bounce_error = errno;
194 SetEvent(bounce_full);
199 * Initialize and start the stdin reading thread for WIN32
202 sysdep_stdin_init(void)
204 bounce_empty = CreateEvent(NULL, FALSE, TRUE, NULL);
205 bounce_full = CreateEvent(NULL, TRUE, FALSE, NULL);
206 ctrl_c_event = CreateEvent(NULL, FALSE, FALSE, NULL);
207 _beginthread(stdin_read_thread, 0, NULL);
211 * This function uses to WaitForMultipleObjects to wait for both
212 * stdin and socket reading or writing.
213 * Stdin is treated special in WIN32. Waiting for stdin is done
214 * via a bounce_full event which is set in the stdin thread.
215 * Execute command file reading is done via handle.
216 * WaitForMultipleObjects will only respond with one object,
217 * so an additonal select is also done to determine
218 * which individual events are active.
221 w32_select(int nfds, fd_set *rdfd, fd_set *wrfd, fd_set *errfd,
222 struct timeval *time)
226 int inp, sockfd, result, s_result, num_handles;
227 struct timeval tv_time = {0, 0};
228 fd_set rdsock, wrsock;
230 switch (rdfd->fd_count) {
233 sockfd = rdfd->fd_array[0];
236 inp = rdfd->fd_array[0];
237 sockfd = rdfd->fd_array[1];
242 sock = w32_fd2socket(sockfd);
244 assert(wrfd->fd_count == 0
245 || (wrfd->fd_count == 1 && wrfd->fd_array[0] == (SOCKET)sockfd));
246 assert(inp < 0 || inp == input_fd);
249 handles[num_handles++] = ctrl_c_event;
251 handles[num_handles++]
252 = inp ? (HANDLE)_get_osfhandle(inp) : bounce_full;
253 /* always wait on the socket */
254 handles[num_handles++] = WSACreateEvent();
256 if (wrfd->fd_count > 0)
257 WSAEventSelect(sock, handles[num_handles - 1],
258 FD_READ | FD_WRITE | FD_CLOSE);
260 WSAEventSelect(sock, handles[num_handles - 1],
263 result = WaitForMultipleObjects(num_handles, handles, 0, INFINITE);
265 errno = GetLastError();
266 WSACloseEvent(handles[num_handles - 1]);
269 WSACloseEvent(handles[num_handles - 1]);
271 if (result == WAIT_OBJECT_0) {
278 FD_SET(sock, &rdsock);
280 FD_SET(sock, &wrsock);
281 s_result = select(sock + 1, &rdsock, &wrsock, NULL, &tv_time);
284 w32_set_winsock_errno();
288 if (!FD_ISSET(sock, &rdsock))
289 FD_CLR((SOCKET)sockfd, rdfd);
290 if (!FD_ISSET(sock, &wrsock))
291 FD_CLR((SOCKET)sockfd, wrfd);
292 if (inp >= 0 && result == WAIT_OBJECT_0 + 1)
295 FD_CLR((SOCKET)inp, rdfd);
301 * Read input from the user either stdin or from file.
302 * For stdin, read from bounce_buf which filled by the stdin thread
303 * otherwise use the regular ring_from_file.
306 w32_ring_from_file_or_bounce_buf(struct ring *r, int fd)
311 return ring_from_file(r, fd);
313 if (bounce_status < 0) {
314 errno = bounce_error;
317 for (i = 0; i < bounce_status; i++) {
318 if (ring_putc(r, bounce_buf[i]) == EOF) {
319 /* more work to do, hold on to bounce_buf */
320 memmove(bounce_buf, bounce_buf + i, bounce_status - i);
328 ResetEvent(bounce_full);
329 SetEvent(bounce_empty);
332 #define ring_from_file w32_ring_from_file_or_bounce_buf
333 #define select(nfds, rd, wr, error, time) \
334 w32_select((nfds), (rd), (wr), (error), (time))
335 #define sigemptyset(mask) ((void)0)
337 #define sysdep_stdin_init() ((void)0)
341 * Receive and process server output from @sock.
342 * Return number of characters received on success, -1 on error.
345 recv_output(int sock)
348 * Read a chunk of server output and feed its characters into a
349 * simple state machine.
350 * Initial state is SCANNING_ID.
351 * In state SCANNING_ID, buffer the character. If it's a space,
352 * decode the id that has been buffered, and enter state BUFFERING
353 * or COPYING depending on its value.
354 * In state BUFFERING, buffer the character. If it's newline,
355 * pass id and buffered text to servercmd(), then enter state
357 * In state COPYING, pass the character to outch(). If it's
358 * newline, enter state SCANNING_ID.
361 SCANNING_ID, BUFFERING, COPYING
362 } state = SCANNING_ID;
364 static struct lbuf lbuf;
370 n = read(sock, buf, sizeof(buf));
374 for (i = 0; i < n; i++) {
379 /* FIXME gripe unexpected! */
383 lbuf_putc(&lbuf, ch);
386 line = lbuf_line(&lbuf);
402 /* unknown or unexpected id, treat like C_DATA */
410 len = lbuf_putc(&lbuf, ch);
412 line = lbuf_line(&lbuf);
413 fd = servercmd(id, line, len);
421 /* successful execute, switch to batch file */
440 #ifdef HAVE_LIBREADLINE
441 static char *input_from_rl;
442 static int has_rl_input;
445 input_handler(char *line)
447 input_from_rl = line;
449 #ifdef HAVE_READLINE_HISTORY
452 #endif /* HAVE_READLINE_HISTORY */
456 ring_from_rl(struct ring *inbuf)
461 assert(has_rl_input && input_from_rl);
463 len = strlen(input_from_rl);
464 n = ring_space(inbuf);
467 if (len >= (size_t)n) {
468 ring_putm(inbuf, input_from_rl, n);
469 memmove(input_from_rl, input_from_rl + n, len - n + 1);
471 ring_putm(inbuf, input_from_rl, len);
472 ring_putc(inbuf, '\n');
480 #endif /* HAVE_LIBREADLINE */
483 * Receive player input from @fd into @inbuf.
484 * Return 1 on receipt of input, zero on EOF, -1 on error.
487 recv_input(int fd, struct ring *inbuf)
492 #ifdef HAVE_LIBREADLINE
495 rl_callback_read_char();
499 n = ring_from_rl(inbuf);
504 n = ring_from_file(inbuf, fd);
509 * Can't put EOF cookie into INBUF here, it may not fit.
510 * Leave it to caller.
519 send_input(int fd, struct ring *inbuf)
525 cnt = ring_to_iovec(inbuf, iov);
526 res = writev(fd, iov, cnt);
530 /* Copy input to @auxfp etc. */
531 for (i = 0; i < res; i++) {
532 ch = ring_getc(inbuf);
540 #ifdef HAVE_LIBREADLINE
541 if (fd == 0 && has_rl_input && input_from_rl)
556 * @history_file is the name of the history file, or null.
557 * The session must be in the playing phase.
558 * Return 0 when the session ended, -1 on error.
561 play(int sock, char *history_file)
564 * Player input flows from INPUT_FD through recv_input() into ring
565 * buffer INBUF, which drains into SOCK. This must not block.
566 * Server output flows from SOCK into recv_output(). Reading SOCK
570 struct ring inbuf; /* input buffer, draining to SOCK */
571 int eof_fd0; /* read fd 0 hit EOF? */
572 int partial_line_sent; /* partial input line sent? */
573 int send_eof; /* need to send EOF_COOKIE */
579 sigemptyset(&sa.sa_mask);
580 sa.sa_handler = intr;
581 sigaction(SIGINT, &sa, NULL);
582 sa.sa_handler = SIG_IGN;
583 sigaction(SIGPIPE, &sa, NULL);
584 #ifdef HAVE_LIBREADLINE
585 rl_already_prompted = 1;
586 #ifdef HAVE_READLINE_HISTORY
588 read_history(history_file);
589 #endif /* HAVE_READLINE_HISTORY */
590 rl_bind_key('\t', rl_insert); /* Disable tab completion */
591 rl_callback_handler_install("", input_handler);
592 #endif /* HAVE_LIBREADLINE */
595 eof_fd0 = partial_line_sent = send_eof = send_intr = 0;
604 * Want to read player input only when we don't need to send
605 * cookies, haven't reached EOF on fd 0, and @inbuf can accept
608 if (!send_intr && !send_eof && (input_fd || !eof_fd0)
609 && ring_space(&inbuf))
610 FD_SET(input_fd, &rdfd);
611 /* Want to send player input only when we have something */
612 if (send_intr || send_eof || ring_len(&inbuf))
614 /* Always want to read server output */
617 n = select(MAX(input_fd, sock) + 1, &rdfd, &wrfd, NULL, NULL);
619 if (errno != EINTR) {
625 if ((send_eof || send_intr) && partial_line_sent
626 && ring_putc(&inbuf, '\n') != EOF)
627 partial_line_sent = 0;
628 if (send_eof && !partial_line_sent
629 && ring_putm(&inbuf, EOF_COOKIE, sizeof(EOF_COOKIE) - 1) >= 0)
631 if (send_intr && !partial_line_sent
632 && ring_putm(&inbuf, INTR_COOKIE, sizeof(INTR_COOKIE) - 1) >= 0) {
635 /* execute aborted, switch back to fd 0 */
643 /* read player input */
644 if (FD_ISSET(input_fd, &rdfd) && ring_space(&inbuf)) {
645 n = recv_input(input_fd, &inbuf);
648 /* execute done, switch back to fd 0 */
650 perror("read batch file");
657 /* stop reading input, drain socket ring buffers */
659 perror("read stdin");
662 sa.sa_handler = SIG_DFL;
663 sigaction(SIGINT, &sa, NULL);
666 } else if (ring_len(&inbuf) > 0)
667 partial_line_sent = ring_peek(&inbuf, -1) != '\n';
670 /* send it to the server */
671 if (FD_ISSET(sock, &wrfd)) {
672 n = send_input(sock, &inbuf);
674 perror("write socket");
679 /* read server output and print it */
680 if (FD_ISSET(sock, &rdfd)) {
681 n = recv_output(sock);
683 perror("read socket");
693 #ifdef HAVE_LIBREADLINE
694 rl_callback_handler_remove();
695 #ifdef HAVE_READLINE_HISTORY
697 write_history(history_file);
698 #endif /* HAVE_READLINE_HISTORY */
704 prompt(int code, char *prompt, char *teles)
708 snprintf(pr, sizeof(pr), "%s%s", teles, prompt);
709 #ifdef HAVE_LIBREADLINE
711 rl_forced_update_display();
712 #else /* !HAVE_LIBREADLINE */
715 #endif /* !HAVE_LIBREADLINE */
717 fprintf(auxfp, "%s%s", teles, prompt);