home · contact · privacy
27a0e0f46d623fc2b31be94c074a2c953d61becb
[plomrogue] / src / client / io.c
1 /* src/client/io.c */
2
3 #include "io.h"
4 #include <errno.h> /* global errno */
5 #include <fcntl.h> /* open() */
6 #include <limits.h> /* PIPE_BUF */
7 #include <ncurses.h> /* halfdelay(), getch() */
8 #include <stddef.h> /* NULL */
9 #include <stdint.h> /* uint8_t, uint16_t, uint32_t */
10 #include <stdio.h> /* FILE, sprintf(), fseek() */
11 #include <string.h> /* strcmp(), strlen(), memcpy() */
12 #include <stdlib.h> /* free(), atoi() */
13 #include <sys/stat.h> /* stat() */
14 #include <unistd.h> /* access(), write() */
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()
19                                   */
20 #include "control.h" /* try_key() */
21 #include "map.h" /* map_center() */
22 #include "misc.h" /* reset_windows() */
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.size.y times world.map.size.x 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 out file has changed since the last read_world(), return a
51  * 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_server_out_file(char * path);
65
66 /* Attempt to read the server's out file as representation of the game world in
67  * a hard-coded serialization format. Returns 1 on success and 0 if the out file
68  * wasn't read for supposedly not having changed since a last 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
76
77 static void read_inventory(char * read_buf, uint32_t linemax, FILE * file)
78 {
79     char * f_name = "read_inventory()";
80     char * delimiter = "%\n";
81     free(world.player_inventory);
82     world.player_inventory = NULL;          /* Avoids illegal strlen() below. */
83     while (1)
84     {
85         try_fgets(read_buf, linemax + 1, file, f_name);
86         if (!(strcmp(read_buf, delimiter)))
87         {
88             break;
89         }
90         int old_size = 0;
91         if (NULL != world.player_inventory)
92         {
93             old_size = strlen(world.player_inventory);
94         }
95         int new_size = strlen(read_buf);
96         char * new_inventory = try_malloc(old_size + new_size + 1, f_name);
97         memcpy(new_inventory, world.player_inventory, old_size);
98         sprintf(new_inventory + old_size, "%s", read_buf);
99         free(world.player_inventory);
100         world.player_inventory = new_inventory;
101     }
102     world.player_inventory[strlen(world.player_inventory) - 1] = '\0';
103     world.player_inventory_select = 0;
104 }
105
106
107
108 static void read_map_cells(FILE * file)
109 {
110     char * f_name = "read_map_cells()";
111     free(world.map.cells);
112     world.map.cells = try_malloc(world.map.size.y * world.map.size.x, f_name);
113     uint16_t y, x;
114     for (y = 0; y < world.map.size.y; y++)
115     {
116         for (x = 0; x < world.map.size.x; x++)
117         {
118             char c = try_fgetc(file, f_name);
119             world.map.cells[(y * world.map.size.x) + x] = c;
120         }
121         try_fgetc(file, f_name);
122     }
123 }
124
125
126
127 static void read_log(char * read_buf, uint32_t linemax, FILE * file)
128 {
129     char * f_name = "read_log()";
130     free(world.log);
131     world.log = NULL;
132     while (try_fgets(read_buf, linemax + 1, file, f_name))
133     {
134         int old_size = 0;
135         if (NULL != world.log)
136         {
137             old_size = strlen(world.log);
138         }
139         int new_size = strlen(read_buf);
140         char * new_log = try_malloc(old_size + new_size + 1, f_name);
141         memcpy(new_log, world.log, old_size);
142         sprintf(new_log + old_size, "%s", read_buf);
143         free(world.log);
144         world.log = new_log;
145     }
146 }
147
148
149
150 static uint16_t read_value_from_line(char * read_buf, uint32_t linemax,
151                                      FILE * file)
152 {
153     char * f_name = "read_value_from_line()";
154     try_fgets(read_buf, linemax + 1, file, f_name);
155     return atoi(read_buf);
156 }
157
158
159
160 static FILE * changed_server_out_file(char * path)
161 {
162     char * f_name = "changed_server_out_file()";
163     struct stat stat_buf;
164     exit_trouble(stat(path, &stat_buf), f_name, "stat()");
165     if (stat_buf.st_mtime != world.last_update)
166     {
167         world.last_update = stat_buf.st_mtime;
168         return try_fopen(path, "r", f_name);
169     }
170     FILE * file = try_fopen(path, "r", f_name);
171     char turn_string[6];
172     try_fgets(turn_string, 6, file, f_name);
173     if (world.turn == atoi(turn_string))
174     {
175         try_fclose(file, f_name);
176         return NULL;
177     }
178     exit_trouble(fseek(file, 0, SEEK_SET), f_name, "fseek()");
179     return file;
180 }
181
182
183
184 static uint8_t read_world()
185 {
186     char * f_name = "read_world()";
187     char * path = "server/out";
188     char * quit_msg = "No server out file found to read. Server may be down.";
189     static uint8_t first_read = 1;
190     exit_err(access(path, F_OK), quit_msg);
191     FILE * file = changed_server_out_file(path);
192     if (!file)
193     {
194         return 0;
195     }
196     uint32_t linemax = textfile_width(file);
197     char * read_buf = try_malloc(linemax + 1, f_name);
198     world.turn = read_value_from_line(read_buf, linemax, file);
199     world.player_lifepoints = read_value_from_line(read_buf, linemax, file);
200     read_inventory(read_buf, linemax, file);
201     world.player_pos.y = read_value_from_line(read_buf, linemax, file);
202     world.player_pos.x = read_value_from_line(read_buf, linemax, file);
203     if (1 == world.turn || first_read)
204     {
205         map_center();
206         first_read = 0;
207     }
208     world.map.size.y = read_value_from_line(read_buf, linemax, file);
209     world.map.size.x = read_value_from_line(read_buf, linemax, file);
210     read_map_cells(file);
211     read_log(read_buf, linemax, file);
212     free(read_buf);
213     try_fclose(file, f_name);
214     return 1;
215 }
216
217
218
219 extern void try_send(char * msg)
220 {
221     char * f_name = "try_send()";
222     uint32_t msg_size = strlen(msg) + 1;
223     char * err = "try_send() tries to send message larger than PIPE_BUF bytes.";
224     exit_err(msg_size > PIPE_BUF, err);
225     int fd_out;
226     uint16_t j = 1;
227     while (0 != j)
228     {
229         fd_out = open(world.path_server_in, O_WRONLY | O_NONBLOCK);
230         if (fd_out > 0)
231         {
232             break;
233         }
234         exit_err(-1 == fd_out && ENXIO != errno, "Server fifo not found.");
235         j++;
236     }
237     exit_err(0 == j, "Failed to open server fifo for writing.");
238     j = 1;
239     while (0 != j)
240     {
241         int test = write(fd_out, msg, msg_size);
242         if (test > 0)
243         {
244             break;
245         }
246         j++;
247     }
248     exit_err(0 == j, "Failed to write to server fifo.");
249     exit_trouble(-1 == close(fd_out), f_name, "close()");
250 }
251
252
253
254 extern char * io_loop()
255 {
256     world.halfdelay = 1;            /* Ensures read_world() is only called 10 */
257     halfdelay(world.halfdelay);     /* times a second during user inactivity. */
258     uint8_t change_in_client = 0;
259     while (1)
260     {
261         if (world.winch)
262         {
263             reset_windows_on_winch();
264             world.winch = 0;
265             change_in_client++;
266         }
267         if (read_world() || change_in_client)
268         {
269             draw_all_wins();
270         }
271         change_in_client = 0;
272         int key = getch();
273         if (ERR != key)
274         {
275             change_in_client = try_key((uint16_t) key);
276             if (2 == change_in_client)
277             {
278                 break;
279             }
280         }
281     }
282     try_send("QUIT");
283     return "Sent QUIT to server.";
284 }