3 #define _POSIX_C_SOURCE 1 /* PIPE_BUF */
5 #include <limits.h> /* PIPE_BUF */
6 #include <ncurses.h> /* halfdelay(), getch() */
7 #include <stddef.h> /* NULL */
8 #include <stdint.h> /* uint8_t, uint16_t, uint32_t */
9 #include <stdio.h> /* FILE, sprintf(), fseek(), fflush() */
10 #include <string.h> /* strcmp(), strlen(), memcpy() */
11 #include <stdlib.h> /* free(), atoi() */
12 #include <sys/stat.h> /* stat() */
13 #include <sys/types.h> /* time_t */
14 #include <time.h> /* time() */
15 #include <unistd.h> /* access() */
16 #include "../common/try_malloc.h" /* try_malloc() */
17 #include "../common/rexit.h" /* exit_trouble(), exit_err() */
18 #include "../common/readwrite.h" /* try_fopen(), try_fclose(), try_fgets(),
19 * try_fgetc(), textfile_width(), try_fputc()
21 #include "control.h" /* try_key() */
22 #include "map.h" /* map_center() */
23 #include "windows.h" /* reset_windows_on_winch(), draw_all_wins() */
24 #include "world.h" /* world global */
28 /* Read next lines of "file" up to (and excluding) a line "%\n" into the
29 * world.player_inventory string.
31 static void read_inventory(char * read_buf, uint32_t linemax, FILE * file);
33 /* Read the next characters in "file" into world.map.cells. In detail: Read
34 * world.map.length times world.map.length characters, followed by one ignored
35 * character (that we assume is a newline).
37 static void read_map_cells(FILE * file);
39 /* Repeatedly use try_fgets() with given arguments to read the remaining lines
40 * of "file" into the world.log string.
42 static void read_log(char * read_buf, uint32_t linemax, FILE * file);
44 /* Return value seen by atoi() in next line of "file" when passed to try_fgets()
45 * with the given arguments.
47 static uint16_t read_value_from_line(char * read_buf, uint32_t linemax,
50 /* If the server's worldstate file has changed since the last read_world(),
51 * return a pointer to its file descriptor; else, return NULL.
53 * Two tests are performed to check for a file change. The file's last data
54 * modification time in seconds via stat() is compared against world.last_update
55 * (and if it is changed, world.last_update is re-set to it). If this does not
56 * verify a change, the first bytes of the file are read to compare the game
57 * turn number described therein to the last read turn number in world.turn.
59 * The stat() check is mostly useless, for it only detects file updates once a
60 * second. But the turn check fails if a new world is generated from turn 1 on:
61 * the new world also starts in turn 1, not signifying any world change to the
62 * turn check. The stat() check detects this change with at most 1 second delay.
64 static FILE * changed_worldstate_file(char * path);
66 /* Attempt to read the server's worldstate file as representation of the game
67 * world in a hard-coded serialization format. Returns 1 on success and 0 if the
68 * out file wasn't read for supposedly not having changed since a last
71 * map_center() is triggered by either, the first successful read_world() (thus
72 * on client start), or on turn 1 (thus on world start).
74 static uint8_t read_world();
76 /* If "last_server_answer_time" is too old, send a PING to the server; or, if a
77 * previous PING has not sparked any answer after a while, abort the client.
79 static void test_ping_pong(time_t last_server_answer_time);
81 /* Update "last_server_answer_time" if new stuff has been written to the
84 static void test_server_activity(time_t * last_server_answer_time);
88 static void read_inventory(char * read_buf, uint32_t linemax, FILE * file)
90 char * f_name = "read_inventory()";
91 char * delimiter = "%\n";
92 free(world.player_inventory);
93 world.player_inventory = NULL; /* Avoids illegal strlen() below. */
96 try_fgets(read_buf, linemax + 1, file, f_name);
97 if (!(strcmp(read_buf, delimiter)))
102 if (NULL != world.player_inventory)
104 old_size = strlen(world.player_inventory);
106 int new_size = strlen(read_buf);
107 char * new_inventory = try_malloc(old_size + new_size + 1, f_name);
108 memcpy(new_inventory, world.player_inventory, old_size);
109 int test = sprintf(new_inventory + old_size, "%s", read_buf);
110 exit_trouble(test < 0, f_name, "sprintf()");
111 free(world.player_inventory);
112 world.player_inventory = new_inventory;
114 world.player_inventory[strlen(world.player_inventory) - 1] = '\0';
115 world.player_inventory_select = 0;
120 static void read_map_cells(FILE * file)
122 char * f_name = "read_map_cells()";
123 free(world.map.cells);
124 world.map.cells = try_malloc(world.map.length * world.map.length, f_name);
126 for (y = 0; y < world.map.length; y++)
128 for (x = 0; x < world.map.length; x++)
130 char c = try_fgetc(file, f_name);
131 world.map.cells[(y * world.map.length) + x] = c;
133 try_fgetc(file, f_name);
139 static void read_log(char * read_buf, uint32_t linemax, FILE * file)
141 char * f_name = "read_log()";
144 while (try_fgets(read_buf, linemax + 1, file, f_name))
147 if (NULL != world.log)
149 old_size = strlen(world.log);
151 int new_size = strlen(read_buf);
152 char * new_log = try_malloc(old_size + new_size + 1, f_name);
153 memcpy(new_log, world.log, old_size);
154 int test = sprintf(new_log + old_size, "%s", read_buf);
155 exit_trouble(test < 0, f_name, "sprintf()");
163 static uint16_t read_value_from_line(char * read_buf, uint32_t linemax,
166 char * f_name = "read_value_from_line()";
167 try_fgets(read_buf, linemax + 1, file, f_name);
168 return atoi(read_buf);
173 static FILE * changed_worldstate_file(char * path)
175 char * f_name = "changed_worldstate_file()";
176 struct stat stat_buf;
177 exit_trouble(stat(path, &stat_buf), f_name, "stat()");
178 if (stat_buf.st_mtime != world.last_update)
180 world.last_update = stat_buf.st_mtime;
181 return try_fopen(path, "r", f_name);
183 FILE * file = try_fopen(path, "r", f_name);
185 try_fgets(turn_string, 6, file, f_name);
186 if (world.turn == atoi(turn_string))
188 try_fclose(file, f_name);
191 exit_trouble(fseek(file, 0, SEEK_SET), f_name, "fseek()");
197 static uint8_t read_world()
199 char * f_name = "read_world()";
200 char * path = "server/worldstate";
201 char * quit_msg = "No worldstate file found to read. Server may be down.";
202 static uint8_t first_read = 1;
203 exit_err(access(path, F_OK), quit_msg);
204 FILE * file = changed_worldstate_file(path);
209 uint32_t linemax = textfile_width(file);
210 char * read_buf = try_malloc(linemax + 1, f_name);
211 world.turn = read_value_from_line(read_buf, linemax, file);
212 world.player_lifepoints = read_value_from_line(read_buf, linemax, file);
213 read_inventory(read_buf, linemax, file);
214 world.player_pos.y = read_value_from_line(read_buf, linemax, file);
215 world.player_pos.x = read_value_from_line(read_buf, linemax, file);
216 if (1 == world.turn || first_read)
221 world.map.length = read_value_from_line(read_buf, linemax, file);
222 read_map_cells(file);
223 read_log(read_buf, linemax, file);
225 try_fclose(file, f_name);
231 static void test_ping_pong(time_t last_server_answer_time)
233 static uint8_t ping_sent = 0;
234 time_t now = time(0);
235 if (ping_sent && last_server_answer_time > now - 3)
239 if (!ping_sent && last_server_answer_time < now - 3)
245 exit_err(last_server_answer_time < now - 6, "Server not answering.");
250 static void test_server_activity(time_t * last_server_answer_time)
252 char * f_name = "test_server_activity()";
253 int test = try_fgetc(world.file_server_out, f_name);
262 while (EOF != (test = try_fgetc(world.file_server_out, f_name)));
263 * last_server_answer_time = time(0);
268 extern void send(char * msg)
270 char * f_name = "send()";
271 uint32_t msg_size = strlen(msg) + 1;
272 char * err = "send() tries to send message larger than PIPE_BUF bytes.";
273 exit_err(msg_size > PIPE_BUF, err);
274 try_fwrite(msg, strlen(msg), 1, world.file_server_in, f_name);
275 try_fputc('\n', world.file_server_in, f_name);
276 fflush(world.file_server_in);
281 extern char * io_loop()
283 world.halfdelay = 1; /* Ensures read_world() is only called 10 */
284 halfdelay(world.halfdelay); /* times a second during user inactivity. */
285 uint8_t change_in_client = 0;
286 uint16_t last_focused_turn = world.turn;
287 time_t last_server_answer_time = time(0);
290 test_server_activity(&last_server_answer_time);
291 test_ping_pong(last_server_answer_time);
294 reset_windows_on_winch();
298 if (change_in_client || read_world())
300 if (world.turn != last_focused_turn && world.focus_each_turn)
302 last_focused_turn = world.turn;
307 change_in_client = 0;
311 change_in_client = try_key((uint16_t) key);
312 if (2 == change_in_client)
319 return "Sent QUIT to server.";