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"
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)
113 ioq_destroy(iop->input);
115 ioq_destroy(iop->output);
116 (void)close(iop->fd);
121 * Return number of bytes read on success, zero on timeout, early
122 * wakeup or EOF, -1 on error, with errno set appropriately. In
123 * particular, return -1 with errno set to EAGAIN or EWOULDBLOCK when
124 * no data is available for non-blocking input (WAITFORINPUT false).
125 * Use io_eof() to distinguish timeout and early wakeup from EOF.
128 io_input(struct iop *iop, int waitforinput)
130 char buf[IO_BUFSIZE];
135 if ((iop->flags & IO_READ) == 0) {
139 /* IOP is markes as in error. */
140 if (iop->flags & IO_ERROR) {
144 /* Wait for the file to have input. */
146 res = empth_select(iop->fd, EMPTH_FD_READ, &iop->input_timeout);
148 iop->flags |= IO_ERROR;
154 /* Do the actual read. */
155 cc = read(iop->fd, buf, sizeof(buf));
157 if (errno != EAGAIN && errno != EWOULDBLOCK)
158 /* Some form of file error occurred... */
159 iop->flags |= IO_ERROR;
165 iop->flags |= IO_EOF;
169 /* Append the input to the IOQ. */
170 ioq_append(iop->input, buf, cc);
175 io_inputwaiting(struct iop *iop)
177 return ioq_qsize(iop->input);
181 io_outputwaiting(struct iop *iop)
183 return ioq_qsize(iop->output);
187 * Write output queued in IOP.
188 * If WAIT, writing may put the thread to sleep.
189 * Return number of bytes written on success, -1 on error.
190 * In particular, return zero when nothing was written because the
191 * queue was empty, or because the write slept and got woken up (only
192 * if WAIT), or because the write refused to sleep (only if !WAIT).
195 io_output(struct iop *iop, int wait)
197 struct iovec iov[16];
203 if (!ioq_qsize(iop->output))
206 if ((iop->flags & IO_WRITE) == 0)
209 if (iop->flags & IO_ERROR)
212 n = ioq_makeiov(iop->output, iov, IO_BUFSIZE);
215 res = empth_select(iop->fd, EMPTH_FD_WRITE, NULL);
219 iop->flags |= IO_ERROR;
224 cc = writev(iop->fd, iov, n);
226 if (errno == EAGAIN || errno == EWOULDBLOCK)
228 iop->flags |= IO_ERROR;
232 ioq_dequeue(iop->output, cc);
233 iop->last_out = ioq_qsize(iop->output);
238 * Write output queued in IOP if enough have been enqueued.
239 * Write if at least one buffer has been filled since the last write.
240 * If WAIT, writing may put the thread to sleep.
241 * Return number of bytes written on success, -1 on error.
242 * In particular, return zero when nothing was written because the
243 * queue was not long, or the write slept and got woken up (only if
244 * WAIT), or the write refused to sleep (only if !WAIT).
247 io_output_if_queue_long(struct iop *iop, int wait)
249 int len = ioq_qsize(iop->output);
251 if (CANT_HAPPEN(iop->last_out > len))
253 if (len - iop->last_out < iop->bufsize)
255 return io_output(iop, wait);
260 io_peek(struct iop *iop, char *buf, int nbytes)
262 if ((iop->flags & IO_READ) == 0)
264 return ioq_peek(iop->input, buf, nbytes);
268 io_read(struct iop *iop, char *buf, int nbytes)
272 if ((iop->flags & IO_READ) == 0)
274 cc = ioq_peek(iop->input, buf, nbytes);
276 ioq_dequeue(iop->input, cc);
281 io_write(struct iop *iop, char *buf, int nbytes)
283 if ((iop->flags & IO_WRITE) == 0)
285 ioq_append(iop->output, buf, nbytes);
290 io_gets(struct iop *iop, char *buf, int nbytes)
292 if ((iop->flags & IO_READ) == 0)
294 return ioq_gets(iop->input, buf, nbytes);
298 io_puts(struct iop *iop, char *buf)
300 if ((iop->flags & IO_WRITE) == 0)
302 return ioq_puts(iop->output, buf);
306 io_shutdown(struct iop *iop, int flags)
308 flags &= (IO_READ | IO_WRITE);
309 if ((iop->flags & flags) != flags)
311 if (flags & IO_READ) {
312 shutdown(iop->fd, 0);
313 ioq_drain(iop->input);
315 if (flags & IO_WRITE) {
316 shutdown(iop->fd, 1);
317 ioq_drain(iop->output);
324 io_error(struct iop *iop)
326 return iop->flags & IO_ERROR;
330 io_eof(struct iop *iop)
332 return iop->flags & IO_EOF;
336 io_fileno(struct iop *iop)