2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2000, 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 */
66 #include "empthread.h"
68 extern struct player *player; /* XXX */
70 static struct iop **io_list;
71 static struct io_mask *iom;
72 static bit_fdmask newoutput;
76 struct ioqueue *input;
77 struct ioqueue *output;
89 iom = iom_create(IO_READ | IO_WRITE);
90 io_list = (struct iop **)calloc(getfdtablesize(), sizeof(*io_list));
91 newoutput = bit_newfdmask();
95 io_open(int fd, int flags, int bufsize, int (*notify) (void),
100 if (io_list[fd] != 0) {
104 flags = flags & (IO_READ | IO_WRITE | IO_NBLOCK | IO_NEWSOCK);
105 if ((flags & (IO_READ | IO_WRITE)) == 0)
107 iop = (struct iop *)malloc(sizeof(struct iop));
112 iop->bufsize = bufsize;
113 if ((flags & IO_READ) && (flags & IO_NEWSOCK) == 0)
114 iop->input = ioq_create(bufsize);
115 if ((flags & IO_WRITE) && (flags & IO_NEWSOCK) == 0)
116 iop->output = ioq_create(bufsize);
117 if (flags & IO_NBLOCK)
118 io_noblocking(iop, 1);
121 iop->notify = notify;
123 iom_set(iom, flags, fd);
128 io_close(struct iop *iop)
132 ioq_destroy(iop->input);
133 if (iop->output != 0)
134 ioq_destroy(iop->output);
135 iom_clear(iom, iop->flags, iop->fd);
136 BIT_CLRB(iop->fd, newoutput);
137 io_list[iop->fd] = 0;
139 (void)close(iop->fd);
141 closesocket(iop->fd);
147 io_input(struct iop *iop, int waitforinput)
149 s_char buf[IO_BUFSIZE];
153 if ((iop->flags & IO_READ) == 0)
155 /* IOP is markes as in error. */
156 if (iop->flags & IO_ERROR)
158 /* Wait for the file to have input. */
160 empth_select(iop->fd, EMPTH_FD_READ);
163 /* Do the actual read. */
164 cc = read(iop->fd, buf, sizeof(buf));
166 /* would block, so nothing to read. */
167 if (errno == EWOULDBLOCK)
170 /* Some form of file error occurred... */
171 iop->flags |= IO_ERROR;
172 iom_clear(iom, IO_READ, iop->fd);
176 cc = recv(iop->fd, buf, sizeof(buf), 0);
177 if (cc == SOCKET_ERROR) {
178 int err = WSAGetLastError();
179 /* Hmm, it would block. file is opened noblock, soooooo.. */
180 if (err == WSAEWOULDBLOCK)
183 /* Some form of file error occurred... */
184 iop->flags |= IO_ERROR;
185 iom_clear(iom, IO_READ, iop->fd);
192 iop->flags |= IO_EOF;
196 /* Append the input to the IOQ. */
197 ioq_append(iop->input, buf, cc);
202 io_inputwaiting(struct iop *iop)
204 return ioq_qsize(iop->input);
208 io_outputwaiting(struct iop *iop)
210 return ioq_qsize(iop->output);
214 io_output(struct iop *iop, int waitforoutput)
217 struct iovec iov[16];
219 s_char buf[IO_BUFSIZE];
225 /* If there is no output waiting. */
226 if (!io_outputwaiting(iop))
230 BIT_CLRB(iop->fd, newoutput);
232 /* If the iop is not write enabled. */
233 if ((iop->flags & IO_WRITE) == 0)
236 /* If the io is marked as in error... */
237 if (iop->flags & IO_ERROR)
240 /* This is the same test as io_outputwaiting.... */
241 if (ioq_qsize(iop->output) == 0)
245 /* make the iov point to the data in the queue. */
246 /* I.E., each of the elements in the queue. */
247 /* returns the number of elements in the iov. */
248 n = ioq_makeiov(iop->output, iov, IO_BUFSIZE);
250 /* Make a buffer containing the output to write. */
251 n = ioq_makebuf(iop->output, buf, sizeof(buf));
255 /* If we got no elements, we have no output.... */
256 iom_clear(iom, IO_WRITE, iop->fd);
260 /* wait for the file to be output ready. */
261 if (waitforoutput != IO_NOWAIT) {
262 /* This waits for the file to be ready for writing, */
263 /* and lets other threads run. */
264 empth_select(iop->fd, EMPTH_FD_WRITE);
267 /* Do the actual write. */
269 cc = writev(iop->fd, iov, n);
271 /* if it failed.... */
273 /* Hmm, it would block. file is opened noblock, soooooo.. */
274 if (errno == EWOULDBLOCK) {
275 /* If there are remaining bytes, set the IO as remaining.. */
276 remain = ioq_qsize(iop->output);
278 iom_set(iom, IO_WRITE, iop->fd);
281 iop->flags |= IO_ERROR;
282 iom_clear(iom, IO_WRITE, iop->fd);
286 cc = send(iop->fd, buf, n, 0);
288 /* if it failed.... */
289 if (cc == SOCKET_ERROR) {
290 int err = WSAGetLastError();
291 /* Hmm, it would block. file is opened noblock, soooooo.. */
292 if (err == WSAEWOULDBLOCK) {
293 /* If there are remaining bytes, set the IO as remaining.. */
294 remain = ioq_qsize(iop->output);
296 iom_set(iom, IO_WRITE, iop->fd);
299 iop->flags |= IO_ERROR;
300 iom_clear(iom, IO_WRITE, iop->fd);
306 /* If no bytes were written, something happened.. Like an EOF. */
309 iop->flags |= IO_EOF;
314 remain = ioq_qsize(iop->output);
316 iom_set(iom, IO_WRITE, iop->fd);
321 /* Remove the number of written bytes from the queue. */
322 ioq_dequeue(iop->output, cc);
324 /* If the queue has stuff remaining, set it still needing output. */
325 remain = ioq_qsize(iop->output);
327 iom_clear(iom, IO_WRITE, iop->fd);
329 iom_set(iom, IO_WRITE, iop->fd);
335 io_select(struct timeval *tv)
338 bit_fdmask writemask;
344 iom_getmask(iom, &nfds, &readmask, &writemask);
345 n = select(nfds + 1, (fd_set *) readmask, (fd_set *) writemask, 0, tv);
351 while ((fd = bit_fd(readmask)) >= 0) {
353 if ((iop->flags & IO_NEWSOCK) == 0)
354 (void)io_input(iop, IO_NOWAIT);
355 if (iop->notify != 0)
356 iop->notify(iop, IO_READ, iop->assoc);
357 BIT_CLRB(fd, readmask);
359 while ((fd = bit_fd(writemask)) >= 0) {
361 if (io_output(iop, IO_NOWAIT) < 0 && iop->notify != 0)
362 iop->notify(iop, IO_WRITE, iop->assoc);
363 BIT_CLRB(fd, writemask);
374 while ((fd = bit_fd(newoutput)) >= 0) {
376 if (io_output(iop, doWait) < 0 && iop->notify != 0)
377 iop->notify(iop, IO_WRITE, iop->assoc);
382 io_peek(struct iop *iop, s_char *buf, int nbytes)
384 if ((iop->flags & IO_READ) == 0)
386 return ioq_peek(iop->input, buf, nbytes);
390 io_read(struct iop *iop, s_char *buf, int nbytes)
394 if ((iop->flags & IO_READ) == 0)
396 cc = ioq_peek(iop->input, buf, nbytes);
398 ioq_dequeue(iop->input, cc);
403 io_write(struct iop *iop, s_char *buf, int nbytes, int doWait)
407 if ((iop->flags & IO_WRITE) == 0)
409 ioq_append(iop->output, buf, nbytes);
410 BIT_SETB(iop->fd, newoutput);
411 len = ioq_qsize(iop->output);
412 if (len > iop->bufsize) {
416 /* only try a write every BUFSIZE characters */
417 if (((len - nbytes) % iop->bufsize) < (len % iop->bufsize))
425 io_output_all(struct iop *iop)
429 while ((n = io_output(iop, IO_NOWAIT)) > 0) {
430 empth_select(iop->fd, EMPTH_FD_WRITE);
436 io_gets(struct iop *iop, s_char *buf, int nbytes)
438 if ((iop->flags & IO_READ) == 0)
440 return ioq_gets(iop->input, buf, nbytes);
444 io_puts(struct iop *iop, s_char *buf)
446 if ((iop->flags & IO_WRITE) == 0)
448 BIT_SETB(iop->fd, newoutput);
449 return ioq_puts(iop->output, buf);
453 io_shutdown(struct iop *iop, int flags)
455 flags &= (IO_READ | IO_WRITE);
456 if ((iop->flags & flags) != flags)
458 if (flags & IO_READ) {
459 shutdown(iop->fd, 0);
460 ioq_drain(iop->input);
462 if (flags & IO_WRITE) {
463 shutdown(iop->fd, 1);
464 ioq_drain(iop->output);
470 io_noblocking(struct iop *iop, int value)
475 flags = fcntl(iop->fd, F_GETFL, 0);
482 if (fcntl(iop->fd, F_SETFL, flags) < 0)
486 ioctlsocket(iop->fd, FIONBIO, &arg);
489 iop->flags &= ~IO_NBLOCK;
491 iop->flags |= IO_NBLOCK;
496 io_conn(struct iop *iop)
498 return (iop->flags & IO_CONN);
502 io_error(struct iop *iop)
504 return (iop->flags & IO_ERROR);
508 io_eof(struct iop *iop)
510 return (iop->flags & IO_EOF);
514 io_fileno(struct iop *iop)