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