summaryrefslogtreecommitdiff
path: root/src/server.c
blob: bf996590c142e7d21d2d82a31e25537f799fc1bc (plain)
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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/time.h>

#include "main.h"
#include "list.h"
#include "tux.h"
#include "network.h"
#include "buffer.h"
#include "proto.h"
#include "server.h"
#include "assert.h"
#include "myTimer.h"
#include "net_multiplayer.h"

#ifndef PUBLIC_SERVER
#include "screen_world.h"
#endif

#ifdef PUBLIC_SERVER
#include "publicServer.h"
#endif

#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 list_t *listClient;
static my_time_t lastSyncClient;
static int maxClients;

void static initServer()
{
	listClient = newList();
	lastSyncClient = getMyTime();
	setServerMaxClients(SERVER_MAX_CLIENTS);
}

#ifdef SUPPORT_NET_UNIX_TCP
int initTcpServer(int port)
{
	sock_server_tcp = bindTcpSocket(port);

	if( sock_server_tcp == NULL )
	{
		return -1;
	}

	initServer();

	printf("server listen TCP port %d\n", port);

	return 0;
}
#endif

#ifdef SUPPORT_NET_UNIX_UDP
int initUdpServer(int port)
{
	sock_server_udp = bindUdpSocket(port);

	if( sock_server_udp == NULL )
	{
		return -1;
	}

	initServer();

	printf("server listen UDP port %d\n", port);

	return 0;
}
#endif

#ifdef SUPPORT_NET_SDL_UDP
int initSdlUdpServer(int port)
{
	sock_server_sdl_udp = bindSdlUdpSocket(port);

	if( sock_server_sdl_udp == NULL )
	{
		return -1;
	}

	initServer();

	printf("server listen UDP port %d\n", port);

	return 0;
}
#endif

static client_t* newAnyClient()
{
	client_t *new;
	
	new = malloc( sizeof(client_t) );
	memset(new, 0, sizeof(client_t));
	
	new->status = NET_STATUS_OK;
	new->buffer = newBuffer(LIMIT_BUFFER);

	return new;
}

#ifdef SUPPORT_NET_UNIX_TCP
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);
	printf("new client from %s:%d\n", str_ip, getSockTcpPort(sock_tcp));

	new = newAnyClient();
	new->socket_tcp = sock_tcp;

	return new;
}
#endif

#ifdef SUPPORT_NET_UNIX_UDP
client_t* newUdpClient(sock_udp_t *sock_udp)
{
	client_t *new;

	assert( sock_udp != NULL );

	printf("new client from %d\n", getSockUdpPort(sock_udp));
	new = newAnyClient();
	new->lastPing = getMyTime();
	new->socket_udp = sock_udp;

	return new;
}
#endif

#ifdef SUPPORT_NET_SDL_UDP
client_t* newSdlUdpClient(sock_sdl_udp_t *sock_sdl_udp)
{
	client_t *new;

	assert( sock_sdl_udp != NULL );

	printf("new client from %d\n", getSockSdlUdpPort(sock_sdl_udp));
	new = newAnyClient();
	new->lastPing = getMyTime();
	new->socket_sdl_udp = sock_sdl_udp;

	return new;
}
#endif

void destroyClient(client_t *p)
{
	int index;

	assert( p != NULL );

#ifdef SUPPORT_NET_UNIX_TCP
	closeTcpSocket(p->socket_tcp);
#endif

#ifdef SUPPORT_NET_UNIX_UDP
	closeUdpSocket(p->socket_udp);
#endif

#ifdef SUPPORT_NET_SDL_UDP
	closeSdlUdpSocket(p->socket_sdl_udp);
#endif

	destroyBuffer(p->buffer);

	if( p->tux != NULL )
	{
		index = searchListItem(getWorldArena()->listTux, p->tux);
		delListItem(getWorldArena()->listTux, index, 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;

#ifdef SUPPORT_NET_UNIX_TCP
		ret = writeTcpSocket(p->socket_tcp, msg, strlen(msg));	

		if( ret == 0 )
		{
			fprintf(stderr, "client sa odpojil\n");
			p->status = NET_STATUS_ZOMBIE;
		}
	
		if( ret < 0 )
		{
			fprintf(stderr, "nastala chyba pri posielanie spravy clientovy\n");
			p->status = NET_STATUS_ZOMBIE;
		}
#endif

#ifdef SUPPORT_NET_UNIX_UDP
		ret = writeUdpSocket(sock_server_udp, p->socket_udp, msg, strlen(msg));
#endif

#ifdef SUPPORT_NET_SDL_UDP
		ret = writeSdlUdpSocket(sock_server_sdl_udp, p->socket_sdl_udp, msg, strlen(msg));
#endif
	}
}

void sendAllClientBut(char *msg, client_t *p)
{
	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 )
		{
			sendClient(thisClient, msg);
		}
	}
}

