home · contact · privacy
Client: Abort test_ping_pong() earlier when server known to be active.
[plomrogue] / src / client / io.c
index d0b9dc022b303f1e61de8a12d64e140c2273c8af..b3724b5ffaf566cc51322d023961f2a945c2aa7a 100644 (file)
@@ -1,4 +1,9 @@
-/* src/client/io.c */
+/* src/client/io.c
+ *
+ * This file is part of PlomRogue. PlomRogue is licensed under the GPL version 3
+ * or any later version. For details on its copyright, license, and warranties,
+ * see the file NOTICE in the root directory of the PlomRogue source package.
+ */
 
 #define _POSIX_C_SOURCE 1 /* PIPE_BUF */
 #include "io.h"
  */
 static void read_inventory(char * read_buf, uint32_t linemax, FILE * file);
 
-/* Read the next characters in "file" into world.map.cells. In detail: Read
+/* Read the next characters in "file" into "map". In detail: Read
  * world.map.length times world.map.length characters, followed by one ignored
  * character (that we assume is a newline).
  */
-static void read_map_cells(FILE * file);
+static void read_map_cells(FILE * file, char ** map);
 
 /* Repeatedly use try_fgets() with given arguments to read the remaining lines
  * of "file" into the world.log string.
@@ -98,7 +103,7 @@ static void read_inventory(char * read_buf, uint32_t linemax, FILE * file)
             break;
         }
         int old_size = 0;
-        if (NULL != world.player_inventory)
+        if (world.player_inventory)
         {
             old_size = strlen(world.player_inventory);
         }
@@ -116,17 +121,22 @@ static void read_inventory(char * read_buf, uint32_t linemax, FILE * file)
 
 
 
-static void read_map_cells(FILE * file)
+static void read_map_cells(FILE * file, char ** map)
 {
-    free(world.map.cells);
-    world.map.cells = try_malloc(world.map.length * world.map.length, __func__);
+    if (*map)
+    {
+        free(*map);
+        *map = NULL;
+    }
+    *map = try_malloc(world.map.length * world.map.length, __func__);
+    char * map_cells = *map;
     uint16_t y, x;
     for (y = 0; y < world.map.length; y++)
     {
         for (x = 0; x < world.map.length; x++)
         {
             char c = try_fgetc(file, __func__);
-            world.map.cells[(y * world.map.length) + x] = c;
+            map_cells[y * world.map.length + x] = c;
         }
         try_fgetc(file, __func__);
     }
@@ -141,7 +151,7 @@ static void read_log(char * read_buf, uint32_t linemax, FILE * file)
     while (try_fgets(read_buf, linemax + 1, file, __func__))
     {
         int old_size = 0;
-        if (NULL != world.log)
+        if (world.log)
         {
             old_size = strlen(world.log);
         }
@@ -213,7 +223,8 @@ static uint8_t read_world()
         first_read = 0;
     }
     world.map.length = read_value_from_line(read_buf, linemax, file);
-    read_map_cells(file);
+    read_map_cells(file, &world.map.cells);
+    read_map_cells(file, &world.mem_map);
     read_log(read_buf, linemax, file);
     free(read_buf);
     try_fclose(file, __func__);
@@ -226,9 +237,10 @@ static void test_ping_pong(time_t last_server_answer_time)
 {
     static uint8_t ping_sent = 0;
     time_t now = time(0);
-    if (ping_sent && last_server_answer_time > now - 3)
-    {
-        ping_sent = 0;
+    if (ping_sent && last_server_answer_time > now - 3)  /* Re-set if last    */
+    {                                                    /* ping was answered */
+        ping_sent = 0;                                   /* with server       */
+        return;                                          /* activity.         */
     }
     if (!ping_sent && last_server_answer_time < now - 3)
     {