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
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/time.h>
#ifndef __WIN32__
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/select.h>
#else
#include <io.h>
#include <winsock2.h>
#endif
#include "main.h"
#include "list.h"
#include "tux.h"
#include "proto.h"
#include "server.h"
#include "myTimer.h"
#include "arena.h"
#include "net_multiplayer.h"
#include "checkFront.h"
#include "protect.h"
#include "index.h"
#ifndef PUBLIC_SERVER
#include "screen_world.h"
#endif
#ifdef PUBLIC_SERVER
#include "publicServer.h"
#include "heightScore.h"
#endif
#include "tcp.h"
#include "tcp_server.h"
static sock_tcp_t *sock_server_tcp;
static sock_tcp_t *sock_server_tcp_second;
client_t* newTcpClient(sock_tcp_t *sock_tcp)
{
client_t *new;
char str_ip[STR_IP_SIZE];
assert( sock_tcp != NULL );
getSockTcpIp(sock_tcp, str_ip, STR_IP_SIZE);
printf("new client TCP from %s %d\n", str_ip, getSockTcpPort(sock_tcp));
new = newAnyClient();
new->type = CLIENT_TYPE_TCP;
new->socket_tcp = sock_tcp;
new->buffer = newBuffer(4096);
return new;
}
void destroyTcpClient(client_t *p)
{
char str_ip[STR_IP_SIZE];
eventMsgInCheckFront(p);
getSockTcpIp(p->socket_tcp, str_ip, STR_IP_SIZE);
printf("closeTCP connect %s %d\n", str_ip, getSockTcpPort(p->socket_tcp) );
closeTcpSocket(p->socket_tcp);
destroyBuffer(p->buffer);
destroyAnyClient(p);
}
int initTcpServer(char *ip, int port, int proto)
{
sock_server_tcp = bindTcpSocket(ip, port, proto);
sock_server_tcp_second = NULL;
if( sock_server_tcp == NULL )
{
return -1;
}
disableNagle(sock_server_tcp);
printf("server listen TCP port %s %d\n", ip, port);
return 0;
}
static void eventNewClient(sock_tcp_t *server_sock)
{
list_t *listClient;
sock_tcp_t *sock;
client_t *client;
listClient = getListServerClient();
sock = getTcpNewClient(server_sock);
disableNagle(sock);
client = newTcpClient(sock);
addList(listClient, client);
}
static void eventTcpClient(client_t *client)
{
char buffer[STR_PROTO_SIZE];
int ret;
memset(buffer, 0, STR_PROTO_SIZE);
ret = readTcpSocket(client->socket_tcp, buffer, STR_PROTO_SIZE-1);
if( ret <= 0 )
{
client->status = NET_STATUS_ZOMBIE;
return;
}
addBuffer(client->buffer, buffer, ret);
while( getBufferLine(client->buffer, buffer, STR_PROTO_SIZE) >= 0 )
{
addList(client->listRecvMsg, strdup(buffer) );
}
}
static int selectServerTcpSocket()
{
list_t *listClient;
struct timeval tv;
fd_set readfds;
fd_set errorfds;
int max_fd;
int ret;
int count;
int i;
listClient = getListServerClient();
#ifndef PUBLIC_SERVER
tv.tv_sec = 0;
tv.tv_usec = 0;
#endif
#ifdef PUBLIC_SERVER
tv.tv_sec = 0;
tv.tv_usec = 1000;
#endif
FD_ZERO(&readfds);
FD_ZERO(&errorfds);
max_fd = 0;
count = 0;
if( sock_server_tcp != NULL )
{
FD_SET(sock_server_tcp->sock, &errorfds);
FD_SET(sock_server_tcp->sock, &readfds);
if( sock_server_tcp->sock > max_fd )
{
max_fd = sock_server_tcp->sock;
}
}
for( i = 0 ; i < listClient->count ; i++ )
{
client_t *client;
client = (client_t *)listClient->list[i];
if( client->status != NET_STATUS_OK )
{
continue;
}
FD_SET(client->socket_tcp->sock, &errorfds);
FD_SET(client->socket_tcp->sock, &readfds);
if( client->socket_tcp->sock > max_fd )
{
max_fd = client->socket_tcp->sock;
}
}
#ifdef PUBLIC_SERVER
if( listClient->count == 0 )
{
ret = select(max_fd+1, &readfds, (fd_set *)NULL, &errorfds, NULL);
setServerTimer();
}
else
{
ret = select(max_fd+1, &readfds, (fd_set *)NULL,&errorfds, &tv);
}
#endif
#ifndef PUBLIC_SERVER
ret = select(max_fd+1, &readfds, (fd_set *)NULL,&errorfds, &tv);
#endif
if( ret < 0 )
{
//printf("select ret = %d\n", ret);
return 0;
}
if( sock_server_tcp != NULL )
{
if( FD_ISSET(sock_server_tcp->sock, &readfds) )
{
eventNewClient(sock_server_tcp);
count = 1;
}
if( FD_ISSET(sock_server_tcp->sock, &errorfds) )
{
printf("error\n");
}
}
for( i = 0 ; i < listClient->count ; i++ )
{
client_t *client;
client = (client_t *)listClient->list[i];
if( FD_ISSET(client->socket_tcp->sock, &readfds) )
{
//printf("eventTcpClient(client);\n");
eventTcpClient(client);
count = 1;
}
if( FD_ISSET(client->socket_tcp->sock, &errorfds) )
{
//printf("error client\n");
count = 1;
}
}
return count;
}
void eventTcpServer()
{
#ifndef PUBLIC_SERVER
int count;
#ifdef SUPPORT_NET_UNIX_UDP
do{
count = selectServerTcpSocket();
}while( count > 0 );
#endif
#endif
#ifdef PUBLIC_SERVER
selectServerTcpSocket();
#endif
}
void quitTcpServer()
{
if( sock_server_tcp != NULL )
{
printf("close port %d\n", getSockTcpPort(sock_server_tcp));
closeTcpSocket(sock_server_tcp);
}
if( sock_server_tcp_second != NULL )
{
printf("close port %d\n", getSockTcpPort(sock_server_tcp_second));
closeTcpSocket(sock_server_tcp_second);
}
printf("quit TCP port\n");
}
|