]> git.pond.sub.org Git - empserver/blob - src/client/serverio.c
7861e8c2ffb8aefc55ed0d79fde63bb89c4c016e
[empserver] / src / client / serverio.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  *  serverio.c: Handle input from server
29  * 
30  *  Known contributors to this file:
31  *     Steve McClure, 1998
32  */
33
34 #include <config.h>
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #ifdef _WIN32
39 #include <io.h>
40 #else
41 #include <unistd.h>
42 #endif
43 #include "misc.h"
44 #include "ioqueue.h"
45
46 int
47 serverio(int s, struct ioqueue *ioq)
48 {
49     char *buf;
50     int n;
51
52     if ((buf = malloc(ioq->bsize)) == NULL) {
53         fprintf(stderr, "malloc server i/o failed\n");
54         return 0;
55     }
56 #ifdef _WIN32
57     n = recv(s, buf, ioq->bsize, 0);
58 #else
59     n = read(s, buf, ioq->bsize);
60 #endif
61     if (n < 0) {
62 #ifdef _WIN32
63         errno = WSAGetLastError();
64 #endif
65         perror("server i/o read");
66         free(buf);
67         return 0;
68     }
69     if (n == 0) {
70         fprintf(stderr, "Server EOF\n");
71         (void)close(s);
72         return 0;
73     }
74     if (n != ioq->bsize)
75         buf = realloc(buf, n);
76     ioq_write(ioq, buf, n);
77     return 1;
78 }