home · contact · privacy
d0b9dc022b303f1e61de8a12d64e140c2273c8af
[plomrogue] / src / client / io.c
1 /* src/client/io.c */
2
3 #define _POSIX_C_SOURCE 1 /* PIPE_BUF */
4 #include "io.h"
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()
20                                   */
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 */
25
26
27
28 /* Read next lines of "file" up to (and excluding) a line "%\n" into the
29  * world.player_inventory string.
30  */
31 static void read_inventory(char * read_buf, uint32_t linemax, FILE * file);
32
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).
36  */
37 static void read_map_cells(FILE * file);
38
39 /* Repeatedly use try_fgets() with given arguments to read the remaining lines
40  * of "file" into the world.log string.
41  */
42 static void read_log(char * read_buf, uint32_t linemax, FILE * file);
43
44 /* Return value seen by atoi() in next line of "file" when passed to try_fgets()
45  * with the given arguments.
46  */
47 static uint16_t read_value_from_line(char * read_buf, uint32_t linemax,
48                                      FILE * file);
49
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.
52  *
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.
58  *
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.
63  */
64 static FILE * changed_worldstate_file(char * path);
65
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
69  * read_world() call.
70  *
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).
73  */
74 static uint8_t read_world();
75
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.
78  */
79 static void test_ping_pong(time_t last_server_answer_time);
80
81 /* Update "last_server_answer_time" if new stuff has been written to the
82  * server's out file.
83  */
84 static void test_server_activity(time_t * last_server_answer_time);
85
86
87
88 static void read_inventory(char * read_buf, uint32_t linemax, FILE * file)
89 {
90     char * delimiter = "%\n";
91     free(world.player_inventory);
92     world.player_inventory = NULL;          /* Avoids illegal strlen() below. */
93     while (1)
94     {
95         try_fgets(read_buf, linemax + 1, file, __func__);
96         if (!(strcmp(read_buf, delimiter)))
97         {
98             break;
99         }
100         int old_size = 0;
101         if (NULL != world.player_inventory)
102         {
103             old_size = strlen(world.player_inventory);
104         }
105         int new_size = strlen(read_buf);
106         char * new_inventory = try_malloc(old_size + new_size + 1, __func__);
107         memcpy(new_inventory, world.player_inventory, old_size);
108         int test = sprintf(new_inventory + old_size, "%s", read_buf);
109         exit_trouble(test < 0, __func__, "sprintf");
110         free(world.player_inventory);
111         world.player_inventory = new_inventory;
112     }
113     world.player_inventory[strlen(world.player_inventory) - 1] = '\0';
114     world.player_inventory_select = 0;
115 }
116
117
118
119 static void read_map_cells(FILE * file)
120 {
121     free(world.map.cells);
122     world.map.cells = try_malloc(world.map.length * world.map.length, __func__);
123     uint16_t y, x;
124     for (y = 0; y < world.map.length; y++)
125     {
126         for (x = 0; x < world.map.length; x++)
127         {
128             char c = try_fgetc(file, __func__);
129             world.map.cells[(y * world.map.length) + x] = c;
130         }
131         try_fgetc(file, __func__);
132     }
133 }
134
135
136
137 static void read_log(char * read_buf, uint32_t linemax, FILE * file)
138 {
139     free(world.log);
140     world.log = NULL;
141     while (try_fgets(read_buf, linemax + 1, file, __func__))
142     {
143         int old_size = 0;
144         if (NULL != world.log)
145         {
146             old_size = strlen(world.log);
147         }
148         int new_size = strlen(read_buf);
149         char * new_log = try_malloc(old_size + new_size + 1, __func__);
150         memcpy(new_log, world.log, old_size);
151         int test = sprintf(new_log + old_size, "%s", read_buf);
152         exit_trouble(test < 0, __func__, "sprintf");
153         free(world.log);
154         world.log = new_log;
155     }
156 }
157
158
159
160 static uint16_t read_value_from_line(char * read_buf, uint32_t linemax,
161                                      FILE * file)
162 {
163     try_fgets(read_buf, linemax + 1, file, __func__);
164     return atoi(read_buf);
165 }
166
167
168
169 static FILE * changed_worldstate_file(char * path)
170 {
171     struct stat stat_buf;
172     exit_trouble(stat(path, &stat_buf), __func__, "stat");
173     if (stat_buf.st_mtime != world.last_update)
174     {
175         world.last_update = stat_buf.st_mtime;
176         return try_fopen(path, "r", __func__);
177     }
178     FILE * file = try_fopen(path, "r", __func__);
179     char turn_string[6];
180     try_fgets(turn_string, 6, file, __func__);
181     if (world.turn == atoi(turn_string))
182     {
183         try_fclose(file, __func__);
184         return NULL;
185     }
186     exit_trouble(fseek(file, 0, SEEK_SET), __func__, "fseek");
187     return file;
188 }
189
190
191
192 static uint8_t read_world()
193 {
194     char * path = "server/worldstate";
195     char * quit_msg = "No worldstate file found to read. Server may be down.";
196     static uint8_t first_read = 1;
197     exit_err(access(path, F_OK), quit_msg);
198     FILE * file = changed_worldstate_file(path);
199     if (!file)
200     {
201         return 0;
202     }
203     uint32_t linemax = textfile_width(file);
204     char * read_buf = try_malloc(linemax + 1, __func__);
205     world.turn = read_value_from_line(read_buf, linemax, file);
206     world.player_lifepoints = read_value_from_line(read_buf, linemax, file);
207     read_inventory(read_buf, linemax, file);
208     world.player_pos.y = read_value_from_line(read_buf, linemax, file);
209     world.player_pos.x = read_value_from_line(read_buf, linemax, file);
210     if (1 == world.turn || first_read)
211     {
212         map_center();
213         first_read = 0;
214     }
215     world.map.length = read_value_from_line(read_buf, linemax, file);
216     read_map_cells(file);
217     read_log(read_buf, linemax, file);
218     free(read_buf);
219     try_fclose(file, __func__);
220     return 1;
221 }
222
223
224
225 static void test_ping_pong(time_t last_server_answer_time)
226 {
227     static uint8_t ping_sent = 0;
228     time_t now = time(0);
229     if (ping_sent && last_server_answer_time > now - 3)
230     {
231         ping_sent = 0;
232     }
233     if (!ping_sent && last_server_answer_time < now - 3)
234     {
235         send("PING");
236         ping_sent = 1;
237         return;
238     }
239     exit_err(last_server_answer_time < now - 6, "Server not answering.");
240 }
241
242
243
244 static void test_server_activity(time_t * last_server_answer_time)
245 {
246     int test = try_fgetc(world.file_server_out, __func__);
247     if (EOF == test)
248     {
249         return;
250     }
251     do
252     {
253         ;
254     }
255     while (EOF != (test = try_fgetc(world.file_server_out, __func__)));
256     * last_server_answer_time = time(0);
257 }
258
259
260
261 extern void send(char * msg)
262 {
263     uint32_t msg_size = strlen(msg) + 1;
264     char * err = "send() tries to send message larger than PIPE_BUF bytes.";
265     exit_err(msg_size > PIPE_BUF, err);
266     try_fwrite(msg, strlen(msg), 1, world.file_server_in, __func__);
267     try_fputc('\n', world.file_server_in, __func__);
268     fflush(world.file_server_in);
269 }
270
271
272
273 extern char * io_loop()
274 {
275     world.halfdelay = 1;            /* Ensures read_world() is only called 10 */
276     halfdelay(world.halfdelay);     /* times a second during user inactivity. */
277     uint8_t change_in_client = 0;
278     uint16_t last_focused_turn = world.turn;
279     time_t last_server_answer_time = time(0);
280     while (1)
281     {
282         test_server_activity(&last_server_answer_time);
283         test_ping_pong(last_server_answer_time);
284         if (world.winch)
285         {
286             reset_windows_on_winch();
287             world.winch = 0;
288             change_in_client++;
289         }
290         if (change_in_client || read_world())
291         {
292             if (world.turn != last_focused_turn && world.focus_each_turn)
293             {
294                 last_focused_turn = world.turn;
295                 map_center();
296             }
297             draw_all_wins();
298         }
299         change_in_client = 0;
300         int key = getch();
301         if (ERR != key)
302         {
303             change_in_client = try_key((uint16_t) key);
304             if (2 == change_in_client)
305             {
306                 break;
307             }
308         }
309     }
310     send("QUIT");
311     return "Sent QUIT to server.";
312 }