2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2009, 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 files README, COPYING and CREDITS in the root of the source
23 * tree for related information and legal notices. It is expected
24 * that future 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:
33 * Ron Koenderink, 2009
37 * Arrange for input and output on a file descriptor
38 * to be queued. Provide main loop -- a mechanism for
39 * blocking across all registered file descriptors, and
40 * reading or writing when appropriate.
48 #include <sys/socket.h>
49 #include <sys/types.h>
53 #include "empthread.h"
60 struct ioqueue *input;
61 struct ioqueue *output;
65 struct timeval input_timeout;
74 io_open(int fd, int flags, int bufsize, struct timeval timeout)
79 flags = flags & (IO_READ | IO_WRITE);
80 if ((flags & (IO_READ | IO_WRITE)) == 0)
83 fdfl = fcntl(fd, F_GETFL, 0);
87 if (fcntl(fd, F_SETFL, fdfl) < 0)
90 iop = malloc(sizeof(struct iop));
98 iop->bufsize = bufsize;
99 iop->input_timeout = timeout;
101 iop->input = ioq_create(bufsize);
102 if (flags & IO_WRITE)
103 iop->output = ioq_create(bufsize);
108 io_close(struct iop *iop)
112 ioq_destroy(iop->input);
114 ioq_destroy(iop->output);
115 (void)close(iop->fd);
120 * Return number of bytes read on success, zero on timeout, early
121 * wakeup or EOF, -1 on error, with errno set appropriately. In
122 * particular, return -1 with errno set to EAGAIN or EWOULDBLOCK when
123 * no data is available for non-blocking input (WAITFORINPUT false).
124 * Use io_eof() to distinguish timeout and early wakeup from EOF.
127 io_input(struct iop *iop, int waitforinput)
129 char buf[IO_BUFSIZE];
134 if ((iop->flags & IO_READ) == 0) {
138 /* IOP is markes as in error. */
139 if (iop->flags & IO_ERROR) {
143 /* Wait for the file to have input. */
145 res = empth_select(iop->fd, EMPTH_FD_READ, &iop->input_timeout);
147 iop->flags |= IO_ERROR;
153 /* Do the actual read. */
154 cc = read(iop->fd, buf, sizeof(buf));
156 if (errno != EAGAIN && errno != EWOULDBLOCK)
157 /* Some form of file error occurred... */
158 iop->flags |= IO_ERROR;
164 iop->flags |= IO_EOF;
168 /* Append the input to the IOQ. */
169 ioq_append(iop->input, buf, cc);
174 io_inputwaiting(struct iop *iop)
176 return ioq_qsize(iop->input);
180 io_outputwaiting(struct iop *iop)
182 return ioq_qsize(iop->output);
186 * Write output queued in IOP.
187 * If WAIT, writing may put the thread to sleep.
188 * Return number of bytes written on success, -1 on error.
189 * In particular, return zero when nothing was written because the
190 * queue was empty, or because the write slept and got woken up (only
191 * if WAIT), or because the write refused to sleep (only if !WAIT).
194 io_output(struct iop *iop, int wait)
196 struct iovec iov[16];
199 if (!ioq_qsize(iop->output))
202 if ((iop->flags & IO_WRITE) == 0)
205 if (iop->flags & IO_ERROR)
208 n = ioq_makeiov(iop->output, iov, IO_BUFSIZE);
211 res = empth_select(iop->fd, EMPTH_FD_WRITE, NULL);
215 iop->flags |= IO_ERROR;
220 cc = writev(iop->fd, iov, n);
222 if (errno == EAGAIN || errno == EWOULDBLOCK)
224 iop->flags |= IO_ERROR;
228 ioq_dequeue(iop->output, cc);
229 iop->last_out = ioq_qsize(iop->output);
234 * Write output queued in IOP if enough have been enqueued.
235 * Write if at least one buffer has been filled since the last write.
236 * If WAIT, writing may put the thread to sleep.
237 * Return number of bytes written on success, -1 on error.
238 * In particular, return zero when nothing was written because the
239 * queue was not long, or the write slept and got woken up (only if
240 * WAIT), or the write refused to sleep (only if !WAIT).
243 io_output_if_queue_long(struct iop *iop, int wait)
245 int len = ioq_qsize(iop->output);
247 if (CANT_HAPPEN(iop->last_out > len))
249 if (len - iop->last_out < iop->bufsize)
251 return io_output(iop, wait);
256 io_peek(struct iop *iop, char *buf, int nbytes)
258 if ((iop->flags & IO_READ) == 0)
260 return ioq_peek(iop->input, buf, nbytes);
264 io_read(struct iop *iop, char *buf, int nbytes)
268 if ((iop->flags & IO_READ) == 0)
270 cc = ioq_peek(iop->input, buf, nbytes);
272 ioq_dequeue(iop->input, cc);
277 io_write(struct iop *iop, char *buf, int nbytes)
279 if ((iop->flags & IO_WRITE) == 0)
281 ioq_append(iop->output, buf, nbytes);
286 io_gets(struct iop *iop, char *buf, int nbytes)
288 if ((iop->flags & IO_READ) == 0)
290 return ioq_gets(iop->input, buf, nbytes);
294 io_puts(struct iop *iop, char *buf)
296 if ((iop->flags & IO_WRITE) == 0)
298 return ioq_puts(iop->output, buf);
302 io_shutdown(struct iop *iop, int flags)
304 flags &= (IO_READ | IO_WRITE);
305 if ((iop->flags & flags) != flags)
307 if (flags & IO_READ) {
308 shutdown(iop->fd, 0);
309 ioq_drain(iop->input);
311 if (flags & IO_WRITE) {
312 shutdown(iop->fd, 1);
313 ioq_drain(iop->output);
320 io_error(struct iop *iop)
322 return iop->flags & IO_ERROR;
326 io_eof(struct iop *iop)
328 return iop->flags & IO_EOF;
332 io_fileno(struct iop *iop)