]> git.pond.sub.org Git - empserver/blob - src/lib/gen/io.c
(iop): Remove unused members assoc, notify.
[empserver] / src / lib / gen / io.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2006, 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  */
34
35 /*
36  * Arrange for input and output on a file descriptor
37  * to be queued.  Provide main loop -- a mechanism for
38  * blocking across all registered file descriptors, and
39  * reading or writing when appropriate.
40  */
41
42 #include <config.h>
43
44 #include <errno.h>
45 #include <sys/types.h>
46 #if !defined(_WIN32)
47 #include <sys/uio.h>
48 #include <sys/file.h>
49 #include <unistd.h>             /* close read shutdown select */
50 #include <sys/socket.h>
51 #endif
52 #include <time.h>
53 #include <fcntl.h>
54 #include <stdlib.h>             /* malloc calloc free */
55
56 #if defined(_WIN32)
57 #define WIN32
58 #include <winsock2.h>
59 #undef NS_ALL
60 #endif
61
62 #include "misc.h"
63 #include "queue.h"
64 #include "ioqueue.h"
65 #include "empio.h"
66 #include "gen.h"                /* getfdtablesize */
67 #include "server.h"
68
69 #include "empthread.h"
70
71 struct iop {
72     int fd;
73     struct ioqueue *input;
74     struct ioqueue *output;
75     int flags;
76     int bufsize;
77 };
78
79 void
80 io_init(void)
81 {
82 }
83
84 struct iop *
85 io_open(int fd, int flags, int bufsize)
86 {
87     struct iop *iop;
88
89     flags = flags & (IO_READ | IO_WRITE | IO_NBLOCK | IO_NEWSOCK);
90     if ((flags & (IO_READ | IO_WRITE)) == 0)
91         return NULL;
92     iop = malloc(sizeof(struct iop));
93     if (!iop)
94         return NULL;
95     iop->fd = fd;
96     iop->input = 0;
97     iop->output = 0;
98     iop->flags = 0;
99     iop->bufsize = bufsize;
100     if ((flags & IO_READ) && (flags & IO_NEWSOCK) == 0)
101         iop->input = ioq_create(bufsize);
102     if ((flags & IO_WRITE) && (flags & IO_NEWSOCK) == 0)
103         iop->output = ioq_create(bufsize);
104     if (flags & IO_NBLOCK)
105         io_noblocking(iop, 1);  /* FIXME check success */
106     iop->flags = flags;
107     return iop;
108 }
109
110 void
111 io_close(struct iop *iop)
112 {
113
114     if (iop->input != 0)
115         ioq_destroy(iop->input);
116     if (iop->output != 0)
117         ioq_destroy(iop->output);
118 #if !defined(_WIN32)
119     (void)close(iop->fd);
120 #else
121     closesocket(iop->fd);
122 #endif
123     free(iop);
124 }
125
126 int
127 io_input(struct iop *iop, int waitforinput)
128 {
129     s_char buf[IO_BUFSIZE];
130     int cc;
131
132     /* Not a read IOP */
133     if ((iop->flags & IO_READ) == 0)
134         return -1;
135     /* IOP is markes as in error. */
136     if (iop->flags & IO_ERROR)
137         return -1;
138     /* Wait for the file to have input. */
139     if (waitforinput) {
140         empth_select(iop->fd, EMPTH_FD_READ);
141     }
142 #if !defined(_WIN32)
143     /* Do the actual read. */
144     cc = read(iop->fd, buf, sizeof(buf));
145     if (cc < 0) {
146         /* would block, so nothing to read. */
147         if (errno == EAGAIN || errno == EWOULDBLOCK)
148             return 0;
149
150         /* Some form of file error occurred... */
151         iop->flags |= IO_ERROR;
152         return -1;
153     }
154 #else
155     cc = recv(iop->fd, buf, sizeof(buf), 0);
156     if (cc == SOCKET_ERROR) {
157         int err = WSAGetLastError();
158         /* Hmm, it would block.  file is opened noblock, soooooo.. */
159         if (err == WSAEWOULDBLOCK)
160             return 0;
161
162         /* Some form of file error occurred... */
163         iop->flags |= IO_ERROR;
164         return -1;
165     }
166 #endif
167
168     /* We eof'd */
169     if (cc == 0) {
170         iop->flags |= IO_EOF;
171         return 0;
172     }
173
174     /* Append the input to the IOQ. */
175     ioq_append(iop->input, buf, cc);
176     return cc;
177 }
178
179 int
180 io_inputwaiting(struct iop *iop)
181 {
182     return ioq_qsize(iop->input);
183 }
184
185 int
186 io_outputwaiting(struct iop *iop)
187 {
188     return ioq_qsize(iop->output);
189 }
190
191 int
192 io_output(struct iop *iop, int waitforoutput)
193 {
194 #if !defined(_WIN32)
195     struct iovec iov[16];
196 #else
197     s_char buf[IO_BUFSIZE];
198 #endif
199     int cc;
200     int n;
201     int remain;
202
203     /* If there is no output waiting. */
204     if (!io_outputwaiting(iop))
205         return 0;
206
207     /* If the iop is not write enabled. */
208     if ((iop->flags & IO_WRITE) == 0)
209         return -1;
210
211     /* If the io is marked as in error... */
212     if (iop->flags & IO_ERROR)
213         return -1;
214
215 #if !defined(_WIN32)
216     /* make the iov point to the data in the queue. */
217     /* I.E., each of the elements in the queue. */
218     /* returns the number of elements in the iov. */
219     n = ioq_makeiov(iop->output, iov, IO_BUFSIZE);
220 #else
221     /* Make a buffer containing the output to write. */
222     n = ioq_makebuf(iop->output, buf, sizeof(buf));
223 #endif
224
225     if (n <= 0) {
226         return 0;
227     }
228
229     /* wait for the file to be output ready. */
230     if (waitforoutput != IO_NOWAIT) {
231         /* This waits for the file to be ready for writing, */
232         /* and lets other threads run. */
233         empth_select(iop->fd, EMPTH_FD_WRITE);
234     }
235
236     /* Do the actual write. */
237 #if !defined(_WIN32)
238     cc = writev(iop->fd, iov, n);
239
240     /* if it failed.... */
241     if (cc < 0) {
242         /* Hmm, it would block.  file is opened noblock, soooooo.. */
243         if (errno == EAGAIN || errno == EWOULDBLOCK) {
244             /* If there are remaining bytes, set the IO as remaining.. */
245             remain = ioq_qsize(iop->output);
246             return remain;
247         }
248         iop->flags |= IO_ERROR;
249         return -1;
250     }
251 #else
252     cc = send(iop->fd, buf, n, 0);
253
254     /* if it failed.... */
255     if (cc == SOCKET_ERROR) {
256         int err = WSAGetLastError();
257         /* Hmm, it would block.  file is opened noblock, soooooo.. */
258         if (err == WSAEWOULDBLOCK) {
259             /* If there are remaining bytes, set the IO as remaining.. */
260             remain = ioq_qsize(iop->output);
261             return remain;
262         }
263         iop->flags |= IO_ERROR;
264         return -1;
265     }
266 #endif
267
268     /* If no bytes were written, something happened..  Like an EOF. */
269     if (cc == 0) {
270         iop->flags |= IO_EOF;
271         return 0;
272     }
273
274     /* Remove the number of written bytes from the queue. */
275     ioq_dequeue(iop->output, cc);
276
277     return cc;
278 }
279
280 int
281 io_peek(struct iop *iop, s_char *buf, int nbytes)
282 {
283     if ((iop->flags & IO_READ) == 0)
284         return -1;
285     return ioq_peek(iop->input, buf, nbytes);
286 }
287
288 int
289 io_read(struct iop *iop, s_char *buf, int nbytes)
290 {
291     int cc;
292
293     if ((iop->flags & IO_READ) == 0)
294         return -1;
295     cc = ioq_peek(iop->input, buf, nbytes);
296     if (cc > 0)
297         ioq_dequeue(iop->input, cc);
298     return cc;
299 }
300
301 int
302 io_write(struct iop *iop, s_char *buf, int nbytes, int doWait)
303 {
304     int len;
305
306     if ((iop->flags & IO_WRITE) == 0)
307         return -1;
308     ioq_append(iop->output, buf, nbytes);
309     len = ioq_qsize(iop->output);
310     if (len > iop->bufsize) {
311         if (doWait) {
312             io_output_all(iop);
313         } else {
314             /* only try a write every BUFSIZE characters */
315             if (((len - nbytes) % iop->bufsize) < (len % iop->bufsize))
316                 io_output(iop, IO_NOWAIT);
317         }
318     }
319     return nbytes;
320 }
321
322 int
323 io_output_all(struct iop *iop)
324 {
325     int n;
326
327     /*
328      * Mustn't block a player thread while update is pending, or else
329      * a malicous player could delay the update indefinitely
330      */
331     while (((n = io_output(iop, IO_NOWAIT)) > 0) && !update_pending) {
332         empth_select(iop->fd, EMPTH_FD_WRITE);
333     }
334     return n;
335 }
336
337 int
338 io_gets(struct iop *iop, s_char *buf, int nbytes)
339 {
340     if ((iop->flags & IO_READ) == 0)
341         return -1;
342     return ioq_gets(iop->input, buf, nbytes);
343 }
344
345 int
346 io_puts(struct iop *iop, s_char *buf)
347 {
348     if ((iop->flags & IO_WRITE) == 0)
349         return -1;
350     return ioq_puts(iop->output, buf);
351 }
352
353 int
354 io_shutdown(struct iop *iop, int flags)
355 {
356     flags &= (IO_READ | IO_WRITE);
357     if ((iop->flags & flags) != flags)
358         return -1;
359     if (flags & IO_READ) {
360         shutdown(iop->fd, 0);
361         ioq_drain(iop->input);
362     }
363     if (flags & IO_WRITE) {
364         shutdown(iop->fd, 1);
365         ioq_drain(iop->output);
366     }
367     return 0;
368 }
369
370 int
371 io_noblocking(struct iop *iop, int value)
372 {
373 #if !defined(_WIN32)
374     int flags;
375
376     flags = fcntl(iop->fd, F_GETFL, 0);
377     if (flags < 0)
378         return -1;
379     if (value == 0)
380         flags &= ~O_NONBLOCK;
381     else
382         flags |= O_NONBLOCK;
383     if (fcntl(iop->fd, F_SETFL, flags) < 0)
384         return -1;
385 #else
386     u_long arg = value;
387     ioctlsocket(iop->fd, FIONBIO, &arg);
388 #endif
389     if (value == 0)
390         iop->flags &= ~IO_NBLOCK;
391     else
392         iop->flags |= IO_NBLOCK;
393     return 0;
394 }
395
396 int
397 io_error(struct iop *iop)
398 {
399     return iop->flags & IO_ERROR;
400 }
401
402 int
403 io_eof(struct iop *iop)
404 {
405     return iop->flags & IO_EOF;
406 }
407
408 int
409 io_fileno(struct iop *iop)
410 {
411     return iop->fd;
412 }