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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
|
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
#include "tux.h"
#include "network.h"
#include "buffer.h"
#include "net_multiplayer.h"
#include "screen_world.h"
#include "proto.h"
#include "client.h"
static int protocolType;
#ifdef SUPPORT_NET_UNIX_TCP
#include "tcp.h"
static sock_tcp_t *sock_server_tcp;
#endif
#ifdef SUPPORT_NET_UNIX_UDP
#include "udp.h"
static sock_udp_t *sock_server_udp;
#endif
#ifdef SUPPORT_NET_SDL_UDP
#include "sdl_udp.h"
static sock_sdl_udp_t *sock_server_sdl_udp;
#endif
static buffer_t *clientBuffer;
static my_time_t lastPing;
static my_time_t lastPingServerAlive;
static void initClient()
{
clientBuffer = newBuffer( LIMIT_BUFFER );
lastPing = getMyTime();
lastPingServerAlive = getMyTime();
proto_send_hello_client();
}
#ifdef SUPPORT_NET_UNIX_TCP
int initTcpClient(char *ip, int port)
{
sock_server_tcp = connectTcpSocket(ip, port);
if( sock_server_tcp == NULL )
{
return -1;
}
printf("connect TCP %s %d\n", ip, port);
protocolType = NET_PROTOCOL_TYPE_TCP;
initClient();
return 0;
}
#endif
#ifdef SUPPORT_NET_UNIX_UDP
int initUdpClient(char *ip, int port)
{
sock_server_udp = connectUdpSocket(ip, port);
if( sock_server_udp == NULL )
{
return -1;
}
printf("connect UDP %s %d\n", ip, port);
protocolType = NET_PROTOCOL_TYPE_UDP;
initClient();
return 0;
}
#endif
#ifdef SUPPORT_NET_SDL_UDP
int initSdlUdpClient(char *ip, int port)
{
sock_server_sdl_udp = connectSdlUdpSocket(ip, port);
if( sock_server_sdl_udp == NULL )
{
return -1;
}
printf("connect UDP %s %d\n", ip, port);
protocolType = NET_PROTOCOL_TYPE_UDP;
initClient();
return 0;
}
#endif
void sendServer(char *msg)
{
int ret;
assert( msg != NULL );
#ifdef SUPPORT_NET_UNIX_TCP
ret = writeTcpSocket(sock_server_tcp, msg, strlen(msg));
if ( ret == 0 )
{
fprintf(stderr, "server uzatvoril sietovy socket\n");
setWorldEnd();
}
if ( ret < 0 )
{
fprintf(stderr, "nastala chyba pri poslani spravy serveru\n");
setWorldEnd();
}
#endif
#ifdef SUPPORT_NET_UNIX_UDP
ret = writeUdpSocket(sock_server_udp, sock_server_udp, msg, strlen(msg));
#endif
#ifdef SUPPORT_NET_SDL_UDP
ret = writeSdlUdpSocket(sock_server_sdl_udp, sock_server_sdl_udp, msg, strlen(msg));
#endif
}
static int eventServerSelect()
{
char buffer[STR_SIZE];
int ret;
memset(buffer,0 ,STR_SIZE);
#ifdef SUPPORT_NET_UNIX_TCP
ret = readTcpSocket(sock_server_tcp, buffer, STR_SIZE-1);
if( ret == 0 )
{
fprintf(stderr, "server uzatovril sietovy socket\n");
setWorldEnd();
return ret;
}
if( ret < 0 )
{
fprintf(stderr, "chyba, spojenie prerusene \n");
setWorldEnd();
return ret;
}
#endif
#ifdef SUPPORT_NET_UNIX_UDP
ret = readUdpSocket(sock_server_udp, sock_server_udp, buffer, STR_SIZE-1);
#endif
#ifdef SUPPORT_NET_SDL_UDP
ret = readSdlUdpSocket(sock_server_sdl_udp, sock_server_sdl_udp, buffer, STR_SIZE-1);
if( ret < 0 )
{
return ret;
}
#endif
if( addBuffer(clientBuffer, buffer, ret) != 0 )
{
fprintf(stderr, "chyba, nemozem zapisovat do mojho buffera !\n");
setWorldEnd();
return ret;
}
return ret;
}
void eventServerBuffer()
{
char line[STR_SIZE];
/* obsluha udalosti od servera */
assert( clientBuffer != NULL );
while ( getBufferLine(clientBuffer, line, STR_SIZE) >= 0 )
{
//printf("spracuvavam %s", line);
if( strncmp(line, "init", 4) == 0 )proto_recv_init_client(line);
if( strncmp(line, "event", 5) == 0 )proto_recv_event_client(line);
if( strncmp(line, "newtux", 6) == 0 )proto_recv_newtux_client(line);
if( strncmp(line, "deltux", 6) == 0 )proto_recv_deltux_client(line);
if( strncmp(line, "additem", 7) == 0 )proto_recv_additem_client(line);
if( strncmp(line, "shot", 4) == 0 )proto_recv_shot_client(line);
if( strncmp(line, "kill", 4) == 0 )proto_recv_kill_client(line);
if( strncmp(line, "score", 5) == 0 )proto_recv_score_client(line);
if( strncmp(line, "ping", 4) == 0 )proto_recv_ping_client(line);
if( strncmp(line, "end", 3) == 0 )proto_recv_end_client(line);
}
}
#ifdef SUPPORT_NET_UNIX_TCP
void selectClientTcpSocket()
{
fd_set readfds;
struct timeval tv;
int max_fd;
bool_t isNext;
do{
isNext = FALSE;
tv.tv_sec = 0;
tv.tv_usec = 0;
FD_ZERO(&readfds);
FD_SET(sock_server_tcp->sock, &readfds);
max_fd = sock_server_tcp->sock;
select(max_fd+1, &readfds, (fd_set *)NULL, (fd_set *)NULL, &tv);
if( FD_ISSET(sock_server_tcp->sock, &readfds) )
{
eventServerSelect();
isNext = TRUE;
}
}while( isNext == TRUE );
}
#endif
#if defined SUPPORT_NET_UNIX_UDP || defined SUPPORT_NET_SDL_UDP
void eventPingServer()
{
my_time_t currentTime;
currentTime = getMyTime();
if( currentTime - lastPing > CLIENT_TIMEOUT )
{
proto_send_ping_client();
lastPing = getMyTime();
}
}
void refreshPingServerAlive()
{
lastPingServerAlive = getMyTime();
}
bool_t isServerAlive()
{
my_time_t currentTime;
currentTime = getMyTime();
if( currentTime - lastPingServerAlive > SERVER_TIMEOUT_ALIVE )
{
return FALSE;
}
return TRUE;
}
#endif
#ifdef SUPPORT_NET_UNIX_UDP
void selectClientUdpSocket()
{
fd_set readfds;
struct timeval tv;
int max_fd;
bool_t isNext;
if( isServerAlive() == FALSE )
{
fprintf(stderr, "server neodpoveda !\n");
setWorldEnd();
return;
}
do{
isNext = FALSE;
tv.tv_sec = 0;
tv.tv_usec = 0;
FD_ZERO(&readfds);
FD_SET(sock_server_udp->sock, &readfds);
max_fd = sock_server_udp->sock;
select(max_fd+1, &readfds, (fd_set *)NULL, (fd_set *)NULL, &tv);
if( FD_ISSET(sock_server_udp->sock, &readfds) )
{
eventServerSelect();
isNext = TRUE;
}
}while( isNext == TRUE );
}
#endif
#ifdef SUPPORT_NET_SDL_UDP
void selectClientSdlUdpSocket()
{
int ret;
if( isServerAlive() == FALSE )
{
fprintf(stderr, "server neodpoveda !\n");
setWorldEnd();
return;
}
do{
ret = eventServerSelect();
}while( ret > 0);
}
#endif
static void quitClient()
{
proto_send_end_client();
assert( clientBuffer != NULL );
destroyBuffer(clientBuffer);
}
#ifdef SUPPORT_NET_UNIX_TCP
void quitTcpClient()
{
quitClient();
assert( sock_server_tcp != NULL );
closeTcpSocket(sock_server_tcp);
printf("quit TCP conenct\n");
}
#endif
#ifdef SUPPORT_NET_UNIX_UDP
void quitUdpClient()
{
quitClient();
assert( sock_server_udp != NULL );
closeUdpSocket(sock_server_udp);
printf("quit UDP conenct\n");
}
#endif
#ifdef SUPPORT_NET_SDL_UDP
void quitSdlUdpClient()
{
quitClient();
assert( sock_server_sdl_udp != NULL );
closeSdlUdpSocket(sock_server_sdl_udp);
printf("quit UDP conenct\n");
}
#endif
|