2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2014, 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"
60 struct ioqueue *input;
61 struct ioqueue *output;
67 static struct timeval *io_timeout(struct timeval *, time_t);
75 io_open(int fd, int flags, int bufsize)
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;
101 iop->input = ioq_create(bufsize);
102 if (flags & IO_WRITE)
103 iop->output = ioq_create(bufsize);
109 * Flush output and wait for the client to close the connection.
110 * Wait at most until DEADLINE. (time_t)-1 means wait as long as it
111 * takes (no timeout).
112 * Both the flush and the wait can be separately cut short by
113 * empth_wakeup(). This is almost certainly not what you want. If
114 * you need early wakeup, better fix this function not to go to sleep
115 * after wakeup during flush.
118 io_close(struct iop *iop, time_t deadline)
120 struct timeval timeout;
121 char buf[IO_BUFSIZE];
124 while (io_output(iop, deadline) > 0) ;
125 shutdown(iop->fd, SHUT_WR);
126 while (empth_select(iop->fd, EMPTH_FD_READ,
127 io_timeout(&timeout, deadline)) > 0) {
128 ret = read(iop->fd, buf, sizeof(buf));
133 ioq_destroy(iop->input);
135 ioq_destroy(iop->output);
136 (void)close(iop->fd);
140 static struct timeval *
141 io_timeout(struct timeval *timeout, time_t deadline)
145 if (deadline == (time_t)-1)
146 return NULL; /* no deadline */
148 gettimeofday(&now, NULL);
149 if (now.tv_sec >= deadline) {
150 /* deadline reached already */
152 timeout->tv_usec = 0;
154 /* deadline in future */
155 timeout->tv_sec = deadline - now.tv_sec - 1;
156 timeout->tv_usec = 999999 - now.tv_usec;
157 /* yes, this is 1usec early; sue me */
164 * Read input from IOP and enqueue it.
165 * Wait at most until DEADLINE for input to arrive. (time_t)-1 means
166 * wait as long as it takes (no timeout).
167 * Does not yield the processor when DEADLINE is zero.
168 * A wait for input can be cut short by empth_wakeup().
169 * Return number of bytes read on success, -1 on error.
170 * In particular, return zero on timeout, early wakeup or EOF. Use
171 * io_eof() to distinguish timeout and early wakeup from EOF.
174 io_input(struct iop *iop, time_t deadline)
176 struct timeval timeout;
177 char buf[IO_BUFSIZE];
181 if ((iop->flags & IO_READ) == 0)
183 if (iop->flags & IO_ERROR)
185 if (iop->flags & IO_EOF)
189 res = empth_select(iop->fd, EMPTH_FD_READ,
190 io_timeout(&timeout, deadline));
192 iop->flags |= IO_ERROR;
198 cc = read(iop->fd, buf, sizeof(buf));
200 if (errno == EAGAIN || errno == EWOULDBLOCK)
202 iop->flags |= IO_ERROR;
206 iop->flags |= IO_EOF;
210 ioq_append(iop->input, buf, cc);
215 io_inputwaiting(struct iop *iop)
217 return ioq_qsize(iop->input);
221 io_outputwaiting(struct iop *iop)
223 return ioq_qsize(iop->output);
227 * Write output queued in IOP.
228 * Wait at most until DEADLINE for input to arrive. (time_t)-1 means
229 * wait as long as it takes (no timeout).
230 * Does not yield the processor when DEADLINE is zero.
231 * A wait for output can be cut short by empth_wakeup().
232 * Return number of bytes written on success, -1 on error.
233 * In particular, return zero when nothing was written because the
234 * queue is empty, and on timeout or early wakeup. Use
235 * io_outputwaiting() to distinguish timeout and early wakeup from
239 io_output(struct iop *iop, time_t deadline)
241 struct timeval timeout;
242 struct iovec iov[16];
248 if ((iop->flags & IO_WRITE) == 0)
251 if (iop->flags & IO_ERROR)
254 if (!ioq_qsize(iop->output))
258 res = empth_select(iop->fd, EMPTH_FD_WRITE,
259 io_timeout(&timeout, deadline));
263 iop->flags |= IO_ERROR;
268 n = ioq_makeiov(iop->output, iov, IO_BUFSIZE);
269 cc = writev(iop->fd, iov, n);
271 if (errno == EAGAIN || errno == EWOULDBLOCK)
273 iop->flags |= IO_ERROR;
277 ioq_dequeue(iop->output, cc);
278 iop->last_out = ioq_qsize(iop->output);
283 * Write output queued in IOP if enough have been enqueued.
284 * Write if at least one buffer has been filled since the last write.
285 * Wait at most until DEADLINE for output to be accepted. (time_t)-1
286 * means wait as long as it takes (no timeout).
287 * Does not yield the processor when DEADLINE is zero.
288 * A wait for output can be cut short by empth_wakeup().
289 * Return number of bytes written on success, -1 on error.
290 * In particular, return zero when nothing was written because the
291 * queue isn't long, and on timeout or early wakeup.
294 io_output_if_queue_long(struct iop *iop, time_t deadline)
296 int len = ioq_qsize(iop->output);
298 if (CANT_HAPPEN(iop->last_out > len))
300 if (len - iop->last_out < iop->bufsize) {
305 return io_output(iop, deadline);
309 io_peek(struct iop *iop, char *buf, int nbytes)
311 if ((iop->flags & IO_READ) == 0)
313 return ioq_peek(iop->input, buf, nbytes);
317 io_read(struct iop *iop, char *buf, int nbytes)
321 if ((iop->flags & IO_READ) == 0)
323 cc = ioq_peek(iop->input, buf, nbytes);
325 ioq_dequeue(iop->input, cc);
330 io_write(struct iop *iop, char *buf, int nbytes)
332 if ((iop->flags & IO_WRITE) == 0)
334 ioq_append(iop->output, buf, nbytes);
339 io_gets(struct iop *iop, char *buf, int nbytes)
341 if ((iop->flags & IO_READ) == 0)
343 return ioq_gets(iop->input, buf, nbytes);
347 io_puts(struct iop *iop, char *buf)
349 if ((iop->flags & IO_WRITE) == 0)
351 return ioq_puts(iop->output, buf);
355 io_error(struct iop *iop)
357 return iop->flags & IO_ERROR;
361 io_eof(struct iop *iop)
363 return iop->flags & IO_EOF;
367 * Discard IOP's buffered input and set its EOF flag.
368 * No more input can be read from IOP.
371 io_set_eof(struct iop *iop)
373 ioq_drain(iop->input);
374 iop->flags |= IO_EOF;
378 io_fileno(struct iop *iop)