void sendAllClient(char *msg)
{
	assert( msg != NULL );

	sendAllClientBut(msg, 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,
		(tux_t *)(getWorldArena()->listTux->list[SERVER_INDEX_ROOT_TUX]) );
#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 < getWorldArena()->listItem->count; i++)
	{
		thisItem = (item_t *) getWorldArena()->listItem->list[i];
		proto_send_additem_server(PROTO_SEND_ONE, client, thisItem);
	}
}

#ifdef SUPPORT_NET_UNIX_TCP

static void eventCreateNewTcpClient(sock_tcp_t *socket_tcp)
{
	client_t *client;

	assert( socket_tcp != NULL );

	client = newTcpClient( getTcpNewClient(socket_tcp) );
	addList(listClient, client);
}

#endif

#ifdef SUPPORT_NET_UNIX_UDP

static void eventCreateNewUdpClient(sock_udp_t *socket_udp)
{
	client_t *client;

	assert( socket_udp != NULL );

	client = newUdpClient( socket_udp );
	addList(listClient, client);
}

#endif

#ifdef SUPPORT_NET_SDL_UDP

static void eventCreateNewSdlUdpClient(sock_sdl_udp_t *socket_sdl_udp)
{
	client_t *client;

	assert( socket_sdl_udp != NULL );

	client = newSdlUdpClient( socket_sdl_udp );
	addList(listClient, client);
}

#endif

#ifdef SUPPORT_NET_UNIX_TCP

static void eventClientTcpSelect(client_t *client)
{
	char buffer[STR_PROTO_SIZE];
	int ret;

	assert( client != NULL );

	memset(buffer, 0, STR_SIZE);
	ret = readTcpSocket(client->socket_tcp, buffer, STR_PROTO_SIZE-1);

	if( ret <= 0 )
	{
		client->status = NET_STATUS_ZOMBIE;
		return;
	}

	if( addBuffer(client->buffer, buffer, ret) != 0 )
	{
		client->status = NET_STATUS_ZOMBIE;
		return;
	}
}

#endif

static void eventClientBuffer(client_t *client)
{
	char line[STR_PROTO_SIZE];

	assert( client != NULL );

	/* obsluha udalosti od clientov */
	
	while( getBufferLine(client->buffer, line, STR_PROTO_SIZE) >= 0 )
	{
#ifdef DEBUG_SERVER_RECV
		printf("recv client msg->%s", line);
#endif

#if defined SUPPORT_NET_UNIX_UDP || defined SUPPORT_NET_SDL_UDP
		client->lastPing = getMyTime();
#endif
		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);

		if( client->tux != NULL )
		{
			if( strncmp(line, "event", 5) == 0 )
			{
				proto_recv_event_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;
			}
		}

		proto_send_error_server(PROTO_SEND_ONE, client, PROTO_ERROR_CODE_BAD_COMMAND);
	}
}

void eventClientListBuffer()
{
	int i;
	client_t *thisClient;

	for( i = 0 ; i < listClient->count; i++)
	{
		thisClient = (client_t *) listClient->list[i];
		eventClientBuffer(thisClient);
	}
}

#ifdef SUPPORT_NET_UNIX_TCP

