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);
109 io_close(struct iop *iop, time_t deadline)
111 struct timeval timeout;
112 char buf[IO_BUFSIZE];
115 while (io_output(iop, (time_t)-1) > 0) ;
116 shutdown(iop->fd, SHUT_WR);
117 while (empth_select(iop->fd, EMPTH_FD_READ,
118 io_timeout(&timeout, deadline)) > 0) {
119 ret = read(iop->fd, buf, sizeof(buf));
124 ioq_destroy(iop->input);
126 ioq_destroy(iop->output);
127 (void)close(iop->fd);
131 static struct timeval *
132 io_timeout(struct timeval *timeout, time_t deadline)
136 if (deadline == (time_t)-1)
137 return NULL; /* no deadline */
139 gettimeofday(&now, NULL);
140 if (now.tv_sec >= deadline) {
141 /* deadline reached already */
143 timeout->tv_usec = 0;
145 /* deadline in future */
146 timeout->tv_sec = deadline - now.tv_sec - 1;
147 timeout->tv_usec = 999999 - now.tv_usec;
148 /* yes, this is 1usec early; sue me */
155 * Read input from IOP and enqueue it.
156 * Wait at most until DEADLINE for input to arrive. (time_t)-1 means
157 * wait as long as it takes (no timeout).
158 * Does not yield the processor when DEADLINE is zero.
159 * A wait for input can be cut short by empth_wakeup().
160 * Return number of bytes read on success, -1 on error.
161 * In particular, return zero on timeout, early wakeup or EOF. Use
162 * io_eof() to distinguish timeout and early wakeup from EOF.
165 io_input(struct iop *iop, time_t deadline)
167 struct timeval timeout;
168 char buf[IO_BUFSIZE];
172 if ((iop->flags & IO_READ) == 0)
174 if (iop->flags & IO_ERROR)
176 if (iop->flags & IO_EOF)
180 res = empth_select(iop->fd, EMPTH_FD_READ,
181 io_timeout(&timeout, deadline));
183 iop->flags |= IO_ERROR;
189 cc = read(iop->fd, buf, sizeof(buf));
191 if (errno == EAGAIN || errno == EWOULDBLOCK)
193 iop->flags |= IO_ERROR;
197 iop->flags |= IO_EOF;
201 ioq_append(iop->input, buf, cc);
206 io_inputwaiting(struct iop *iop)
208 return ioq_qsize(iop->input);
212 io_outputwaiting(struct iop *iop)
214 return ioq_qsize(iop->output);
218 * Write output queued in IOP.
219 * Wait at most until DEADLINE for input to arrive. (time_t)-1 means
220 * wait as long as it takes (no timeout).
221 * Does not yield the processor when DEADLINE is zero.
222 * A wait for output can be cut short by empth_wakeup().
223 * Return number of bytes written on success, -1 on error.
224 * In particular, return zero when nothing was written because the
225 * queue is empty, and on timeout or early wakeup. Use
226 * io_outputwaiting() to distinguish timeout and early wakeup from
230 io_output(struct iop *iop, time_t deadline)
232 struct timeval timeout;
233 struct iovec iov[16];
239 if ((iop->flags & IO_WRITE) == 0)
242 if (iop->flags & IO_ERROR)
245 if (!ioq_qsize(iop->output))
249 res = empth_select(iop->fd, EMPTH_FD_WRITE,
250 io_timeout(&timeout, deadline));
254 iop->flags |= IO_ERROR;
259 n = ioq_makeiov(iop->output, iov, IO_BUFSIZE);
260 cc = writev(iop->fd, iov, n);
262 if (errno == EAGAIN || errno == EWOULDBLOCK)
264 iop->flags |= IO_ERROR;
268 ioq_dequeue(iop->output, cc);
269 iop->last_out = ioq_qsize(iop->output);
274 * Write output queued in IOP if enough have been enqueued.
275 * Write if at least one buffer has been filled since the last write.
276 * Wait at most until DEADLINE for output to be accepted. (time_t)-1
277 * means wait as long as it takes (no timeout).
278 * Does not yield the processor when DEADLINE is zero.
279 * A wait for output can be cut short by empth_wakeup().
280 * Return number of bytes written on success, -1 on error.
281 * In particular, return zero when nothing was written because the
282 * queue isn't long, and on timeout or early wakeup.
285 io_output_if_queue_long(struct iop *iop, time_t deadline)
287 int len = ioq_qsize(iop->output);
289 if (CANT_HAPPEN(iop->last_out > len))
291 if (len - iop->last_out < iop->bufsize) {
296 return io_output(iop, deadline);
300 io_peek(struct iop *iop, char *buf, int nbytes)
302 if ((iop->flags & IO_READ) == 0)
304 return ioq_peek(iop->input, buf, nbytes);
308 io_read(struct iop *iop, char *buf, int nbytes)
312 if ((iop->flags & IO_READ) == 0)
314 cc = ioq_peek(iop->input, buf, nbytes);
316 ioq_dequeue(iop->input, cc);
321 io_write(struct iop *iop, char *buf, int nbytes)
323 if ((iop->flags & IO_WRITE) == 0)
325 ioq_append(iop->output, buf, nbytes);
330 io_gets(struct iop *iop, char *buf, int nbytes)
332 if ((iop->flags & IO_READ) == 0)
334 return ioq_gets(iop->input, buf, nbytes);
338 io_puts(struct iop *iop, char *buf)
340 if ((iop->flags & IO_WRITE) == 0)
342 return ioq_puts(iop->output, buf);
346 io_shutdown(struct iop *iop, int flags)
348 flags &= (IO_READ | IO_WRITE);
349 if ((iop->flags & flags) != flags)
351 if (flags & IO_READ) {
352 shutdown(iop->fd, 0);
353 ioq_drain(iop->input);
355 if (flags & IO_WRITE) {
356 shutdown(iop->fd, 1);
357 ioq_drain(iop->output);
364 io_error(struct iop *iop)
366 return iop->flags & IO_ERROR;
370 io_eof(struct iop *iop)
372 return iop->flags & IO_EOF;
376 * Discard IOP's buffered input and set its EOF flag.
377 * No more input can be read from IOP.
380 io_set_eof(struct iop *iop)
382 ioq_drain(iop->input);
383 iop->flags |= IO_EOF;
387 io_fileno(struct iop *iop)