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