forked from eddoww/Astonia-3.5
/
io.c
340 lines (266 loc) · 8.18 KB
/
io.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
* $Id: io.c,v 1.2 2007/02/24 14:09:54 devel Exp $
*
* $Log: io.c,v $
* Revision 1.2 2007/02/24 14:09:54 devel
* NWO first checkin, feb 24
*
*/
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <net/if.h>
#include <netdb.h>
#include <errno.h>
#include <zlib.h>
#include "server.h"
#include "client.h"
#define NEED_PLAYER_STRUCT
#include "player.h"
#undef NEED_PLAYER_STRUCT
#include "log.h"
#include "mem.h"
#include "date.h"
#include "io.h"
struct player **player;
void *my_zlib_malloc(void *dummy,unsigned int cnt,unsigned int size)
{
return xmalloc(cnt*size,IM_ZLIB);
}
void my_zlib_free(void *dummy,void *ptr)
{
return xfree(ptr);
}
// close connection to player nr
void exit_player(int nr)
{
online--;
close(player[nr]->sock);
deflateEnd(&player[nr]->zs);
xfree(player[nr]);
player[nr]=NULL;
mem_usage-=sizeof(struct player);
}
/* Process a new connection by finding, initializing and connecting a player entry to a new socket */
static void new_player(int sock)
{
int n,nsock,len=sizeof(struct sockaddr_in);
u_long one=1;
struct sockaddr_in addr;
char buf[16];
nsock=accept(sock,(struct sockaddr *)&addr,&len);
if (nsock==-1) return;
ioctl(nsock,FIONBIO,(u_long*)&one); // non-blocking mode
for (n=1; n<MAXPLAYER; n++) if (!player[n]) break;
if (n==MAXPLAYER) { close(nsock); return; }
player[n]=xcalloc(sizeof(struct player),IM_PLAYER); mem_usage+=sizeof(struct player);
if (player[n]==NULL) { close(nsock); return; }
player[n]->zs.zalloc=my_zlib_malloc;
player[n]->zs.zfree=my_zlib_free;
if (deflateInit2(&player[n]->zs,1,Z_DEFLATED,14,7,Z_DEFAULT_STRATEGY)) { close(nsock); xfree(player[n]); player[n]=NULL; mem_usage-=sizeof(struct player); return; }
player[n]->sock=nsock;
player[n]->addr=addr.sin_addr.s_addr;
player[n]->state=ST_CONNECT;
player[n]->lastcmd=ticker;
buf[0]=SV_REALTIME;
*(unsigned int*)(buf+1)=realtime;
psend(n,buf,5);
online++;
if (!player[n]) return;
}
static void send_player(int nr)
{
int ret,len;
unsigned long long prof;
if (player[nr]->iptr<player[nr]->optr) {
len=OBUFSIZE-player[nr]->optr;
} else {
len=player[nr]->iptr-player[nr]->optr;
}
prof=prof_start(11); ret=send(player[nr]->sock,player[nr]->obuf+player[nr]->optr,len,0); prof_stop(11,prof);
if (ret==-1) { // send failure
kick_player(nr,NULL);
return;
}
player[nr]->optr+=ret;
if (player[nr]->optr==OBUFSIZE) player[nr]->optr=0;
sent_bytes_raw+=ret+60;
sent_bytes+=ret;
}
static int csend(int nr,unsigned char *buf,int len)
{
int size;
if (!player[nr]) return -1;
while (len) {
if (player[nr]->iptr>=player[nr]->optr) {
size=OBUFSIZE-player[nr]->iptr;
} else {
size=player[nr]->optr-player[nr]->iptr;
}
size=min(size,len);
memcpy(player[nr]->obuf+player[nr]->iptr,buf,size);
player[nr]->iptr+=size; if (player[nr]->iptr==OBUFSIZE) player[nr]->iptr=0;
buf+=size; len-=size;
if (player[nr]->iptr==player[nr]->optr) {
kick_player(nr,NULL);
return -1;
}
}
return 0;
}
static void rec_player(int nr)
{
int len;
len=recv(player[nr]->sock,(char*)player[nr]->inbuf+player[nr]->in_len,256-player[nr]->in_len,0);
if (len<1) { // receive failure
if (errno!=EWOULDBLOCK) {
//xlog("receive failure, kicking player %d",nr);
kick_player(nr,NULL);
}
return;
}
player[nr]->in_len+=len;
rec_bytes_raw+=len+60;
rec_bytes+=len;
}
static int io_sock;
void io_loop(void)
{
int n,fmax=0,tmp;
fd_set in_fd,out_fd;
struct timeval tv;
int panic=0;
unsigned long long prof;
if (ticker%8) return;
while (panic++<100) {
FD_ZERO(&in_fd); FD_ZERO(&out_fd); fmax=0;
FD_SET(io_sock,&in_fd);
if (io_sock>fmax) fmax=io_sock;
for (n=1; n<MAXPLAYER; n++) {
if (player[n]) {
if (player[n]->in_len<256) {
FD_SET(player[n]->sock,&in_fd);
if (player[n]->sock>fmax) fmax=player[n]->sock;
}
}
if (player[n]) {
if (player[n]->iptr!=player[n]->optr) {
FD_SET(player[n]->sock,&out_fd);
if (player[n]->sock>fmax) fmax=player[n]->sock;
}
}
}
tv.tv_sec=0;
tv.tv_usec=0;
tmp=select(fmax+1,&in_fd,&out_fd,NULL,&tv);
if (tmp<1) break;
if (FD_ISSET(io_sock,&in_fd)) new_player(io_sock);
for (n=1; n<MAXPLAYER; n++) {
if (!player[n]) continue;
if (FD_ISSET(player[n]->sock,&in_fd)) { prof=prof_start(9); rec_player(n); prof_stop(9,prof); }
if (!player[n]) continue; // yuck - rec_player might have kicked the player
if (FD_ISSET(player[n]->sock,&out_fd)){ prof=prof_start(10); send_player(n); prof_stop(10,prof); }
}
}
}
int init_io(void)
{
int sock,port;
u_long one=1;
struct sockaddr_in addr;
player=xcalloc(sizeof(struct player*)*MAXPLAYER,IM_BASE);
if (!player) return 0;
xlog("Allocated players: %.2fM (%d*%d)",sizeof(struct player*)*MAXPLAYER/1024.0/1024.0,sizeof(struct player*),MAXPLAYER);
mem_usage+=sizeof(struct player*)*MAXPLAYER;
xlog("Size of player data: %d bytes",sizeof(struct player));
sock=socket(PF_INET,SOCK_STREAM,0);
if (sock==-1) return 0;
ioctl(sock,FIONBIO,(u_long*)&one); // non-blocking mode
setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(const char *)&one,sizeof(int));
for (port=27584; port<27684; port++) {
addr.sin_family=AF_INET;
addr.sin_port=htons(port);
addr.sin_addr.s_addr=0;
if (!bind(sock,(struct sockaddr *)&addr,sizeof(addr))) break;
}
if (listen(sock,50)) return 0;
io_sock=sock;
server_port=port;
xlog("IO Init done: ID=%d",serverID);
return 1;
}
void exit_io(void)
{
close(io_sock);
}
void psend(int nr,char *buf,int len)
{
if (!player[nr]) return;
if (player[nr]->tptr+len>=OBUFSIZE) {
xlog("tptr overflow!");
kick_player(nr,NULL);
return;
}
memcpy(player[nr]->tbuf+player[nr]->tptr,buf,len);
player[nr]->tptr+=len;
}
// careful here, any csend might clear player[n]!
void pflush(void)
{
int n,ilen,olen,csize,ret,olow,ohigh;
unsigned char obuf[OBUFSIZE];
unsigned long long prof;
for (n=1; n<MAXPLAYER; n++) {
if (!player[n]) continue;
ilen=player[n]->tptr;
if (ilen>16) {
player[n]->zs.next_in=player[n]->tbuf;
player[n]->zs.avail_in=ilen;
player[n]->zs.next_out=obuf;
player[n]->zs.avail_out=OBUFSIZE;
prof=prof_start(12); ret=deflate(&player[n]->zs,Z_SYNC_FLUSH); prof_stop(12,prof);
if (ret!=Z_OK) {
elog("compression failure #1, kicking player %d",n);
kick_player(n,NULL);
continue;
}
if (player[n]->zs.avail_in) {
elog("compression failure #2, kicking player %d",n);
kick_player(n,NULL);
continue;
}
csize=OBUFSIZE-player[n]->zs.avail_out;
olen=(csize);
if (olen>63) {
ohigh=(olen>>8)|0x80;
olow=olen&255;
csend(n,(void*)(&ohigh),1);
csend(n,(void*)(&olow),1);
} else {
ohigh=olen|(0x40|0x80);
csend(n,(void*)(&ohigh),1);
}
csend(n,obuf,csize);
} else {
csize=player[n]->tptr;
olen=(csize);
if (olen>63) {
ohigh=olen>>8;
olow=olen&255;
csend(n,(void*)(&ohigh),1);
csend(n,(void*)(&olow),1);
} else {
ohigh=olen|0x40;
csend(n,(void*)(&ohigh),1);
}
if (ilen && player[n]) csend(n,player[n]->tbuf,ilen);
}
//xlog("ilen=%d, olen=%d",ilen,olen);
if (player[n]) player[n]->tptr=0;
}
}