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 * Markus Armbruster, 2004-2012
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"
61 struct ioqueue *input;
62 struct ioqueue *output;
66 struct timeval input_timeout;
75 io_open(int fd, int flags, int bufsize, struct timeval timeout)
80 flags = flags & (IO_READ | IO_WRITE);
81 if ((flags & (IO_READ | IO_WRITE)) == 0)
84 fdfl = fcntl(fd, F_GETFL, 0);
88 if (fcntl(fd, F_SETFL, fdfl) < 0)
91 iop = malloc(sizeof(struct iop));
99 iop->bufsize = bufsize;
100 iop->input_timeout = timeout;
102 iop->input = ioq_create(bufsize);
103 if (flags & IO_WRITE)
104 iop->output = ioq_create(bufsize);
109 io_close(struct iop *iop)
111 char buf[IO_BUFSIZE];
114 while (io_output(iop, 1) > 0) ;
115 shutdown(iop->fd, SHUT_WR);
116 while (empth_select(iop->fd, EMPTH_FD_READ, &iop->input_timeout) > 0) {
117 ret = read(iop->fd, buf, sizeof(buf));
122 ioq_destroy(iop->input);
124 ioq_destroy(iop->output);
125 (void)close(iop->fd);
130 * Return number of bytes read on success, zero on timeout, early
131 * wakeup or EOF, -1 on error, with errno set appropriately. In
132 * particular, return -1 with errno set to EAGAIN or EWOULDBLOCK when
133 * no data is available for non-blocking input (WAITFORINPUT false).
134 * Use io_eof() to distinguish timeout and early wakeup from EOF.
137 io_input(struct iop *iop, int waitforinput)
139 char buf[IO_BUFSIZE];
143 if ((iop->flags & IO_READ) == 0) {
147 if (iop->flags & IO_ERROR) {
151 if (iop->flags & IO_EOF)
154 /* Wait for the file to have input. */
156 res = empth_select(iop->fd, EMPTH_FD_READ, &iop->input_timeout);
158 iop->flags |= IO_ERROR;
164 /* Do the actual read. */
165 cc = read(iop->fd, buf, sizeof(buf));
167 if (errno != EAGAIN && errno != EWOULDBLOCK)
168 /* Some form of file error occurred... */
169 iop->flags |= IO_ERROR;
175 iop->flags |= IO_EOF;
179 /* Append the input to the IOQ. */
180 ioq_append(iop->input, buf, cc);
185 io_inputwaiting(struct iop *iop)
187 return ioq_qsize(iop->input);
191 io_outputwaiting(struct iop *iop)
193 return ioq_qsize(iop->output);
197 * Write output queued in IOP.
198 * If WAIT, writing may put the thread to sleep.
199 * Return number of bytes written on success, -1 on error.
200 * In particular, return zero when nothing was written because the
201 * queue was empty, or because the write slept and got woken up (only
202 * if WAIT), or because the write refused to sleep (only if !WAIT).
205 io_output(struct iop *iop, int wait)
207 struct iovec iov[16];
213 if ((iop->flags & IO_WRITE) == 0)
216 if (iop->flags & IO_ERROR)
219 if (!ioq_qsize(iop->output))
223 res = empth_select(iop->fd, EMPTH_FD_WRITE, NULL);
227 iop->flags |= IO_ERROR;
232 n = ioq_makeiov(iop->output, iov, IO_BUFSIZE);
233 cc = writev(iop->fd, iov, n);
235 if (errno == EAGAIN || errno == EWOULDBLOCK)
237 iop->flags |= IO_ERROR;
241 ioq_dequeue(iop->output, cc);
242 iop->last_out = ioq_qsize(iop->output);
247 * Write output queued in IOP if enough have been enqueued.
248 * Write if at least one buffer has been filled since the last write.
249 * If WAIT, writing may put the thread to sleep.
250 * Return number of bytes written on success, -1 on error.
251 * In particular, return zero when nothing was written because the
252 * queue was not long, or the write slept and got woken up (only if
253 * WAIT), or the write refused to sleep (only if !WAIT).
256 io_output_if_queue_long(struct iop *iop, int wait)
258 int len = ioq_qsize(iop->output);
260 if (CANT_HAPPEN(iop->last_out > len))
262 if (len - iop->last_out < iop->bufsize) {
267 return io_output(iop, wait);
271 io_peek(struct iop *iop, char *buf, int nbytes)
273 if ((iop->flags & IO_READ) == 0)
275 return ioq_peek(iop->input, buf, nbytes);
279 io_read(struct iop *iop, char *buf, int nbytes)
283 if ((iop->flags & IO_READ) == 0)
285 cc = ioq_peek(iop->input, buf, nbytes);
287 ioq_dequeue(iop->input, cc);
292 io_write(struct iop *iop, char *buf, int nbytes)
294 if ((iop->flags & IO_WRITE) == 0)
296 ioq_append(iop->output, buf, nbytes);
301 io_gets(struct iop *iop, char *buf, int nbytes)
303 if ((iop->flags & IO_READ) == 0)
305 return ioq_gets(iop->input, buf, nbytes);
309 io_puts(struct iop *iop, char *buf)
311 if ((iop->flags & IO_WRITE) == 0)
313 return ioq_puts(iop->output, buf);
317 io_shutdown(struct iop *iop, int flags)
319 flags &= (IO_READ | IO_WRITE);
320 if ((iop->flags & flags) != flags)
322 if (flags & IO_READ) {
323 shutdown(iop->fd, 0);
324 ioq_drain(iop->input);
326 if (flags & IO_WRITE) {
327 shutdown(iop->fd, 1);
328 ioq_drain(iop->output);
335 io_error(struct iop *iop)
337 return iop->flags & IO_ERROR;
341 io_eof(struct iop *iop)
343 return iop->flags & IO_EOF;
347 * Discard IOP's buffered input and set its EOF flag.
348 * No more input can be read from IOP.
351 io_set_eof(struct iop *iop)
353 ioq_drain(iop->input);
354 iop->flags |= IO_EOF;
358 io_fileno(struct iop *iop)