From: Christian Heller <c.heller@plomlompom.de>
Date: Thu, 13 Nov 2014 22:30:44 +0000 (+0100)
Subject: Server: In get_message_from_queue(), retry if 1st chunk starts with \0.
X-Git-Tag: tce~603
X-Git-Url: https://plomlompom.com/repos/%7B%7Bprefix%7D%7D/%7B%7B%20web_path%20%7D%7D/static/%7B%7Bdb.prefix%7D%7D/test.html?a=commitdiff_plain;h=27183012c54fb7a85cfad4d824c4fc020ee11619;p=plomrogue

Server: In get_message_from_queue(), retry if 1st chunk starts with \0.
---

diff --git a/src/server/io.c b/src/server/io.c
index 0f7c3a8..c2a3354 100644
--- a/src/server/io.c
+++ b/src/server/io.c
@@ -43,12 +43,12 @@ static void write_thing(FILE * file, struct Thing * t);
 /* Cut out and return first \0-terminated string from world.queue and
  * appropriately reduce world.queue_size. Return NULL if queue is empty.
  * Superfluous \0 bytes after the string are also cut out. Should the queue
- * start with \0 bytes, those are cut out, but NULL is returned instead of "".
-*/
+ * start with \0 bytes, those are cut out before returning anything after them.
+ */
 static char * get_message_from_queue();
 
 /* Poll input file for world.queue input. Wait a few seconds until giving up;
- * poll only every 0.03 seconds.. Translate '\n' chars in input file into '\0'.
+ * poll only every 0.03 seconds. Translate '\n' chars in input file into '\0'.
  */
 static void read_file_into_queue();
 
@@ -193,6 +193,7 @@ static char * get_message_from_queue()
     if (world.queue_size)
     {
         size_t cutout_len = strlen(world.queue);
+        uint8_t is_nullbyte_chunk = !cutout_len;
         if (0 < cutout_len)
         {
             cutout_len++;
@@ -214,6 +215,10 @@ static char * get_message_from_queue()
             memcpy(new_queue, &(world.queue[cutout_len]), world.queue_size);
             free(world.queue);
             world.queue = new_queue;
+            if (is_nullbyte_chunk)
+            {
+                return get_message_from_queue();
+            }
         }
     }
     return message;
diff --git a/src/server/io.h b/src/server/io.h
index 6f59bd0..5b67f6b 100644
--- a/src/server/io.h
+++ b/src/server/io.h
@@ -23,9 +23,9 @@
  * Each such call cuts off bytes from the beginning of world.queue, up to and
  * including the last \0 byte that is followed by a non-\0 byte or ends the
  * queue. If the queue starts with a \0 byte, it and its \0 followers are cut
- * and a NULL pointer is returned. Reading from the input file stops only when
- * one or more byte were read and the next read returns 0 bytes. If the
- * re-filled queue does not end in a \0 byte, a \0 byte is appended to it.
+ * before returning anything after them. Reading from the input file stops only
+ * at its end or when one or more byte were read and the next read returns 0
+ * bytes. If the re-filled queue doesn't end in \0, a \0 byte is appended to it.
  */
 extern char * io_round();