2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2016, 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-2010
31 * Ron Koenderink, 2007-2009
43 #include <sys/socket.h>
45 #include <sys/select.h>
55 static CRITICAL_SECTION signal_critical_section;
56 static LPCRITICAL_SECTION signal_critical_section_ptr = NULL;
58 static unsigned char bounce_buf[RING_SIZE];
60 * Set bounce_empty to indicate bounce_buf is available for the stdin thread
63 static HANDLE bounce_empty;
65 * Set bounce_full to indicate bounce_buf is contains data from the
66 * stdin thread and is available for recv_input
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;
75 void (*sa_handler)(int sig);
79 static void (*ctrl_handler)(int sig) = { SIG_DFL };
82 * Ctrl-C handler for emulating the SIGINT in WIN32
85 w32_signal_handler(DWORD ctrl_type)
87 if (ctrl_type == CTRL_C_EVENT) {
88 EnterCriticalSection(signal_critical_section_ptr);
89 if (ctrl_handler != SIG_DFL) {
91 LeaveCriticalSection(signal_critical_section_ptr);
92 SetEvent(ctrl_c_event);
95 LeaveCriticalSection(signal_critical_section_ptr);
101 * WIN32 equivalent for sigaction supports the following:
102 * set handler for SIGINT using WIN32 Ctrl-C handler
103 * reset handler SIGINT to SIG_DFL
107 sigaction(int signal, struct sigaction *action, struct sigaction *oaction)
112 if (signal == SIGPIPE)
113 assert(action->sa_handler == SIG_IGN);
115 assert(signal == SIGINT && action->sa_handler != SIG_IGN);
116 if (ctrl_handler == action->sa_handler)
118 if (signal_critical_section_ptr == NULL) {
119 signal_critical_section_ptr = &signal_critical_section;
120 InitializeCriticalSection(signal_critical_section_ptr);
122 EnterCriticalSection(signal_critical_section_ptr);
123 if (!SetConsoleCtrlHandler(w32_signal_handler,
124 action->sa_handler != SIG_DFL)) {
125 errno = GetLastError();
126 LeaveCriticalSection(signal_critical_section_ptr);
129 ctrl_handler = action->sa_handler;
130 LeaveCriticalSection(signal_critical_section_ptr);
136 * Read the stdin in WIN32 environment
137 * WIN32 does not support select type function on console input
138 * so the client uses a separate thread to read input
141 stdin_read_thread(void *dummy)
144 switch (WaitForSingleObject(bounce_empty, INFINITE)) {
147 bounce_error = GetLastError();
150 bounce_status = _read(0, bounce_buf, sizeof(bounce_buf));
151 bounce_error = errno;
158 SetEvent(bounce_full);
163 * Initialize and start the stdin reading thread for WIN32
166 sysdep_stdin_init(void)
168 bounce_empty = CreateEvent(NULL, FALSE, TRUE, NULL);
169 bounce_full = CreateEvent(NULL, TRUE, FALSE, NULL);
170 ctrl_c_event = CreateEvent(NULL, FALSE, FALSE, NULL);
171 _beginthread(stdin_read_thread, 0, NULL);
175 * This function uses to WaitForMultipleObjects to wait for both
176 * stdin and socket reading or writing.
177 * Stdin is treated special in WIN32. Waiting for stdin is done
178 * via a bounce_full event which is set in the stdin thread.
179 * Execute command file reading is done via handle.
180 * WaitForMultipleObjects will only respond with one object,
181 * so an additonal select is also done to determine
182 * which individual events are active.
185 w32_select(int nfds, fd_set *rdfd, fd_set *wrfd, fd_set *errfd,
186 struct timeval *time)
190 int inp, sockfd, result, s_result, num_handles;
191 struct timeval tv_time = {0, 0};
192 fd_set rdsock, wrsock;
194 switch (rdfd->fd_count) {
197 sockfd = rdfd->fd_array[0];
200 inp = rdfd->fd_array[0];
201 sockfd = rdfd->fd_array[1];
206 sock = w32_fd2socket(sockfd);
208 assert(wrfd->fd_count == 0
209 || (wrfd->fd_count == 1 && wrfd->fd_array[0] == (SOCKET)sockfd));
210 assert(inp < 0 || inp == input_fd);
213 handles[num_handles++] = ctrl_c_event;
215 handles[num_handles++]
216 = inp ? (HANDLE)_get_osfhandle(inp) : bounce_full;
217 /* always wait on the socket */
218 handles[num_handles++] = WSACreateEvent();
220 if (wrfd->fd_count > 0)
221 WSAEventSelect(sock, handles[num_handles - 1],
222 FD_READ | FD_WRITE | FD_CLOSE);
224 WSAEventSelect(sock, handles[num_handles - 1],
227 result = WaitForMultipleObjects(num_handles, handles, 0, INFINITE);
229 errno = GetLastError();
230 WSACloseEvent(handles[num_handles - 1]);
233 WSACloseEvent(handles[num_handles - 1]);
235 if (result == WAIT_OBJECT_0) {
242 FD_SET(sock, &rdsock);
244 FD_SET(sock, &wrsock);
245 s_result = select(sock + 1, &rdsock, &wrsock, NULL, &tv_time);
248 w32_set_winsock_errno();
252 if (!FD_ISSET(sock, &rdsock))
253 FD_CLR((SOCKET)sockfd, rdfd);
254 if (!FD_ISSET(sock, &wrsock))
255 FD_CLR((SOCKET)sockfd, wrfd);
256 if (inp >= 0 && result == WAIT_OBJECT_0 + 1)
259 FD_CLR((SOCKET)inp, rdfd);
265 * Read input from the user either stdin or from file.
266 * For stdin, read from bounce_buf which filled by the stdin thread
267 * otherwise use the regular ring_from_file.
270 w32_ring_from_file_or_bounce_buf(struct ring *r, int fd)
275 return ring_from_file(r, fd);
277 if (bounce_status < 0) {
278 errno = bounce_error;
281 for (i = 0; i < bounce_status; i++) {
282 if (ring_putc(r, bounce_buf[i]) == EOF) {
283 /* more work to do, hold on to bounce_buf */
284 memmove(bounce_buf, bounce_buf + i, bounce_status - i);
292 ResetEvent(bounce_full);
293 SetEvent(bounce_empty);
296 #define ring_from_file w32_ring_from_file_or_bounce_buf
297 #define select(nfds, rd, wr, error, time) \
298 w32_select((nfds), (rd), (wr), (error), (time))
299 #define sigemptyset(mask) ((void)0)
301 #define sysdep_stdin_init() ((void)0)
304 #define EOF_COOKIE "ctld\n"
305 #define INTR_COOKIE "aborted\n"
308 int send_eof; /* need to send EOF_COOKIE */
309 static volatile sig_atomic_t send_intr; /* need to send INTR_COOKIE */
312 * Receive and process server output from @sock.
313 * Return number of characters received on success, -1 on error.
316 recv_output(int sock)
319 * Read a chunk of server output and feed its characters into a
320 * simple state machine.
321 * Initial state is SCANNING_ID.
322 * In state SCANNING_ID, buffer the character. If it's a space,
323 * decode the id that has been buffered, and enter state BUFFERING
324 * or COPYING depending on its value.
325 * In state BUFFERING, buffer the character. If it's newline,
326 * pass id and buffered text to servercmd(), then enter state
328 * In state COPYING, pass the character to outch(). If it's
329 * newline, enter state SCANNING_ID.
332 SCANNING_ID, BUFFERING, COPYING
333 } state = SCANNING_ID;
335 static struct lbuf lbuf;
341 n = read(sock, buf, sizeof(buf));
345 for (i = 0; i < n; i++) {
350 /* FIXME gripe unexpected! */
354 lbuf_putc(&lbuf, ch);
357 line = lbuf_line(&lbuf);
373 /* unknown or unexpected id, treat like C_DATA */
381 len = lbuf_putc(&lbuf, ch);
383 line = lbuf_line(&lbuf);
384 servercmd(id, line, len);
401 * Receive command input from @fd into @inbuf.
402 * Return 1 on receipt of input, zero on EOF, -1 on error.
405 recv_input(int fd, struct ring *inbuf)
407 static struct lbuf cmdbuf;
412 n = ring_from_file(inbuf, fd);
417 if (lbuf_len(&cmdbuf)) {
418 /* incomplete line */
419 ring_putc(inbuf, '\n');
423 * Can't put EOF cookie into INBUF here, it may not fit.
424 * Leave it to caller.
429 /* copy input to AUXFP etc. */
430 for (i = -n; i < 0; i++) {
431 ch = ring_peek(inbuf, i);
433 if (ch != '\r' && lbuf_putc(&cmdbuf, ch) > 0) {
434 line = lbuf_line(&cmdbuf);
453 * The session must be in the playing phase.
454 * Return 0 when the session ended, -1 on error.
460 * Player input flows from INPUT_FD through recv_input() into ring
461 * buffer INBUF, which drains into SOCK. This must not block.
462 * Server output flows from SOCK into recv_output(). Reading SOCK
466 struct ring inbuf; /* input buffer, draining to SOCK */
467 int eof_fd0; /* read fd 0 hit EOF? */
468 int partial_line_sent; /* partial input line sent? */
473 sigemptyset(&sa.sa_mask);
474 sa.sa_handler = intr;
475 sigaction(SIGINT, &sa, NULL);
476 sa.sa_handler = SIG_IGN;
477 sigaction(SIGPIPE, &sa, NULL);
480 eof_fd0 = partial_line_sent = send_eof = send_intr = 0;
489 * Want to read player input only when we don't need to send
490 * cookies, and INPUT_FD is still open, and INBUF can accept
493 if (!send_intr && !send_eof && input_fd >= 0 && ring_space(&inbuf))
494 FD_SET(input_fd, &rdfd);
495 /* Want to send player input only when we have something */
496 if (send_intr || send_eof || ring_len(&inbuf))
498 /* Always want to read server output */
501 n = select(MAX(input_fd, sock) + 1, &rdfd, &wrfd, NULL, NULL);
503 if (errno != EINTR) {
509 if ((send_eof || send_intr) && partial_line_sent
510 && ring_putc(&inbuf, '\n') != EOF)
511 partial_line_sent = 0;
512 if (send_eof && !partial_line_sent
513 && ring_putm(&inbuf, EOF_COOKIE, sizeof(EOF_COOKIE) - 1) >= 0)
515 if (send_intr && !partial_line_sent
516 && ring_putm(&inbuf, INTR_COOKIE, sizeof(INTR_COOKIE) - 1) >= 0) {
519 /* execute aborted, switch back to fd 0 */
521 input_fd = eof_fd0 ? -1 : 0;
528 /* read player input */
529 if (input_fd >= 0 && FD_ISSET(input_fd, &rdfd)) {
530 n = recv_input(input_fd, &inbuf);
532 perror("read stdin"); /* FIXME stdin misleading, could be execing */
539 /* execute done, switch back to fd 0 */
541 input_fd = eof_fd0 ? -1 : 0;
543 /* stop reading input, drain socket ring buffers */
546 sa.sa_handler = SIG_DFL;
547 sigaction(SIGINT, &sa, NULL);
550 partial_line_sent = ring_peek(&inbuf, -1) != '\n';
553 /* send it to the server */
554 if (FD_ISSET(sock, &wrfd)) {
555 n = ring_to_file(&inbuf, sock);
557 perror("write socket");
562 /* read server output and print it */
563 if (FD_ISSET(sock, &rdfd)) {
564 n = recv_output(sock);
566 perror("read socket");