void selectServerTcpSocket()
{
	fd_set readfds;
	struct timeval tv;
	client_t *thisClient;
	int max_fd;
	int i;

	tv.tv_sec = 0;
	tv.tv_usec = 0;
	
	FD_ZERO(&readfds);
	FD_SET(sock_server_tcp->sock, &readfds);
	max_fd = sock_server_tcp->sock;

	for( i = 0 ; i < listClient->count; i++)
	{
		thisClient = (client_t *) listClient->list[i];

		FD_SET(thisClient->socket_tcp->sock, &readfds);
	
		if( thisClient->socket_tcp->sock > max_fd )
		{
			max_fd = thisClient->socket_tcp->sock;
		}
	}

	select(max_fd+1, &readfds, (fd_set *)NULL, (fd_set *)NULL, &tv);

	if( FD_ISSET(sock_server_tcp->sock, &readfds) )
	{
		eventCreateNewTcpClient(sock_server_tcp);
	}

	for( i = 0 ; i < listClient->count; i++)
	{
		thisClient = (client_t *) listClient->list[i];

		if( FD_ISSET(thisClient->socket_tcp->sock, &readfds) )
		{
			eventClientTcpSelect(thisClient);
		}

		if( thisClient->status == NET_STATUS_ZOMBIE )
		{
			if( thisClient->tux != NULL )
			{
				proto_send_deltux_server(PROTO_SEND_ALL, NULL, thisClient);
			}

			delListItem(listClient, i, destroyClient);
		}
	}
}

#endif

#if defined SUPPORT_NET_UNIX_UDP || defined SUPPORT_NET_SDL_UDP

static void delZombieCLient()
{
	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( currentTime - thisClient->lastPing > SERVER_TIMEOUT )
		{
			proto_send_error_server(PROTO_SEND_ONE, thisClient, PROTO_ERROR_CODE_TIMEOUT);
			thisClient->status = NET_STATUS_ZOMBIE;
		}

		if( thisClient->status == NET_STATUS_ZOMBIE )
		{
			if( thisClient->tux != NULL )
			{
				proto_send_deltux_server(PROTO_SEND_ALL, NULL, thisClient);
			}

			delListItem(listClient, i, destroyClient);
		}
	}
}

void eventPeriodicSyncClient()
{
	my_time_t currentTime;

 	currentTime = getMyTime();

	if( currentTime - lastSyncClient > SERVER_TIME_SYNC )
	{
		client_t *thisClientInfo;
		client_t *thisClientSend;
		tux_t *thisTux;
		int i, j;
	
		for( i = 0 ; i < listClient->count; i++)
		{
			thisClientSend = (client_t *) listClient->list[i];

			if( thisClientSend->tux != NULL )
			{
				proto_send_ping_server(PROTO_SEND_ONE, thisClientSend);

#ifndef PUBLIC_SERVER
				proto_send_newtux_server(PROTO_SEND_ONE, thisClientSend,
				(tux_t *)(getWorldArena()->listTux->list[SERVER_INDEX_ROOT_TUX]));
#endif
			}

			for( j = 0 ; j < listClient->count; j++)
			{
				thisClientInfo = (client_t *) listClient->list[j];
				thisTux = thisClientInfo->tux;

				if( thisTux != NULL &&
				    thisClientSend->tux != NULL &&
				    thisClientSend != thisClientInfo &&
				    thisTux->status == TUX_STATUS_ALIVE )
				{
					proto_send_newtux_server(PROTO_SEND_ONE,
						thisClientSend, thisTux);
				}
			}
		}

		lastSyncClient = getMyTime();
	}
}

#endif

#ifdef SUPPORT_NET_UNIX_UDP

static client_t* findUdpClient(sock_udp_t *sock_udp)
{
	int i;
	client_t *thisClient;

	for( i = 0 ; i < listClient->count; i++)
	{
		thisClient = (client_t *) listClient->list[i];

		if( getSockUdpPort(thisClient->socket_udp) == getSockUdpPort(sock_udp) )
		{
			return thisClient;
		}
	}

	return NULL;
}

