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