2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2011, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
6 * Empire 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 3 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, see <http://www.gnu.org/licenses/>.
21 * See files README, COPYING and CREDITS in the root of the source
22 * tree for related information and legal notices. It is expected
23 * that future projects/authors will amend these files as needed.
27 * io.c: Arrange for input and output on a file descriptor to be queued.
29 * Known contributors to this file:
32 * Ron Koenderink, 2009
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.
47 #include <sys/socket.h>
48 #include <sys/types.h>
52 #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];
202 if (!ioq_qsize(iop->output))
205 if ((iop->flags & IO_WRITE) == 0)
208 if (iop->flags & IO_ERROR)
212 res = empth_select(iop->fd, EMPTH_FD_WRITE, NULL);
216 iop->flags |= IO_ERROR;
221 n = ioq_makeiov(iop->output, iov, IO_BUFSIZE);
222 cc = writev(iop->fd, iov, n);
224 if (errno == EAGAIN || errno == EWOULDBLOCK)
226 iop->flags |= IO_ERROR;
230 ioq_dequeue(iop->output, cc);
231 iop->last_out = ioq_qsize(iop->output);
236 * Write output queued in IOP if enough have been enqueued.
237 * Write if at least one buffer has been filled since the last write.
238 * If WAIT, writing may put the thread to sleep.
239 * Return number of bytes written on success, -1 on error.
240 * In particular, return zero when nothing was written because the
241 * queue was not long, or the write slept and got woken up (only if
242 * WAIT), or the write refused to sleep (only if !WAIT).
245 io_output_if_queue_long(struct iop *iop, int wait)
247 int len = ioq_qsize(iop->output);
249 if (CANT_HAPPEN(iop->last_out > len))
251 if (len - iop->last_out < iop->bufsize) {
256 return io_output(iop, wait);
261 io_peek(struct iop *iop, char *buf, int nbytes)
263 if ((iop->flags & IO_READ) == 0)
265 return ioq_peek(iop->input, buf, nbytes);
269 io_read(struct iop *iop, char *buf, int nbytes)
273 if ((iop->flags & IO_READ) == 0)
275 cc = ioq_peek(iop->input, buf, nbytes);
277 ioq_dequeue(iop->input, cc);
282 io_write(struct iop *iop, char *buf, int nbytes)
284 if ((iop->flags & IO_WRITE) == 0)
286 ioq_append(iop->output, buf, nbytes);
291 io_gets(struct iop *iop, char *buf, int nbytes)
293 if ((iop->flags & IO_READ) == 0)
295 return ioq_gets(iop->input, buf, nbytes);
299 io_puts(struct iop *iop, char *buf)
301 if ((iop->flags & IO_WRITE) == 0)
303 return ioq_puts(iop->output, buf);
307 io_shutdown(struct iop *iop, int flags)
309 flags &= (IO_READ | IO_WRITE);
310 if ((iop->flags & flags) != flags)
312 if (flags & IO_READ) {
313 shutdown(iop->fd, 0);
314 ioq_drain(iop->input);
316 if (flags & IO_WRITE) {
317 shutdown(iop->fd, 1);
318 ioq_drain(iop->output);
325 io_error(struct iop *iop)
327 return iop->flags & IO_ERROR;
331 io_eof(struct iop *iop)
333 return iop->flags & IO_EOF;
337 io_fileno(struct iop *iop)