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;
74 io_open(int fd, int flags, int bufsize)
79 flags = flags & (IO_READ | IO_WRITE);
80 if ((flags & (IO_READ | IO_WRITE)) == 0)
83 fdfl = fcntl(fd, F_GETFL, 0);
87 if (fcntl(fd, F_SETFL, fdfl) < 0)
90 iop = malloc(sizeof(struct iop));
98 iop->bufsize = bufsize;
100 iop->input = ioq_create(bufsize);
101 if (flags & IO_WRITE)
102 iop->output = ioq_create(bufsize);
107 io_close(struct iop *iop, struct timeval *timeout)
109 char buf[IO_BUFSIZE];
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));
120 ioq_destroy(iop->input);
122 ioq_destroy(iop->output);
123 (void)close(iop->fd);
128 io_timeout(struct timeval *timeout, time_t deadline)
132 gettimeofday(&now, NULL);
133 if (now.tv_sec >= deadline) {
134 /* deadline reached already */
136 timeout->tv_usec = 0;
138 /* deadline in future */
139 timeout->tv_sec = deadline - now.tv_sec - 1;
140 timeout->tv_usec = 999999 - now.tv_usec;
141 /* yes, this is 1usec early; sue me */
146 * Read input from IOP and enqueue it.
147 * If TIMEOUT is non-null, wait at most that long for input to arrive.
148 * Does not yield the processor when timeout is zero.
149 * A wait for input can be cut short by empth_wakeup().
150 * Return number of bytes read on success, -1 on error.
151 * In particular, return zero on timeout, early wakeup or EOF. Use
152 * io_eof() to distinguish timeout and early wakeup from EOF.
155 io_input(struct iop *iop, struct timeval *timeout)
157 char buf[IO_BUFSIZE];
161 if ((iop->flags & IO_READ) == 0)
163 if (iop->flags & IO_ERROR)
165 if (iop->flags & IO_EOF)
168 if (!timeout || timeout->tv_sec || timeout->tv_usec) {
169 res = empth_select(iop->fd, EMPTH_FD_READ, timeout);
171 iop->flags |= IO_ERROR;
177 cc = read(iop->fd, buf, sizeof(buf));
179 if (errno == EAGAIN || errno == EWOULDBLOCK)
181 iop->flags |= IO_ERROR;
185 iop->flags |= IO_EOF;
189 ioq_append(iop->input, buf, cc);
194 io_inputwaiting(struct iop *iop)
196 return ioq_qsize(iop->input);
200 io_outputwaiting(struct iop *iop)
202 return ioq_qsize(iop->output);
206 * Write output queued in IOP.
207 * If WAIT, writing may put the thread to sleep.
208 * Return number of bytes written on success, -1 on error.
209 * In particular, return zero when nothing was written because the
210 * queue was empty, or because the write slept and got woken up (only
211 * if WAIT), or because the write refused to sleep (only if !WAIT).
214 io_output(struct iop *iop, int wait)
216 struct iovec iov[16];
222 if ((iop->flags & IO_WRITE) == 0)
225 if (iop->flags & IO_ERROR)
228 if (!ioq_qsize(iop->output))
232 res = empth_select(iop->fd, EMPTH_FD_WRITE, NULL);
236 iop->flags |= IO_ERROR;
241 n = ioq_makeiov(iop->output, iov, IO_BUFSIZE);
242 cc = writev(iop->fd, iov, n);
244 if (errno == EAGAIN || errno == EWOULDBLOCK)
246 iop->flags |= IO_ERROR;
250 ioq_dequeue(iop->output, cc);
251 iop->last_out = ioq_qsize(iop->output);
256 * Write output queued in IOP if enough have been enqueued.
257 * Write if at least one buffer has been filled since the last write.
258 * If WAIT, writing may put the thread to sleep.
259 * Return number of bytes written on success, -1 on error.
260 * In particular, return zero when nothing was written because the
261 * queue was not long, or the write slept and got woken up (only if
262 * WAIT), or the write refused to sleep (only if !WAIT).
265 io_output_if_queue_long(struct iop *iop, int wait)
267 int len = ioq_qsize(iop->output);
269 if (CANT_HAPPEN(iop->last_out > len))
271 if (len - iop->last_out < iop->bufsize) {
276 return io_output(iop, wait);
280 io_peek(struct iop *iop, char *buf, int nbytes)
282 if ((iop->flags & IO_READ) == 0)
284 return ioq_peek(iop->input, buf, nbytes);
288 io_read(struct iop *iop, char *buf, int nbytes)
292 if ((iop->flags & IO_READ) == 0)
294 cc = ioq_peek(iop->input, buf, nbytes);
296 ioq_dequeue(iop->input, cc);
301 io_write(struct iop *iop, char *buf, int nbytes)
303 if ((iop->flags & IO_WRITE) == 0)
305 ioq_append(iop->output, buf, nbytes);
310 io_gets(struct iop *iop, char *buf, int nbytes)
312 if ((iop->flags & IO_READ) == 0)
314 return ioq_gets(iop->input, buf, nbytes);
318 io_puts(struct iop *iop, char *buf)
320 if ((iop->flags & IO_WRITE) == 0)
322 return ioq_puts(iop->output, buf);
326 io_shutdown(struct iop *iop, int flags)
328 flags &= (IO_READ | IO_WRITE);
329 if ((iop->flags & flags) != flags)
331 if (flags & IO_READ) {
332 shutdown(iop->fd, 0);
333 ioq_drain(iop->input);
335 if (flags & IO_WRITE) {
336 shutdown(iop->fd, 1);
337 ioq_drain(iop->output);
344 io_error(struct iop *iop)
346 return iop->flags & IO_ERROR;
350 io_eof(struct iop *iop)
352 return iop->flags & IO_EOF;
356 * Discard IOP's buffered input and set its EOF flag.
357 * No more input can be read from IOP.
360 io_set_eof(struct iop *iop)
362 ioq_drain(iop->input);
363 iop->flags |= IO_EOF;
367 io_fileno(struct iop *iop)