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>
55 #include <sys/socket.h>
66 #include "empthread.h"
70 struct ioqueue *input;
71 struct ioqueue *output;
82 io_open(int fd, int flags, int bufsize)
86 flags = flags & (IO_READ | IO_WRITE | IO_NBLOCK | IO_NEWSOCK);
87 if ((flags & (IO_READ | IO_WRITE)) == 0)
89 iop = malloc(sizeof(struct iop));
96 iop->bufsize = bufsize;
97 if ((flags & IO_READ) && (flags & IO_NEWSOCK) == 0)
98 iop->input = ioq_create(bufsize);
99 if ((flags & IO_WRITE) && (flags & IO_NEWSOCK) == 0)
100 iop->output = ioq_create(bufsize);
101 if (flags & IO_NBLOCK)
102 io_noblocking(iop, 1); /* FIXME check success */
108 io_close(struct iop *iop)
112 ioq_destroy(iop->input);
113 if (iop->output != 0)
114 ioq_destroy(iop->output);
116 (void)close(iop->fd);
118 closesocket(iop->fd);
124 io_input(struct iop *iop, int waitforinput)
126 char buf[IO_BUFSIZE];
130 if ((iop->flags & IO_READ) == 0)
132 /* IOP is markes as in error. */
133 if (iop->flags & IO_ERROR)
135 /* Wait for the file to have input. */
137 empth_select(iop->fd, EMPTH_FD_READ);
140 /* Do the actual read. */
141 cc = read(iop->fd, buf, sizeof(buf));
143 /* would block, so nothing to read. */
144 if (errno == EAGAIN || errno == EWOULDBLOCK)
147 /* Some form of file error occurred... */
148 iop->flags |= IO_ERROR;
152 cc = recv(iop->fd, buf, sizeof(buf), 0);
153 if (cc == SOCKET_ERROR) {
154 int err = WSAGetLastError();
155 /* Hmm, it would block. file is opened noblock, soooooo.. */
156 if (err == WSAEWOULDBLOCK)
159 /* Some form of file error occurred... */
160 iop->flags |= IO_ERROR;
167 iop->flags |= IO_EOF;
171 /* Append the input to the IOQ. */
172 ioq_append(iop->input, buf, cc);
177 io_inputwaiting(struct iop *iop)
179 return ioq_qsize(iop->input);
183 io_outputwaiting(struct iop *iop)
185 return ioq_qsize(iop->output);
189 io_output(struct iop *iop, int waitforoutput)
192 struct iovec iov[16];
194 char buf[IO_BUFSIZE];
200 /* If there is no output waiting. */
201 if (!io_outputwaiting(iop))
204 /* If the iop is not write enabled. */
205 if ((iop->flags & IO_WRITE) == 0)
208 /* If the io is marked as in error... */
209 if (iop->flags & IO_ERROR)
213 /* make the iov point to the data in the queue. */
214 /* I.E., each of the elements in the queue. */
215 /* returns the number of elements in the iov. */
216 n = ioq_makeiov(iop->output, iov, IO_BUFSIZE);
218 /* Make a buffer containing the output to write. */
219 n = ioq_makebuf(iop->output, buf, sizeof(buf));
226 /* wait for the file to be output ready. */
227 if (waitforoutput != IO_NOWAIT) {
228 /* This waits for the file to be ready for writing, */
229 /* and lets other threads run. */
230 empth_select(iop->fd, EMPTH_FD_WRITE);
233 /* Do the actual write. */
235 cc = writev(iop->fd, iov, n);
237 /* if it failed.... */
239 /* Hmm, it would block. file is opened noblock, soooooo.. */
240 if (errno == EAGAIN || errno == EWOULDBLOCK) {
241 /* If there are remaining bytes, set the IO as remaining.. */
242 remain = ioq_qsize(iop->output);
245 iop->flags |= IO_ERROR;
249 cc = send(iop->fd, buf, n, 0);
251 /* if it failed.... */
252 if (cc == SOCKET_ERROR) {
253 int err = WSAGetLastError();
254 /* Hmm, it would block. file is opened noblock, soooooo.. */
255 if (err == WSAEWOULDBLOCK) {
256 /* If there are remaining bytes, set the IO as remaining.. */
257 remain = ioq_qsize(iop->output);
260 iop->flags |= IO_ERROR;
265 /* If no bytes were written, something happened.. Like an EOF. */
267 iop->flags |= IO_EOF;
271 /* Remove the number of written bytes from the queue. */
272 ioq_dequeue(iop->output, cc);
278 io_peek(struct iop *iop, char *buf, int nbytes)
280 if ((iop->flags & IO_READ) == 0)
282 return ioq_peek(iop->input, buf, nbytes);
286 io_read(struct iop *iop, char *buf, int nbytes)
290 if ((iop->flags & IO_READ) == 0)
292 cc = ioq_peek(iop->input, buf, nbytes);
294 ioq_dequeue(iop->input, cc);
299 io_write(struct iop *iop, char *buf, int nbytes, int doWait)
303 if ((iop->flags & IO_WRITE) == 0)
305 ioq_append(iop->output, buf, nbytes);
306 len = ioq_qsize(iop->output);
307 if (len > iop->bufsize) {
311 /* only try a write every BUFSIZE characters */
312 if (((len - nbytes) % iop->bufsize) < (len % iop->bufsize))
313 io_output(iop, IO_NOWAIT);
320 io_output_all(struct iop *iop)
325 * Mustn't block a player thread while update is pending, or else
326 * a malicous player could delay the update indefinitely
328 while (((n = io_output(iop, IO_NOWAIT)) > 0) && !update_pending) {
329 empth_select(iop->fd, EMPTH_FD_WRITE);
335 io_gets(struct iop *iop, char *buf, int nbytes)
337 if ((iop->flags & IO_READ) == 0)
339 return ioq_gets(iop->input, buf, nbytes);
343 io_puts(struct iop *iop, char *buf)
345 if ((iop->flags & IO_WRITE) == 0)
347 return ioq_puts(iop->output, buf);
351 io_shutdown(struct iop *iop, int flags)
353 flags &= (IO_READ | IO_WRITE);
354 if ((iop->flags & flags) != flags)
356 if (flags & IO_READ) {
357 shutdown(iop->fd, 0);
358 ioq_drain(iop->input);
360 if (flags & IO_WRITE) {
361 shutdown(iop->fd, 1);
362 ioq_drain(iop->output);
368 io_noblocking(struct iop *iop, int value)
373 flags = fcntl(iop->fd, F_GETFL, 0);
377 flags &= ~O_NONBLOCK;
380 if (fcntl(iop->fd, F_SETFL, flags) < 0)
384 ioctlsocket(iop->fd, FIONBIO, &arg);
387 iop->flags &= ~IO_NBLOCK;
389 iop->flags |= IO_NBLOCK;
394 io_error(struct iop *iop)
396 return iop->flags & IO_ERROR;
400 io_eof(struct iop *iop)
402 return iop->flags & IO_EOF;
406 io_fileno(struct iop *iop)