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 */
63 #include "gen.h" /* getfdtablesize */
65 #include "empthread.h"
67 extern struct player *player; /* XXX */
69 static struct io_mask *iom;
70 static int fdmax; /* largest file descriptor seen */
71 static fd_set newoutput;
75 struct ioqueue *input;
76 struct ioqueue *output;
86 iom = iom_create(IO_READ | IO_WRITE);
92 io_open(int fd, int flags, int bufsize, int (*notify) (void),
97 flags = flags & (IO_READ | IO_WRITE | IO_NBLOCK | IO_NEWSOCK);
98 if ((flags & (IO_READ | IO_WRITE)) == 0)
100 iop = (struct iop *)malloc(sizeof(struct iop));
107 iop->bufsize = bufsize;
108 if ((flags & IO_READ) && (flags & IO_NEWSOCK) == 0)
109 iop->input = ioq_create(bufsize);
110 if ((flags & IO_WRITE) && (flags & IO_NEWSOCK) == 0)
111 iop->output = ioq_create(bufsize);
112 if (flags & IO_NBLOCK)
113 io_noblocking(iop, 1);
116 iop->notify = notify;
117 iom_set(iom, flags, fd);
118 if (fd > fdmax) fdmax = fd;
123 io_close(struct iop *iop)
127 ioq_destroy(iop->input);
128 if (iop->output != 0)
129 ioq_destroy(iop->output);
130 iom_clear(iom, iop->flags, iop->fd);
131 FD_CLR(iop->fd, &newoutput);
133 (void)close(iop->fd);
135 closesocket(iop->fd);
141 io_input(struct iop *iop, int waitforinput)
143 s_char buf[IO_BUFSIZE];
147 if ((iop->flags & IO_READ) == 0)
149 /* IOP is markes as in error. */
150 if (iop->flags & IO_ERROR)
152 /* Wait for the file to have input. */
154 empth_select(iop->fd, EMPTH_FD_READ);
157 /* Do the actual read. */
158 cc = read(iop->fd, buf, sizeof(buf));
160 /* would block, so nothing to read. */
161 if (errno == EWOULDBLOCK)
164 /* Some form of file error occurred... */
165 iop->flags |= IO_ERROR;
166 iom_clear(iom, IO_READ, iop->fd);
170 cc = recv(iop->fd, buf, sizeof(buf), 0);
171 if (cc == SOCKET_ERROR) {
172 int err = WSAGetLastError();
173 /* Hmm, it would block. file is opened noblock, soooooo.. */
174 if (err == WSAEWOULDBLOCK)
177 /* Some form of file error occurred... */
178 iop->flags |= IO_ERROR;
179 iom_clear(iom, IO_READ, iop->fd);
186 iop->flags |= IO_EOF;
190 /* Append the input to the IOQ. */
191 ioq_append(iop->input, buf, cc);
196 io_inputwaiting(struct iop *iop)
198 return ioq_qsize(iop->input);
202 io_outputwaiting(struct iop *iop)
204 return ioq_qsize(iop->output);
208 io_output(struct iop *iop, int waitforoutput)
211 struct iovec iov[16];
213 s_char buf[IO_BUFSIZE];
219 /* If there is no output waiting. */
220 if (!io_outputwaiting(iop))
224 FD_CLR(iop->fd, &newoutput);
226 /* If the iop is not write enabled. */
227 if ((iop->flags & IO_WRITE) == 0)
230 /* If the io is marked as in error... */
231 if (iop->flags & IO_ERROR)
234 /* This is the same test as io_outputwaiting.... */
235 if (ioq_qsize(iop->output) == 0)
239 /* make the iov point to the data in the queue. */
240 /* I.E., each of the elements in the queue. */
241 /* returns the number of elements in the iov. */
242 n = ioq_makeiov(iop->output, iov, IO_BUFSIZE);
244 /* Make a buffer containing the output to write. */
245 n = ioq_makebuf(iop->output, buf, sizeof(buf));
249 /* If we got no elements, we have no output.... */
250 iom_clear(iom, IO_WRITE, iop->fd);
254 /* wait for the file to be output ready. */
255 if (waitforoutput != IO_NOWAIT) {
256 /* This waits for the file to be ready for writing, */
257 /* and lets other threads run. */
258 empth_select(iop->fd, EMPTH_FD_WRITE);
261 /* Do the actual write. */
263 cc = writev(iop->fd, iov, n);
265 /* if it failed.... */
267 /* Hmm, it would block. file is opened noblock, soooooo.. */
268 if (errno == EWOULDBLOCK) {
269 /* If there are remaining bytes, set the IO as remaining.. */
270 remain = ioq_qsize(iop->output);
272 iom_set(iom, IO_WRITE, iop->fd);
275 iop->flags |= IO_ERROR;
276 iom_clear(iom, IO_WRITE, iop->fd);
280 cc = send(iop->fd, buf, n, 0);
282 /* if it failed.... */
283 if (cc == SOCKET_ERROR) {
284 int err = WSAGetLastError();
285 /* Hmm, it would block. file is opened noblock, soooooo.. */
286 if (err == WSAEWOULDBLOCK) {
287 /* If there are remaining bytes, set the IO as remaining.. */
288 remain = ioq_qsize(iop->output);
290 iom_set(iom, IO_WRITE, iop->fd);
293 iop->flags |= IO_ERROR;
294 iom_clear(iom, IO_WRITE, iop->fd);
300 /* If no bytes were written, something happened.. Like an EOF. */
303 iop->flags |= IO_EOF;
308 remain = ioq_qsize(iop->output);
310 iom_set(iom, IO_WRITE, iop->fd);
315 /* Remove the number of written bytes from the queue. */
316 ioq_dequeue(iop->output, cc);
318 /* If the queue has stuff remaining, set it still needing output. */
319 remain = ioq_qsize(iop->output);
321 iom_clear(iom, IO_WRITE, iop->fd);
323 iom_set(iom, IO_WRITE, iop->fd);
329 io_peek(struct iop *iop, s_char *buf, int nbytes)
331 if ((iop->flags & IO_READ) == 0)
333 return ioq_peek(iop->input, buf, nbytes);
337 io_read(struct iop *iop, s_char *buf, int nbytes)
341 if ((iop->flags & IO_READ) == 0)
343 cc = ioq_peek(iop->input, buf, nbytes);
345 ioq_dequeue(iop->input, cc);
350 io_write(struct iop *iop, s_char *buf, int nbytes, int doWait)
354 if ((iop->flags & IO_WRITE) == 0)
356 ioq_append(iop->output, buf, nbytes);
357 FD_SET(iop->fd, &newoutput);
358 len = ioq_qsize(iop->output);
359 if (len > iop->bufsize) {
363 /* only try a write every BUFSIZE characters */
364 if (((len - nbytes) % iop->bufsize) < (len % iop->bufsize))
372 io_output_all(struct iop *iop)
376 while ((n = io_output(iop, IO_NOWAIT)) > 0) {
377 empth_select(iop->fd, EMPTH_FD_WRITE);
383 io_gets(struct iop *iop, s_char *buf, int nbytes)
385 if ((iop->flags & IO_READ) == 0)
387 return ioq_gets(iop->input, buf, nbytes);
391 io_puts(struct iop *iop, s_char *buf)
393 if ((iop->flags & IO_WRITE) == 0)
395 FD_SET(iop->fd, &newoutput);
396 return ioq_puts(iop->output, buf);
400 io_shutdown(struct iop *iop, int flags)
402 flags &= (IO_READ | IO_WRITE);
403 if ((iop->flags & flags) != flags)
405 if (flags & IO_READ) {
406 shutdown(iop->fd, 0);
407 ioq_drain(iop->input);
409 if (flags & IO_WRITE) {
410 shutdown(iop->fd, 1);
411 ioq_drain(iop->output);
417 io_noblocking(struct iop *iop, int value)
422 flags = fcntl(iop->fd, F_GETFL, 0);
429 if (fcntl(iop->fd, F_SETFL, flags) < 0)
433 ioctlsocket(iop->fd, FIONBIO, &arg);
436 iop->flags &= ~IO_NBLOCK;
438 iop->flags |= IO_NBLOCK;
443 io_conn(struct iop *iop)
445 return (iop->flags & IO_CONN);
449 io_error(struct iop *iop)
451 return (iop->flags & IO_ERROR);
455 io_eof(struct iop *iop)
457 return (iop->flags & IO_EOF);
461 io_fileno(struct iop *iop)