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;
85 io_open(int fd, int flags, int bufsize)
89 flags = flags & (IO_READ | IO_WRITE | IO_NBLOCK | IO_NEWSOCK);
90 if ((flags & (IO_READ | IO_WRITE)) == 0)
92 iop = malloc(sizeof(struct iop));
99 iop->bufsize = bufsize;
100 if ((flags & IO_READ) && (flags & IO_NEWSOCK) == 0)
101 iop->input = ioq_create(bufsize);
102 if ((flags & IO_WRITE) && (flags & IO_NEWSOCK) == 0)
103 iop->output = ioq_create(bufsize);
104 if (flags & IO_NBLOCK)
105 io_noblocking(iop, 1); /* FIXME check success */
111 io_close(struct iop *iop)
115 ioq_destroy(iop->input);
116 if (iop->output != 0)
117 ioq_destroy(iop->output);
119 (void)close(iop->fd);
121 closesocket(iop->fd);
127 io_input(struct iop *iop, int waitforinput)
129 s_char buf[IO_BUFSIZE];
133 if ((iop->flags & IO_READ) == 0)
135 /* IOP is markes as in error. */
136 if (iop->flags & IO_ERROR)
138 /* Wait for the file to have input. */
140 empth_select(iop->fd, EMPTH_FD_READ);
143 /* Do the actual read. */
144 cc = read(iop->fd, buf, sizeof(buf));
146 /* would block, so nothing to read. */
147 if (errno == EAGAIN || errno == EWOULDBLOCK)
150 /* Some form of file error occurred... */
151 iop->flags |= IO_ERROR;
155 cc = recv(iop->fd, buf, sizeof(buf), 0);
156 if (cc == SOCKET_ERROR) {
157 int err = WSAGetLastError();
158 /* Hmm, it would block. file is opened noblock, soooooo.. */
159 if (err == WSAEWOULDBLOCK)
162 /* Some form of file error occurred... */
163 iop->flags |= IO_ERROR;
170 iop->flags |= IO_EOF;
174 /* Append the input to the IOQ. */
175 ioq_append(iop->input, buf, cc);
180 io_inputwaiting(struct iop *iop)
182 return ioq_qsize(iop->input);
186 io_outputwaiting(struct iop *iop)
188 return ioq_qsize(iop->output);
192 io_output(struct iop *iop, int waitforoutput)
195 struct iovec iov[16];
197 s_char buf[IO_BUFSIZE];
203 /* If there is no output waiting. */
204 if (!io_outputwaiting(iop))
207 /* If the iop is not write enabled. */
208 if ((iop->flags & IO_WRITE) == 0)
211 /* If the io is marked as in error... */
212 if (iop->flags & IO_ERROR)
216 /* make the iov point to the data in the queue. */
217 /* I.E., each of the elements in the queue. */
218 /* returns the number of elements in the iov. */
219 n = ioq_makeiov(iop->output, iov, IO_BUFSIZE);
221 /* Make a buffer containing the output to write. */
222 n = ioq_makebuf(iop->output, buf, sizeof(buf));
229 /* wait for the file to be output ready. */
230 if (waitforoutput != IO_NOWAIT) {
231 /* This waits for the file to be ready for writing, */
232 /* and lets other threads run. */
233 empth_select(iop->fd, EMPTH_FD_WRITE);
236 /* Do the actual write. */
238 cc = writev(iop->fd, iov, n);
240 /* if it failed.... */
242 /* Hmm, it would block. file is opened noblock, soooooo.. */
243 if (errno == EAGAIN || errno == EWOULDBLOCK) {
244 /* If there are remaining bytes, set the IO as remaining.. */
245 remain = ioq_qsize(iop->output);
248 iop->flags |= IO_ERROR;
252 cc = send(iop->fd, buf, n, 0);
254 /* if it failed.... */
255 if (cc == SOCKET_ERROR) {
256 int err = WSAGetLastError();
257 /* Hmm, it would block. file is opened noblock, soooooo.. */
258 if (err == WSAEWOULDBLOCK) {
259 /* If there are remaining bytes, set the IO as remaining.. */
260 remain = ioq_qsize(iop->output);
263 iop->flags |= IO_ERROR;
268 /* If no bytes were written, something happened.. Like an EOF. */
270 iop->flags |= IO_EOF;
274 /* Remove the number of written bytes from the queue. */
275 ioq_dequeue(iop->output, cc);
281 io_peek(struct iop *iop, s_char *buf, int nbytes)
283 if ((iop->flags & IO_READ) == 0)
285 return ioq_peek(iop->input, buf, nbytes);
289 io_read(struct iop *iop, s_char *buf, int nbytes)
293 if ((iop->flags & IO_READ) == 0)
295 cc = ioq_peek(iop->input, buf, nbytes);
297 ioq_dequeue(iop->input, cc);
302 io_write(struct iop *iop, s_char *buf, int nbytes, int doWait)
306 if ((iop->flags & IO_WRITE) == 0)
308 ioq_append(iop->output, buf, nbytes);
309 len = ioq_qsize(iop->output);
310 if (len > iop->bufsize) {
314 /* only try a write every BUFSIZE characters */
315 if (((len - nbytes) % iop->bufsize) < (len % iop->bufsize))
316 io_output(iop, IO_NOWAIT);
323 io_output_all(struct iop *iop)
328 * Mustn't block a player thread while update is pending, or else
329 * a malicous player could delay the update indefinitely
331 while (((n = io_output(iop, IO_NOWAIT)) > 0) && !update_pending) {
332 empth_select(iop->fd, EMPTH_FD_WRITE);
338 io_gets(struct iop *iop, s_char *buf, int nbytes)
340 if ((iop->flags & IO_READ) == 0)
342 return ioq_gets(iop->input, buf, nbytes);
346 io_puts(struct iop *iop, s_char *buf)
348 if ((iop->flags & IO_WRITE) == 0)
350 return ioq_puts(iop->output, buf);
354 io_shutdown(struct iop *iop, int flags)
356 flags &= (IO_READ | IO_WRITE);
357 if ((iop->flags & flags) != flags)
359 if (flags & IO_READ) {
360 shutdown(iop->fd, 0);
361 ioq_drain(iop->input);
363 if (flags & IO_WRITE) {
364 shutdown(iop->fd, 1);
365 ioq_drain(iop->output);
371 io_noblocking(struct iop *iop, int value)
376 flags = fcntl(iop->fd, F_GETFL, 0);
380 flags &= ~O_NONBLOCK;
383 if (fcntl(iop->fd, F_SETFL, flags) < 0)
387 ioctlsocket(iop->fd, FIONBIO, &arg);
390 iop->flags &= ~IO_NBLOCK;
392 iop->flags |= IO_NBLOCK;
397 io_error(struct iop *iop)
399 return iop->flags & IO_ERROR;
403 io_eof(struct iop *iop)
405 return iop->flags & IO_EOF;
409 io_fileno(struct iop *iop)