]> git.pond.sub.org Git - empserver/blob - src/client/play.c
Rewrite much of client's playing phase code:
[empserver] / src / client / play.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2007, 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  */
33
34 #include <config.h>
35
36 #include <assert.h>
37 #include <errno.h>
38 #include <signal.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <sys/time.h>
42 #include <sys/types.h>
43 #include <unistd.h>
44 #include "linebuf.h"
45 #include "misc.h"
46 #include "proto.h"
47 #include "ringbuf.h"
48 #include "secure.h"
49
50 #define EOF_COOKIE "ctld\n"
51 #define INTR_COOKIE "\naborted\n"
52
53 int input_fd;
54 static volatile sig_atomic_t send_intr; /* need to send INTR_COOKIE */
55
56 /*
57  * Receive and process server output from SOCK.
58  * Return number of characters received on success, -1 on error.
59  */
60 static int
61 recv_output(int sock)
62 {
63     /*
64      * Read a chunk of server output and feed its characters into a
65      * simple state machine.
66      * Initial state is SCANNING_ID.
67      * In state SCANNING_ID, buffer the character.  If it's a space,
68      * decode the id that has been buffered, and enter state BUFFERING
69      * or COPYING depending on its value.
70      * In state BUFFERING, buffer the character.  If it's newline,
71      * pass id and buffered text to servercmd(), then enter state
72      * SCANNING_ID.
73      * In state COPYING, pass the character to outch().  If it's
74      * newline, enter state SCANNING_ID.
75      */
76     static enum {
77         SCANNING_ID, BUFFERING, COPYING
78     } state = SCANNING_ID;
79     static int id;
80     static struct lbuf lbuf;
81     char buf[4096];
82     ssize_t n;
83     int i, ch, len;
84     char *line, *end;
85
86     n = read(sock, buf, sizeof(buf));
87     if (n < 0)
88         return -1;
89
90     for (i = 0; i < n; i++) {
91         ch = buf[i];
92         switch (state) {
93         case SCANNING_ID:
94             if (ch == '\n') {
95                 /* FIXME gripe unexpected! */
96                 lbuf_init(&lbuf);
97                 break;
98             }
99             if (ch != ' ') {
100                 lbuf_putc(&lbuf, ch);
101                 break;
102             }
103             line = lbuf_line(&lbuf);
104             id = strtol(line, &end, 16);
105             if (end == line || *end) {
106                 /* FIXME gripe bad id */
107                 id = -1;
108             }
109             lbuf_init(&lbuf);
110
111             switch (id) {
112             case C_PROMPT:
113             case C_FLUSH:
114             case C_EXECUTE:
115             case C_EXIT:
116             case C_FLASH:
117             case C_INFORM:
118             case C_PIPE:
119             case C_REDIR:
120                 state = BUFFERING;
121                 break;
122             default:
123                 /* unknown or unexpected id, treat like C_DATA */
124             case C_DATA:
125                 state = COPYING;
126                 break;
127             }
128             break;
129
130         case BUFFERING:
131             len = lbuf_putc(&lbuf, ch);
132             if (len) {
133                 line = lbuf_line(&lbuf);
134                 servercmd(id, line, len);
135                 lbuf_init(&lbuf);
136                 state = SCANNING_ID;
137             }
138             break;
139
140         case COPYING:
141             outch(ch);
142             if (ch == '\n')
143                 state = SCANNING_ID;
144         }
145     }
146
147     return n;
148 }
149
150 /*
151  * Receive command input from FD into INBUF.
152  * Return 1 on receipt of input, zero on EOF, -1 on error.
153  */
154 static int
155 recv_input(int fd, struct ring *inbuf)
156 {
157     static struct lbuf cmdbuf;
158     int n, i, ch;
159     char *line;
160     int res = 1;
161
162     n = ring_from_file(inbuf, fd);
163     if (n < 0)
164         return -1;
165     if (n == 0) {
166         /* EOF on input */
167         if (lbuf_len(&cmdbuf)) {
168             /* incomplete line */
169             ring_putc(inbuf, '\n');
170             n++;
171         }
172         /*
173          * Can't put EOF cookie into INBUF here, it may not fit.
174          * Leave it to caller.
175          */
176         res = 0;
177     }
178
179     /* copy input to AUXFP etc. */
180     for (i = -n; i < 0; i++) {
181         ch = ring_peek(inbuf, i);
182         assert(ch != EOF);
183         if (lbuf_putc(&cmdbuf, ch)) {
184             line = lbuf_line(&cmdbuf);
185             if (auxfp)
186                 fputs(line, auxfp);
187             save_input(line);
188             lbuf_init(&cmdbuf);
189         }
190     }
191
192     return res;
193 }
194
195 static void
196 intr(int sig)
197 {
198     send_intr = 1;
199 #ifdef _WIN32
200     signal(SIGINT, intr);
201 #endif
202 }
203
204 /*
205  * Play on SOCK.
206  * The session must be in the playing phase.
207  * Return 0 when the session ended, -1 on error.
208  */
209 int
210 play(int sock)
211 {
212     /*
213      * Player input flows from INPUT_FD through recv_input() into ring
214      * buffer INBUF, which drains into SOCK.  This must not block.
215      * Server output flows from SOCK into recv_output().  Reading SOCK
216      * must not block.
217      */
218     struct sigaction sa;
219     struct ring inbuf;          /* input buffer, draining to SOCK */
220     int eof_fd0;                /* read fd 0 hit EOF? */
221     int send_eof;               /* need to send EOF_COOKIE */
222     fd_set rdfd, wrfd;
223     int n;
224
225     sa.sa_flags = 0;
226     sa.sa_handler = intr;
227     sigaction(SIGINT, &sa, NULL);
228     sa.sa_handler = SIG_IGN;
229     sigaction(SIGPIPE, &sa, NULL);
230
231     ring_init(&inbuf);
232     eof_fd0 = send_eof = 0;
233
234     for (;;) {
235         FD_ZERO(&rdfd);
236         FD_ZERO(&wrfd);
237
238         /*
239          * Want to read player input only when we don't need to send
240          * cookies, and we haven't hit EOF on fd 0, and INBUF can
241          * accept some.
242          */
243         if (!send_intr && !send_eof && !eof_fd0 && ring_space(&inbuf))
244             FD_SET(input_fd, &rdfd);
245         /* Want to send player input only when we have something */
246         if (send_intr || send_eof || ring_len(&inbuf))
247             FD_SET(sock, &wrfd);
248         /* Always want to read server output */
249         FD_SET(sock, &rdfd);
250
251         n = select(MAX(input_fd, sock) + 1, &rdfd, &wrfd, NULL, NULL);
252         if (n < 0) {
253             if (errno != EINTR) {
254                 perror("select");
255                 return -1;
256             }
257         }
258
259         if (send_eof
260             && ring_putm(&inbuf, EOF_COOKIE, sizeof(EOF_COOKIE) - 1) >= 0)
261             send_eof = 0;
262         if (send_intr
263             && ring_putm(&inbuf, INTR_COOKIE, sizeof(INTR_COOKIE) - 1) >= 0)
264             send_intr = 0;
265
266         if (n < 0)
267             continue;
268
269         /* read player input */
270         if (FD_ISSET(input_fd, &rdfd)) {
271             n = recv_input(input_fd, &inbuf);
272             if (n < 0) {
273                 perror("read stdin"); /* FIXME stdin misleading, could be execing */
274                 return -1;
275             }
276             if (n == 0) {
277                 /* EOF on input */
278                 send_eof = 1;
279                 if (input_fd) {
280                     /* execute done, switch back to fd 0 */
281                     input_fd = 0;
282                 } else {
283                     /* stop reading input, drain socket ring buffers */
284                     eof_fd0 = 1;
285                     sa.sa_handler = SIG_DFL;
286                     sigaction(SIGINT, &sa, NULL);
287                 }
288             }
289         }
290
291         /* send it to the server */
292         if (FD_ISSET(sock, &wrfd)) {
293             n = ring_to_file(&inbuf, sock);
294             if (n < 0) {
295                 perror("write socket");
296                 return -1;
297             }
298         }
299
300         /* read server output and print it */
301         if (FD_ISSET(sock, &rdfd)) {
302             n = recv_output(sock);
303             if (n < 0) {
304                 perror("read socket");
305                 return -1;
306             }
307             if (n == 0)
308                 return 0;
309             if (input_fd < 0) {
310                 /* execute failed */
311                 input_fd = 0;
312                 send_eof = 1;
313             }
314         }
315     }
316 }