From d4ae3915f817fa25f938c5ab47cea2823ba5aaa9 Mon Sep 17 00:00:00 2001
From: Christian Heller <c.heller@plomlompom.de>
Date: Thu, 20 Nov 2014 22:53:35 +0100
Subject: [PATCH] Server: Only fflush() via send_to_outfile() when messages are
 finished.

---
 src/server/init.c          |  2 +-
 src/server/run.c           | 17 ++++++++++-------
 src/server/run.h           |  4 ++--
 src/server/thing_actions.c |  6 +++---
 4 files changed, 16 insertions(+), 13 deletions(-)

diff --git a/src/server/init.c b/src/server/init.c
index f4c191b..fefe3f3 100644
--- a/src/server/init.c
+++ b/src/server/init.c
@@ -226,7 +226,7 @@ extern uint8_t remake_world()
     world.turn = 1;
     world.do_update = 1;
     world.exists = 1;
-    send_to_outfile("NEW_WORLD\n");
+    send_to_outfile("NEW_WORLD\n", 1);
     return 0;
 }
 
diff --git a/src/server/run.c b/src/server/run.c
index ba48d8c..5537631 100644
--- a/src/server/run.c
+++ b/src/server/run.c
@@ -300,13 +300,13 @@ static uint8_t meta_commands(char * msg)
     if (!strcmp("PING", msg))
     {
         free(msg);
-        send_to_outfile("PONG\n");
+        send_to_outfile("PONG\n", 1);
         return 1;
     }
     if (!strcmp("STACK", msg))
     {
         free(msg);
-        send_to_outfile("THINGS_BELOW_PLAYER START\n");
+        send_to_outfile("THINGS_BELOW_PLAYER START\n", 1);
         struct Thing * player = get_player();
         struct Thing * t;
         for (t = world.things; t; t = t->next)
@@ -315,11 +315,11 @@ static uint8_t meta_commands(char * msg)
                 && t != player)
             {
                 struct ThingType * tt = get_thing_type(t->type);
-                send_to_outfile(tt->name);
-                send_to_outfile("\n");
+                send_to_outfile(tt->name, 0);
+                send_to_outfile("\n", 1);
             }
         }
-        send_to_outfile("THINGS_BELOW_PLAYER END\n");
+        send_to_outfile("THINGS_BELOW_PLAYER END\n", 1);
         return 1;
     }
     return 0;
@@ -327,10 +327,13 @@ static uint8_t meta_commands(char * msg)
 
 
 
-extern void send_to_outfile(char * answer)
+extern void send_to_outfile(char * answer, uint8_t flush)
 {
     try_fwrite(answer, strlen(answer), 1, world.file_out, __func__);
-    fflush(world.file_out);
+    if (flush)
+    {
+        fflush(world.file_out);
+    }
 }
 
 
diff --git a/src/server/run.h b/src/server/run.h
index bd13d1d..26a0c2f 100644
--- a/src/server/run.h
+++ b/src/server/run.h
@@ -14,8 +14,8 @@
 
 
 
-/* Append "answer" to server output file, with instant fflush(). */
-extern void send_to_outfile(char * answer);
+/* Append "answer" to server output file, with instant fflush() if "flush". */
+extern void send_to_outfile(char * answer, uint8_t flush);
 
 /* Record save and record file data. Both are only written if "force" is set, or
  * on the first run with unset "force", or if 15 seconds have passed since the
diff --git a/src/server/thing_actions.c b/src/server/thing_actions.c
index 4889e5f..3542254 100644
--- a/src/server/thing_actions.c
+++ b/src/server/thing_actions.c
@@ -48,9 +48,9 @@ static void playerbonus_use(uint8_t no_thing, uint8_t wrong_thing);
 
 static void update_log(char * text)
 {
-    send_to_outfile("LOG ");
-    send_to_outfile(text);
-    send_to_outfile("\n");
+    send_to_outfile("LOG ", 0);
+    send_to_outfile(text, 0);
+    send_to_outfile("\n", 1);
 }
 
 
-- 
2.30.2