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;
68 static struct timeval *io_timeout(struct timeval *, time_t);
76 io_open(int fd, int flags, int bufsize)
81 flags = flags & (IO_READ | IO_WRITE);
82 if ((flags & (IO_READ | IO_WRITE)) == 0)
85 fdfl = fcntl(fd, F_GETFL, 0);
89 if (fcntl(fd, F_SETFL, fdfl) < 0)
92 iop = malloc(sizeof(struct iop));
100 iop->bufsize = bufsize;
102 iop->input = ioq_create(bufsize);
103 if (flags & IO_WRITE)
104 iop->output = ioq_create(bufsize);
110 * Flush output and wait for the client to close the connection.
111 * Wait at most until DEADLINE. (time_t)-1 means wait as long as it
112 * takes (no timeout).
113 * Both the flush and the wait can be separately cut short by
114 * empth_wakeup(). This is almost certainly not what you want. If
115 * you need early wakeup, better fix this function not to go to sleep
116 * after wakeup during flush.
119 io_close(struct iop *iop, time_t deadline)
121 struct timeval timeout;
122 char buf[IO_BUFSIZE];
125 while (io_output(iop, deadline) > 0) ;
126 shutdown(iop->fd, SHUT_WR);
127 while (empth_select(iop->fd, EMPTH_FD_READ,
128 io_timeout(&timeout, deadline)) > 0) {
129 ret = read(iop->fd, buf, sizeof(buf));
134 ioq_destroy(iop->input);
136 ioq_destroy(iop->output);
137 (void)close(iop->fd);
141 static struct timeval *
142 io_timeout(struct timeval *timeout, time_t deadline)
146 if (deadline == (time_t)-1)
147 return NULL; /* no deadline */
149 gettimeofday(&now, NULL);
150 if (now.tv_sec >= deadline) {
151 /* deadline reached already */
153 timeout->tv_usec = 0;
155 /* deadline in future */
156 timeout->tv_sec = deadline - now.tv_sec - 1;
157 timeout->tv_usec = 999999 - now.tv_usec;
158 /* yes, this is 1usec early; sue me */
165 * Read input from IOP and enqueue it.
166 * Wait at most until DEADLINE for input to arrive. (time_t)-1 means
167 * wait as long as it takes (no timeout).
168 * Does not yield the processor when DEADLINE is zero.
169 * A wait for input can be cut short by empth_wakeup().
170 * Return number of bytes read on success, -1 on error.
171 * In particular, return zero on timeout, early wakeup or EOF. Use
172 * io_eof() to distinguish timeout and early wakeup from EOF.
175 io_input(struct iop *iop, time_t deadline)
177 struct timeval timeout;
178 char buf[IO_BUFSIZE];
182 if ((iop->flags & IO_READ) == 0)
184 if (iop->flags & IO_ERROR)
186 if (iop->flags & IO_EOF)
190 res = empth_select(iop->fd, EMPTH_FD_READ,
191 io_timeout(&timeout, deadline));
193 iop->flags |= IO_ERROR;
199 cc = read(iop->fd, buf, sizeof(buf));
201 if (errno == EAGAIN || errno == EWOULDBLOCK)
203 iop->flags |= IO_ERROR;
207 iop->flags |= IO_EOF;
211 ioq_append(iop->input, buf, cc);
216 io_inputwaiting(struct iop *iop)
218 return ioq_qsize(iop->input);
222 io_outputwaiting(struct iop *iop)
224 return ioq_qsize(iop->output);
228 * Write output queued in IOP.
229 * Wait at most until DEADLINE for input to arrive. (time_t)-1 means
230 * wait as long as it takes (no timeout).
231 * Does not yield the processor when DEADLINE is zero.
232 * A wait for output can be cut short by empth_wakeup().
233 * Return number of bytes written on success, -1 on error.
234 * In particular, return zero when nothing was written because the
235 * queue is empty, and on timeout or early wakeup. Use
236 * io_outputwaiting() to distinguish timeout and early wakeup from
240 io_output(struct iop *iop, time_t deadline)
242 struct timeval timeout;
243 struct iovec iov[16];
249 if ((iop->flags & IO_WRITE) == 0)
252 if (iop->flags & IO_ERROR)
255 if (!ioq_qsize(iop->output))
259 res = empth_select(iop->fd, EMPTH_FD_WRITE,
260 io_timeout(&timeout, deadline));
264 iop->flags |= IO_ERROR;
269 n = ioq_makeiov(iop->output, iov, IO_BUFSIZE);
270 cc = writev(iop->fd, iov, n);
272 if (errno == EAGAIN || errno == EWOULDBLOCK)
274 iop->flags |= IO_ERROR;
278 ioq_dequeue(iop->output, cc);
279 iop->last_out = ioq_qsize(iop->output);
284 * Write output queued in IOP if enough have been enqueued.
285 * Write if at least one buffer has been filled since the last write.
286 * Wait at most until DEADLINE for output to be accepted. (time_t)-1
287 * means wait as long as it takes (no timeout).
288 * Does not yield the processor when DEADLINE is zero.
289 * A wait for output can be cut short by empth_wakeup().
290 * Return number of bytes written on success, -1 on error.
291 * In particular, return zero when nothing was written because the
292 * queue isn't long, and on timeout or early wakeup.
295 io_output_if_queue_long(struct iop *iop, time_t deadline)
297 int len = ioq_qsize(iop->output);
299 if (CANT_HAPPEN(iop->last_out > len))
301 if (len - iop->last_out < iop->bufsize) {
306 return io_output(iop, deadline);
310 io_peek(struct iop *iop, char *buf, int nbytes)
312 if ((iop->flags & IO_READ) == 0)
314 return ioq_peek(iop->input, buf, nbytes);
318 io_read(struct iop *iop, char *buf, int nbytes)
322 if ((iop->flags & IO_READ) == 0)
324 cc = ioq_peek(iop->input, buf, nbytes);
326 ioq_dequeue(iop->input, cc);
331 io_write(struct iop *iop, char *buf, int nbytes)
333 if ((iop->flags & IO_WRITE) == 0)
335 ioq_append(iop->output, buf, nbytes);
340 io_gets(struct iop *iop, char *buf, int nbytes)
342 if ((iop->flags & IO_READ) == 0)
344 return ioq_gets(iop->input, buf, nbytes);
348 io_puts(struct iop *iop, char *buf)
350 if ((iop->flags & IO_WRITE) == 0)
352 return ioq_puts(iop->output, buf);
356 io_shutdown(struct iop *iop, int flags)
358 flags &= (IO_READ | IO_WRITE);
359 if ((iop->flags & flags) != flags)
361 if (flags & IO_READ) {
362 shutdown(iop->fd, 0);
363 ioq_drain(iop->input);
365 if (flags & IO_WRITE) {
366 shutdown(iop->fd, 1);
367 ioq_drain(iop->output);
374 io_error(struct iop *iop)
376 return iop->flags & IO_ERROR;
380 io_eof(struct iop *iop)
382 return iop->flags & IO_EOF;
386 * Discard IOP's buffered input and set its EOF flag.
387 * No more input can be read from IOP.
390 io_set_eof(struct iop *iop)
392 ioq_drain(iop->input);
393 iop->flags |= IO_EOF;
397 io_fileno(struct iop *iop)