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