home · contact · privacy
MAJOR re-write. Split plomrogue into a server and a client. Re-wrote large parts
[plomrogue] / src / server / io.c
1 /* src/server/io.c */
2
3 #include "io.h"
4 #include <errno.h> /* global errno */
5 #include <fcntl.h> /* open(), O_RDONLY, O_NONBLOCK */
6 #include <limits.h> /* PIPE_BUF */
7 #include <stddef.h> /* size_t */
8 #include <stdint.h> /* uint8_t, uint16_t, uint32_t */
9 #include <stdio.h> /* define FILE, sprintf() */
10 #include <stdlib.h> /* free() */
11 #include <string.h> /* strlen(), memset(), memcpy() */
12 #include <unistd.h> /* read(), close() */
13 #include "../common/readwrite.h" /* try_fopen(), try_fclose_unlink_rename(),
14                                   * try_fwrite(), try_fputc()
15                                   */
16 #include "../common/rexit.h" /* exit_trouble() */
17 #include "../common/try_malloc.h" /* try_malloc() */
18 #include "cleanup.h" /* set_cleanup_flag() */
19 #include "map_objects.h" /* structs MapObj, MapObjDef, get_map_obj_def() */
20 #include "world.h" /* global world  */
21
22
23
24 /* Cut out and return first \0-terminated string from world.queue and
25  * appropriately reduce world.queue_size. Return NULL if queue is empty.
26  * Superfluous \0 bytes after the string are also cut out. Should the queue
27  * start with \0 bytes, those are cut out, but NULL is returned instead of "".
28 */
29 static char * get_message_from_queue();
30
31 /* Read fifo input to put into queue. Only stop reading when bytes were received
32  * and the receiving has stopped. Read max. PIPE_BUF-sized chunks for atomicity.
33  */
34 static void read_fifo_into_queue();
35
36 /* Write to output file the world state as it is to be visible to clients. */
37 static void update_out_file();
38
39 /* Write "value" to new \n-delimited line of "file". */
40 static void write_value_as_line(uint32_t value, FILE * file);
41
42 /* Write to "file" player's inventory, one item name per line. End in "%\n". */
43 static void write_inventory(struct MapObj * player, FILE * file);
44
45 /* Write to "file" game map, with map objects super-imposed. Write one row per
46  * \n-delimited line. Super-impose animated objects over inanimate objects.
47  */
48 static void write_map(FILE * file);
49
50
51
52 static char * get_message_from_queue()
53 {
54     char * f_name = "get_message_from_queue()";
55     char * message = NULL;
56     size_t cutout_len = strlen(world.queue);
57     if (0 < cutout_len)
58     {
59         cutout_len++;
60         message = try_malloc(cutout_len, f_name);
61         memcpy(message, world.queue, cutout_len);
62     }
63     for (;
64          cutout_len != world.queue_size && '\0' == world.queue[cutout_len];
65          cutout_len++);
66     world.queue_size = world.queue_size - cutout_len;
67     if (0 == world.queue_size)
68     {
69         free(world.queue);
70         world.queue = NULL;
71     }
72     else
73     {
74         char * new_queue = try_malloc(world.queue_size, f_name);
75         memcpy(new_queue, &(world.queue[cutout_len]), world.queue_size);
76         free(world.queue);
77         world.queue = new_queue;
78     }
79     return message;
80 }
81
82
83
84 static void read_fifo_into_queue()
85 {
86     char * f_name = "read_fifo_into_queue()";
87     uint32_t buf_size = PIPE_BUF;
88     int fdesc_infile = open(world.path_in, O_RDONLY | O_NONBLOCK);
89     exit_trouble(-1 == fdesc_infile, "open()", f_name);
90     char buf[buf_size];
91     memset(buf, 0, buf_size);
92     int bytes_read;
93     uint8_t read_state = 0; /* 0: waiting for input. 1: started receiving it. */
94     while (1)
95     {
96         bytes_read = read(fdesc_infile, buf, buf_size);
97         if (bytes_read > 0)
98         {
99             read_state = 1;
100             uint32_t old_queue_size = world.queue_size;
101             world.queue_size = world.queue_size + bytes_read;
102             char * new_queue = try_malloc(world.queue_size, f_name);
103             memcpy(new_queue, world.queue, old_queue_size);
104             memcpy(&(new_queue[old_queue_size]), buf, bytes_read);
105             free(world.queue);
106             world.queue = new_queue;
107             memset(buf, 0, buf_size);
108             continue;
109         }
110         exit_trouble(-1 == bytes_read && errno != EAGAIN, "read()", f_name);
111         if (1 == read_state)
112         {
113             break;
114         }
115     }
116     exit_trouble(close(fdesc_infile), f_name, "close()");
117 }
118
119
120
121 static void update_out_file()
122 {
123     char * f_name = "update_out_file()";
124     char path_tmp[strlen(world.path_out) + strlen(world.tmp_suffix) + 1];
125     sprintf(path_tmp, "%s%s", world.path_out, world.tmp_suffix);
126     FILE * file = try_fopen(path_tmp, "w", f_name);
127     struct MapObj * player = get_player();
128     write_value_as_line(world.turn, file);
129     write_value_as_line(world.score, file);
130     write_value_as_line(player->lifepoints, file);
131     write_inventory(player, file);
132     write_value_as_line(player->pos.y, file);
133     write_value_as_line(player->pos.x, file);
134     write_value_as_line(world.map.size.y, file);
135     write_value_as_line(world.map.size.x, file);
136     write_map(file);
137     if (world.log)
138     {
139         try_fwrite(world.log, strlen(world.log), 1, file, f_name);
140     }
141     try_fclose_unlink_rename(file, path_tmp, world.path_out, f_name);
142     set_cleanup_flag(CLEANUP_OUTFILE);
143 }
144
145
146
147 static void write_value_as_line(uint32_t value, FILE * file)
148 {
149     char * f_name = "write_value_as_line()";
150     char write_buf[12];     /* Holds 10 digits of uint32_t maximum + \n + \0. */
151     sprintf(write_buf, "%u\n", value);
152     try_fwrite(write_buf, strlen(write_buf), 1, file, f_name);
153 }
154
155
156
157 static void write_inventory(struct MapObj * player, FILE * file)
158 {
159     char * f_name = "write_inventory()";
160     struct MapObj * owned = player->owns;
161     if (NULL == owned)
162     {
163         char * empty = "(none)\n";
164         try_fwrite(empty, strlen(empty), 1, file, f_name);
165     }
166     else
167     {
168         uint8_t q;
169         for (q = 0; NULL != owned; q++)
170         {
171             struct MapObjDef * mod = get_map_object_def(owned->type);
172             try_fwrite(mod->name, strlen(mod->name), 1, file, f_name);
173             try_fputc('\n', file, f_name);
174             owned = owned->next;
175         }
176     }
177     try_fputc('%', file, f_name);
178     try_fputc('\n', file, f_name);
179 }
180
181
182
183 static void write_map(FILE * file)
184 {
185     char * f_name = "write_map()";
186     uint32_t map_size = world.map.size.y * world.map.size.x;
187     char visible_map[map_size];
188     memcpy(visible_map, world.map.cells, map_size);
189     struct MapObj * o;
190     struct MapObjDef * d;
191     char c;
192     uint8_t i;
193     for (i = 0; i < 2; i++)
194     {
195         for (o = world.map_objs; o != 0; o = o->next)
196         {
197             if ((   (0 == i && 0 == o->lifepoints)
198                  || (1 == i && 0 < o->lifepoints)))
199             {
200                 d = get_map_object_def(o->type);
201                 c = d->char_on_map;
202                 visible_map[(o->pos.y * world.map.size.x) + o->pos.x] = c;
203             }
204         }
205     }
206     uint16_t x, y;
207     for (y = 0; y < world.map.size.y; y++)
208     {
209         for (x = 0; x < world.map.size.x; x++)
210         {
211             try_fputc(visible_map[(y * world.map.size.x) + x], file, f_name);
212         }
213         try_fputc('\n', file, f_name);
214     }
215 }
216
217
218
219 extern char * io_round()
220 {
221     char * f_name = "io_round()";
222     if (0 < world.queue_size)
223     {
224         return get_message_from_queue();
225     }
226     if (world.turn != world.last_update_turn)
227     {
228         update_out_file();
229         world.last_update_turn = world.turn;
230     }
231     read_fifo_into_queue();
232     if ('\0' != world.queue[world.queue_size - 1])
233     {
234         char * new_queue = try_malloc(world.queue_size + 1, f_name);
235         memcpy(new_queue, world.queue, world.queue_size);
236         new_queue[world.queue_size] = '\0';
237         world.queue_size++;
238         free(world.queue);
239         world.queue = new_queue;
240     }
241     return get_message_from_queue();
242 }