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 io_output(struct iop *iop, int waitforoutput)
187 struct iovec iov[16];
192 /* If there is no output waiting. */
193 if (!io_outputwaiting(iop))
196 /* If the iop is not write enabled. */
197 if ((iop->flags & IO_WRITE) == 0)
200 /* If the io is marked as in error... */
201 if (iop->flags & IO_ERROR)
204 /* make the iov point to the data in the queue. */
205 /* I.E., each of the elements in the queue. */
206 /* returns the number of elements in the iov. */
207 n = ioq_makeiov(iop->output, iov, IO_BUFSIZE);
213 /* wait for the file to be output ready. */
214 if (waitforoutput != IO_NOWAIT) {
215 /* This waits for the file to be ready for writing, */
216 /* and lets other threads run. */
217 empth_select(iop->fd, EMPTH_FD_WRITE, NULL);
220 /* Do the actual write. */
221 cc = writev(iop->fd, iov, n);
223 /* if it failed.... */
225 /* Hmm, it would block. file is opened noblock, soooooo.. */
226 if (errno == EAGAIN || errno == EWOULDBLOCK) {
227 /* If there are remaining bytes, set the IO as remaining.. */
228 remain = ioq_qsize(iop->output);
231 iop->flags |= IO_ERROR;
235 ioq_dequeue(iop->output, cc);
240 io_peek(struct iop *iop, char *buf, int nbytes)
242 if ((iop->flags & IO_READ) == 0)
244 return ioq_peek(iop->input, buf, nbytes);
248 io_read(struct iop *iop, char *buf, int nbytes)
252 if ((iop->flags & IO_READ) == 0)
254 cc = ioq_peek(iop->input, buf, nbytes);
256 ioq_dequeue(iop->input, cc);
261 io_write(struct iop *iop, char *buf, int nbytes, int doWait)
265 if ((iop->flags & IO_WRITE) == 0)
267 ioq_append(iop->output, buf, nbytes);
268 len = ioq_qsize(iop->output);
269 if (len > iop->bufsize) {
273 /* only try a write every BUFSIZE characters */
274 if (((len - nbytes) % iop->bufsize) < (len % iop->bufsize))
275 io_output(iop, IO_NOWAIT);
282 io_output_all(struct iop *iop)
287 * Mustn't block a player thread while update is pending, or else
288 * a malicous player could delay the update indefinitely
290 while ((n = io_output(iop, IO_NOWAIT)) > 0 && !play_wrlock_wanted)
291 empth_select(iop->fd, EMPTH_FD_WRITE, NULL);
297 io_gets(struct iop *iop, char *buf, int nbytes)
299 if ((iop->flags & IO_READ) == 0)
301 return ioq_gets(iop->input, buf, nbytes);
305 io_puts(struct iop *iop, char *buf)
307 if ((iop->flags & IO_WRITE) == 0)
309 return ioq_puts(iop->output, buf);
313 io_shutdown(struct iop *iop, int flags)
315 flags &= (IO_READ | IO_WRITE);
316 if ((iop->flags & flags) != flags)
318 if (flags & IO_READ) {
319 shutdown(iop->fd, 0);
320 ioq_drain(iop->input);
322 if (flags & IO_WRITE) {
323 shutdown(iop->fd, 1);
324 ioq_drain(iop->output);
330 io_error(struct iop *iop)
332 return iop->flags & IO_ERROR;
336 io_eof(struct iop *iop)
338 return iop->flags & IO_EOF;
342 io_fileno(struct iop *iop)