]> git.pond.sub.org Git - empserver/blob - src/lib/gen/io.c
[_WIN32]: Add #define WIN32 whereever winsock2.h is used. There is bug
[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
66 #include "empthread.h"
67
68 struct iop {
69     int fd;
70     struct ioqueue *input;
71     struct ioqueue *output;
72     int flags;
73     s_char *assoc;
74     int bufsize;
75     int (*notify)(void);
76 };
77
78 void
79 io_init(void)
80 {
81 }
82
83 struct iop *
84 io_open(int fd, int flags, int bufsize, int (*notify)(void),
85         s_char *assoc)
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 = (struct 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);
106     iop->flags = flags;
107     iop->assoc = assoc;
108     iop->notify = notify;
109     return iop;
110 }
111
112 void
113 io_close(struct iop *iop)
114 {
115
116     if (iop->input != 0)
117         ioq_destroy(iop->input);
118     if (iop->output != 0)
119         ioq_destroy(iop->output);
120 #if !defined(_WIN32)
121     (void)close(iop->fd);
122 #else
123     closesocket(iop->fd);
124 #endif
125     free((s_char *)iop);
126 }
127
128 int
129 io_input(struct iop *iop, int waitforinput)
130 {
131     s_char buf[IO_BUFSIZE];
132     int cc;
133
134     /* Not a read IOP */
135     if ((iop->flags & IO_READ) == 0)
136         return -1;
137     /* IOP is markes as in error. */
138     if (iop->flags & IO_ERROR)
139         return -1;
140     /* Wait for the file to have input. */
141     if (waitforinput) {
142         empth_select(iop->fd, EMPTH_FD_READ);
143     }
144 #if !defined(_WIN32)
145     /* Do the actual read. */
146     cc = read(iop->fd, buf, sizeof(buf));
147     if (cc < 0) {
148         /* would block, so nothing to read. */
149         if (errno == EWOULDBLOCK)
150             return 0;
151
152         /* Some form of file error occurred... */
153         iop->flags |= IO_ERROR;
154         return -1;
155     }
156 #else
157     cc = recv(iop->fd, buf, sizeof(buf), 0);
158     if (cc == SOCKET_ERROR) {
159         int err = WSAGetLastError();
160         /* Hmm, it would block.  file is opened noblock, soooooo.. */
161         if (err == WSAEWOULDBLOCK)
162             return 0;
163
164         /* Some form of file error occurred... */
165         iop->flags |= IO_ERROR;
166         return -1;
167     }
168 #endif
169
170     /* We eof'd */
171     if (cc == 0) {
172         iop->flags |= IO_EOF;
173         return 0;
174     }
175
176     /* Append the input to the IOQ. */
177     ioq_append(iop->input, buf, cc);
178     return cc;
179 }
180
181 int
182 io_inputwaiting(struct iop *iop)
183 {
184     return ioq_qsize(iop->input);
185 }
186
187 int
188 io_outputwaiting(struct iop *iop)
189 {
190     return ioq_qsize(iop->output);
191 }
192
193 int
194 io_output(struct iop *iop, int waitforoutput)
195 {
196 #if !defined(_WIN32)
197     struct iovec iov[16];
198 #else
199     s_char buf[IO_BUFSIZE];
200 #endif
201     int cc;
202     int n;
203     int remain;
204
205     /* If there is no output waiting. */
206     if (!io_outputwaiting(iop))
207         return 0;
208
209     /* If the iop is not write enabled. */
210     if ((iop->flags & IO_WRITE) == 0)
211         return -1;
212
213     /* If the io is marked as in error... */
214     if (iop->flags & IO_ERROR)
215         return -1;
216
217     /* This is the same test as io_outputwaiting.... */
218     if (ioq_qsize(iop->output) == 0)
219         return 0;
220
221 #if !defined(_WIN32)
222     /* make the iov point to the data in the queue. */
223     /* I.E., each of the elements in the queue. */
224     /* returns the number of elements in the iov. */
225     n = ioq_makeiov(iop->output, iov, IO_BUFSIZE);
226 #else
227     /* Make a buffer containing the output to write. */
228     n = ioq_makebuf(iop->output, buf, sizeof(buf));
229 #endif
230
231     if (n <= 0) {
232         return 0;
233     }
234
235     /* wait for the file to be output ready. */
236     if (waitforoutput != IO_NOWAIT) {
237         /* This waits for the file to be ready for writing, */
238         /* and lets other threads run. */
239         empth_select(iop->fd, EMPTH_FD_WRITE);
240     }
241
242     /* Do the actual write. */
243 #if !defined(_WIN32)
244     cc = writev(iop->fd, iov, n);
245
246     /* if it failed.... */
247     if (cc < 0) {
248         /* Hmm, it would block.  file is opened noblock, soooooo.. */
249         if (errno == EWOULDBLOCK) {
250             /* If there are remaining bytes, set the IO as remaining.. */
251             remain = ioq_qsize(iop->output);
252             return remain;
253         }
254         iop->flags |= IO_ERROR;
255         return -1;
256     }
257 #else
258     cc = send(iop->fd, buf, n, 0);
259
260     /* if it failed.... */
261     if (cc == SOCKET_ERROR) {
262         int err = WSAGetLastError();
263         /* Hmm, it would block.  file is opened noblock, soooooo.. */
264         if (err == WSAEWOULDBLOCK) {
265             /* If there are remaining bytes, set the IO as remaining.. */
266             remain = ioq_qsize(iop->output);
267             return remain;
268         }
269         iop->flags |= IO_ERROR;
270         return -1;
271     }
272 #endif
273
274
275     /* If no bytes were written, something happened..  Like an EOF. */
276 #ifndef hpux
277     if (cc == 0) {
278         iop->flags |= IO_EOF;
279         return 0;
280     }
281 #else
282     if (cc == 0) {
283         remain = ioq_qsize(iop->output);
284         return remain;
285     }
286 #endif /* hpux */
287
288     /* Remove the number of written bytes from the queue. */
289     ioq_dequeue(iop->output, cc);
290
291     return cc;
292 }
293
294 int
295 io_peek(struct iop *iop, s_char *buf, int nbytes)
296 {
297     if ((iop->flags & IO_READ) == 0)
298         return -1;
299     return ioq_peek(iop->input, buf, nbytes);
300 }
301
302 int
303 io_read(struct iop *iop, s_char *buf, int nbytes)
304 {
305     int cc;
306
307     if ((iop->flags & IO_READ) == 0)
308         return -1;
309     cc = ioq_peek(iop->input, buf, nbytes);
310     if (cc > 0)
311         ioq_dequeue(iop->input, cc);
312     return cc;
313 }
314
315 int
316 io_write(struct iop *iop, s_char *buf, int nbytes, int doWait)
317 {
318     int len;
319
320     if ((iop->flags & IO_WRITE) == 0)
321         return -1;
322     ioq_append(iop->output, buf, nbytes);
323     len = ioq_qsize(iop->output);
324     if (len > iop->bufsize) {
325         if (doWait) {
326             io_output_all(iop);
327         } else {
328             /* only try a write every BUFSIZE characters */
329             if (((len - nbytes) % iop->bufsize) < (len % iop->bufsize))
330                 io_output(iop, 0);
331         }
332     }
333     return nbytes;
334 }
335
336 int
337 io_output_all(struct iop *iop)
338 {
339     int n;
340
341     while ((n = io_output(iop, IO_NOWAIT)) > 0) {
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 }