Rewrite recvclient() for clarity

Behavior differs for the following scenario: if, while the thread
sleeps in io_input() called from recvclient(), at least one line of
input arrives and the thread gets awakened by the update aborting
commands, then the old code throws away the first line of input, but
the new code doesn't.
This commit is contained in:
Markus Armbruster 2008-04-29 21:28:28 +02:00
parent 8209b88a54
commit f3202225f2

View file

@ -52,22 +52,24 @@
* is "ctld", set the player's eof flag and return -1. * is "ctld", set the player's eof flag and return -1.
* Else return the length of the line. * Else return the length of the line.
* Design bug: there is no way to indicate truncation of a long line. * Design bug: there is no way to indicate truncation of a long line.
* FIXME caller needs to know whether a line was consumed, to prompt correctly
*/ */
int int
recvclient(char *cmd, int size) recvclient(char *cmd, int size)
{ {
int count; int count;
if (player->eof) count = -1;
return -1; while (!player->aborted && !player->eof) {
if (player->aborted) /* Try to get a line of input */
return -2; count = io_gets(player->iop, cmd, size);
if (count >= 0) {
count = io_gets(player->iop, cmd, size); /* got it */
if (strcmp(cmd, "ctld") == 0)
while (!player->aborted && !player->eof && count < 0) { player->eof = 1;
/* Sleep for input */ if (strcmp(cmd, "aborted") == 0)
player->aborted = 1;
break;
}
/* Make sure player sees prompt */ /* Make sure player sees prompt */
io_output_all(player->iop); io_output_all(player->iop);
@ -77,22 +79,14 @@ recvclient(char *cmd, int size)
* abortion, we must return without blocking in io_input(). * abortion, we must return without blocking in io_input().
*/ */
if (player->aborted) if (player->aborted)
return -2; break;
/* Await more input */
io_input(player->iop, IO_WAIT); io_input(player->iop, IO_WAIT);
if (io_error(player->iop)) if (io_error(player->iop))
player->aborted = 1; player->aborted = 1;
else if (io_eof(player->iop)) else if (io_eof(player->iop))
player->eof = 1; player->eof = 1;
else
count = io_gets(player->iop, cmd, size);
}
if (count > 0) {
if (strcmp(cmd, "ctld") == 0)
player->eof = 1;
if (strcmp(cmd, "aborted") == 0)
player->aborted = 1;
} }
if (player->eof) if (player->eof)