2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2006, 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.
45 #include <sys/types.h>
49 #include <unistd.h> /* close read shutdown select */
50 #include <sys/socket.h>
54 #include <stdlib.h> /* malloc calloc free */
66 #include "gen.h" /* getfdtablesize */
69 #include "empthread.h"
73 struct ioqueue *input;
74 struct ioqueue *output;
87 io_open(int fd, int flags, int bufsize, int (*notify)(void),
92 flags = flags & (IO_READ | IO_WRITE | IO_NBLOCK | IO_NEWSOCK);
93 if ((flags & (IO_READ | IO_WRITE)) == 0)
95 iop = malloc(sizeof(struct iop));
102 iop->bufsize = bufsize;
103 if ((flags & IO_READ) && (flags & IO_NEWSOCK) == 0)
104 iop->input = ioq_create(bufsize);
105 if ((flags & IO_WRITE) && (flags & IO_NEWSOCK) == 0)
106 iop->output = ioq_create(bufsize);
107 if (flags & IO_NBLOCK)
108 io_noblocking(iop, 1); /* FIXME check success */
111 iop->notify = notify;
116 io_close(struct iop *iop)
120 ioq_destroy(iop->input);
121 if (iop->output != 0)
122 ioq_destroy(iop->output);
124 (void)close(iop->fd);
126 closesocket(iop->fd);
132 io_input(struct iop *iop, int waitforinput)
134 s_char buf[IO_BUFSIZE];
138 if ((iop->flags & IO_READ) == 0)
140 /* IOP is markes as in error. */
141 if (iop->flags & IO_ERROR)
143 /* Wait for the file to have input. */
145 empth_select(iop->fd, EMPTH_FD_READ);
148 /* Do the actual read. */
149 cc = read(iop->fd, buf, sizeof(buf));
151 /* would block, so nothing to read. */
152 if (errno == EAGAIN || errno == EWOULDBLOCK)
155 /* Some form of file error occurred... */
156 iop->flags |= IO_ERROR;
160 cc = recv(iop->fd, buf, sizeof(buf), 0);
161 if (cc == SOCKET_ERROR) {
162 int err = WSAGetLastError();
163 /* Hmm, it would block. file is opened noblock, soooooo.. */
164 if (err == WSAEWOULDBLOCK)
167 /* Some form of file error occurred... */
168 iop->flags |= IO_ERROR;
175 iop->flags |= IO_EOF;
179 /* Append the input to the IOQ. */
180 ioq_append(iop->input, buf, cc);
185 io_inputwaiting(struct iop *iop)
187 return ioq_qsize(iop->input);
191 io_outputwaiting(struct iop *iop)
193 return ioq_qsize(iop->output);
197 io_output(struct iop *iop, int waitforoutput)
200 struct iovec iov[16];
202 s_char buf[IO_BUFSIZE];
208 /* If there is no output waiting. */
209 if (!io_outputwaiting(iop))
212 /* If the iop is not write enabled. */
213 if ((iop->flags & IO_WRITE) == 0)
216 /* If the io is marked as in error... */
217 if (iop->flags & IO_ERROR)
221 /* make the iov point to the data in the queue. */
222 /* I.E., each of the elements in the queue. */
223 /* returns the number of elements in the iov. */
224 n = ioq_makeiov(iop->output, iov, IO_BUFSIZE);
226 /* Make a buffer containing the output to write. */
227 n = ioq_makebuf(iop->output, buf, sizeof(buf));
234 /* wait for the file to be output ready. */
235 if (waitforoutput != IO_NOWAIT) {
236 /* This waits for the file to be ready for writing, */
237 /* and lets other threads run. */
238 empth_select(iop->fd, EMPTH_FD_WRITE);
241 /* Do the actual write. */
243 cc = writev(iop->fd, iov, n);
245 /* if it failed.... */
247 /* Hmm, it would block. file is opened noblock, soooooo.. */
248 if (errno == EAGAIN || errno == EWOULDBLOCK) {
249 /* If there are remaining bytes, set the IO as remaining.. */
250 remain = ioq_qsize(iop->output);
253 iop->flags |= IO_ERROR;
257 cc = send(iop->fd, buf, n, 0);
259 /* if it failed.... */
260 if (cc == SOCKET_ERROR) {
261 int err = WSAGetLastError();
262 /* Hmm, it would block. file is opened noblock, soooooo.. */
263 if (err == WSAEWOULDBLOCK) {
264 /* If there are remaining bytes, set the IO as remaining.. */
265 remain = ioq_qsize(iop->output);
268 iop->flags |= IO_ERROR;
273 /* If no bytes were written, something happened.. Like an EOF. */
275 iop->flags |= IO_EOF;
279 /* Remove the number of written bytes from the queue. */
280 ioq_dequeue(iop->output, cc);
286 io_peek(struct iop *iop, s_char *buf, int nbytes)
288 if ((iop->flags & IO_READ) == 0)
290 return ioq_peek(iop->input, buf, nbytes);
294 io_read(struct iop *iop, s_char *buf, int nbytes)
298 if ((iop->flags & IO_READ) == 0)
300 cc = ioq_peek(iop->input, buf, nbytes);
302 ioq_dequeue(iop->input, cc);
307 io_write(struct iop *iop, s_char *buf, int nbytes, int doWait)
311 if ((iop->flags & IO_WRITE) == 0)
313 ioq_append(iop->output, buf, nbytes);
314 len = ioq_qsize(iop->output);
315 if (len > iop->bufsize) {
319 /* only try a write every BUFSIZE characters */
320 if (((len - nbytes) % iop->bufsize) < (len % iop->bufsize))
321 io_output(iop, IO_NOWAIT);
328 io_output_all(struct iop *iop)
333 * Mustn't block a player thread while update is pending, or else
334 * a malicous player could delay the update indefinitely
336 while (((n = io_output(iop, IO_NOWAIT)) > 0) && !update_pending) {
337 empth_select(iop->fd, EMPTH_FD_WRITE);
343 io_gets(struct iop *iop, s_char *buf, int nbytes)
345 if ((iop->flags & IO_READ) == 0)
347 return ioq_gets(iop->input, buf, nbytes);
351 io_puts(struct iop *iop, s_char *buf)
353 if ((iop->flags & IO_WRITE) == 0)
355 return ioq_puts(iop->output, buf);
359 io_shutdown(struct iop *iop, int flags)
361 flags &= (IO_READ | IO_WRITE);
362 if ((iop->flags & flags) != flags)
364 if (flags & IO_READ) {
365 shutdown(iop->fd, 0);
366 ioq_drain(iop->input);
368 if (flags & IO_WRITE) {
369 shutdown(iop->fd, 1);
370 ioq_drain(iop->output);
376 io_noblocking(struct iop *iop, int value)
381 flags = fcntl(iop->fd, F_GETFL, 0);
385 flags &= ~O_NONBLOCK;
388 if (fcntl(iop->fd, F_SETFL, flags) < 0)
392 ioctlsocket(iop->fd, FIONBIO, &arg);
395 iop->flags &= ~IO_NBLOCK;
397 iop->flags |= IO_NBLOCK;
402 io_error(struct iop *iop)
404 return iop->flags & IO_ERROR;
408 io_eof(struct iop *iop)
410 return iop->flags & IO_EOF;
414 io_fileno(struct iop *iop)