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>
52 #include <sys/socket.h>
62 #include "empthread.h"
66 struct ioqueue *input;
67 struct ioqueue *output;
78 io_open(int fd, int flags, int bufsize)
82 flags = flags & (IO_READ | IO_WRITE | IO_NBLOCK | IO_NEWSOCK);
83 if ((flags & (IO_READ | IO_WRITE)) == 0)
85 iop = malloc(sizeof(struct iop));
92 iop->bufsize = bufsize;
93 if ((flags & IO_READ) && (flags & IO_NEWSOCK) == 0)
94 iop->input = ioq_create(bufsize);
95 if ((flags & IO_WRITE) && (flags & IO_NEWSOCK) == 0)
96 iop->output = ioq_create(bufsize);
97 if (flags & IO_NBLOCK)
98 io_noblocking(iop, 1); /* FIXME check success */
104 io_close(struct iop *iop)
108 ioq_destroy(iop->input);
109 if (iop->output != 0)
110 ioq_destroy(iop->output);
111 (void)close(iop->fd);
116 io_input(struct iop *iop, int waitforinput)
118 char buf[IO_BUFSIZE];
122 if ((iop->flags & IO_READ) == 0)
124 /* IOP is markes as in error. */
125 if (iop->flags & IO_ERROR)
127 /* Wait for the file to have input. */
129 empth_select(iop->fd, EMPTH_FD_READ);
131 /* Do the actual read. */
132 cc = read(iop->fd, buf, sizeof(buf));
134 /* would block, so nothing to read. */
135 if (errno == EAGAIN || errno == EWOULDBLOCK)
138 /* Some form of file error occurred... */
139 iop->flags |= IO_ERROR;
145 iop->flags |= IO_EOF;
149 /* Append the input to the IOQ. */
150 ioq_append(iop->input, buf, cc);
155 io_inputwaiting(struct iop *iop)
157 return ioq_qsize(iop->input);
161 io_outputwaiting(struct iop *iop)
163 return ioq_qsize(iop->output);
167 io_output(struct iop *iop, int waitforoutput)
169 struct iovec iov[16];
174 /* If there is no output waiting. */
175 if (!io_outputwaiting(iop))
178 /* If the iop is not write enabled. */
179 if ((iop->flags & IO_WRITE) == 0)
182 /* If the io is marked as in error... */
183 if (iop->flags & IO_ERROR)
186 /* make the iov point to the data in the queue. */
187 /* I.E., each of the elements in the queue. */
188 /* returns the number of elements in the iov. */
189 n = ioq_makeiov(iop->output, iov, IO_BUFSIZE);
195 /* wait for the file to be output ready. */
196 if (waitforoutput != IO_NOWAIT) {
197 /* This waits for the file to be ready for writing, */
198 /* and lets other threads run. */
199 empth_select(iop->fd, EMPTH_FD_WRITE);
202 /* Do the actual write. */
203 cc = writev(iop->fd, iov, n);
205 /* if it failed.... */
207 /* Hmm, it would block. file is opened noblock, soooooo.. */
208 if (errno == EAGAIN || errno == EWOULDBLOCK) {
209 /* If there are remaining bytes, set the IO as remaining.. */
210 remain = ioq_qsize(iop->output);
213 iop->flags |= IO_ERROR;
217 /* If no bytes were written, something happened.. Like an EOF. */
219 iop->flags |= IO_EOF;
223 /* Remove the number of written bytes from the queue. */
224 ioq_dequeue(iop->output, cc);
230 io_peek(struct iop *iop, char *buf, int nbytes)
232 if ((iop->flags & IO_READ) == 0)
234 return ioq_peek(iop->input, buf, nbytes);
238 io_read(struct iop *iop, char *buf, int nbytes)
242 if ((iop->flags & IO_READ) == 0)
244 cc = ioq_peek(iop->input, buf, nbytes);
246 ioq_dequeue(iop->input, cc);
251 io_write(struct iop *iop, char *buf, int nbytes, int doWait)
255 if ((iop->flags & IO_WRITE) == 0)
257 ioq_append(iop->output, buf, nbytes);
258 len = ioq_qsize(iop->output);
259 if (len > iop->bufsize) {
263 /* only try a write every BUFSIZE characters */
264 if (((len - nbytes) % iop->bufsize) < (len % iop->bufsize))
265 io_output(iop, IO_NOWAIT);
272 io_output_all(struct iop *iop)
277 * Mustn't block a player thread while update is pending, or else
278 * a malicous player could delay the update indefinitely
280 while ((n = io_output(iop, IO_NOWAIT)) > 0 && !play_wrlock_wanted)
281 empth_select(iop->fd, EMPTH_FD_WRITE);
287 io_gets(struct iop *iop, char *buf, int nbytes)
289 if ((iop->flags & IO_READ) == 0)
291 return ioq_gets(iop->input, buf, nbytes);
295 io_puts(struct iop *iop, char *buf)
297 if ((iop->flags & IO_WRITE) == 0)
299 return ioq_puts(iop->output, buf);
303 io_shutdown(struct iop *iop, int flags)
305 flags &= (IO_READ | IO_WRITE);
306 if ((iop->flags & flags) != flags)
308 if (flags & IO_READ) {
309 shutdown(iop->fd, 0);
310 ioq_drain(iop->input);
312 if (flags & IO_WRITE) {
313 shutdown(iop->fd, 1);
314 ioq_drain(iop->output);
320 io_noblocking(struct iop *iop, int value)
324 flags = fcntl(iop->fd, F_GETFL, 0);
328 flags &= ~O_NONBLOCK;
331 if (fcntl(iop->fd, F_SETFL, flags) < 0)
334 iop->flags &= ~IO_NBLOCK;
336 iop->flags |= IO_NBLOCK;
341 io_error(struct iop *iop)
343 return iop->flags & IO_ERROR;
347 io_eof(struct iop *iop)
349 return iop->flags & IO_EOF;
353 io_fileno(struct iop *iop)