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