]> git.pond.sub.org Git - empserver/blob - src/lib/empthread/io.c
47a2b1093e4de2a9cdb188131270077a7e4e276a
[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     struct timeval input_timeout;
67 };
68
69 void
70 io_init(void)
71 {
72 }
73
74 struct iop *
75 io_open(int fd, int flags, int bufsize, struct timeval timeout)
76 {
77     int fdfl;
78     struct iop *iop;
79
80     flags = flags & (IO_READ | IO_WRITE);
81     if ((flags & (IO_READ | IO_WRITE)) == 0)
82         return NULL;
83
84     fdfl = fcntl(fd, F_GETFL, 0);
85     if (fdfl < 0)
86         return NULL;
87     fdfl |= O_NONBLOCK;
88     if (fcntl(fd, F_SETFL, fdfl) < 0)
89         return NULL;
90
91     iop = malloc(sizeof(struct iop));
92     if (!iop)
93         return NULL;
94     iop->fd = fd;
95     iop->input = NULL;
96     iop->output = NULL;
97     iop->flags = flags;
98     iop->last_out = 0;
99     iop->bufsize = bufsize;
100     iop->input_timeout = timeout;
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 void
109 io_close(struct iop *iop)
110 {
111     char buf[IO_BUFSIZE];
112     int ret;
113
114     while (io_output(iop, 1) > 0) ;
115     shutdown(iop->fd, SHUT_WR);
116     while (empth_select(iop->fd, EMPTH_FD_READ, &iop->input_timeout) > 0) {
117         ret = read(iop->fd, buf, sizeof(buf));
118         if (ret <= 0)
119             break;
120     }
121     if (iop->input)
122         ioq_destroy(iop->input);
123     if (iop->output)
124         ioq_destroy(iop->output);
125     (void)close(iop->fd);
126     free(iop);
127 }
128
129 /*
130  * Return number of bytes read on success, zero on timeout, early
131  * wakeup or EOF, -1 on error, with errno set appropriately.  In
132  * particular, return -1 with errno set to EAGAIN or EWOULDBLOCK when
133  * no data is available for non-blocking input (WAITFORINPUT false).
134  * Use io_eof() to distinguish timeout and early wakeup from EOF.
135  */
136 int
137 io_input(struct iop *iop, int waitforinput)
138 {
139     char buf[IO_BUFSIZE];
140     int cc;
141     int res;
142
143     if ((iop->flags & IO_READ) == 0) {
144         errno = EBADF;
145         return -1;
146     }
147     if (iop->flags & IO_ERROR) {
148         errno = EBADF;
149         return -1;
150     }
151     if (iop->flags & IO_EOF)
152         return 0;
153
154     /* Wait for the file to have input. */
155     if (waitforinput) {
156         res = empth_select(iop->fd, EMPTH_FD_READ, &iop->input_timeout);
157         if (res < 0) {
158             iop->flags |= IO_ERROR;
159             return -1;
160         } else if (res == 0)
161             return 0;
162     }
163
164     /* Do the actual read. */
165     cc = read(iop->fd, buf, sizeof(buf));
166     if (cc < 0) {
167         if (errno != EAGAIN && errno != EWOULDBLOCK)
168             /* Some form of file error occurred... */
169             iop->flags |= IO_ERROR;
170         return -1;
171     }
172
173     /* We eof'd */
174     if (cc == 0) {
175         iop->flags |= IO_EOF;
176         return 0;
177     }
178
179     /* Append the input to the IOQ. */
180     ioq_append(iop->input, buf, cc);
181     return cc;
182 }
183
184 int
185 io_inputwaiting(struct iop *iop)
186 {
187     return ioq_qsize(iop->input);
188 }
189
190 int
191 io_outputwaiting(struct iop *iop)
192 {
193     return ioq_qsize(iop->output);
194 }
195
196 /*
197  * Write output queued in IOP.
198  * If WAIT, writing may put the thread to sleep.
199  * Return number of bytes written on success, -1 on error.
200  * In particular, return zero when nothing was written because the
201  * queue was empty, or because the write slept and got woken up (only
202  * if WAIT), or because the write refused to sleep (only if !WAIT).
203  */
204 int
205 io_output(struct iop *iop, int wait)
206 {
207     struct iovec iov[16];
208     int n, res, cc;
209
210     if (wait)
211         ef_make_stale();
212
213     if ((iop->flags & IO_WRITE) == 0)
214         return -1;
215
216     if (iop->flags & IO_ERROR)
217         return -1;
218
219     if (!ioq_qsize(iop->output))
220         return 0;
221
222     if (wait) {
223         res = empth_select(iop->fd, EMPTH_FD_WRITE, NULL);
224         if (res == 0)
225             return 0;
226         if (res < 0) {
227             iop->flags |= IO_ERROR;
228             return -1;
229         }
230     }
231
232     n = ioq_makeiov(iop->output, iov, IO_BUFSIZE);
233     cc = writev(iop->fd, iov, n);
234     if (cc < 0) {
235         if (errno == EAGAIN || errno == EWOULDBLOCK)
236             return 0;
237         iop->flags |= IO_ERROR;
238         return -1;
239     }
240
241     ioq_dequeue(iop->output, cc);
242     iop->last_out = ioq_qsize(iop->output);
243     return cc;
244 }
245
246 /*
247  * Write output queued in IOP if enough have been enqueued.
248  * Write if at least one buffer has been filled since the last write.
249  * If WAIT, writing may put the thread to sleep.
250  * Return number of bytes written on success, -1 on error.
251  * In particular, return zero when nothing was written because the
252  * queue was not long, or the write slept and got woken up (only if
253  * WAIT), or the write refused to sleep (only if !WAIT).
254  */
255 int
256 io_output_if_queue_long(struct iop *iop, int wait)
257 {
258     int len = ioq_qsize(iop->output);
259
260     if (CANT_HAPPEN(iop->last_out > len))
261         iop->last_out = 0;
262     if (len - iop->last_out < iop->bufsize) {
263         if (wait)
264             ef_make_stale();
265         return 0;
266     }
267     return io_output(iop, wait);
268 }
269
270 int
271 io_peek(struct iop *iop, char *buf, int nbytes)
272 {
273     if ((iop->flags & IO_READ) == 0)
274         return -1;
275     return ioq_peek(iop->input, buf, nbytes);
276 }
277
278 int
279 io_read(struct iop *iop, char *buf, int nbytes)
280 {
281     int cc;
282
283     if ((iop->flags & IO_READ) == 0)
284         return -1;
285     cc = ioq_peek(iop->input, buf, nbytes);
286     if (cc > 0)
287         ioq_dequeue(iop->input, cc);
288     return cc;
289 }
290
291 int
292 io_write(struct iop *iop, char *buf, int nbytes)
293 {
294     if ((iop->flags & IO_WRITE) == 0)
295         return -1;
296     ioq_append(iop->output, buf, nbytes);
297     return nbytes;
298 }
299
300 int
301 io_gets(struct iop *iop, char *buf, int nbytes)
302 {
303     if ((iop->flags & IO_READ) == 0)
304         return -1;
305     return ioq_gets(iop->input, buf, nbytes);
306 }
307
308 int
309 io_puts(struct iop *iop, char *buf)
310 {
311     if ((iop->flags & IO_WRITE) == 0)
312         return -1;
313     return ioq_puts(iop->output, buf);
314 }
315
316 int
317 io_shutdown(struct iop *iop, int flags)
318 {
319     flags &= (IO_READ | IO_WRITE);
320     if ((iop->flags & flags) != flags)
321         return -1;
322     if (flags & IO_READ) {
323         shutdown(iop->fd, 0);
324         ioq_drain(iop->input);
325     }
326     if (flags & IO_WRITE) {
327         shutdown(iop->fd, 1);
328         ioq_drain(iop->output);
329         iop->last_out = 0;
330     }
331     return 0;
332 }
333
334 int
335 io_error(struct iop *iop)
336 {
337     return iop->flags & IO_ERROR;
338 }
339
340 int
341 io_eof(struct iop *iop)
342 {
343     return iop->flags & IO_EOF;
344 }
345
346 /*
347  * Discard IOP's buffered input and set its EOF flag.
348  * No more input can be read from IOP.
349  */
350 void
351 io_set_eof(struct iop *iop)
352 {
353     ioq_drain(iop->input);
354     iop->flags |= IO_EOF;
355 }
356
357 int
358 io_fileno(struct iop *iop)
359 {
360     return iop->fd;
361 }