2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2007, 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/types.h>
53 #include <sys/socket.h>
64 #include "empthread.h"
68 struct ioqueue *input;
69 struct ioqueue *output;
80 io_open(int fd, int flags, int bufsize)
84 flags = flags & (IO_READ | IO_WRITE | IO_NBLOCK | IO_NEWSOCK);
85 if ((flags & (IO_READ | IO_WRITE)) == 0)
87 iop = malloc(sizeof(struct iop));
94 iop->bufsize = bufsize;
95 if ((flags & IO_READ) && (flags & IO_NEWSOCK) == 0)
96 iop->input = ioq_create(bufsize);
97 if ((flags & IO_WRITE) && (flags & IO_NEWSOCK) == 0)
98 iop->output = ioq_create(bufsize);
99 if (flags & IO_NBLOCK)
100 io_noblocking(iop, 1); /* FIXME check success */
106 io_close(struct iop *iop)
110 ioq_destroy(iop->input);
111 if (iop->output != 0)
112 ioq_destroy(iop->output);
113 (void)close(iop->fd);
118 io_input(struct iop *iop, int waitforinput)
120 char buf[IO_BUFSIZE];
124 if ((iop->flags & IO_READ) == 0)
126 /* IOP is markes as in error. */
127 if (iop->flags & IO_ERROR)
129 /* Wait for the file to have input. */
131 empth_select(iop->fd, EMPTH_FD_READ);
133 /* Do the actual read. */
134 cc = read(iop->fd, buf, sizeof(buf));
136 /* would block, so nothing to read. */
137 if (errno == EAGAIN || errno == EWOULDBLOCK)
140 /* Some form of file error occurred... */
141 iop->flags |= IO_ERROR;
147 iop->flags |= IO_EOF;
151 /* Append the input to the IOQ. */
152 ioq_append(iop->input, buf, cc);
157 io_inputwaiting(struct iop *iop)
159 return ioq_qsize(iop->input);
163 io_outputwaiting(struct iop *iop)
165 return ioq_qsize(iop->output);
169 io_output(struct iop *iop, int waitforoutput)
172 struct iovec iov[16];
174 char buf[IO_BUFSIZE];
180 /* If there is no output waiting. */
181 if (!io_outputwaiting(iop))
184 /* If the iop is not write enabled. */
185 if ((iop->flags & IO_WRITE) == 0)
188 /* If the io is marked as in error... */
189 if (iop->flags & IO_ERROR)
193 /* make the iov point to the data in the queue. */
194 /* I.E., each of the elements in the queue. */
195 /* returns the number of elements in the iov. */
196 n = ioq_makeiov(iop->output, iov, IO_BUFSIZE);
198 /* Make a buffer containing the output to write. */
199 n = ioq_makebuf(iop->output, buf, sizeof(buf));
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);
213 /* Do the actual write. */
215 cc = writev(iop->fd, iov, n);
217 cc = write(iop->fd, buf, n);
220 /* if it failed.... */
222 /* Hmm, it would block. file is opened noblock, soooooo.. */
223 if (errno == EAGAIN || errno == EWOULDBLOCK) {
224 /* If there are remaining bytes, set the IO as remaining.. */
225 remain = ioq_qsize(iop->output);
228 iop->flags |= IO_ERROR;
232 /* If no bytes were written, something happened.. Like an EOF. */
234 iop->flags |= IO_EOF;
238 /* Remove the number of written bytes from the queue. */
239 ioq_dequeue(iop->output, cc);
245 io_peek(struct iop *iop, char *buf, int nbytes)
247 if ((iop->flags & IO_READ) == 0)
249 return ioq_peek(iop->input, buf, nbytes);
253 io_read(struct iop *iop, char *buf, int nbytes)
257 if ((iop->flags & IO_READ) == 0)
259 cc = ioq_peek(iop->input, buf, nbytes);
261 ioq_dequeue(iop->input, cc);
266 io_write(struct iop *iop, char *buf, int nbytes, int doWait)
270 if ((iop->flags & IO_WRITE) == 0)
272 ioq_append(iop->output, buf, nbytes);
273 len = ioq_qsize(iop->output);
274 if (len > iop->bufsize) {
278 /* only try a write every BUFSIZE characters */
279 if (((len - nbytes) % iop->bufsize) < (len % iop->bufsize))
280 io_output(iop, IO_NOWAIT);
287 io_output_all(struct iop *iop)
292 * Mustn't block a player thread while update is pending, or else
293 * a malicous player could delay the update indefinitely
295 while ((n = io_output(iop, IO_NOWAIT)) > 0 && !play_wrlock_wanted)
296 empth_select(iop->fd, EMPTH_FD_WRITE);
302 io_gets(struct iop *iop, char *buf, int nbytes)
304 if ((iop->flags & IO_READ) == 0)
306 return ioq_gets(iop->input, buf, nbytes);
310 io_puts(struct iop *iop, char *buf)
312 if ((iop->flags & IO_WRITE) == 0)
314 return ioq_puts(iop->output, buf);
318 io_shutdown(struct iop *iop, int flags)
320 flags &= (IO_READ | IO_WRITE);
321 if ((iop->flags & flags) != flags)
323 if (flags & IO_READ) {
324 shutdown(iop->fd, 0);
325 ioq_drain(iop->input);
327 if (flags & IO_WRITE) {
328 shutdown(iop->fd, 1);
329 ioq_drain(iop->output);
335 io_noblocking(struct iop *iop, int value)
339 flags = fcntl(iop->fd, F_GETFL, 0);
343 flags &= ~O_NONBLOCK;
346 if (fcntl(iop->fd, F_SETFL, flags) < 0)
349 iop->flags &= ~IO_NBLOCK;
351 iop->flags |= IO_NBLOCK;
356 io_error(struct iop *iop)
358 return iop->flags & IO_ERROR;
362 io_eof(struct iop *iop)
364 return iop->flags & IO_EOF;
368 io_fileno(struct iop *iop)