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];
126 if ((iop->flags & IO_READ) == 0) {
130 /* IOP is markes as in error. */
131 if (iop->flags & IO_ERROR) {
135 /* Wait for the file to have input. */
137 res = empth_select(iop->fd, EMPTH_FD_READ, &iop->input_timeout);
139 iop->flags |= IO_ERROR;
145 /* Do the actual read. */
146 cc = read(iop->fd, buf, sizeof(buf));
148 if (errno != EAGAIN && errno != EWOULDBLOCK)
149 /* Some form of file error occurred... */
150 iop->flags |= IO_ERROR;
156 iop->flags |= IO_EOF;
160 /* Append the input to the IOQ. */
161 ioq_append(iop->input, buf, cc);
166 io_inputwaiting(struct iop *iop)
168 return ioq_qsize(iop->input);
172 io_outputwaiting(struct iop *iop)
174 return ioq_qsize(iop->output);
178 io_output(struct iop *iop, int waitforoutput)
180 struct iovec iov[16];
185 /* If there is no output waiting. */
186 if (!io_outputwaiting(iop))
189 /* If the iop is not write enabled. */
190 if ((iop->flags & IO_WRITE) == 0)
193 /* If the io is marked as in error... */
194 if (iop->flags & IO_ERROR)
197 /* make the iov point to the data in the queue. */
198 /* I.E., each of the elements in the queue. */
199 /* returns the number of elements in the iov. */
200 n = ioq_makeiov(iop->output, iov, IO_BUFSIZE);
206 /* wait for the file to be output ready. */
207 if (waitforoutput != IO_NOWAIT) {
208 /* This waits for the file to be ready for writing, */
209 /* and lets other threads run. */
210 empth_select(iop->fd, EMPTH_FD_WRITE, NULL);
213 /* Do the actual write. */
214 cc = writev(iop->fd, iov, n);
216 /* if it failed.... */
218 /* Hmm, it would block. file is opened noblock, soooooo.. */
219 if (errno == EAGAIN || errno == EWOULDBLOCK) {
220 /* If there are remaining bytes, set the IO as remaining.. */
221 remain = ioq_qsize(iop->output);
224 iop->flags |= IO_ERROR;
228 /* If no bytes were written, something happened.. Like an EOF. */
230 iop->flags |= IO_EOF;
234 /* Remove the number of written bytes from the queue. */
235 ioq_dequeue(iop->output, cc);
241 io_peek(struct iop *iop, char *buf, int nbytes)
243 if ((iop->flags & IO_READ) == 0)
245 return ioq_peek(iop->input, buf, nbytes);
249 io_read(struct iop *iop, char *buf, int nbytes)
253 if ((iop->flags & IO_READ) == 0)
255 cc = ioq_peek(iop->input, buf, nbytes);
257 ioq_dequeue(iop->input, cc);
262 io_write(struct iop *iop, char *buf, int nbytes, int doWait)
266 if ((iop->flags & IO_WRITE) == 0)
268 ioq_append(iop->output, buf, nbytes);
269 len = ioq_qsize(iop->output);
270 if (len > iop->bufsize) {
274 /* only try a write every BUFSIZE characters */
275 if (((len - nbytes) % iop->bufsize) < (len % iop->bufsize))
276 io_output(iop, IO_NOWAIT);
283 io_output_all(struct iop *iop)
288 * Mustn't block a player thread while update is pending, or else
289 * a malicous player could delay the update indefinitely
291 while ((n = io_output(iop, IO_NOWAIT)) > 0 && !play_wrlock_wanted)
292 empth_select(iop->fd, EMPTH_FD_WRITE, NULL);
298 io_gets(struct iop *iop, char *buf, int nbytes)
300 if ((iop->flags & IO_READ) == 0)
302 return ioq_gets(iop->input, buf, nbytes);
306 io_puts(struct iop *iop, char *buf)
308 if ((iop->flags & IO_WRITE) == 0)
310 return ioq_puts(iop->output, buf);
314 io_shutdown(struct iop *iop, int flags)
316 flags &= (IO_READ | IO_WRITE);
317 if ((iop->flags & flags) != flags)
319 if (flags & IO_READ) {
320 shutdown(iop->fd, 0);
321 ioq_drain(iop->input);
323 if (flags & IO_WRITE) {
324 shutdown(iop->fd, 1);
325 ioq_drain(iop->output);
331 io_noblocking(struct iop *iop, int value)
335 flags = fcntl(iop->fd, F_GETFL, 0);
339 flags &= ~O_NONBLOCK;
342 if (fcntl(iop->fd, F_SETFL, flags) < 0)
345 iop->flags &= ~IO_NBLOCK;
347 iop->flags |= IO_NBLOCK;
352 io_error(struct iop *iop)
354 return iop->flags & IO_ERROR;
358 io_eof(struct iop *iop)
360 return iop->flags & IO_EOF;
364 io_fileno(struct iop *iop)