]> git.pond.sub.org Git - empserver/blob - src/lib/empthread/io.c
Move queue flush out of io.c
[empserver] / src / lib / empthread / io.c
1 /*
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
5  *
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.
10  *
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.
15  *
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
19  *
20  *  ---
21  *
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.
25  *
26  *  ---
27  *
28  *  io.c: Arrange for input and output on a file descriptor to be queued.
29  *
30  *  Known contributors to this file:
31  *      Doug Hay, 1998
32  *      Steve McClure, 1998
33  *      Ron Koenderink, 2009
34  */
35
36 /*
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.
41  */
42
43 #include <config.h>
44
45 #include <errno.h>
46 #include <fcntl.h>
47 #include <stdlib.h>
48 #include <sys/socket.h>
49 #include <sys/types.h>
50 #include <sys/uio.h>
51 #include <unistd.h>
52 #include "empio.h"
53 #include "empthread.h"
54 #include "ioqueue.h"
55 #include "misc.h"
56 #include "queue.h"
57
58 struct iop {
59     int fd;
60     struct ioqueue *input;
61     struct ioqueue *output;
62     int flags;
63     int bufsize;
64     int last_out;
65     struct timeval input_timeout;
66 };
67
68 void
69 io_init(void)
70 {
71 }
72
73 struct iop *
74 io_open(int fd, int flags, int bufsize, struct timeval timeout)
75 {
76     int fdfl;
77     struct iop *iop;
78
79     flags = flags & (IO_READ | IO_WRITE);
80     if ((flags & (IO_READ | IO_WRITE)) == 0)
81         return NULL;
82
83     fdfl = fcntl(fd, F_GETFL, 0);
84     if (fdfl < 0)
85         return NULL;
86     fdfl |= O_NONBLOCK;
87     if (fcntl(fd, F_SETFL, fdfl) < 0)
88         return NULL;
89
90     iop = malloc(sizeof(struct iop));
91     if (!iop)
92         return NULL;
93     iop->fd = fd;
94     iop->input = NULL;
95     iop->output = NULL;
96     iop->flags = flags;
97     iop->last_out = 0;
98     iop->bufsize = bufsize;
99     iop->input_timeout = timeout;
100     if (flags & IO_READ)
101         iop->input = ioq_create(bufsize);
102     if (flags & IO_WRITE)
103         iop->output = ioq_create(bufsize);
104     return iop;
105 }
106
107 void
108 io_close(struct iop *iop)
109 {
110
111     if (iop->input)
112         ioq_destroy(iop->input);
113     if (iop->output)
114         ioq_destroy(iop->output);
115     (void)close(iop->fd);
116     free(iop);
117 }
118
119 /*
120  * Return number of bytes read on success, zero on timeout, early
121  * wakeup or EOF, -1 on error, with errno set appropriately.  In
122  * particular, return -1 with errno set to EAGAIN or EWOULDBLOCK when
123  * no data is available for non-blocking input (WAITFORINPUT false).
124  * Use io_eof() to distinguish timeout and early wakeup from EOF.
125  */
126 int
127 io_input(struct iop *iop, int waitforinput)
128 {
129     char buf[IO_BUFSIZE];
130     int cc;
131     int res;
132
133     /* Not a read IOP */
134     if ((iop->flags & IO_READ) == 0) {
135         errno = EBADF;
136         return -1;
137     }
138     /* IOP is markes as in error. */
139     if (iop->flags & IO_ERROR) {
140         errno = EBADF;
141         return -1;
142     }
143     /* Wait for the file to have input. */
144     if (waitforinput) {
145         res = empth_select(iop->fd, EMPTH_FD_READ, &iop->input_timeout);
146         if (res < 0) {
147             iop->flags |= IO_ERROR;
148             return -1;
149         } else if (res == 0)
150             return 0;
151     }
152
153     /* Do the actual read. */
154     cc = read(iop->fd, buf, sizeof(buf));
155     if (cc < 0) {
156         if (errno != EAGAIN && errno != EWOULDBLOCK)
157             /* Some form of file error occurred... */
158             iop->flags |= IO_ERROR;
159         return -1;
160     }
161
162     /* We eof'd */
163     if (cc == 0) {
164         iop->flags |= IO_EOF;
165         return 0;
166     }
167
168     /* Append the input to the IOQ. */
169     ioq_append(iop->input, buf, cc);
170     return cc;
171 }
172
173 int
174 io_inputwaiting(struct iop *iop)
175 {
176     return ioq_qsize(iop->input);
177 }
178
179 int
180 io_outputwaiting(struct iop *iop)
181 {
182     return ioq_qsize(iop->output);
183 }
184
185 /*
186  * Write output queued in IOP.
187  * If WAIT, writing may put the thread to sleep.
188  * Return number of bytes written on success, -1 on error.
189  * In particular, return zero when nothing was written because the
190  * queue was empty, or because the write slept and got woken up (only
191  * if WAIT), or because the write refused to sleep (only if !WAIT).
192  */
193 int
194 io_output(struct iop *iop, int wait)
195 {
196     struct iovec iov[16];
197     int n, res, cc;
198
199     if (!ioq_qsize(iop->output))
200         return 0;
201
202     if ((iop->flags & IO_WRITE) == 0)
203         return -1;
204
205     if (iop->flags & IO_ERROR)
206         return -1;
207
208     n = ioq_makeiov(iop->output, iov, IO_BUFSIZE);
209
210     if (wait) {
211         res = empth_select(iop->fd, EMPTH_FD_WRITE, NULL);
212         if (res == 0)
213             return 0;
214         if (res < 0) {
215             iop->flags |= IO_ERROR;
216             return -1;
217         }
218     }
219
220     cc = writev(iop->fd, iov, n);
221     if (cc < 0) {
222         if (errno == EAGAIN || errno == EWOULDBLOCK)
223             return 0;
224         iop->flags |= IO_ERROR;
225         return -1;
226     }
227
228     ioq_dequeue(iop->output, cc);
229     iop->last_out = ioq_qsize(iop->output);
230     return cc;
231 }
232
233 /*
234  * Write output queued in IOP if enough have been enqueued.
235  * Write if at least one buffer has been filled since the last write.
236  * If WAIT, writing may put the thread to sleep.
237  * Return number of bytes written on success, -1 on error.
238  * In particular, return zero when nothing was written because the
239  * queue was not long, or the write slept and got woken up (only if
240  * WAIT), or the write refused to sleep (only if !WAIT).
241  */
242 int
243 io_output_if_queue_long(struct iop *iop, int wait)
244 {
245     int len = ioq_qsize(iop->output);
246
247     if (CANT_HAPPEN(iop->last_out > len))
248         iop->last_out = 0;
249     if (len - iop->last_out < iop->bufsize)
250         return 0;
251     return io_output(iop, wait);
252 }
253
254
255 int
256 io_peek(struct iop *iop, char *buf, int nbytes)
257 {
258     if ((iop->flags & IO_READ) == 0)
259         return -1;
260     return ioq_peek(iop->input, buf, nbytes);
261 }
262
263 int
264 io_read(struct iop *iop, char *buf, int nbytes)
265 {
266     int cc;
267
268     if ((iop->flags & IO_READ) == 0)
269         return -1;
270     cc = ioq_peek(iop->input, buf, nbytes);
271     if (cc > 0)
272         ioq_dequeue(iop->input, cc);
273     return cc;
274 }
275
276 int
277 io_write(struct iop *iop, char *buf, int nbytes)
278 {
279     if ((iop->flags & IO_WRITE) == 0)
280         return -1;
281     ioq_append(iop->output, buf, nbytes);
282     return nbytes;
283 }
284
285 int
286 io_gets(struct iop *iop, char *buf, int nbytes)
287 {
288     if ((iop->flags & IO_READ) == 0)
289         return -1;
290     return ioq_gets(iop->input, buf, nbytes);
291 }
292
293 int
294 io_puts(struct iop *iop, char *buf)
295 {
296     if ((iop->flags & IO_WRITE) == 0)
297         return -1;
298     return ioq_puts(iop->output, buf);
299 }
300
301 int
302 io_shutdown(struct iop *iop, int flags)
303 {
304     flags &= (IO_READ | IO_WRITE);
305     if ((iop->flags & flags) != flags)
306         return -1;
307     if (flags & IO_READ) {
308         shutdown(iop->fd, 0);
309         ioq_drain(iop->input);
310     }
311     if (flags & IO_WRITE) {
312         shutdown(iop->fd, 1);
313         ioq_drain(iop->output);
314         iop->last_out = 0;
315     }
316     return 0;
317 }
318
319 int
320 io_error(struct iop *iop)
321 {
322     return iop->flags & IO_ERROR;
323 }
324
325 int
326 io_eof(struct iop *iop)
327 {
328     return iop->flags & IO_EOF;
329 }
330
331 int
332 io_fileno(struct iop *iop)
333 {
334     return iop->fd;
335 }