home · contact · privacy
Got rid of misc.h. Split off remains into array_append.h and control.h.
[plomrogue] / src / client / io.c
1 /* src/client/io.c */
2
3 #include "io.h"
4 #include <limits.h> /* PIPE_BUF */
5 #include <ncurses.h> /* halfdelay(), getch() */
6 #include <stddef.h> /* NULL */
7 #include <stdint.h> /* uint8_t, uint16_t, uint32_t */
8 #include <stdio.h> /* FILE, sprintf(), fseek(), fflush() */
9 #include <string.h> /* strcmp(), strlen(), memcpy() */
10 #include <stdlib.h> /* free(), atoi() */
11 #include <sys/stat.h> /* stat() */
12 #include <sys/types.h> /* time_t */
13 #include <time.h> /* time() */
14 #include <unistd.h> /* access() */
15 #include "../common/try_malloc.h" /* try_malloc() */
16 #include "../common/rexit.h" /* exit_trouble(), exit_err() */
17 #include "../common/readwrite.h" /* try_fopen(), try_fclose(), try_fgets(),
18                                   * try_fgetc(), textfile_width(), try_fputc()
19                                   */
20 #include "control.h" /* try_key() */
21 #include "map.h" /* map_center() */
22 #include "windows.h" /* reset_windows_on_winch(), draw_all_wins() */
23 #include "world.h" /* world global */
24
25
26
27 /* Read next lines of "file" up to (and excluding) a line "%\n" into the
28  * world.player_inventory string.
29  */
30 static void read_inventory(char * read_buf, uint32_t linemax, FILE * file);
31
32 /* Read the next characters in "file" into world.map.cells. In detail: Read
33  * world.map.size.y times world.map.size.x characters, followed by one ignored
34  * character (that we assume is a newline).
35  */
36 static void read_map_cells(FILE * file);
37
38 /* Repeatedly use try_fgets() with given arguments to read the remaining lines
39  * of "file" into the world.log string.
40  */
41 static void read_log(char * read_buf, uint32_t linemax, FILE * file);
42
43 /* Return value seen by atoi() in next line of "file" when passed to try_fgets()
44  * with the given arguments.
45  */
46 static uint16_t read_value_from_line(char * read_buf, uint32_t linemax,
47                                      FILE * file);
48
49 /* If the server's worldstate file has changed since the last read_world(),
50  * return a pointer to its file descriptor; else, return NULL.
51  *
52  * Two tests are performed to check for a file change. The file's last data
53  * modification time in seconds via stat() is compared against world.last_update
54  * (and if it is changed, world.last_update is re-set to it). If this does not
55  * verify a change, the first bytes of the file are read to compare the game
56  * turn number described therein to the last read turn number in world.turn.
57  *
58  * The stat() check is mostly useless, for it only detects file updates once a
59  * second. But the turn check fails if a new world is generated from turn 1 on:
60  * the new world also starts in turn 1, not signifying any world change to the
61  * turn check. The stat() check detects this change with at most 1 second delay.
62  */
63 static FILE * changed_worldstate_file(char * path);
64
65 /* Attempt to read the server's worldstate file as representation of the game
66  * world in a hard-coded serialization format. Returns 1 on success and 0 if the
67  * out file wasn't read for supposedly not having changed since a last
68  * read_world() call.
69  *
70  * map_center() is triggered by the first successful read_world() or on turn 1,
71  * so the client focuses the map window on the player on client and world start.
72  */
73 static uint8_t read_world();
74
75 /* If "last_server_answer_time" is too old, send a PING to the server; or, if a
76  * previous PING has not sparked any answer after a while, abort the client.
77  */
78 static void test_ping_pong(time_t last_server_answer_time);
79
80 /* Update "last_server_answer_time" if new stuff has been written to the
81  * server's out file.
82  */
83 static void test_server_activity(time_t * last_server_answer_time);
84
85
86
87 static void read_inventory(char * read_buf, uint32_t linemax, FILE * file)
88 {
89     char * f_name = "read_inventory()";
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, f_name);
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, f_name);
107         memcpy(new_inventory, world.player_inventory, old_size);
108         sprintf(new_inventory + old_size, "%s", read_buf);
109         free(world.player_inventory);
110         world.player_inventory = new_inventory;
111     }
112     world.player_inventory[strlen(world.player_inventory) - 1] = '\0';
113     world.player_inventory_select = 0;
114 }
115
116
117
118 static void read_map_cells(FILE * file)
119 {
120     char * f_name = "read_map_cells()";
121     free(world.map.cells);
122     world.map.cells = try_malloc(world.map.size.y * world.map.size.x, f_name);
123     uint16_t y, x;
124     for (y = 0; y < world.map.size.y; y++)
125     {
126         for (x = 0; x < world.map.size.x; x++)
127         {
128             char c = try_fgetc(file, f_name);
129             world.map.cells[(y * world.map.size.x) + x] = c;
130         }
131         try_fgetc(file, f_name);
132     }
133 }
134
135
136
137 static void read_log(char * read_buf, uint32_t linemax, FILE * file)
138 {
139     char * f_name = "read_log()";
140     free(world.log);
141     world.log = NULL;
142     while (try_fgets(read_buf, linemax + 1, file, f_name))
143     {
144         int old_size = 0;
145         if (NULL != world.log)
146         {
147             old_size = strlen(world.log);
148         }
149         int new_size = strlen(read_buf);
150         char * new_log = try_malloc(old_size + new_size + 1, f_name);
151         memcpy(new_log, world.log, old_size);
152         sprintf(new_log + old_size, "%s", read_buf);
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     char * f_name = "read_value_from_line()";
164     try_fgets(read_buf, linemax + 1, file, f_name);
165     return atoi(read_buf);
166 }
167
168
169
170 static FILE * changed_worldstate_file(char * path)
171 {
172     char * f_name = "changed_worldstate_file()";
173     struct stat stat_buf;
174     exit_trouble(stat(path, &stat_buf), f_name, "stat()");
175     if (stat_buf.st_mtime != world.last_update)
176     {
177         world.last_update = stat_buf.st_mtime;
178         return try_fopen(path, "r", f_name);
179     }
180     FILE * file = try_fopen(path, "r", f_name);
181     char turn_string[6];
182     try_fgets(turn_string, 6, file, f_name);
183     if (world.turn == atoi(turn_string))
184     {
185         try_fclose(file, f_name);
186         return NULL;
187     }
188     exit_trouble(fseek(file, 0, SEEK_SET), f_name, "fseek()");
189     return file;
190 }
191
192
193
194 static uint8_t read_world()
195 {
196     char * f_name = "read_world()";
197     char * path = "server/worldstate";
198     char * quit_msg = "No worldstate file found to read. Server may be down.";
199     static uint8_t first_read = 1;
200     exit_err(access(path, F_OK), quit_msg);
201     FILE * file = changed_worldstate_file(path);
202     if (!file)
203     {
204         return 0;
205     }
206     uint32_t linemax = textfile_width(file);
207     char * read_buf = try_malloc(linemax + 1, f_name);
208     world.turn = read_value_from_line(read_buf, linemax, file);
209     world.player_lifepoints = read_value_from_line(read_buf, linemax, file);
210     read_inventory(read_buf, linemax, file);
211     world.player_pos.y = read_value_from_line(read_buf, linemax, file);
212     world.player_pos.x = read_value_from_line(read_buf, linemax, file);
213     if (1 == world.turn || first_read)
214     {
215         map_center();
216         first_read = 0;
217     }
218     world.map.size.y = read_value_from_line(read_buf, linemax, file);
219     world.map.size.x = read_value_from_line(read_buf, linemax, file);
220     read_map_cells(file);
221     read_log(read_buf, linemax, file);
222     free(read_buf);
223     try_fclose(file, f_name);
224     return 1;
225 }
226
227
228
229 static void test_ping_pong(time_t last_server_answer_time)
230 {
231     static uint8_t ping_sent = 0;
232     time_t now = time(0);
233     if (ping_sent && last_server_answer_time > now - 3)
234     {
235         ping_sent = 0;
236     }
237     if (!ping_sent && last_server_answer_time < now - 3)
238     {
239         send("PING");
240         ping_sent = 1;
241         return;
242     }
243     exit_err(last_server_answer_time < now - 6, "Server not answering.");
244 }
245
246
247
248 static void test_server_activity(time_t * last_server_answer_time)
249 {
250     char * f_name = "test_server_activity()";
251     int test = try_fgetc(world.file_server_out, f_name);
252     if (EOF == test)
253     {
254         return;
255     }
256     do
257     {
258         ;
259     }
260     while (EOF != (test = try_fgetc(world.file_server_out, f_name)));
261     * last_server_answer_time = time(0);
262 }
263
264
265
266 extern void send(char * msg)
267 {
268     char * f_name = "send()";
269     uint32_t msg_size = strlen(msg) + 1;
270     char * err = "send() tries to send message larger than PIPE_BUF bytes.";
271     exit_err(msg_size > PIPE_BUF, err);
272     try_fwrite(msg, strlen(msg), 1, world.file_server_in, f_name);
273     try_fputc('\n', world.file_server_in, f_name);
274     fflush(world.file_server_in);
275 }
276
277
278
279 extern char * io_loop()
280 {
281     world.halfdelay = 1;            /* Ensures read_world() is only called 10 */
282     halfdelay(world.halfdelay);     /* times a second during user inactivity. */
283     uint8_t change_in_client = 0;
284     time_t last_server_answer_time = time(0);
285     while (1)
286     {
287         test_server_activity(&last_server_answer_time);
288         test_ping_pong(last_server_answer_time);
289         if (world.winch)
290         {
291             reset_windows_on_winch();
292             world.winch = 0;
293             change_in_client++;
294         }
295         if (read_world() || change_in_client)
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 }