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