-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetplay.c
1209 lines (989 loc) · 32.5 KB
/
netplay.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
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
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "netplay.h"
extern int gtk_stop_asked;
#define DEBUG
const int PACKET_IDENTIFIER = 0;
const int PACKET_IDENTIFICATION_ID = 0x55;
const int PACKET_IDENTIFICATION_ACKNOWLEDGE_ID = 0x96;
const int PACKET_STATUS_ID = 0xC2;
const int PACKET_DIGEST_ID = 0x3B;
const int PACKET_INTERNET_DIGEST_ID = 0x1F;
const int PACKET_IDENTIFICATION_NUMBER_REQUESTED_SLOTS = 1;
const int PACKET_IDENTIFICATION_INPUT_DEVICE_INDEX = 2;
const int PACKET_IDENTIFICATION_INPUT_DEVICE_NUMBER = 5;
const int PACKET_IDENTIFICATION_CHECKSUM = 7;
const int PACKET_IDENTIFICATION_LENGTH = 8;
const int PACKET_IDENTIFICATION_ACKNOWLEDGE_NUMBER_ALLOCATED_SLOTS = 1;
const int PACKET_IDENTIFICATION_ACKNOWLEDGE_CHECKSUM = 2;
const int PACKET_IDENTIFICATION_ACKNOWLEDGE_LENGTH = 3;
const int PACKET_STATUS_FRAME_NUMBER = 1;
const int PACKET_STATUS_INPUT_DEVICE_INDEX = 5;
const int PACKET_STATUS_INPUT_DEVICE_NUMBER = 5;
const int PACKET_STATUS_CHECKSUM = 10;
const int PACKET_STATUS_LENGTH = 11;
const int PACKET_INTERNET_DIGEST_FRAME_NUMBER = 1;
const int PACKET_INTERNET_DIGEST_NUMBER_DIGEST = 5;
const int PACKET_INTERNET_DIGEST_DIGEST_INDEX = 6;
const int PACKET_INTERNET_DIGEST_DIGEST_NUMBER = 5;
const int PACKET_INTERNET_DIGEST_BASE_LENGTH = 7;
const int PACKET_INTERNET_DIGEST_INCREMENT_LENGTH = 5;
const int MAX_NUMBER_PLAYER = 5;
const int MIN_NUMBER_PLAYER = 1;
const int SERVER_SOCKET_TIMEOUT = 1000; /* In millisecond */
const int CLIENT_SOCKET_TIMEOUT = 10; /* In millisecond */
const int CLIENT_SOCKET_INTERNET_TIMEOUT = 20; /* In millisecond */
const int CLIENT_PACKET_SIZE = 11;
const int SERVER_PACKET_SIZE = 7 + 5 * 5;
const int DEFAULT_SERVER_PORT = 25679;
const int FRAME_PER_SECOND = 60;
const int DIGEST_SIZE = 5;
const int BUFFER_SIZE_SECONDS = 60 * 2; /* two minuts */
const int BUFFER_SIZE_BYTES = 60 * 2 * 60 * 5; /* size in seconds * frame per seconds * digest size */
const int AUTO_OUTDATE = 60 * 2 * 60; /* size in seconds * frame per seconds */
const double DOUBLE_FRAME_DURATION = 1.0 / 60.0; /* In second */
const int SDL_TICKS_PER_SECOND = 1000;
/*!
* In the lan protocol implementation, this array holds the input for the previous frame
*/
static UChar previous_input_value[5];
/*!
* In the internet protocol implementation, this pointer holds the history */
static UChar *history_digest = NULL;
//! Send packet for the previous frame
void send_previous_digest_packet (global_status_type * global_status,
global_option_type * global_option,
IPaddress address);
/*!
* Initializes the network for netplay. SDL has to be initialized first.
*/
int
init_network ()
{
if (SDLNet_Init () == -1)
{
printf ("SDLNet_Init: Error\n");
return 2;
}
return 0;
}
/*!
* Release the network for netplay. It doesn't change the state of SDL
*/
void
shutdown_network ()
{
SDLNet_Quit ();
}
/*!
* Compute the rotation of the value (equivalent of ROL in ia32 asm)
* \param value the value to rotate
* \return the rotated value
*/
UChar
rotate_left (UChar value)
{
return (UChar) ((value << 1) + ((value & 0x80) ? 1 : 0));
}
/*!
* Compute the check sum value according to the algorithm found in the header file
* \param data the array on which perform the computation
* \param index_min first index in the array to take into account
* \param index_max last index (EXCLUSIVE) in the array to take into account
*/
unsigned char
compute_checksum (unsigned char *data, int index_min, int index_max)
{
UChar checksum;
int index;
checksum = 0;
for (index = index_min; index < index_max; index++)
{
checksum ^= data[index];
checksum = rotate_left (checksum);
}
return checksum;
}
/*!
* Side effect free minimum computation
*/
static int
min (int lhs, int rhs)
{
return (lhs < rhs) ? lhs : rhs;
}
/*!
* Returns the number of free (unidentified and under the limit requested) slots
*/
int
count_remaining_slot (global_status_type * global_status,
global_option_type * global_option)
{
return global_option->number_player -
global_status->number_identified_players;
}
/*!
* Check if two SDLNet addresses are the same
* \return non null value is addresses match, else 0
*/
int
equals_address (IPaddress lhs, IPaddress rhs)
{
return (lhs.host == rhs.host) && (lhs.port == rhs.port);
}
/*!
* Build and send a packet to acknowledge an identification. We use the digest
* packet structure to prepare it.
*/
void
send_identification_acknowledge_packet (global_status_type * global_status,
int number_allocated_slots)
{
int number_destination;
/* Set the identifier */
global_status->digest_packet->data[PACKET_IDENTIFIER] =
PACKET_IDENTIFICATION_ACKNOWLEDGE_ID;
/* Set the number of allocated slots */
global_status->digest_packet->
data[PACKET_IDENTIFICATION_ACKNOWLEDGE_NUMBER_ALLOCATED_SLOTS] =
number_allocated_slots;
/* Compute the checksum */
global_status->digest_packet->
data[PACKET_IDENTIFICATION_ACKNOWLEDGE_CHECKSUM] =
compute_checksum (global_status->digest_packet->data, PACKET_IDENTIFIER,
PACKET_IDENTIFICATION_ACKNOWLEDGE_CHECKSUM);
global_status->digest_packet->len =
PACKET_IDENTIFICATION_ACKNOWLEDGE_LENGTH;
/* Back to sender */
global_status->digest_packet->address =
global_status->current_packet->address;
number_destination = SDLNet_UDP_Send (global_status->server_socket,
-1, global_status->digest_packet);
if (number_destination != 1)
{
fprintf (stderr,
"Couldn't send the identification acknowledge packet to client\n");
}
}
/*!
* Compare given address with previous address from which slot allocation have been successfully processed
* \param global_status the global status for the server
* \parem address the address to check
* \return -1 is the address was never seen
* \return the number of slots we allocated for it back then, else
*/
int
compare_request_to_previous (global_status_type * global_status,
IPaddress address)
{
int slot_index;
for (slot_index = 0; slot_index < global_status->number_allocation_request;
slot_index++)
{
if (equals_address
(address, global_status->allocation_request[slot_index].address))
{
/* We found a previous request */
return global_status->allocation_request[slot_index].
allocated_slots;
}
}
return -1;
}
/*!
* Process an identification request from a client, filling the interesting
* fields in the global_status structure to find later the client
*/
void
identify_client (global_status_type * global_status,
global_option_type * global_option)
{
int requested_slots;
int allocated_slots;
int slot_index;
int already_allocated_slot_by_client;
if (global_status->current_packet->len != PACKET_IDENTIFICATION_LENGTH)
{
/* Discarding invalid packet */
#if defined(DEBUG)
fprintf (stderr,
"Invalid packet received when in identification phase (received length: %d, expected length: %d)\n",
global_status->current_packet->len,
PACKET_IDENTIFICATION_LENGTH);
#endif
return;
}
if (global_status->current_packet->data[PACKET_IDENTIFIER] !=
PACKET_IDENTIFICATION_ID)
{
/* Discarding invalid packet */
#if defined(DEBUG)
fprintf (stderr,
"Invalid packet received when in identification phase (received ID: 0x%02x, expected ID: 0x%02x)\n",
global_status->current_packet->data[PACKET_IDENTIFIER],
PACKET_IDENTIFICATION_ID);
#endif
return;
}
if (global_status->current_packet->data[PACKET_IDENTIFICATION_CHECKSUM] !=
compute_checksum (global_status->current_packet->data,
PACKET_IDENTIFIER, PACKET_IDENTIFICATION_CHECKSUM))
{
/* Discarding invalid packet */
#if defined(DEBUG)
fprintf (stderr,
"Packet checksum received when in identification phase (received checksum: 0x%02x, expected checksum: 0x%02x)\n",
global_status->current_packet->
data[PACKET_IDENTIFICATION_CHECKSUM],
compute_checksum (global_status->current_packet->data,
PACKET_IDENTIFIER,
PACKET_IDENTIFICATION_CHECKSUM));
#endif
return;
}
requested_slots =
global_status->current_packet->
data[PACKET_IDENTIFICATION_NUMBER_REQUESTED_SLOTS];
allocated_slots =
min (requested_slots,
count_remaining_slot (global_status, global_option));
/* Check if it's a new request or a re-request (happens when the client didn't get the acknowledgement) */
already_allocated_slot_by_client =
compare_request_to_previous (global_status,
global_status->current_packet->address);
if (already_allocated_slot_by_client != -1)
{
/* We already answered this client, we'll reacknowledge */
send_identification_acknowledge_packet (global_status,
already_allocated_slot_by_client);
return;
}
for (slot_index = global_status->number_identified_players;
slot_index <
global_status->number_identified_players + allocated_slots;
slot_index++)
{
/* Set the address for identified player */
global_status->input_mapping[slot_index].address.port =
global_status->current_packet->address.port;
global_status->input_mapping[slot_index].address.host =
global_status->current_packet->address.host;
/* Set the remote input device number */
global_status->input_mapping[slot_index].remote_input_device =
global_status->current_packet->
data[PACKET_IDENTIFICATION_INPUT_DEVICE_INDEX + slot_index -
global_status->number_identified_players];
/* Change status from UNIDENTIFIED */
global_status->player_status[slot_index] = WAITING;
}
global_status->allocation_request[global_status->number_allocation_request].
address.port = global_status->current_packet->address.port;
global_status->allocation_request[global_status->number_allocation_request].
address.host = global_status->current_packet->address.host;
global_status->allocation_request[global_status->number_allocation_request].
allocated_slots = allocated_slots;
global_status->number_allocation_request++;
global_status->number_identified_players += allocated_slots;
send_identification_acknowledge_packet (global_status, allocated_slots);
}
/*!
* Read an incoming packet and stores it for future computations
* \return non null if a packet was read
* \return 0 if no packet is available
*/
int
read_incoming_server_packet (global_status_type * global_status)
{
switch (SDLNet_UDP_Recv
(global_status->server_socket, global_status->current_packet))
{
case 0:
return 0;
case 1:
#if defined(DEBUG)
fprintf (stderr, "Stored a packet from client.\n");
#endif
return 1;
default:
fprintf (stderr, "Internal warning: impossible case (%s:%s)\n",
__FILE__, __LINE__);
return 0;
}
return 0;
}
/*!
* Process a status packet and updates global input status and client status
*/
void
store_remote_status_lan (global_status_type * global_status,
global_option_type * global_option)
{
int input_index;
if (global_status->current_packet->len != PACKET_STATUS_LENGTH)
{
/* Discarding invalid packet */
#if defined(DEBUG)
fprintf (stderr,
"Invalid packet received when in status receiving phase (received length: %d, expected length: %d)\n",
global_status->current_packet->len, PACKET_STATUS_LENGTH);
#endif
/* If may be an identification packet which was lost at some point */
identify_client (global_status, global_option);
return;
}
if (global_status->current_packet->data[PACKET_IDENTIFIER] !=
PACKET_STATUS_ID)
{
/* Discarding invalid packet */
#if defined(DEBUG)
fprintf (stderr,
"Invalid packet received when in status receiving phase (received ID: 0x%02x, expected ID: 0x%02x)\n",
global_status->current_packet->data[PACKET_IDENTIFIER],
PACKET_STATUS_ID);
#endif
return;
}
if (global_status->frame_number !=
SDLNet_Read32 (global_status->current_packet->data +
PACKET_STATUS_FRAME_NUMBER))
{
if (global_status->frame_number ==
SDLNet_Read32 (global_status->current_packet->data +
PACKET_STATUS_FRAME_NUMBER) + 1)
{
/*
* This a request for the previous frame, i.e. a client lost his digest, we resend it to him
*/
send_previous_digest_packet (global_status, global_option,
global_status->current_packet->
address);
return;
}
/* Discarding status not corresponding to this frame */
#if defined(DEBUG)
fprintf (stderr,
"Invalid packet received when in status receiving phase (status for frame %d, awaiting for frame %d)\n",
SDLNet_Read32 (global_status->current_packet->data +
PACKET_STATUS_FRAME_NUMBER),
global_status->frame_number);
#endif
return;
}
for (input_index = 0; input_index < MAX_NUMBER_PLAYER; input_index++)
{
if (equals_address
(global_status->current_packet->address,
global_status->input_mapping[input_index].address))
{
if (global_status->player_status[input_index] == READY)
{
#if defined(DEBUG)
fprintf (stderr,
"We have another status for the same frame, same origin.");
#endif
/* TODO : check if it's the same status we already have stored */
}
/* Global input number input_index will be filled by this packet status */
global_status->input_value[input_index] =
global_status->current_packet->
data[PACKET_STATUS_INPUT_DEVICE_INDEX +
global_status->input_mapping[input_index].
remote_input_device];
#if defined(DEBUG)
fprintf (stderr, "input value[%d] = 0x%02x (data[%d])\n",
input_index,
global_status->current_packet->
data[PACKET_STATUS_INPUT_DEVICE_INDEX +
global_status->input_mapping[input_index].
remote_input_device],
PACKET_STATUS_INPUT_DEVICE_INDEX +
global_status->input_mapping[input_index].
remote_input_device);
#endif
/* Slot is now filled */
global_status->player_status[input_index] = READY;
}
}
}
/*!
* Reset the input value to begin with a new frame
*/
void
init_frame_status (global_status_type * global_status,
global_option_type * global_option)
{
int player_index;
for (player_index = 0; player_index < global_option->number_player;
player_index++)
{
previous_input_value[player_index] =
global_status->input_value[player_index];
global_status->input_value[player_index] = 0;
global_status->player_status[player_index] = WAITING;
}
}
/*!
* Send a digest packet to a single client
*/
void
send_individual_digest_packet (IPaddress client_address,
global_status_type * global_status)
{
int number_destination;
global_status->digest_packet->address = client_address;
number_destination = SDLNet_UDP_Send (global_status->server_socket,
-1, global_status->digest_packet);
if (number_destination != 1)
{
fprintf (stderr, "Couldn't send the status packet to client\n");
}
}
/*!
* Send a digest packet to clients if they reeeeeeeeeally deserve it
*/
void
send_digest_lan_packets (global_status_type * global_status,
global_option_type * global_option)
{
int player_index;
for (player_index = 0; player_index < global_option->number_player;
player_index++)
{
if (global_status->player_status[player_index] != READY)
{
#if defined(DEBUG)
fprintf (stderr,
"Player number %d not being READY prevent the sending of the digest packet\n",
player_index);
#endif
return;
}
}
/* If we're here, all concerned clients are READY (== we got their status) */
/* Set the identifier */
global_status->digest_packet->data[PACKET_IDENTIFIER] = PACKET_DIGEST_ID;
/* Set the current frame */
SDLNet_Write32 (global_status->frame_number,
global_status->digest_packet->
data + PACKET_STATUS_FRAME_NUMBER);
/* Set the digest input values */
for (player_index = 0; player_index < global_option->number_player;
player_index++)
{
global_status->digest_packet->data[PACKET_STATUS_INPUT_DEVICE_NUMBER +
player_index] =
global_status->input_value[player_index];
}
/* Compute the checksum */
global_status->digest_packet->
data[PACKET_STATUS_CHECKSUM] =
compute_checksum (global_status->digest_packet->data, PACKET_IDENTIFIER,
PACKET_STATUS_CHECKSUM);
global_status->digest_packet->len = PACKET_STATUS_LENGTH;
/* Now, global_status->digest_packet is THE digest packet we can send to all clients */
for (player_index = 0; player_index < global_option->number_player;
player_index++)
{
/* TODO : find a much more elegant and efficient way to find unique client addresses */
int duplicate_index;
int already_sent;
already_sent = 0;
for (duplicate_index = 0; duplicate_index < player_index;
duplicate_index++)
{
if (equals_address
(global_status->input_mapping[player_index].address,
global_status->input_mapping[duplicate_index].address))
{
already_sent = 1;
break;
}
}
if (!already_sent)
{
send_individual_digest_packet (global_status->
input_mapping[player_index].address,
global_status);
}
}
/* We're done with this frame, skip to the next one */
global_status->frame_number++;
/* Get ready for receiving status for next (now current) frame */
init_frame_status (global_status, global_option);
}
/*!
* Send the digest packet of the previous frame to a single client
*/
void
send_previous_digest_packet (global_status_type * global_status,
global_option_type * global_option,
IPaddress address)
{
int player_index;
/* Set the identifier */
global_status->digest_packet->data[PACKET_IDENTIFIER] = PACKET_DIGEST_ID;
/* Set the previous frame */
SDLNet_Write32 (global_status->frame_number - 1,
global_status->digest_packet->
data + PACKET_STATUS_FRAME_NUMBER);
/* Set the digest input values */
for (player_index = 0; player_index < global_option->number_player;
player_index++)
{
global_status->digest_packet->data[PACKET_STATUS_INPUT_DEVICE_NUMBER +
player_index] =
previous_input_value[player_index];
}
/* Compute the checksum */
global_status->digest_packet->
data[PACKET_STATUS_CHECKSUM] =
compute_checksum (global_status->digest_packet->data, PACKET_IDENTIFIER,
PACKET_STATUS_CHECKSUM);
global_status->digest_packet->len = PACKET_STATUS_LENGTH;
/* Now, global_status->digest_packet is the previous digest packet we can send to this given client */
send_individual_digest_packet (global_status->input_mapping[player_index].
address, global_status);
}
/*!
* Main loop of the lan protocol service
*/
void
serve_clients_lan_protocol (global_status_type * global_status,
global_option_type * global_option)
{
init_frame_status (global_status, global_option);
for (;;)
{
int number_ready_socket;
#if defined(DEBUG)
fprintf (stderr, "Waiting for status packets for frame %u\n",
global_status->frame_number);
#endif
#if defined(GTK)
while (gtk_events_pending())
{
if (gtk_main_iteration())
{
return;
}
}
if (gtk_stop_asked)
{
return;
}
#endif
number_ready_socket =
SDLNet_CheckSockets (global_status->server_socket_set,
SERVER_SOCKET_TIMEOUT);
if (number_ready_socket == -1)
{
fprintf (stderr, "Error in socket waiting (disconnection ?).\n");
break;
}
if (number_ready_socket == 1)
{
#if defined(DEBUG)
fprintf (stderr, "Got a packet\n");
#endif
/* We're awaiting a packet in the server socket */
if (read_incoming_server_packet (global_status))
{
/* Stores the incoming status and updates clients status accordingly */
store_remote_status_lan (global_status, global_option);
/* Sends packet (if needed) to client accordingly to their status */
send_digest_lan_packets (global_status, global_option);
}
}
}
}
/*!
* Return the time remaining before the next timeout to which issue a digest to all clients
* \param global_status the global status of the server
*/
int
get_remaining_time (global_status_type * global_status)
{
double next_time;
UInt32 current_time;
current_time = SDL_GetTicks ();
next_time =
global_status->start_time +
global_status->frame_number * DOUBLE_FRAME_DURATION;
#if defined(DEBUG)
fprintf (stderr,
"Current time : %u ticks (%f second(s))\nTime up next frame (frame %d) : %u ticks (%f second(s))\n",
current_time,
(double) current_time / (double) SDL_TICKS_PER_SECOND,
global_status->frame_number,
(UInt32) (next_time * 1000.0) - current_time,
next_time - current_time / (double) SDL_TICKS_PER_SECOND);
#endif
if ((UInt32) (next_time * 1000.0) < current_time)
{
fprintf (stderr, "The server is too slow, we're late for frame %u\n",
global_status->frame_number);
return 0;
}
return (UInt32) (next_time * 1000.0) - current_time;
}
/*!
* Store an individual input value
*/
void
store_individual_status (global_status_type * global_status,
int input_index, UChar input_value)
{
global_status->input_value[input_index] = input_value;
}
/*!
* Store a packet information and updates the current status as well as the next frame asked by the client
*/
void
store_remote_status_internet (global_status_type * global_status,
global_option_type * global_option)
{
int input_index;
if (global_status->current_packet->len != PACKET_STATUS_LENGTH)
{
/* Discarding invalid packet */
#if defined(DEBUG)
fprintf (stderr,
"Invalid packet received when in status receiving phase (received length: %d, expected length: %d)\n",
global_status->current_packet->len, PACKET_STATUS_LENGTH);
#endif
/* If may be an identification packet which was lost at some point */
identify_client (global_status, global_option);
return;
}
if (global_status->current_packet->data[PACKET_IDENTIFIER] !=
PACKET_STATUS_ID)
{
/* Discarding invalid packet */
#if defined(DEBUG)
fprintf (stderr,
"Invalid packet received when in status receiving phase (received ID: 0x%02x, expected ID: 0x%02x)\n",
global_status->current_packet->data[PACKET_IDENTIFIER],
PACKET_STATUS_ID);
#endif
return;
}
/* TODO : add checksum testing */
for (input_index = 0; input_index < MAX_NUMBER_PLAYER; input_index++)
{
if (equals_address
(global_status->current_packet->address,
global_status->input_mapping[input_index].address))
{
/* Update the status with the client status */
store_individual_status (global_status, input_index,
global_status->current_packet->
data[PACKET_STATUS_INPUT_DEVICE_INDEX +
global_status->
input_mapping[input_index].
remote_input_device]);
if (SDLNet_Read32
(global_status->current_packet->data +
PACKET_STATUS_FRAME_NUMBER) != 0)
{
/* The client requested a given frame */
/* Update the next frame to send to this player */
global_status->next_frame_asked[input_index] =
SDLNet_Read32 (global_status->current_packet->data +
PACKET_STATUS_FRAME_NUMBER);
#if defined(DEBUG)
if (global_status->next_frame_asked[input_index] ==
global_status->frame_number)
{
fprintf (stderr,
"Great, player is waiting for the current frame, lag is less than a frame delay (player %d).\n",
input_index);
}
if (global_status->next_frame_asked[input_index] ==
global_status->next_frame_to_send[input_index])
{
fprintf (stderr,
"Half great, we don't have packets loss, the game should be smooth (player %d).\n",
input_index);
}
#endif
if (global_status->next_frame_asked[input_index] >
global_status->frame_number)
{
fprintf (stderr,
"Error, player asked a frame which hasn't been reached yet! (asked: %d, maximum: %d)\n",
global_status->next_frame_asked[input_index],
global_status->frame_number);
global_status->next_frame_asked[input_index] =
global_status->frame_number;
}
if ((global_status->frame_number > AUTO_OUTDATE)
&& (global_status->next_frame_asked[input_index] <=
global_status->frame_number - AUTO_OUTDATE))
{
fprintf (stderr,
"FATAL ERROR !! Player requested an outdated frame, we can't provide it! (requested : %d, available range : %d - %d)\n",
global_status->next_frame_asked[input_index],
global_status->frame_number - AUTO_OUTDATE,
global_status->frame_number);
/* TODO: think about actions which should be performed in this case. */
}
/* Update the counter for sending frames */
global_status->next_frame_to_send[input_index] =
global_status->next_frame_asked[input_index];
}
else
{
#if defined(DEBUG)
fprintf (stderr,
"Client sends a status without requesting a specific frame.\n");
#endif
}
}
}
}
/*!
* Allocate memory for the history
*/
void
init_internet_history ()
{
if (history_digest != NULL)
{
fprintf (stderr, "Attempting to reallocate the history.\n");
return;
}
history_digest = (UChar *) malloc ((size_t) BUFFER_SIZE_BYTES);
if (history_digest == NULL)
{
fprintf (stderr,
"FATAL ERROR ! Couldn't allocate memory for the history. Exiting.\n");
exit (2);
}
}
/*!
* Free memory for the history
*/
void
release_internet_history ()
{
if (history_digest == NULL)
{
fprintf (stderr, "Attempting to free non allocated history.\n");
return;
}
free (history_digest);
}
/*!
* Push the current frame status in the history
*/
void
store_current_status_in_history (global_status_type * global_status)
{
memcpy (history_digest +
(global_status->frame_number % AUTO_OUTDATE) * DIGEST_SIZE,
global_status->input_value, DIGEST_SIZE);
}
/*!
* Send a packet to a client to fit the need in term of frame number(s)
* \param global_status global status for the application
* \param client_index index of the global player to which send a packet
*/
void
send_individual_internet_digest_packet (global_status_type * global_status,
int client_index)
{
unsigned int frame_number;
unsigned int frame_number_start;
int number_digest;
int digest_index;
int number_destination;
/* Set the identifier */
global_status->digest_packet->data[PACKET_IDENTIFIER] =
PACKET_INTERNET_DIGEST_ID;
/* Set the number of the first frame digest */
SDLNet_Write32 (global_status->next_frame_to_send[client_index],
global_status->digest_packet->data +
PACKET_INTERNET_DIGEST_FRAME_NUMBER);
/* Compute the number of digest to send */
number_digest =
min (PACKET_INTERNET_DIGEST_DIGEST_NUMBER, global_status->frame_number);