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
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.
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.
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/>.
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.
27 * io.c: Arrange for input and output on a file descriptor to be queued.
29 * Known contributors to this file:
32 * Markus Armbruster, 2004-2012
33 * Ron Koenderink, 2009
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.
48 #include <sys/socket.h>
49 #include <sys/types.h>
53 #include "empthread.h"
61 struct ioqueue *input;
62 struct ioqueue *output;
68 static struct timeval *io_timeout(struct timeval *, time_t);
76 io_open(int fd, int flags, int bufsize)
81 flags = flags & (IO_READ | IO_WRITE);
82 if ((flags & (IO_READ | IO_WRITE)) == 0)
85 fdfl = fcntl(fd, F_GETFL, 0);
89 if (fcntl(fd, F_SETFL, fdfl) < 0)
92 iop = malloc(sizeof(struct iop));
100 iop->bufsize = bufsize;
102 iop->input = ioq_create(bufsize);
103 if (flags & IO_WRITE)
104 iop->output = ioq_create(bufsize);
109 io_close(struct iop *iop, time_t deadline)
111 struct timeval timeout;
112 char buf[IO_BUFSIZE];
115 while (io_output(iop, 1) > 0) ;
116 shutdown(iop->fd, SHUT_WR);
117 while (empth_select(iop->fd, EMPTH_FD_READ,
118 io_timeout(&timeout, deadline)) > 0) {
119 ret = read(iop->fd, buf, sizeof(buf));
124 ioq_destroy(iop->input);
126 ioq_destroy(iop->output);
127 (void)close(iop->fd);
131 static struct timeval *
132 io_timeout(struct timeval *timeout, time_t deadline)
136 if (deadline == (time_t)-1)
137 return NULL; /* no deadline */
139 gettimeofday(&now, NULL);
140 if (now.tv_sec >= deadline) {
141 /* deadline reached already */
143 timeout->tv_usec = 0;
145 /* deadline in future */
146 timeout->tv_sec = deadline - now.tv_sec - 1;
147 timeout->tv_usec = 999999 - now.tv_usec;
148 /* yes, this is 1usec early; sue me */
155 * Read input from IOP and enqueue it.
156 * Wait at most until DEADLINE for input to arrive. (time_t)-1 means
157 * wait as long as it takes (no timeout).
158 * Does not yield the processor when DEADLINE is zero.
159 * A wait for input can be cut short by empth_wakeup().
160 * Return number of bytes read on success, -1 on error.
161 * In particular, return zero on timeout, early wakeup or EOF. Use
162 * io_eof() to distinguish timeout and early wakeup from EOF.
165 io_input(struct iop *iop, time_t deadline)
167 struct timeval timeout;
168 char buf[IO_BUFSIZE];
172 if ((iop->flags & IO_READ) == 0)
174 if (iop->flags & IO_ERROR)
176 if (iop->flags & IO_EOF)
180 res = empth_select(iop->fd, EMPTH_FD_READ,
181 io_timeout(&timeout, deadline));
183 iop->flags |= IO_ERROR;
189 cc = read(iop->fd, buf, sizeof(buf));
191 if (errno == EAGAIN || errno == EWOULDBLOCK)
193 iop->flags |= IO_ERROR;
197 iop->flags |= IO_EOF;
201 ioq_append(iop->input, buf, cc);
206 io_inputwaiting(struct iop *iop)
208 return ioq_qsize(iop->input);
212 io_outputwaiting(struct iop *iop)
214 return ioq_qsize(iop->output);
218 * Write output queued in IOP.
219 * If WAIT, writing may put the thread to sleep.
220 * Return number of bytes written on success, -1 on error.
221 * In particular, return zero when nothing was written because the
222 * queue was empty, or because the write slept and got woken up (only
223 * if WAIT), or because the write refused to sleep (only if !WAIT).
226 io_output(struct iop *iop, int wait)
228 struct iovec iov[16];
234 if ((iop->flags & IO_WRITE) == 0)
237 if (iop->flags & IO_ERROR)
240 if (!ioq_qsize(iop->output))
244 res = empth_select(iop->fd, EMPTH_FD_WRITE, NULL);
248 iop->flags |= IO_ERROR;
253 n = ioq_makeiov(iop->output, iov, IO_BUFSIZE);
254 cc = writev(iop->fd, iov, n);
256 if (errno == EAGAIN || errno == EWOULDBLOCK)
258 iop->flags |= IO_ERROR;
262 ioq_dequeue(iop->output, cc);
263 iop->last_out = ioq_qsize(iop->output);
268 * Write output queued in IOP if enough have been enqueued.
269 * Write if at least one buffer has been filled since the last write.
270 * If WAIT, writing may put the thread to sleep.
271 * Return number of bytes written on success, -1 on error.
272 * In particular, return zero when nothing was written because the
273 * queue was not long, or the write slept and got woken up (only if
274 * WAIT), or the write refused to sleep (only if !WAIT).
277 io_output_if_queue_long(struct iop *iop, int wait)
279 int len = ioq_qsize(iop->output);
281 if (CANT_HAPPEN(iop->last_out > len))
283 if (len - iop->last_out < iop->bufsize) {
288 return io_output(iop, wait);
292 io_peek(struct iop *iop, char *buf, int nbytes)
294 if ((iop->flags & IO_READ) == 0)
296 return ioq_peek(iop->input, buf, nbytes);
300 io_read(struct iop *iop, char *buf, int nbytes)
304 if ((iop->flags & IO_READ) == 0)
306 cc = ioq_peek(iop->input, buf, nbytes);
308 ioq_dequeue(iop->input, cc);
313 io_write(struct iop *iop, char *buf, int nbytes)
315 if ((iop->flags & IO_WRITE) == 0)
317 ioq_append(iop->output, buf, nbytes);
322 io_gets(struct iop *iop, char *buf, int nbytes)
324 if ((iop->flags & IO_READ) == 0)
326 return ioq_gets(iop->input, buf, nbytes);
330 io_puts(struct iop *iop, char *buf)
332 if ((iop->flags & IO_WRITE) == 0)
334 return ioq_puts(iop->output, buf);
338 io_shutdown(struct iop *iop, int flags)
340 flags &= (IO_READ | IO_WRITE);
341 if ((iop->flags & flags) != flags)
343 if (flags & IO_READ) {
344 shutdown(iop->fd, 0);
345 ioq_drain(iop->input);
347 if (flags & IO_WRITE) {
348 shutdown(iop->fd, 1);
349 ioq_drain(iop->output);
356 io_error(struct iop *iop)
358 return iop->flags & IO_ERROR;
362 io_eof(struct iop *iop)
364 return iop->flags & IO_EOF;
368 * Discard IOP's buffered input and set its EOF flag.
369 * No more input can be read from IOP.
372 io_set_eof(struct iop *iop)
374 ioq_drain(iop->input);
375 iop->flags |= IO_EOF;
379 io_fileno(struct iop *iop)