]> git.pond.sub.org Git - empserver/blob - src/lib/empthread/io.c
io_shutdown() is now unused, remove
[empserver] / src / lib / empthread / io.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2011, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire is free software: you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation, either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  io.c: Arrange for input and output on a file descriptor to be queued.
28  *
29  *  Known contributors to this file:
30  *     Doug Hay, 1998
31  *     Steve McClure, 1998
32  *     Markus Armbruster, 2004-2012
33  *     Ron Koenderink, 2009
34  */
35
36 /*
37  * Arrange for input and output on a file descriptor
38  * to be queued.  Provide main loop -- a mechanism for
39  * blocking across all registered file descriptors, and
40  * reading or writing when appropriate.
41  */
42
43 #include <config.h>
44
45 #include <errno.h>
46 #include <fcntl.h>
47 #include <stdlib.h>
48 #include <sys/socket.h>
49 #include <sys/types.h>
50 #include <sys/uio.h>
51 #include <unistd.h>
52 #include "empio.h"
53 #include "empthread.h"
54 #include "file.h"
55 #include "ioqueue.h"
56 #include "misc.h"
57 #include "queue.h"
58
59 struct iop {
60     int fd;
61     struct ioqueue *input;
62     struct ioqueue *output;
63     int flags;
64     int bufsize;
65     int last_out;
66 };
67
68 static struct timeval *io_timeout(struct timeval *, time_t);
69
70 void
71 io_init(void)
72 {
73 }
74
75 struct iop *
76 io_open(int fd, int flags, int bufsize)
77 {
78     int fdfl;
79     struct iop *iop;
80
81     flags = flags & (IO_READ | IO_WRITE);
82     if ((flags & (IO_READ | IO_WRITE)) == 0)
83         return NULL;
84
85     fdfl = fcntl(fd, F_GETFL, 0);
86     if (fdfl < 0)
87         return NULL;
88     fdfl |= O_NONBLOCK;
89     if (fcntl(fd, F_SETFL, fdfl) < 0)
90         return NULL;
91
92     iop = malloc(sizeof(struct iop));
93     if (!iop)
94         return NULL;
95     iop->fd = fd;
96     iop->input = NULL;
97     iop->output = NULL;
98     iop->flags = flags;
99     iop->last_out = 0;
100     iop->bufsize = bufsize;
101     if (flags & IO_READ)
102         iop->input = ioq_create(bufsize);
103     if (flags & IO_WRITE)
104         iop->output = ioq_create(bufsize);
105     return iop;
106 }
107
108 /*
109  * Close IOP.
110  * Flush output and wait for the client to close the connection.
111  * Wait at most until DEADLINE.  (time_t)-1 means wait as long as it
112  * takes (no timeout).
113  * Both the flush and the wait can be separately cut short by
114  * empth_wakeup().  This is almost certainly not what you want.  If
115  * you need early wakeup, better fix this function not to go to sleep
116  * after wakeup during flush.
117  */
118 void
119 io_close(struct iop *iop, time_t deadline)
120 {
121     struct timeval timeout;
122     char buf[IO_BUFSIZE];
123     int ret;
124
125     while (io_output(iop, deadline) > 0) ;
126     shutdown(iop->fd, SHUT_WR);
127     while (empth_select(iop->fd, EMPTH_FD_READ,
128                         io_timeout(&timeout, deadline)) > 0) {
129         ret = read(iop->fd, buf, sizeof(buf));
130         if (ret <= 0)
131             break;
132     }
133     if (iop->input)
134         ioq_destroy(iop->input);
135     if (iop->output)
136         ioq_destroy(iop->output);
137     (void)close(iop->fd);
138     free(iop);
139 }
140
141 static struct timeval *
142 io_timeout(struct timeval *timeout, time_t deadline)
143 {
144     struct timeval now;
145
146     if (deadline == (time_t)-1)
147         return NULL;            /* no deadline */
148
149     gettimeofday(&now, NULL);
150     if (now.tv_sec >= deadline) {
151         /* deadline reached already */
152         timeout->tv_sec = 0;
153         timeout->tv_usec = 0;
154     } else {
155         /* deadline in future */
156         timeout->tv_sec = deadline - now.tv_sec - 1;
157         timeout->tv_usec = 999999 - now.tv_usec;
158         /* yes, this is 1usec early; sue me */
159     }
160
161     return timeout;
162 }
163
164 /*
165  * Read input from IOP and enqueue it.
166  * Wait at most until DEADLINE for input to arrive.  (time_t)-1 means
167  * wait as long as it takes (no timeout).
168  * Does not yield the processor when DEADLINE is zero.
169  * A wait for input can be cut short by empth_wakeup().
170  * Return number of bytes read on success, -1 on error.
171  * In particular, return zero on timeout, early wakeup or EOF.  Use
172  * io_eof() to distinguish timeout and early wakeup from EOF.
173  */
174 int
175 io_input(struct iop *iop, time_t deadline)
176 {
177     struct timeval timeout;
178     char buf[IO_BUFSIZE];
179     int cc;
180     int res;
181
182     if ((iop->flags & IO_READ) == 0)
183         return -1;
184     if (iop->flags & IO_ERROR)
185         return -1;
186     if (iop->flags & IO_EOF)
187         return 0;
188
189     if (deadline) {
190         res = empth_select(iop->fd, EMPTH_FD_READ,
191                            io_timeout(&timeout, deadline));
192         if (res < 0) {
193             iop->flags |= IO_ERROR;
194             return -1;
195         } else if (res == 0)
196             return 0;
197     }
198
199     cc = read(iop->fd, buf, sizeof(buf));
200     if (cc < 0) {
201         if (errno == EAGAIN || errno == EWOULDBLOCK)
202             return 0;
203         iop->flags |= IO_ERROR;
204         return -1;
205     }
206     if (cc == 0) {
207         iop->flags |= IO_EOF;
208         return 0;
209     }
210
211     ioq_append(iop->input, buf, cc);
212     return cc;
213 }
214
215 int
216 io_inputwaiting(struct iop *iop)
217 {
218     return ioq_qsize(iop->input);
219 }
220
221 int
222 io_outputwaiting(struct iop *iop)
223 {
224     return ioq_qsize(iop->output);
225 }
226
227 /*
228  * Write output queued in IOP.
229  * Wait at most until DEADLINE for input to arrive.  (time_t)-1 means
230  * wait as long as it takes (no timeout).
231  * Does not yield the processor when DEADLINE is zero.
232  * A wait for output can be cut short by empth_wakeup().
233  * Return number of bytes written on success, -1 on error.
234  * In particular, return zero when nothing was written because the
235  * queue is empty, and on timeout or early wakeup.  Use
236  * io_outputwaiting() to distinguish timeout and early wakeup from
237  * empty queue.
238  */
239 int
240 io_output(struct iop *iop, time_t deadline)
241 {
242     struct timeval timeout;
243     struct iovec iov[16];
244     int n, res, cc;
245
246     if (deadline)
247         ef_make_stale();
248
249     if ((iop->flags & IO_WRITE) == 0)
250         return -1;
251
252     if (iop->flags & IO_ERROR)
253         return -1;
254
255     if (!ioq_qsize(iop->output))
256         return 0;
257
258     if (deadline) {
259         res = empth_select(iop->fd, EMPTH_FD_WRITE,
260                            io_timeout(&timeout, deadline));
261         if (res == 0)
262             return 0;
263         if (res < 0) {
264             iop->flags |= IO_ERROR;
265             return -1;
266         }
267     }
268
269     n = ioq_makeiov(iop->output, iov, IO_BUFSIZE);
270     cc = writev(iop->fd, iov, n);
271     if (cc < 0) {
272         if (errno == EAGAIN || errno == EWOULDBLOCK)
273             return 0;
274         iop->flags |= IO_ERROR;
275         return -1;
276     }
277
278     ioq_dequeue(iop->output, cc);
279     iop->last_out = ioq_qsize(iop->output);
280     return cc;
281 }
282
283 /*
284  * Write output queued in IOP if enough have been enqueued.
285  * Write if at least one buffer has been filled since the last write.
286  * Wait at most until DEADLINE for output to be accepted.  (time_t)-1
287  * means wait as long as it takes (no timeout).
288  * Does not yield the processor when DEADLINE is zero.
289  * A wait for output can be cut short by empth_wakeup().
290  * Return number of bytes written on success, -1 on error.
291  * In particular, return zero when nothing was written because the
292  * queue isn't long, and on timeout or early wakeup.
293  */
294 int
295 io_output_if_queue_long(struct iop *iop, time_t deadline)
296 {
297     int len = ioq_qsize(iop->output);
298
299     if (CANT_HAPPEN(iop->last_out > len))
300         iop->last_out = 0;
301     if (len - iop->last_out < iop->bufsize) {
302         if (deadline)
303             ef_make_stale();
304         return 0;
305     }
306     return io_output(iop, deadline);
307 }
308
309 int
310 io_peek(struct iop *iop, char *buf, int nbytes)
311 {
312     if ((iop->flags & IO_READ) == 0)
313         return -1;
314     return ioq_peek(iop->input, buf, nbytes);
315 }
316
317 int
318 io_read(struct iop *iop, char *buf, int nbytes)
319 {
320     int cc;
321
322     if ((iop->flags & IO_READ) == 0)
323         return -1;
324     cc = ioq_peek(iop->input, buf, nbytes);
325     if (cc > 0)
326         ioq_dequeue(iop->input, cc);
327     return cc;
328 }
329
330 int
331 io_write(struct iop *iop, char *buf, int nbytes)
332 {
333     if ((iop->flags & IO_WRITE) == 0)
334         return -1;
335     ioq_append(iop->output, buf, nbytes);
336     return nbytes;
337 }
338
339 int
340 io_gets(struct iop *iop, char *buf, int nbytes)
341 {
342     if ((iop->flags & IO_READ) == 0)
343         return -1;
344     return ioq_gets(iop->input, buf, nbytes);
345 }
346
347 int
348 io_puts(struct iop *iop, char *buf)
349 {
350     if ((iop->flags & IO_WRITE) == 0)
351         return -1;
352     return ioq_puts(iop->output, buf);
353 }
354
355 int
356 io_error(struct iop *iop)
357 {
358     return iop->flags & IO_ERROR;
359 }
360
361 int
362 io_eof(struct iop *iop)
363 {
364     return iop->flags & IO_EOF;
365 }
366
367 /*
368  * Discard IOP's buffered input and set its EOF flag.
369  * No more input can be read from IOP.
370  */
371 void
372 io_set_eof(struct iop *iop)
373 {
374     ioq_drain(iop->input);
375     iop->flags |= IO_EOF;
376 }
377
378 int
379 io_fileno(struct iop *iop)
380 {
381     return iop->fd;
382 }