]> git.pond.sub.org Git - empserver/commitdiff
Rewrite recvclient() for clarity
authorMarkus Armbruster <armbru@pond.sub.org>
Tue, 29 Apr 2008 19:28:28 +0000 (21:28 +0200)
committerMarkus Armbruster <armbru@pond.sub.org>
Tue, 29 Apr 2008 19:35:02 +0000 (21:35 +0200)
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.

src/lib/player/recvclient.c

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