]> git.pond.sub.org Git - empserver/blob - src/lib/player/recvclient.c
Move queue flush out of io.c
[empserver] / src / lib / player / recvclient.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2009, 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  *  recvclient.c: Receive input from the client
29  *
30  *  Known contributors to this file:
31  *     Dave Pare, 1986
32  *     Markus Armbruster, 2006-2009
33  *     Ron Koenderink, 2009
34  */
35
36 #include <config.h>
37
38 #include "empio.h"
39 #include "journal.h"
40 #include "player.h"
41 #include "prototypes.h"
42 #include "server.h"
43
44 /*
45  * Receive a line of input from the current player.
46  * If the player's eof flag is set, return -1 without receiving input.
47  * If the player's aborted flag is set, return -2 without receiving
48  * input.
49  * Else receive one line and store it in CMD[SIZE].
50  * This may block for input, yielding the processor.  Flush buffered
51  * output when blocking, to make sure player sees the prompt.
52  * If the player's connection has the I/O error or EOF indicator set,
53  * or the line is "ctld", set the player's eof and aborted flag and
54  * return -1.
55  * If the line is "aborted", set the player's aborted flag and return
56  * -2.
57  * Else return the length of the line.
58  * Design bug: there is no way to indicate truncation of a long line.
59  */
60 int
61 recvclient(char *cmd, int size)
62 {
63     int count, res;
64
65     count = -1;
66     while (!player->aborted) {
67         /* Try to get a line of input */
68         count = io_gets(player->iop, cmd, size);
69         if (count >= 0) {
70             /* got it */
71             if (strcmp(cmd, "ctld") == 0)
72                 player->aborted = player->eof = 1;
73             if (strcmp(cmd, "aborted") == 0)
74                 player->aborted = 1;
75             journal_input(cmd);
76             break;
77         }
78
79         /*
80          * Flush all queued output before potentially sleeping in
81          * io_input(), to make sure player sees the prompt.
82          */
83         while (io_output(player->iop, !play_wrlock_wanted) > 0) ;
84
85         /*
86          * If io_output_all() blocked and got unblocked by command
87          * abortion, we must return without blocking in io_input().
88          */
89         if (player->aborted)
90             break;
91
92         res = io_input(player->iop, 1);
93         if (res > 0)
94             ;
95         else if (res < 0)
96             player->aborted = player->eof = 1;
97         else if (io_eof(player->iop))
98             player->aborted = player->eof = 1;
99         else if (!player->aborted) {
100             pr_flash(player, "idle connection terminated\n");
101             player->aborted = player->eof = 1;
102         }
103     }
104
105     if (player->aborted) {
106         player->recvfail++;
107         if (player->recvfail > 255) {
108             /*
109              * Looks like the thread is stuck in a loop that fails to
110              * check errors; oops once, then slow it down drastically.
111              */
112             CANT_HAPPEN(player->recvfail == 256);
113             empth_sleep(time(NULL) + 60);
114         }
115         return player->eof ? -1 : -2;
116     }
117
118     player->recvfail = 0;
119     return count;
120 }