static void eventClientUdpSelect(sock_udp_t *sock_server)
{
	sock_udp_t *sock_client;
	client_t *client;
	char buffer[STR_SIZE];
	bool_t isCreateNewClient;
	int ret;

	assert( sock_server != NULL );

	sock_client = newSockUdp();
	isCreateNewClient = FALSE;

	memset(buffer, 0, STR_SIZE);

	ret = readUdpSocket(sock_server, sock_client, buffer, STR_SIZE-1);

	client = findUdpClient(sock_client);

	if( client == NULL )
	{
		if( getWorldArena()->listTux->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;
	}

	if( addBuffer(client->buffer, buffer, ret) != 0 )
	{
		client->status = NET_STATUS_ZOMBIE;
		return;
	}
}

void selectServerUdpSocket()
{
	struct timeval tv;
	fd_set readfds;
	int max_fd;
	int ret;

#ifndef PUBLIC_SERVER
	tv.tv_sec = 0;
	tv.tv_usec = 0;
#endif	

#ifdef PUBLIC_SERVER
	tv.tv_sec = 0;
	tv.tv_usec = 5000;
#endif	

	FD_ZERO(&readfds);
	FD_SET(sock_server_udp->sock, &readfds);
	max_fd = sock_server_udp->sock;
	
	delZombieCLient();
	
	ret = select(max_fd+1, &readfds, (fd_set *)NULL, (fd_set *)NULL, &tv);
	
	if( ret < 0 )
	{
		return;
	}
	
	if( FD_ISSET(sock_server_udp->sock, &readfds) )
	{
		eventClientUdpSelect(sock_server_udp);
	}
}

#endif

#ifdef SUPPORT_NET_SDL_UDP

static client_t* findSdlUdpClient(sock_sdl_udp_t *sock_sdl_udp)
{
	int i;
	client_t *thisClient;

	for( i = 0 ; i < listClient->count; i++)
	{
		thisClient = (client_t *) listClient->list[i];

		if( getSockSdlUdpPort(thisClient->socket_sdl_udp) == getSockSdlUdpPort(sock_sdl_udp) )
		{
			return thisClient;
		}
	}

	return NULL;
}

void selectServerSdlUdpSocket()
{
	sock_sdl_udp_t *sock_client;
	client_t *client;
	char buffer[STR_PROTO_SIZE];
	bool_t isCreateNewClient;
	int ret;

	assert( sock_server_sdl_udp != NULL );

	delZombieCLient();

	do{
		sock_client = newSdlSockUdp();
		isCreateNewClient = FALSE;
	
		memset(buffer, 0, STR_PROTO_SIZE);
	
		ret = readSdlUdpSocket(sock_server_sdl_udp, sock_client, buffer, STR_PROTO_SIZE-1);
	
		if( ret < 0 )
		{
			destroySockSdlUdp(sock_client);
			break;
		}
	
		client = findSdlUdpClient(sock_client);
	
		if( client == NULL )
		{
			eventCreateNewSdlUdpClient(sock_client);
			client = findSdlUdpClient(sock_client);
			isCreateNewClient = TRUE;
		}
	
		if( client == NULL )
		{
			fprintf(stderr, "Client total not found !\n");
			return;
		}
		
		if( isCreateNewClient == FALSE )
		{
			destroySockSdlUdp(sock_client);
		}
	
		if( ret <= 0 )
		{
			client->status = NET_STATUS_ZOMBIE;
			return;
		}
	
		if( addBuffer(client->buffer, buffer, ret) != 0 )
		{
			client->status = NET_STATUS_ZOMBIE;
			return;
		}

	}while( ret > 0 );
}

#endif

static void quitServer()
{
	proto_send_end_server(PROTO_SEND_ALL, NULL);
	assert( listClient != NULL );
	destroyListItem(listClient, destroyClient);
}

#ifdef SUPPORT_NET_UNIX_TCP

void quitTcpServer()
{
	quitServer();

	assert( sock_server_tcp != NULL );
	closeTcpSocket(sock_server_tcp);

	printf("quit TCP port\n");
}

#endif

#ifdef SUPPORT_NET_UNIX_UDP

void quitUdpServer()
{
	quitServer();

	assert( sock_server_udp != NULL );
	closeUdpSocket(sock_server_udp);

	printf("quit UDP port\n");
}

#endif

#ifdef SUPPORT_NET_SDL_UDP

void quitSdlUdpServer()
{
	quitServer();

	assert( sock_server_sdl_udp != NULL );
	closeSdlUdpSocket(sock_server_sdl_udp);

	printf("quit UDP port\n");
}

#endif