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>
54 #include <sys/socket.h>
65 #include "empthread.h"
69 struct ioqueue *input;
70 struct ioqueue *output;
81 io_open(int fd, int flags, int bufsize)
85 flags = flags & (IO_READ | IO_WRITE | IO_NBLOCK | IO_NEWSOCK);
86 if ((flags & (IO_READ | IO_WRITE)) == 0)
88 iop = malloc(sizeof(struct iop));
95 iop->bufsize = bufsize;
96 if ((flags & IO_READ) && (flags & IO_NEWSOCK) == 0)
97 iop->input = ioq_create(bufsize);
98 if ((flags & IO_WRITE) && (flags & IO_NEWSOCK) == 0)
99 iop->output = ioq_create(bufsize);
100 if (flags & IO_NBLOCK)
101 io_noblocking(iop, 1); /* FIXME check success */
107 io_close(struct iop *iop)
111 ioq_destroy(iop->input);
112 if (iop->output != 0)
113 ioq_destroy(iop->output);
115 (void)close(iop->fd);
117 closesocket(iop->fd);
123 io_input(struct iop *iop, int waitforinput)
125 char buf[IO_BUFSIZE];
129 if ((iop->flags & IO_READ) == 0)
131 /* IOP is markes as in error. */
132 if (iop->flags & IO_ERROR)
134 /* Wait for the file to have input. */
136 empth_select(iop->fd, EMPTH_FD_READ);
139 /* Do the actual read. */
140 cc = read(iop->fd, buf, sizeof(buf));
142 /* would block, so nothing to read. */
143 if (errno == EAGAIN || errno == EWOULDBLOCK)
146 /* Some form of file error occurred... */
147 iop->flags |= IO_ERROR;
151 cc = recv(iop->fd, buf, sizeof(buf), 0);
152 if (cc == SOCKET_ERROR) {
153 int err = WSAGetLastError();
154 /* Hmm, it would block. file is opened noblock, soooooo.. */
155 if (err == WSAEWOULDBLOCK)
158 /* Some form of file error occurred... */
159 iop->flags |= IO_ERROR;
166 iop->flags |= IO_EOF;
170 /* Append the input to the IOQ. */
171 ioq_append(iop->input, buf, cc);
176 io_inputwaiting(struct iop *iop)
178 return ioq_qsize(iop->input);
182 io_outputwaiting(struct iop *iop)
184 return ioq_qsize(iop->output);
188 io_output(struct iop *iop, int waitforoutput)
191 struct iovec iov[16];
193 char buf[IO_BUFSIZE];
199 /* If there is no output waiting. */
200 if (!io_outputwaiting(iop))
203 /* If the iop is not write enabled. */
204 if ((iop->flags & IO_WRITE) == 0)
207 /* If the io is marked as in error... */
208 if (iop->flags & IO_ERROR)
212 /* make the iov point to the data in the queue. */
213 /* I.E., each of the elements in the queue. */
214 /* returns the number of elements in the iov. */
215 n = ioq_makeiov(iop->output, iov, IO_BUFSIZE);
217 /* Make a buffer containing the output to write. */
218 n = ioq_makebuf(iop->output, buf, sizeof(buf));
225 /* wait for the file to be output ready. */
226 if (waitforoutput != IO_NOWAIT) {
227 /* This waits for the file to be ready for writing, */
228 /* and lets other threads run. */
229 empth_select(iop->fd, EMPTH_FD_WRITE);
232 /* Do the actual write. */
234 cc = writev(iop->fd, iov, n);
236 /* if it failed.... */
238 /* Hmm, it would block. file is opened noblock, soooooo.. */
239 if (errno == EAGAIN || errno == EWOULDBLOCK) {
240 /* If there are remaining bytes, set the IO as remaining.. */
241 remain = ioq_qsize(iop->output);
244 iop->flags |= IO_ERROR;
248 cc = send(iop->fd, buf, n, 0);
250 /* if it failed.... */
251 if (cc == SOCKET_ERROR) {
252 int err = WSAGetLastError();
253 /* Hmm, it would block. file is opened noblock, soooooo.. */
254 if (err == WSAEWOULDBLOCK) {
255 /* If there are remaining bytes, set the IO as remaining.. */
256 remain = ioq_qsize(iop->output);
259 iop->flags |= IO_ERROR;
264 /* If no bytes were written, something happened.. Like an EOF. */
266 iop->flags |= IO_EOF;
270 /* Remove the number of written bytes from the queue. */
271 ioq_dequeue(iop->output, cc);
277 io_peek(struct iop *iop, char *buf, int nbytes)
279 if ((iop->flags & IO_READ) == 0)
281 return ioq_peek(iop->input, buf, nbytes);
285 io_read(struct iop *iop, char *buf, int nbytes)
289 if ((iop->flags & IO_READ) == 0)
291 cc = ioq_peek(iop->input, buf, nbytes);
293 ioq_dequeue(iop->input, cc);
298 io_write(struct iop *iop, char *buf, int nbytes, int doWait)
302 if ((iop->flags & IO_WRITE) == 0)
304 ioq_append(iop->output, buf, nbytes);
305 len = ioq_qsize(iop->output);
306 if (len > iop->bufsize) {
310 /* only try a write every BUFSIZE characters */
311 if (((len - nbytes) % iop->bufsize) < (len % iop->bufsize))
312 io_output(iop, IO_NOWAIT);
319 io_output_all(struct iop *iop)
324 * Mustn't block a player thread while update is pending, or else
325 * a malicous player could delay the update indefinitely
327 while (((n = io_output(iop, IO_NOWAIT)) > 0) && !update_pending) {
328 empth_select(iop->fd, EMPTH_FD_WRITE);
334 io_gets(struct iop *iop, char *buf, int nbytes)
336 if ((iop->flags & IO_READ) == 0)
338 return ioq_gets(iop->input, buf, nbytes);
342 io_puts(struct iop *iop, char *buf)
344 if ((iop->flags & IO_WRITE) == 0)
346 return ioq_puts(iop->output, buf);
350 io_shutdown(struct iop *iop, int flags)
352 flags &= (IO_READ | IO_WRITE);
353 if ((iop->flags & flags) != flags)
355 if (flags & IO_READ) {
356 shutdown(iop->fd, 0);
357 ioq_drain(iop->input);
359 if (flags & IO_WRITE) {
360 shutdown(iop->fd, 1);
361 ioq_drain(iop->output);
367 io_noblocking(struct iop *iop, int value)
372 flags = fcntl(iop->fd, F_GETFL, 0);
376 flags &= ~O_NONBLOCK;
379 if (fcntl(iop->fd, F_SETFL, flags) < 0)
383 ioctlsocket(iop->fd, FIONBIO, &arg);
386 iop->flags &= ~IO_NBLOCK;
388 iop->flags |= IO_NBLOCK;
393 io_error(struct iop *iop)
395 return iop->flags & IO_ERROR;
399 io_eof(struct iop *iop)
401 return iop->flags & IO_EOF;
405 io_fileno(struct iop *iop)