2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2009, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
6 * This program 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 2 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, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * See files README, COPYING and CREDITS in the root of the source
23 * tree for related information and legal notices. It is expected
24 * that future projects/authors will amend these files as needed.
28 * io.c: Arrange for input and output on a file descriptor to be queued.
30 * Known contributors to this file:
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;
65 struct timeval input_timeout;
74 io_open(int fd, int flags, int bufsize, struct timeval timeout)
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));
97 iop->bufsize = bufsize;
98 iop->input_timeout = timeout;
100 iop->input = ioq_create(bufsize);
101 if (flags & IO_WRITE)
102 iop->output = ioq_create(bufsize);
107 io_close(struct iop *iop)
111 ioq_destroy(iop->input);
113 ioq_destroy(iop->output);
114 (void)close(iop->fd);
119 * Return number of bytes read on success, zero on timeout, early
120 * wakeup or EOF, -1 on error, with errno set appropriately. In
121 * particular, return -1 with errno set to EAGAIN or EWOULDBLOCK when
122 * no data is available for non-blocking input (WAITFORINPUT false).
123 * Use io_eof() to distinguish timeout and early wakeup from EOF.
126 io_input(struct iop *iop, int waitforinput)
128 char buf[IO_BUFSIZE];
133 if ((iop->flags & IO_READ) == 0) {
137 /* IOP is markes as in error. */
138 if (iop->flags & IO_ERROR) {
142 /* Wait for the file to have input. */
144 res = empth_select(iop->fd, EMPTH_FD_READ, &iop->input_timeout);
146 iop->flags |= IO_ERROR;
152 /* Do the actual read. */
153 cc = read(iop->fd, buf, sizeof(buf));
155 if (errno != EAGAIN && errno != EWOULDBLOCK)
156 /* Some form of file error occurred... */
157 iop->flags |= IO_ERROR;
163 iop->flags |= IO_EOF;
167 /* Append the input to the IOQ. */
168 ioq_append(iop->input, buf, cc);
173 io_inputwaiting(struct iop *iop)
175 return ioq_qsize(iop->input);
179 io_outputwaiting(struct iop *iop)
181 return ioq_qsize(iop->output);
185 * Write output queued in IOP.
186 * If WAITFOROUTPUT != IO_NOWAIT, writing may put the thread to sleep.
187 * Return number of bytes written on success, -1 on error.
188 * In particular, return zero when nothing was written because the
189 * queue was empty, or because the write slept and got woken up (only
190 * if WAITFOROUTPUT != IO_NOWAIT), or because the write refused to
191 * sleep (only if WAITFOROUTPUT == IO_NOWAIT).
194 io_output(struct iop *iop, int waitforoutput)
196 struct iovec iov[16];
199 if (!ioq_qsize(iop->output))
202 if ((iop->flags & IO_WRITE) == 0)
205 if (iop->flags & IO_ERROR)
208 n = ioq_makeiov(iop->output, iov, IO_BUFSIZE);
210 if (waitforoutput != IO_NOWAIT) {
211 res = empth_select(iop->fd, EMPTH_FD_WRITE, NULL);
215 iop->flags |= IO_ERROR;
220 cc = writev(iop->fd, iov, n);
222 if (errno == EAGAIN || errno == EWOULDBLOCK)
224 iop->flags |= IO_ERROR;
228 ioq_dequeue(iop->output, cc);
233 io_peek(struct iop *iop, char *buf, int nbytes)
235 if ((iop->flags & IO_READ) == 0)
237 return ioq_peek(iop->input, buf, nbytes);
241 io_read(struct iop *iop, char *buf, int nbytes)
245 if ((iop->flags & IO_READ) == 0)
247 cc = ioq_peek(iop->input, buf, nbytes);
249 ioq_dequeue(iop->input, cc);
254 io_write(struct iop *iop, char *buf, int nbytes, int doWait)
258 if ((iop->flags & IO_WRITE) == 0)
260 ioq_append(iop->output, buf, nbytes);
261 len = ioq_qsize(iop->output);
262 if (len > iop->bufsize) {
266 /* only try a write every BUFSIZE characters */
267 if (((len - nbytes) % iop->bufsize) < (len % iop->bufsize))
268 io_output(iop, IO_NOWAIT);
275 io_output_all(struct iop *iop)
280 * Mustn't block a player thread while update is pending, or else
281 * a malicous player could delay the update indefinitely
283 while ((n = io_output(iop, IO_NOWAIT)) > 0 && !play_wrlock_wanted)
284 empth_select(iop->fd, EMPTH_FD_WRITE, NULL);
290 io_gets(struct iop *iop, char *buf, int nbytes)
292 if ((iop->flags & IO_READ) == 0)
294 return ioq_gets(iop->input, buf, nbytes);
298 io_puts(struct iop *iop, char *buf)
300 if ((iop->flags & IO_WRITE) == 0)
302 return ioq_puts(iop->output, buf);
306 io_shutdown(struct iop *iop, int flags)
308 flags &= (IO_READ | IO_WRITE);
309 if ((iop->flags & flags) != flags)
311 if (flags & IO_READ) {
312 shutdown(iop->fd, 0);
313 ioq_drain(iop->input);
315 if (flags & IO_WRITE) {
316 shutdown(iop->fd, 1);
317 ioq_drain(iop->output);
323 io_error(struct iop *iop)
325 return iop->flags & IO_ERROR;
329 io_eof(struct iop *iop)
331 return iop->flags & IO_EOF;
335 io_fileno(struct iop *iop)