]> git.pond.sub.org Git - empserver/blob - src/client/serverio.c
Import of Empire 4.2.12
[empserver] / src / client / serverio.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2000, 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  *  serverio.c: Handle input from server
29  * 
30  *  Known contributors to this file:
31  *     Steve McClure, 1998
32  */
33
34 #include "misc.h"
35 #include "queue.h"
36 #include "ioqueue.h"
37
38 #include <stdio.h>
39
40 #if !defined(_WIN32)
41 #include <unistd.h>
42 #endif
43
44 #ifdef _WIN32
45 #include <malloc.h>
46 #include <winsock.h>
47 #endif
48
49 void free();
50 void ioq_write();
51
52 int
53 serverio(s, ioq)
54         int     s;
55         struct  ioqueue *ioq;
56 {
57 #ifndef _WIN32
58         extern  s_char *realloc();
59 #else
60         extern void *realloc();
61 #endif
62         s_char  *buf;
63         int     n;
64
65         if ((buf = malloc(ioq->bsize)) == 0) {
66                 fprintf(stderr, "malloc server i/o failed\n");
67                 return 0;
68         }
69 #ifdef _WIN32
70         n = recv(s, buf, ioq->bsize, 0);
71 #else
72         n = read(s, buf, ioq->bsize);
73 #endif
74         if (n < 0) {
75 #ifdef _WIN32
76                 errno = WSAGetLastError();
77 #endif
78                 perror("server i/o read");
79                 free(buf);
80                 return 0;
81         }
82         if (n == 0) {
83                 fprintf(stderr, "Server EOF\n");
84                 (void) close(s);
85                 return 0;
86         }
87         if (n != ioq->bsize)
88                 buf = (s_char *)realloc(buf, n);
89         ioq_write(ioq, buf, n);
90         return 1;
91 }
92