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)
218 /* This is the same test as io_outputwaiting.... */
219 if (ioq_qsize(iop->output) == 0)
223 /* make the iov point to the data in the queue. */
224 /* I.E., each of the elements in the queue. */
225 /* returns the number of elements in the iov. */
226 n = ioq_makeiov(iop->output, iov, IO_BUFSIZE);
228 /* Make a buffer containing the output to write. */
229 n = ioq_makebuf(iop->output, buf, sizeof(buf));
236 /* wait for the file to be output ready. */
237 if (waitforoutput != IO_NOWAIT) {
238 /* This waits for the file to be ready for writing, */
239 /* and lets other threads run. */
240 empth_select(iop->fd, EMPTH_FD_WRITE);
243 /* Do the actual write. */
245 cc = writev(iop->fd, iov, n);
247 /* if it failed.... */
249 /* Hmm, it would block. file is opened noblock, soooooo.. */
250 if (errno == EWOULDBLOCK) {
251 /* If there are remaining bytes, set the IO as remaining.. */
252 remain = ioq_qsize(iop->output);
255 iop->flags |= IO_ERROR;
259 cc = send(iop->fd, buf, n, 0);
261 /* if it failed.... */
262 if (cc == SOCKET_ERROR) {
263 int err = WSAGetLastError();
264 /* Hmm, it would block. file is opened noblock, soooooo.. */
265 if (err == WSAEWOULDBLOCK) {
266 /* If there are remaining bytes, set the IO as remaining.. */
267 remain = ioq_qsize(iop->output);
270 iop->flags |= IO_ERROR;
275 /* If no bytes were written, something happened.. Like an EOF. */
278 iop->flags |= IO_EOF;
283 remain = ioq_qsize(iop->output);
288 /* Remove the number of written bytes from the queue. */
289 ioq_dequeue(iop->output, cc);
295 io_peek(struct iop *iop, s_char *buf, int nbytes)
297 if ((iop->flags & IO_READ) == 0)
299 return ioq_peek(iop->input, buf, nbytes);
303 io_read(struct iop *iop, s_char *buf, int nbytes)
307 if ((iop->flags & IO_READ) == 0)
309 cc = ioq_peek(iop->input, buf, nbytes);
311 ioq_dequeue(iop->input, cc);
316 io_write(struct iop *iop, s_char *buf, int nbytes, int doWait)
320 if ((iop->flags & IO_WRITE) == 0)
322 ioq_append(iop->output, buf, nbytes);
323 len = ioq_qsize(iop->output);
324 if (len > iop->bufsize) {
328 /* only try a write every BUFSIZE characters */
329 if (((len - nbytes) % iop->bufsize) < (len % iop->bufsize))
330 io_output(iop, IO_NOWAIT);
337 io_output_all(struct iop *iop)
342 * Mustn't block a player thread while update is pending, or else
343 * a malicous player could delay the update indefinitely
345 while (((n = io_output(iop, IO_NOWAIT)) > 0) && !update_pending) {
346 empth_select(iop->fd, EMPTH_FD_WRITE);
352 io_gets(struct iop *iop, s_char *buf, int nbytes)
354 if ((iop->flags & IO_READ) == 0)
356 return ioq_gets(iop->input, buf, nbytes);
360 io_puts(struct iop *iop, s_char *buf)
362 if ((iop->flags & IO_WRITE) == 0)
364 return ioq_puts(iop->output, buf);
368 io_shutdown(struct iop *iop, int flags)
370 flags &= (IO_READ | IO_WRITE);
371 if ((iop->flags & flags) != flags)
373 if (flags & IO_READ) {
374 shutdown(iop->fd, 0);
375 ioq_drain(iop->input);
377 if (flags & IO_WRITE) {
378 shutdown(iop->fd, 1);
379 ioq_drain(iop->output);
385 io_noblocking(struct iop *iop, int value)
390 flags = fcntl(iop->fd, F_GETFL, 0);
397 if (fcntl(iop->fd, F_SETFL, flags) < 0)
401 ioctlsocket(iop->fd, FIONBIO, &arg);
404 iop->flags &= ~IO_NBLOCK;
406 iop->flags |= IO_NBLOCK;
411 io_conn(struct iop *iop)
413 return iop->flags & IO_CONN;
417 io_error(struct iop *iop)
419 return iop->flags & IO_ERROR;
423 io_eof(struct iop *iop)
425 return iop->flags & IO_EOF;
429 io_fileno(struct iop *iop)