2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2005, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23 * related information and legal notices. It is expected that any future
24 * 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.
43 #include <sys/types.h>
47 #include <unistd.h> /* close read shutdown select */
48 #include <sys/socket.h>
52 #include <stdlib.h> /* malloc calloc free */
64 #include "gen.h" /* getfdtablesize */
67 #include "empthread.h"
71 struct ioqueue *input;
72 struct ioqueue *output;
85 io_open(int fd, int flags, int bufsize, int (*notify)(void),
90 flags = flags & (IO_READ | IO_WRITE | IO_NBLOCK | IO_NEWSOCK);
91 if ((flags & (IO_READ | IO_WRITE)) == 0)
93 iop = malloc(sizeof(struct iop));
100 iop->bufsize = bufsize;
101 if ((flags & IO_READ) && (flags & IO_NEWSOCK) == 0)
102 iop->input = ioq_create(bufsize);
103 if ((flags & IO_WRITE) && (flags & IO_NEWSOCK) == 0)
104 iop->output = ioq_create(bufsize);
105 if (flags & IO_NBLOCK)
106 io_noblocking(iop, 1);
109 iop->notify = notify;
114 io_close(struct iop *iop)
118 ioq_destroy(iop->input);
119 if (iop->output != 0)
120 ioq_destroy(iop->output);
122 (void)close(iop->fd);
124 closesocket(iop->fd);
130 io_input(struct iop *iop, int waitforinput)
132 s_char buf[IO_BUFSIZE];
136 if ((iop->flags & IO_READ) == 0)
138 /* IOP is markes as in error. */
139 if (iop->flags & IO_ERROR)
141 /* Wait for the file to have input. */
143 empth_select(iop->fd, EMPTH_FD_READ);
146 /* Do the actual read. */
147 cc = read(iop->fd, buf, sizeof(buf));
149 /* would block, so nothing to read. */
150 if (errno == EWOULDBLOCK)
153 /* Some form of file error occurred... */
154 iop->flags |= IO_ERROR;
158 cc = recv(iop->fd, buf, sizeof(buf), 0);
159 if (cc == SOCKET_ERROR) {
160 int err = WSAGetLastError();
161 /* Hmm, it would block. file is opened noblock, soooooo.. */
162 if (err == WSAEWOULDBLOCK)
165 /* Some form of file error occurred... */
166 iop->flags |= IO_ERROR;
173 iop->flags |= IO_EOF;
177 /* Append the input to the IOQ. */
178 ioq_append(iop->input, buf, cc);
183 io_inputwaiting(struct iop *iop)
185 return ioq_qsize(iop->input);
189 io_outputwaiting(struct iop *iop)
191 return ioq_qsize(iop->output);
195 io_output(struct iop *iop, int waitforoutput)
198 struct iovec iov[16];
200 s_char buf[IO_BUFSIZE];
206 /* If there is no output waiting. */
207 if (!io_outputwaiting(iop))
210 /* If the iop is not write enabled. */
211 if ((iop->flags & IO_WRITE) == 0)
214 /* If the io is marked as in error... */
215 if (iop->flags & IO_ERROR)
219 /* make the iov point to the data in the queue. */
220 /* I.E., each of the elements in the queue. */
221 /* returns the number of elements in the iov. */
222 n = ioq_makeiov(iop->output, iov, IO_BUFSIZE);
224 /* Make a buffer containing the output to write. */
225 n = ioq_makebuf(iop->output, buf, sizeof(buf));
232 /* wait for the file to be output ready. */
233 if (waitforoutput != IO_NOWAIT) {
234 /* This waits for the file to be ready for writing, */
235 /* and lets other threads run. */
236 empth_select(iop->fd, EMPTH_FD_WRITE);
239 /* Do the actual write. */
241 cc = writev(iop->fd, iov, n);
243 /* if it failed.... */
245 /* Hmm, it would block. file is opened noblock, soooooo.. */
246 if (errno == EWOULDBLOCK) {
247 /* If there are remaining bytes, set the IO as remaining.. */
248 remain = ioq_qsize(iop->output);
251 iop->flags |= IO_ERROR;
255 cc = send(iop->fd, buf, n, 0);
257 /* if it failed.... */
258 if (cc == SOCKET_ERROR) {
259 int err = WSAGetLastError();
260 /* Hmm, it would block. file is opened noblock, soooooo.. */
261 if (err == WSAEWOULDBLOCK) {
262 /* If there are remaining bytes, set the IO as remaining.. */
263 remain = ioq_qsize(iop->output);
266 iop->flags |= IO_ERROR;
271 /* If no bytes were written, something happened.. Like an EOF. */
274 iop->flags |= IO_EOF;
279 remain = ioq_qsize(iop->output);
284 /* Remove the number of written bytes from the queue. */
285 ioq_dequeue(iop->output, cc);
291 io_peek(struct iop *iop, s_char *buf, int nbytes)
293 if ((iop->flags & IO_READ) == 0)
295 return ioq_peek(iop->input, buf, nbytes);
299 io_read(struct iop *iop, s_char *buf, int nbytes)
303 if ((iop->flags & IO_READ) == 0)
305 cc = ioq_peek(iop->input, buf, nbytes);
307 ioq_dequeue(iop->input, cc);
312 io_write(struct iop *iop, s_char *buf, int nbytes, int doWait)
316 if ((iop->flags & IO_WRITE) == 0)
318 ioq_append(iop->output, buf, nbytes);
319 len = ioq_qsize(iop->output);
320 if (len > iop->bufsize) {
324 /* only try a write every BUFSIZE characters */
325 if (((len - nbytes) % iop->bufsize) < (len % iop->bufsize))
326 io_output(iop, IO_NOWAIT);
333 io_output_all(struct iop *iop)
338 * Mustn't block a player thread while update is pending, or else
339 * a malicous player could delay the update indefinitely
341 while (((n = io_output(iop, IO_NOWAIT)) > 0) && !update_pending) {
342 empth_select(iop->fd, EMPTH_FD_WRITE);
348 io_gets(struct iop *iop, s_char *buf, int nbytes)
350 if ((iop->flags & IO_READ) == 0)
352 return ioq_gets(iop->input, buf, nbytes);
356 io_puts(struct iop *iop, s_char *buf)
358 if ((iop->flags & IO_WRITE) == 0)
360 return ioq_puts(iop->output, buf);
364 io_shutdown(struct iop *iop, int flags)
366 flags &= (IO_READ | IO_WRITE);
367 if ((iop->flags & flags) != flags)
369 if (flags & IO_READ) {
370 shutdown(iop->fd, 0);
371 ioq_drain(iop->input);
373 if (flags & IO_WRITE) {
374 shutdown(iop->fd, 1);
375 ioq_drain(iop->output);
381 io_noblocking(struct iop *iop, int value)
386 flags = fcntl(iop->fd, F_GETFL, 0);
393 if (fcntl(iop->fd, F_SETFL, flags) < 0)
397 ioctlsocket(iop->fd, FIONBIO, &arg);
400 iop->flags &= ~IO_NBLOCK;
402 iop->flags |= IO_NBLOCK;
407 io_conn(struct iop *iop)
409 return iop->flags & IO_CONN;
413 io_error(struct iop *iop)
415 return iop->flags & IO_ERROR;
419 io_eof(struct iop *iop)
421 return iop->flags & IO_EOF;
425 io_fileno(struct iop *iop)