2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2004, 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 */
62 #include "gen.h" /* getfdtablesize */
64 #include "empthread.h"
68 struct ioqueue *input;
69 struct ioqueue *output;
82 io_open(int fd, int flags, int bufsize, int (*notify)(void),
87 flags = flags & (IO_READ | IO_WRITE | IO_NBLOCK | IO_NEWSOCK);
88 if ((flags & (IO_READ | IO_WRITE)) == 0)
90 iop = (struct iop *)malloc(sizeof(struct iop));
97 iop->bufsize = bufsize;
98 if ((flags & IO_READ) && (flags & IO_NEWSOCK) == 0)
99 iop->input = ioq_create(bufsize);
100 if ((flags & IO_WRITE) && (flags & IO_NEWSOCK) == 0)
101 iop->output = ioq_create(bufsize);
102 if (flags & IO_NBLOCK)
103 io_noblocking(iop, 1);
106 iop->notify = notify;
111 io_close(struct iop *iop)
115 ioq_destroy(iop->input);
116 if (iop->output != 0)
117 ioq_destroy(iop->output);
119 (void)close(iop->fd);
121 closesocket(iop->fd);
127 io_input(struct iop *iop, int waitforinput)
129 s_char buf[IO_BUFSIZE];
133 if ((iop->flags & IO_READ) == 0)
135 /* IOP is markes as in error. */
136 if (iop->flags & IO_ERROR)
138 /* Wait for the file to have input. */
140 empth_select(iop->fd, EMPTH_FD_READ);
143 /* Do the actual read. */
144 cc = read(iop->fd, buf, sizeof(buf));
146 /* would block, so nothing to read. */
147 if (errno == EWOULDBLOCK)
150 /* Some form of file error occurred... */
151 iop->flags |= IO_ERROR;
155 cc = recv(iop->fd, buf, sizeof(buf), 0);
156 if (cc == SOCKET_ERROR) {
157 int err = WSAGetLastError();
158 /* Hmm, it would block. file is opened noblock, soooooo.. */
159 if (err == WSAEWOULDBLOCK)
162 /* Some form of file error occurred... */
163 iop->flags |= IO_ERROR;
170 iop->flags |= IO_EOF;
174 /* Append the input to the IOQ. */
175 ioq_append(iop->input, buf, cc);
180 io_inputwaiting(struct iop *iop)
182 return ioq_qsize(iop->input);
186 io_outputwaiting(struct iop *iop)
188 return ioq_qsize(iop->output);
192 io_output(struct iop *iop, int waitforoutput)
195 struct iovec iov[16];
197 s_char buf[IO_BUFSIZE];
203 /* If there is no output waiting. */
204 if (!io_outputwaiting(iop))
207 /* If the iop is not write enabled. */
208 if ((iop->flags & IO_WRITE) == 0)
211 /* If the io is marked as in error... */
212 if (iop->flags & IO_ERROR)
215 /* This is the same test as io_outputwaiting.... */
216 if (ioq_qsize(iop->output) == 0)
220 /* make the iov point to the data in the queue. */
221 /* I.E., each of the elements in the queue. */
222 /* returns the number of elements in the iov. */
223 n = ioq_makeiov(iop->output, iov, IO_BUFSIZE);
225 /* Make a buffer containing the output to write. */
226 n = ioq_makebuf(iop->output, buf, sizeof(buf));
233 /* wait for the file to be output ready. */
234 if (waitforoutput != IO_NOWAIT) {
235 /* This waits for the file to be ready for writing, */
236 /* and lets other threads run. */
237 empth_select(iop->fd, EMPTH_FD_WRITE);
240 /* Do the actual write. */
242 cc = writev(iop->fd, iov, n);
244 /* if it failed.... */
246 /* Hmm, it would block. file is opened noblock, soooooo.. */
247 if (errno == EWOULDBLOCK) {
248 /* If there are remaining bytes, set the IO as remaining.. */
249 remain = ioq_qsize(iop->output);
252 iop->flags |= IO_ERROR;
256 cc = send(iop->fd, buf, n, 0);
258 /* if it failed.... */
259 if (cc == SOCKET_ERROR) {
260 int err = WSAGetLastError();
261 /* Hmm, it would block. file is opened noblock, soooooo.. */
262 if (err == WSAEWOULDBLOCK) {
263 /* If there are remaining bytes, set the IO as remaining.. */
264 remain = ioq_qsize(iop->output);
267 iop->flags |= IO_ERROR;
273 /* If no bytes were written, something happened.. Like an EOF. */
276 iop->flags |= IO_EOF;
281 remain = ioq_qsize(iop->output);
286 /* Remove the number of written bytes from the queue. */
287 ioq_dequeue(iop->output, cc);
293 io_peek(struct iop *iop, s_char *buf, int nbytes)
295 if ((iop->flags & IO_READ) == 0)
297 return ioq_peek(iop->input, buf, nbytes);
301 io_read(struct iop *iop, s_char *buf, int nbytes)
305 if ((iop->flags & IO_READ) == 0)
307 cc = ioq_peek(iop->input, buf, nbytes);
309 ioq_dequeue(iop->input, cc);
314 io_write(struct iop *iop, s_char *buf, int nbytes, int doWait)
318 if ((iop->flags & IO_WRITE) == 0)
320 ioq_append(iop->output, buf, nbytes);
321 len = ioq_qsize(iop->output);
322 if (len > iop->bufsize) {
326 /* only try a write every BUFSIZE characters */
327 if (((len - nbytes) % iop->bufsize) < (len % iop->bufsize))
335 io_output_all(struct iop *iop)
339 while ((n = io_output(iop, IO_NOWAIT)) > 0) {
340 empth_select(iop->fd, EMPTH_FD_WRITE);
346 io_gets(struct iop *iop, s_char *buf, int nbytes)
348 if ((iop->flags & IO_READ) == 0)
350 return ioq_gets(iop->input, buf, nbytes);
354 io_puts(struct iop *iop, s_char *buf)
356 if ((iop->flags & IO_WRITE) == 0)
358 return ioq_puts(iop->output, buf);
362 io_shutdown(struct iop *iop, int flags)
364 flags &= (IO_READ | IO_WRITE);
365 if ((iop->flags & flags) != flags)
367 if (flags & IO_READ) {
368 shutdown(iop->fd, 0);
369 ioq_drain(iop->input);
371 if (flags & IO_WRITE) {
372 shutdown(iop->fd, 1);
373 ioq_drain(iop->output);
379 io_noblocking(struct iop *iop, int value)
384 flags = fcntl(iop->fd, F_GETFL, 0);
391 if (fcntl(iop->fd, F_SETFL, flags) < 0)
395 ioctlsocket(iop->fd, FIONBIO, &arg);
398 iop->flags &= ~IO_NBLOCK;
400 iop->flags |= IO_NBLOCK;
405 io_conn(struct iop *iop)
407 return (iop->flags & IO_CONN);
411 io_error(struct iop *iop)
413 return (iop->flags & IO_ERROR);
417 io_eof(struct iop *iop)
419 return (iop->flags & IO_EOF);
423 io_fileno(struct iop *iop)