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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#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 "udp.h"
static sock_udp_t *sock_server_udp;
static sock_udp_t *sock_server_udp_second;
static list_t *listClient;
static list_t *listClientIndex;
static list_t *listServerTimer;
static time_t timeUpdate;
static int maxClients;
static void startUpdateServer()
{
timeUpdate = time(NULL);
}
time_t getUpdateServer()
{
return time(NULL) - timeUpdate;
}
static void eventDelClient(client_t *client)
{
int offset;
offset = getFormIndex(listClientIndex, getSockUdpPort(client->socket_udp));
assert( offset != -1 );
delFromIndex(listClientIndex, getSockUdpPort(client->socket_udp));
delListItem(listClient, offset, destroyClient);
}
static void delZombieCLient(void *p_nothink)
{
client_t *thisClient;
my_time_t currentTime;
int i;
currentTime = getMyTime();
for( i = 0 ; i < listClient->count; i++)
{
thisClient = (client_t *) listClient->list[i];
if( isDown(thisClient->protect) == TRUE )
{
proto_send_end_server(PROTO_SEND_ONE, thisClient);
eventMsgInCheckFront(thisClient);
thisClient->status = NET_STATUS_ZOMBIE;
}
if( thisClient->status == NET_STATUS_ZOMBIE )
{
if( thisClient->tux != NULL )
{
proto_send_del_server(PROTO_SEND_ALL, NULL, thisClient->tux->id);
}
eventDelClient(thisClient);
i--;
}
}
}
static void eventPeriodicSyncClient(void *p_nothink)
{
client_t *thisClientSend;
tux_t *thisTux;
int i;
#ifndef PUBLIC_SERVER
proto_send_newtux_server(PROTO_SEND_ALL_SEES_TUX, NULL, getControlTux(TUX_CONTROL_KEYBOARD_RIGHT));
#endif
for( i = 0 ; i < listClient->count; i++ )
{
thisClientSend = (client_t *) listClient->list[i];
thisTux = thisClientSend->tux;
if( thisTux == NULL )
{
continue;
}
//proto_send_newtux_server(PROTO_SEND_ALL_SEES_TUX, thisClientSend, thisTux);
proto_send_newtux_server(PROTO_SEND_BUT, thisClientSend, thisTux);
}
}
static void eventSendPingClients(void *p_nothink)
{
proto_send_ping_server(PROTO_SEND_ALL, NULL);
}
static void setServerTimer()
{
if( listServerTimer != NULL )
{
destroyTimer(listServerTimer);
}
restartTimer();
listServerTimer = newTimer();
addTaskToTimer(listServerTimer, TIMER_PERIODIC, delZombieCLient, NULL, SERVER_TIMEOUT);
addTaskToTimer(listServerTimer, TIMER_PERIODIC, eventPeriodicSyncClient, NULL, SERVER_TIME_SYNC);
addTaskToTimer(listServerTimer, TIMER_PERIODIC, eventSendPingClients, NULL, SERVER_TIME_PING);
}
static void initServer()
{
startUpdateServer();
listServerTimer = NULL;
listClient = newList();
listClientIndex = newIndex();
setServerMaxClients(SERVER_MAX_CLIENTS);
setServerTimer();
}
int initUdpServer(char *ip, int port, int proto)
{
sock_server_udp = bindUdpSocket(ip, port, proto);
sock_server_udp_second = NULL;
if( sock_server_udp == NULL )
{
return -1;
}
initServer();
printf("server listen UDP port %s %d\n", ip, port);
return 0;
}
#ifdef PUBLIC_SERVER
int initUdpPublicServer(char *ip4, int port4, char *ip6, int port6)
{
sock_server_udp = NULL;
sock_server_udp_second = NULL;
if( ip4 != NULL )
{
sock_server_udp = bindUdpSocket(ip4, port4, PROTO_UDPv4);
if( sock_server_udp == NULL )
{
return -1;
}
printf("server listen UDP port %s %d\n", ip4, port4);
}
if( ip6 != NULL )
{
sock_server_udp_second = bindUdpSocket(ip6, port6, PROTO_UDPv6);
if( sock_server_udp_second == NULL )
{
return -1;
}
printf("server listen UDP port %s %d\n", ip6, port6);
}
initServer();
return 0;
}
#endif
client_t* newUdpClient(sock_udp_t *sock_udp)
{
client_t *new;
char str_ip[STR_IP_SIZE];
assert( sock_udp != NULL );
getSockUdpIp(sock_udp, str_ip, STR_IP_SIZE);
printf("new client from %s %d\n", str_ip, getSockUdpPort(sock_udp));
new = malloc( sizeof(client_t) );
memset(new, 0, sizeof(client_t));
new->status = NET_STATUS_OK;
new->listRecvMsg = newList();
new->listSendMsg = newCheckFront();
new->listSeesShot = newList();
new->protect = newProtect();
new->socket_udp = sock_udp;
return new;
}
void destroyClient(client_t *p)
{
char str_ip[STR_IP_SIZE];
assert( p != NULL );
eventMsgInCheckFront(p);
getSockUdpIp(p->socket_udp, str_ip, STR_IP_SIZE);
printf("close connect %s %d\n", str_ip, getSockUdpPort(p->socket_udp) );
closeUdpSocket(p->socket_udp);
destroyListItem(p->listRecvMsg, free);
destroyListItem(p->listSeesShot, free);
destroyCheckFront(p->listSendMsg);
destroyProtect(p->protect);
if( p->tux != NULL )
{
#ifdef PUBLIC_SERVER
addPlayerInHighScore(p->tux->name, p->tux->score);
#endif
delObjectFromSpaceWithObject(getCurrentArena()->spaceTux, p->tux, destroyTux);
}
free(p);
}
list_t* getListServerClient()
{
return listClient;
}
int getServerMaxClients()
{
return maxClients;
}
void setServerMaxClients(int n)
{
maxClients = n;
}
void sendClient(client_t *p, char *msg)
{
assert( p != NULL );
assert( msg != NULL );
if( p->status != NET_STATUS_ZOMBIE )
{
int ret;
#ifndef PUBLIC_SERVER
if( isParamFlag("--send") )
{
printf("send -> %s", msg);
}
#endif
if( p->socket_udp->proto == PROTO_UDPv4 &&
sock_server_udp != NULL &&
sock_server_udp->proto == PROTO_UDPv4 )
{
ret = writeUdpSocket(sock_server_udp, p->socket_udp, msg, strlen(msg));
}
if( p->socket_udp->proto == PROTO_UDPv6 &&
sock_server_udp != NULL &&
sock_server_udp->proto == PROTO_UDPv6 )
{
ret = writeUdpSocket(sock_server_udp, p->socket_udp, msg, strlen(msg));
}
if( p->socket_udp->proto == PROTO_UDPv6 &&
sock_server_udp_second != NULL &&
sock_server_udp_second->proto == PROTO_UDPv6 )
{
ret = writeUdpSocket(sock_server_udp_second, p->socket_udp, msg, strlen(msg));
}
}
}
static void addMsgClient(client_t *p, char *msg, int type, int id)
{
assert( p != NULL );
assert( msg != NULL );
if( p->status != NET_STATUS_ZOMBIE )
{
addMsgInCheckFront(p->listSendMsg, msg, type, id);
}
}
static void addMsgAllClientBut(char *msg, client_t *p, int type, int id)
{
client_t *thisClient;
int i;
assert( msg != NULL );
for( i = 0 ; i < listClient->count; i++)
{
thisClient = (client_t *) listClient->list[i];
if( thisClient->tux != NULL && thisClient != p )
{
addMsgClient(thisClient, msg, type, id);
}
}
}
static void addMsgAllClient(char *msg, int type, int id)
{
assert( msg != NULL );
addMsgAllClientBut(msg, NULL, type, id);
}
static void addMsgAllClientSeesTux(char *msg, tux_t *tux, int type, int id)
{
arena_t *arena;
space_t *space;
int x, y, w, h;
int i;
assert( msg != NULL );
listDoEmpty(listHelp);
arena = getCurrentArena();
space = arena->spaceTux;
x = tux->x - WINDOW_SIZE_X;
y = tux->y - WINDOW_SIZE_Y;
w = 2 * WINDOW_SIZE_X;
h = 2 * WINDOW_SIZE_Y;
if( x < 0 )
{
x = 0;
}
if( y < 0 )
{
y = 0;
}
if( w+x >= arena->w )
{
w = arena->w - (x+1);
}
if( h+y >= arena->h )
{
h = arena->h - (y+1);
}
getObjectFromSpace(space, x, y, w, h, listHelp);
//printf("%d %d %d %d %d\n", x, y, w, h, listHelp->count);
for( i = 0 ; i < listHelp->count ; i++ )
{
tux_t *thisTux;
client_t *thisClient;
thisTux = (tux_t *)listHelp->list[i];
if( thisTux == tux )
{
continue;
}
thisClient = thisTux->client;
if( thisClient != NULL )
{
addMsgClient(thisClient, msg, type, id);
}
}
#if 0
for( i = 0 ; i < listClient->count ; i++ )
{
client_t *thisClient;
tux_t *thisTux;
thisClient = (client_t *)listClient->list[i];
thisTux = (tux_t *)thisClient->tux;
if( thisTux == NULL )
{
continue;
}
if( tux != thisTux &&
isTuxSeesTux(thisTux, tux) )
{
addMsgClient(thisClient, msg, type, id);
}
}
#endif
}
void protoSendClient(int type, client_t *client, char *msg, int type2, int id)
{
assert( msg != NULL );
switch( type )
{
case PROTO_SEND_ONE :
assert( client != NULL );
addMsgClient(client, msg, type2, id);
break;
case PROTO_SEND_ALL :
assert( client == NULL );
addMsgAllClient(msg, type2, id);
break;
case PROTO_SEND_BUT :
assert( client != NULL );
addMsgAllClientBut(msg, client, type2, id);
break;
case PROTO_SEND_ALL_SEES_TUX :
#ifndef PUBLIC_SERVER
if( client != NULL )
{
addMsgAllClientSeesTux(msg, client->tux, type2, id);
}
else
{
addMsgAllClientSeesTux(msg, getControlTux(TUX_CONTROL_KEYBOARD_RIGHT), type2, id);
}
#endif
#ifdef PUBLIC_SERVER
addMsgAllClientSeesTux(msg, client->tux, type2, id);
#endif
break;
default :
assert( ! "Premenna type ma zlu hodnotu !" );
break;
}
}
tux_t *getServerTux()
{
return NULL;
}
client_t *getClientFromTux(tux_t *tux)
{
client_t *thisClient;
int i;
for( i = 0 ; i < listClient->count; i++)
{
thisClient = (client_t *) listClient->list[i];
if( thisClient->tux == tux )
{
return thisClient;
}
}
return NULL;
}
void sendInfoCreateClient(client_t *client)
{
client_t *thisClient;
tux_t *thisTux;
item_t *thisItem;
int i;
assert( client != NULL );
proto_send_init_server(PROTO_SEND_ONE, client, client);
#ifndef PUBLIC_SERVER
proto_send_newtux_server(PROTO_SEND_ONE, client, getControlTux(TUX_CONTROL_KEYBOARD_RIGHT) );
#endif
for( i = 0 ; i < listClient->count; i++)
{
thisClient = (client_t *) listClient->list[i];
thisTux = thisClient->tux;
if( thisTux != NULL && thisTux != client->tux )
{
proto_send_newtux_server(PROTO_SEND_ONE, thisClient, client->tux);
proto_send_newtux_server(PROTO_SEND_ONE, client, thisTux);
}
}
for( i = 0 ; i < getCurrentArena()->spaceItem->list->count; i++)
{
thisItem = (item_t *) getCurrentArena()->spaceItem->list->list[i];
proto_send_additem_server(PROTO_SEND_ONE, client, thisItem);
}
}
static void eventCreateNewUdpClient(sock_udp_t *socket_udp)
{
client_t *client;
int offset;
assert( socket_udp != NULL );
client = newUdpClient( socket_udp );
offset = addToIndex(listClientIndex, getSockUdpPort(client->socket_udp) );
if( offset == -1 )
{
return;
}
insList(listClient, offset, client);
}
static void eventClientBuffer(client_t *client)
{
char *line;
int i;
assert( client != NULL );
/* obsluha udalosti od clientov */
for( i = 0 ; i < client->listRecvMsg->count ; i++ )
{
line = (char *)client->listRecvMsg->list[i];
#ifndef PUBLIC_SERVER
if( isParamFlag("--recv") )
{
printf("recv -> %s", line);
}
#endif
rereshLastPing(client->protect);
if( strncmp(line, "hello", 5) == 0 )
{
proto_recv_hello_server(client, line);
continue;
}
if( strncmp(line, "status", 6) == 0 )
{
proto_recv_status_server(client, line);
continue;
}
#ifdef PUBLIC_SERVER
if( strncmp(line, "list", 4) == 0 )
{
proto_recv_listscore_server(client, line);
continue;
}
#endif
if( client->tux != NULL )
{
if( strncmp(line, "event", 5) == 0 )
{
proto_recv_event_server(client, line);
continue;
}
if( strncmp(line, "check", 5) == 0 )
{
proto_recv_check_server(client, line);
continue;
}
if( strncmp(line, "ping", 4) == 0 )
{
proto_recv_ping_server(client, line);
continue;
}
if( strncmp(line, "end", 3) == 0 )
{
proto_recv_end_server(client, line);
continue;
}
}
if( client->tux != NULL )
{
proto_send_error_server(PROTO_SEND_ONE, client, PROTO_ERROR_CODE_BAD_COMMAND);
}
}
destroyListItem(client->listRecvMsg, free);
client->listRecvMsg = newList();
}
void eventClientListBuffer()
{
int i;
client_t *thisClient;
for( i = 0 ; i < listClient->count; i++)
{
thisClient = (client_t *) listClient->list[i];
eventClientBuffer(thisClient);
eventMsgInCheckFront(thisClient);
}
}
static client_t* findUdpClient(sock_udp_t *sock_udp)
{
int offset;
offset = getFormIndex(listClientIndex, getSockUdpPort(sock_udp));
if( offset == -1 )
{
return NULL;
}
return (client_t *) listClient->list[offset];
}
static void eventClientUdpSelect(sock_udp_t *sock_server)
{
sock_udp_t *sock_client;
client_t *client;
char listRecvMsg[STR_SIZE];
bool_t isCreateNewClient;
int ret;
assert( sock_server != NULL );
sock_client = newSockUdp(sock_server->proto);
isCreateNewClient = FALSE;
memset(listRecvMsg, 0, STR_SIZE);
ret = readUdpSocket(sock_server, sock_client, listRecvMsg, STR_SIZE-1);
client = findUdpClient(sock_client);
if( client == NULL )
{
if( getCurrentArena()->spaceTux->list->count+1 > maxClients )
{
destroySockUdp(sock_client);
return;
}
eventCreateNewUdpClient(sock_client);
client = findUdpClient(sock_client);
isCreateNewClient = TRUE;
}
if( client == NULL )
{
fprintf(stderr, "Client total not found !\n");
return;
}
if( isCreateNewClient == FALSE )
{
destroySockUdp(sock_client);
}
if( ret <= 0 )
{
client->status = NET_STATUS_ZOMBIE;
return;
}
//printf("add packet >>%s<<\n", listRecvMsg);
addList(client->listRecvMsg, strdup(listRecvMsg) );
}
int selectServerUdpSocket()
{
struct timeval tv;
fd_set readfds;
fd_set errorfds;
int max_fd;
int ret;
int count;
#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_udp != NULL )
{
FD_SET(sock_server_udp->sock, &errorfds);
FD_SET(sock_server_udp->sock, &readfds);
if( sock_server_udp->sock > max_fd )
{
max_fd = sock_server_udp->sock;
}
}
if( sock_server_udp_second != NULL )
{
FD_SET(sock_server_udp_second->sock, &readfds);
FD_SET(sock_server_udp_second->sock, &errorfds);
if( sock_server_udp_second->sock > max_fd )
{
max_fd = sock_server_udp_second->sock;
}
}
//printf("select..\n");
#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_udp != NULL )
{
if( FD_ISSET(sock_server_udp->sock, &readfds) )
{
eventClientUdpSelect(sock_server_udp);
count = 1;
}
if( FD_ISSET(sock_server_udp->sock, &errorfds) )
{
printf("error\n");
}
}
if( sock_server_udp_second != NULL )
{
if( FD_ISSET(sock_server_udp_second->sock, &readfds) )
{
eventClientUdpSelect(sock_server_udp_second);
count = 1;
}
if( FD_ISSET(sock_server_udp_second->sock, &errorfds) )
{
printf("error\n");
}
}
return count;
}
void eventServer()
{
#ifndef PUBLIC_SERVER
int count;
#ifdef SUPPORT_NET_UNIX_UDP
do{
count = selectServerUdpSocket();
}while( count > 0 );
#endif
#endif
#ifdef PUBLIC_SERVER
selectServerUdpSocket();
#endif
eventClientListBuffer();
eventTimer(listServerTimer);
}
void quitUdpServer()
{
proto_send_end_server(PROTO_SEND_ALL, NULL);
assert( listClient != NULL );
destroyListItem(listClient, destroyClient);
destroyIndex(listClientIndex);
destroyTimer(listServerTimer);
if( sock_server_udp != NULL )
{
printf("close port %d\n", getSockUdpPort(sock_server_udp));
closeUdpSocket(sock_server_udp);
}
if( sock_server_udp_second != NULL )
{
printf("close port %d\n", getSockUdpPort(sock_server_udp_second));
closeUdpSocket(sock_server_udp_second);
}
printf("quit UDP port\n");
}
|