2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2008, 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:
36 * Arrange for input and output on a file descriptor
37 * to be queued. Provide main loop -- a mechanism for
38 * blocking across all registered file descriptors, and
39 * reading or writing when appropriate.
47 #include <sys/socket.h>
48 #include <sys/types.h>
52 #include "empthread.h"
60 struct ioqueue *input;
61 struct ioqueue *output;
64 struct timeval input_timeout;
73 io_open(int fd, int flags, int bufsize, struct timeval timeout)
77 flags = flags & (IO_READ | IO_WRITE | IO_NBLOCK | IO_NEWSOCK);
78 if ((flags & (IO_READ | IO_WRITE)) == 0)
80 iop = malloc(sizeof(struct iop));
87 iop->input_timeout = timeout;
88 iop->bufsize = bufsize;
89 if ((flags & IO_READ) && (flags & IO_NEWSOCK) == 0)
90 iop->input = ioq_create(bufsize);
91 if ((flags & IO_WRITE) && (flags & IO_NEWSOCK) == 0)
92 iop->output = ioq_create(bufsize);
93 if (flags & IO_NBLOCK)
94 io_noblocking(iop, 1); /* FIXME check success */
100 io_close(struct iop *iop)
104 ioq_destroy(iop->input);
105 if (iop->output != 0)
106 ioq_destroy(iop->output);
107 (void)close(iop->fd);
112 * Return number of bytes read on success, zero on timeout or EOF, -1
113 * on error, with errno set appropriately. In particular, return -1
114 * with errno set to EAGAIN or EWOULDBLOCK when no data is available
115 * for non-blocking input (WAITFORINPUT false). Use io_eof() to
116 * distinguish timeout from EOF.
119 io_input(struct iop *iop, int waitforinput)
121 char buf[IO_BUFSIZE];
124 struct timeval timeout = iop->input_timeout;
127 if ((iop->flags & IO_READ) == 0) {
131 /* IOP is markes as in error. */
132 if (iop->flags & IO_ERROR) {
136 /* Wait for the file to have input. */
138 res = empth_select(iop->fd, EMPTH_FD_READ, &timeout);
140 iop->flags |= IO_ERROR;
146 /* Do the actual read. */
147 cc = read(iop->fd, buf, sizeof(buf));
149 if (errno != EAGAIN && errno != EWOULDBLOCK)
150 /* Some form of file error occurred... */
151 iop->flags |= IO_ERROR;
157 iop->flags |= IO_EOF;
161 /* Append the input to the IOQ. */
162 ioq_append(iop->input, buf, cc);
167 io_inputwaiting(struct iop *iop)
169 return ioq_qsize(iop->input);
173 io_outputwaiting(struct iop *iop)
175 return ioq_qsize(iop->output);
179 io_output(struct iop *iop, int waitforoutput)
181 struct iovec iov[16];
186 /* If there is no output waiting. */
187 if (!io_outputwaiting(iop))
190 /* If the iop is not write enabled. */
191 if ((iop->flags & IO_WRITE) == 0)
194 /* If the io is marked as in error... */
195 if (iop->flags & IO_ERROR)
198 /* make the iov point to the data in the queue. */
199 /* I.E., each of the elements in the queue. */
200 /* returns the number of elements in the iov. */
201 n = ioq_makeiov(iop->output, iov, IO_BUFSIZE);
207 /* wait for the file to be output ready. */
208 if (waitforoutput != IO_NOWAIT) {
209 /* This waits for the file to be ready for writing, */
210 /* and lets other threads run. */
211 empth_select(iop->fd, EMPTH_FD_WRITE, NULL);
214 /* Do the actual write. */
215 cc = writev(iop->fd, iov, n);
217 /* if it failed.... */
219 /* Hmm, it would block. file is opened noblock, soooooo.. */
220 if (errno == EAGAIN || errno == EWOULDBLOCK) {
221 /* If there are remaining bytes, set the IO as remaining.. */
222 remain = ioq_qsize(iop->output);
225 iop->flags |= IO_ERROR;
229 /* If no bytes were written, something happened.. Like an EOF. */
231 iop->flags |= IO_EOF;
235 /* Remove the number of written bytes from the queue. */
236 ioq_dequeue(iop->output, cc);
242 io_peek(struct iop *iop, char *buf, int nbytes)
244 if ((iop->flags & IO_READ) == 0)
246 return ioq_peek(iop->input, buf, nbytes);
250 io_read(struct iop *iop, char *buf, int nbytes)
254 if ((iop->flags & IO_READ) == 0)
256 cc = ioq_peek(iop->input, buf, nbytes);
258 ioq_dequeue(iop->input, cc);
263 io_write(struct iop *iop, char *buf, int nbytes, int doWait)
267 if ((iop->flags & IO_WRITE) == 0)
269 ioq_append(iop->output, buf, nbytes);
270 len = ioq_qsize(iop->output);
271 if (len > iop->bufsize) {
275 /* only try a write every BUFSIZE characters */
276 if (((len - nbytes) % iop->bufsize) < (len % iop->bufsize))
277 io_output(iop, IO_NOWAIT);
284 io_output_all(struct iop *iop)
289 * Mustn't block a player thread while update is pending, or else
290 * a malicous player could delay the update indefinitely
292 while ((n = io_output(iop, IO_NOWAIT)) > 0 && !play_wrlock_wanted)
293 empth_select(iop->fd, EMPTH_FD_WRITE, NULL);
299 io_gets(struct iop *iop, char *buf, int nbytes)
301 if ((iop->flags & IO_READ) == 0)
303 return ioq_gets(iop->input, buf, nbytes);
307 io_puts(struct iop *iop, char *buf)
309 if ((iop->flags & IO_WRITE) == 0)
311 return ioq_puts(iop->output, buf);
315 io_shutdown(struct iop *iop, int flags)
317 flags &= (IO_READ | IO_WRITE);
318 if ((iop->flags & flags) != flags)
320 if (flags & IO_READ) {
321 shutdown(iop->fd, 0);
322 ioq_drain(iop->input);
324 if (flags & IO_WRITE) {
325 shutdown(iop->fd, 1);
326 ioq_drain(iop->output);
332 io_noblocking(struct iop *iop, int value)
336 flags = fcntl(iop->fd, F_GETFL, 0);
340 flags &= ~O_NONBLOCK;
343 if (fcntl(iop->fd, F_SETFL, flags) < 0)
346 iop->flags &= ~IO_NBLOCK;
348 iop->flags |= IO_NBLOCK;
353 io_error(struct iop *iop)
355 return iop->flags & IO_ERROR;
359 io_eof(struct iop *iop)
361 return iop->flags & IO_EOF;
365 io_fileno(struct iop *iop)