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