-
Notifications
You must be signed in to change notification settings - Fork 0
/
enet_protocol.pas
1986 lines (1572 loc) · 79.6 KB
/
enet_protocol.pas
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
unit enet_protocol;
(**
@file protocol.c
@brief ENet protocol functions
1.3.6 freepascal
- fix enet_host_service mistranslation
- fix enet_protocol_send_acknowledgements
- cleanup
*)
{$GOTO ON}
interface
uses enet_consts;
procedure enet_host_flush (host : pENetHost);
function enet_host_service (host : pENetHost; event : pENetEvent; timeout : enet_uint32):integer;
function enet_protocol_check_timeouts (host : pENetHost; peer : pENetPeer; event : pENetEvent):integer;
function enet_protocol_command_size (commandNumber : enet_uint8):enet_size_t;
function enet_protocol_dispatch_incoming_commands (host : pENetHost;event : pENetEvent):integer;
function enet_protocol_handle_acknowledge (host : pENetHost; event : pENetEvent; peer : pENetPeer; command : pENetProtocol):integer;
function enet_protocol_handle_bandwidth_limit (host : pENetHost; peer : pENetPeer; command : pENetProtocol):integer;
function enet_protocol_handle_connect (host : pENetHost;header : pENetProtocolHeader;command : pENetProtocol):pENetPeer;
function enet_protocol_handle_disconnect (host : pENetHost; peer : pENetPeer; command : pENetProtocol):integer;
function enet_protocol_handle_incoming_commands (host : pENetHost; event : pENetEvent):integer;
function enet_protocol_handle_ping (host : pENetHost; peer : pENetPeer; command : pENetProtocol):integer;
function enet_protocol_handle_send_fragment (host : pENetHost; peer : pENetPeer; command : pENetProtocol; currentData : ppenet_uint8):integer;
function enet_protocol_handle_send_reliable (host : pENetHost; peer : pENetPeer; command : pENetProtocol; currentData : ppenet_uint8):integer;
function enet_protocol_handle_send_unreliable (host : pENetHost; peer : pENetPeer;command : pENetProtocol;currentData : ppenet_uint8):integer;
function enet_protocol_handle_send_unsequenced (host : pENetHost; peer : pENetPeer; command : pENetProtocol; currentData : ppenet_uint8):integer;
function enet_protocol_handle_throttle_configure (host : pENetHost; peer : pENetPeer; command : pENetProtocol):integer;
function enet_protocol_handle_verify_connect (host : pENetHost; event : pENetEvent; peer : pENetPeer; command : pENetProtocol):integer;
procedure enet_protocol_notify_connect (host : pENetHost;peer : pENetPeer;event : pENetEvent);
procedure enet_protocol_notify_disconnect (host : pENetHost;peer : pENetPeer;event : pENetEvent);
function enet_protocol_receive_incoming_commands (host : pENetHost; event : pENetEvent):integer;
function enet_protocol_remove_sent_reliable_command (peer : pENetPeer;reliableSequenceNumber : enet_uint16;channelID : enet_uint8):ENetProtocolCommand;
procedure enet_protocol_remove_sent_unreliable_commands (peer : pENetPeer);
procedure enet_protocol_send_acknowledgements (host : pENetHost; peer : pENetPeer);
function enet_protocol_send_outgoing_commands (host : pENetHost; event : pENetEvent; checkForTimeouts : integer):integer;
function enet_protocol_send_reliable_outgoing_commands (host : pENetHost; peer : pENetPeer):Integer;
procedure enet_protocol_send_unreliable_outgoing_commands (host : pENetHost;peer : pENetPeer);
function enet_host_check_events (host : pENetHost; event : pENetEvent):integer;
const
commandSizes : array[0..(ENET_PROTOCOL_COMMAND_COUNT)-1] of enet_size_t =
(
0,
sizeof (ENetProtocolAcknowledge),
sizeof (ENetProtocolConnect),
sizeof (ENetProtocolVerifyConnect),
sizeof (ENetProtocolDisconnect),
sizeof (ENetProtocolPing),
sizeof (ENetProtocolSendReliable),
sizeof (ENetProtocolSendUnreliable),
sizeof (ENetProtocolSendFragment),
sizeof (ENetProtocolSendUnsequenced),
sizeof (ENetProtocolBandwidthLimit),
sizeof (ENetProtocolThrottleConfigure),
sizeof (ENetProtocolSendFragment)
);
implementation
uses enet_packet, enet_host, enet_callbacks, enet_list, enet_socket, enet_peer;
function enet_protocol_command_size (commandNumber : enet_uint8):enet_size_t;
begin
result := commandSizes [commandNumber and ENET_PROTOCOL_COMMAND_MASK];
end;
function enet_protocol_dispatch_incoming_commands (host : pENetHost;event : pENetEvent):integer;
var
peer : pENetPeer;
begin
while (not enet_list_empty (@ host ^. dispatchQueue)) do
begin
peer := pENetPeer ( enet_list_remove (enet_list_begin (@ host ^. dispatchQueue)));
peer ^. needsDispatch := 0;
case (peer ^. state) of
ENET_PEER_STATE_CONNECTION_PENDING,
ENET_PEER_STATE_CONNECTION_SUCCEEDED:
begin
peer ^. state := ENET_PEER_STATE_CONNECTED;
event ^. EventType := ENET_EVENT_TYPE_CONNECT;
event ^. peer := peer;
event ^. data := peer ^. eventData;
Result:=1; exit;
end;
ENET_PEER_STATE_ZOMBIE:
begin
host ^. recalculateBandwidthLimits := 1;
event ^. EventType := ENET_EVENT_TYPE_DISCONNECT;
event ^. peer := peer;
event ^. data := peer ^. eventData;
enet_peer_reset (peer);
Result:=1; exit;
end;
ENET_PEER_STATE_CONNECTED:
begin
if (enet_list_empty (@ peer ^. dispatchedCommands)) then
continue;
event ^. packet := enet_peer_receive (peer, @ event ^. channelID);
if (event ^. packet = nil) then
continue;
event ^. EventType := ENET_EVENT_TYPE_RECEIVE;
event ^. peer := peer;
if (not enet_list_empty (@ peer ^. dispatchedCommands)) then
begin
peer ^. needsDispatch := 1;
enet_list_insert (enet_list_end (@ host ^. dispatchQueue), @ peer ^. dispatchList);
end;
Result:=1; exit;
end;
end;
end;
Result:=0;
end;
procedure enet_protocol_dispatch_state (host:pENetHost; peer:pENetPeer; state: Integer { ENetPeerState });
begin
peer ^. state := state;
if (peer ^. needsDispatch = 0) then
begin
enet_list_insert (enet_list_end (@ host ^. dispatchQueue), @ peer ^. dispatchList);
peer ^. needsDispatch := 1;
end;
end;
procedure enet_protocol_notify_connect (host : pENetHost;peer : pENetPeer;event : pENetEvent);
var
state : Integer;
begin
host ^. recalculateBandwidthLimits := 1;
if (event <> nil) then
begin
peer ^. state := ENET_PEER_STATE_CONNECTED;
event ^. EventType := ENET_EVENT_TYPE_CONNECT;
event ^. peer := peer;
event ^. data := peer ^. eventData;
end
else begin
if peer ^. state = ENET_PEER_STATE_CONNECTING then
state := ENET_PEER_STATE_CONNECTION_SUCCEEDED
else
state := ENET_PEER_STATE_CONNECTION_PENDING;
enet_protocol_dispatch_state (host, peer, state);
end;
end;
procedure enet_protocol_notify_disconnect (host : pENetHost;peer : pENetPeer;event : pENetEvent);
begin
if (peer ^. state >= ENET_PEER_STATE_CONNECTION_PENDING) then
host ^. recalculateBandwidthLimits := 1;
if (peer ^. state <> ENET_PEER_STATE_CONNECTING) and (peer ^. state < ENET_PEER_STATE_CONNECTION_SUCCEEDED) then
enet_peer_reset (peer)
else
if (event <> nil) then
begin
event ^. EventType := ENET_EVENT_TYPE_DISCONNECT;
event ^. peer := peer;
event ^. data := 0;
enet_peer_reset (peer);
end
else
begin
peer ^. eventData := 0;
enet_protocol_dispatch_state (host, peer, ENET_PEER_STATE_ZOMBIE);
end;
end;
procedure enet_protocol_remove_sent_unreliable_commands (peer : pENetPeer);
var
outgoingCommand : pENetOutgoingCommand;
begin
while (not enet_list_empty (@ peer ^. sentUnreliableCommands)) do
begin
outgoingCommand := pENetOutgoingCommand(enet_list_front (@ peer ^. sentUnreliableCommands));
enet_list_remove (@ outgoingCommand ^. outgoingCommandList);
if (outgoingCommand ^. packet <> nil) then
begin
dec(outgoingCommand ^. packet ^. referenceCount);
if (outgoingCommand ^. packet ^. referenceCount = 0) then
enet_packet_destroy (outgoingCommand ^. packet);
end;
enet_free (outgoingCommand);
end;
end;
function enet_protocol_remove_sent_reliable_command (peer : pENetPeer;reliableSequenceNumber : enet_uint16;channelID : enet_uint8):ENetProtocolCommand;
var
outgoingCommand : pENetOutgoingCommand;
currentCommand : ENetListIterator;
commandNumber : ENetProtocolCommand;
channel : pENetChannel;
reliableWindow : enet_uint16;
wasSent : Integer;
begin
outgoingCommand:=nil;
wasSent:=1;
currentCommand := enet_list_begin (@ peer ^. sentReliableCommands);
while PAnsiChar(currentCommand)<>PAnsiChar(enet_list_end (@ peer ^. sentReliableCommands)) do
begin
outgoingCommand := pENetOutgoingCommand (currentCommand);
if (outgoingCommand ^. reliableSequenceNumber = reliableSequenceNumber) and
(outgoingCommand ^. command.header.channelID = channelID) then
break;
currentCommand := enet_list_next (currentCommand);
end;
if PAnsiChar(currentCommand)=PAnsiChar(enet_list_end (@ peer ^. sentReliableCommands)) then
begin
currentCommand := enet_list_begin (@ peer ^. outgoingReliableCommands);
while PAnsiChar(currentCommand)<>PAnsiChar(enet_list_end (@ peer ^. outgoingReliableCommands)) do
begin
outgoingCommand := pENetOutgoingCommand (currentCommand);
if (outgoingCommand ^. sendAttempts < 1) then begin result := ENET_PROTOCOL_COMMAND_NONE; exit; end;
if (outgoingCommand ^. reliableSequenceNumber = reliableSequenceNumber) and
(outgoingCommand ^. command.header.channelID = channelID) then
break;
currentCommand := enet_list_next (currentCommand);
end;
if PAnsiChar(currentCommand)=PAnsiChar(enet_list_end (@ peer ^. outgoingReliableCommands)) then
begin result := ENET_PROTOCOL_COMMAND_NONE; exit; end;
wasSent:=0;
end;
if (outgoingCommand = nil) then
begin Result:= ENET_PROTOCOL_COMMAND_NONE; exit; end;
if (channelID < peer ^. channelCount) then
begin
channel := @ (pENetChannelArray(peer ^. channels)^[channelID]);
reliableWindow := reliableSequenceNumber div ENET_PEER_RELIABLE_WINDOW_SIZE;
if (channel ^. reliableWindows [reliableWindow] > 0) then
begin
dec( channel ^. reliableWindows [reliableWindow]);
if (0 = channel ^. reliableWindows [reliableWindow]) then
channel ^. usedReliableWindows := channel ^. usedReliableWindows and not(1 shl reliableWindow);
end;
end;
commandNumber := ENetProtocolCommand(outgoingCommand ^. command.header.command and ENET_PROTOCOL_COMMAND_MASK);
enet_list_remove (@ outgoingCommand ^. outgoingCommandList);
if (outgoingCommand ^. packet <> nil) then
begin
if wasSent<>0 then
dec(peer ^. reliableDataInTransit , outgoingCommand ^. fragmentLength);
dec( outgoingCommand ^. packet ^. referenceCount);
if (outgoingCommand ^. packet ^. referenceCount = 0) then
enet_packet_destroy (outgoingCommand ^. packet);
end;
enet_free (outgoingCommand);
if (enet_list_empty (@ peer ^. sentReliableCommands)) then
begin result := commandNumber; exit; end;
outgoingCommand := pENetOutgoingCommand (enet_list_front (@ peer ^. sentReliableCommands));
peer ^. nextTimeout := outgoingCommand ^. sentTime + outgoingCommand ^. roundTripTimeout;
result := commandNumber;
end;
function enet_protocol_handle_connect (host : pENetHost;header : pENetProtocolHeader;command : pENetProtocol):pENetPeer;
var
incomingSessionID, outgoingSessionID : enet_uint8;
mtu, windowSize : enet_uint32;
channel : pENetChannel;
channelCount : enet_size_t;
currentPeer : pENetPeer;
verifyCommand : ENetProtocol;
begin
channelCount := ENET_NET_TO_HOST_32 (command ^. connect.channelCount);
if (channelCount < ENET_PROTOCOL_MINIMUM_CHANNEL_COUNT) or
(channelCount > ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT) then
begin result := nil; exit; end;
currentPeer := host ^. peers;
while PAnsiChar(currentPeer)< PAnsiChar(@(pENetPeerArray(host ^. peers)^[host ^. peerCount])) do
begin
if (currentPeer ^. state <> ENET_PEER_STATE_DISCONNECTED) and
(currentPeer ^. address.host = host ^. receivedAddress.host) and
(currentPeer ^. address.port = host ^. receivedAddress.port) and
(currentPeer ^. connectID = command ^. connect.connectID) then
begin result := nil; exit; end;
inc(currentPeer);
end;
currentPeer := host ^. peers;
while PAnsiChar(currentPeer)< PAnsiChar(@(pENetPeerArray(host ^. peers)^[host ^. peerCount])) do
begin
if (currentPeer ^. state = ENET_PEER_STATE_DISCONNECTED) then
break;
inc(currentPeer);
end;
if PAnsiChar(currentPeer) >= PAnsiChar(@(pENetPeerArray(host ^. peers)^[host ^. peerCount])) then
begin result := nil; exit; end;
if (channelCount > host ^. channelLimit) then
channelCount := host ^. channelLimit;
currentPeer ^. channels := pENetChannel (enet_malloc (channelCount * sizeof (ENetChannel)));
if (currentPeer ^. channels = nil) then begin
Result:=nil; exit;
end;
currentPeer ^. channelCount := channelCount;
currentPeer ^. state := ENET_PEER_STATE_ACKNOWLEDGING_CONNECT;
currentPeer ^. connectID := command ^. connect.connectID;
currentPeer ^. address := host ^. receivedAddress;
currentPeer ^. outgoingPeerID := ENET_NET_TO_HOST_16 (command ^. connect.outgoingPeerID);
currentPeer ^. incomingBandwidth := ENET_NET_TO_HOST_32 (command ^. connect.incomingBandwidth);
currentPeer ^. outgoingBandwidth := ENET_NET_TO_HOST_32 (command ^. connect.outgoingBandwidth);
currentPeer ^. packetThrottleInterval := ENET_NET_TO_HOST_32 (command ^. connect.packetThrottleInterval);
currentPeer ^. packetThrottleAcceleration := ENET_NET_TO_HOST_32 (command ^. connect.packetThrottleAcceleration);
currentPeer ^. packetThrottleDeceleration := ENET_NET_TO_HOST_32 (command ^. connect.packetThrottleDeceleration);
currentPeer ^. eventData := ENET_NET_TO_HOST_32 (command ^. connect.data);
if command ^. connect.incomingSessionID=$0ff then
incomingSessionID:= currentPeer ^. outgoingSessionID
else
incomingSessionID:=command ^. connect.incomingSessionID;
incomingSessionID := (incomingSessionID + 1) and (ENET_PROTOCOL_HEADER_SESSION_MASK shr ENET_PROTOCOL_HEADER_SESSION_SHIFT);
if (incomingSessionID = currentPeer ^. outgoingSessionID) then
incomingSessionID := (incomingSessionID + 1) and (ENET_PROTOCOL_HEADER_SESSION_MASK shr ENET_PROTOCOL_HEADER_SESSION_SHIFT);
currentPeer ^. outgoingSessionID := incomingSessionID;
if command ^. connect.outgoingSessionID=$0ff then
outgoingSessionID:=currentPeer ^. incomingSessionID
else
outgoingSessionID:=command ^. connect.outgoingSessionID;
outgoingSessionID := (outgoingSessionID + 1) and (ENET_PROTOCOL_HEADER_SESSION_MASK shr ENET_PROTOCOL_HEADER_SESSION_SHIFT);
if (outgoingSessionID = currentPeer ^. incomingSessionID) then
outgoingSessionID := (outgoingSessionID + 1) and (ENET_PROTOCOL_HEADER_SESSION_MASK shr ENET_PROTOCOL_HEADER_SESSION_SHIFT);
currentPeer ^. incomingSessionID := outgoingSessionID;
channel := currentPeer ^. channels;
while PAnsiChar(channel)< PAnsiChar(@(pENetChannelArray(currentPeer ^. channels)^[channelCount])) do
begin
channel ^. outgoingReliableSequenceNumber := 0;
channel ^. outgoingUnreliableSequenceNumber := 0;
channel ^. incomingReliableSequenceNumber := 0;
channel ^. incomingUnreliableSequenceNumber := 0;
enet_list_clear (@ channel ^. incomingReliableCommands);
enet_list_clear (@ channel ^. incomingUnreliableCommands);
channel ^. usedReliableWindows := 0;
FillChar(channel ^. reliableWindows, sizeof (channel ^. reliableWindows), 0);
inc(channel);
end;
mtu := ENET_NET_TO_HOST_32 (command ^. connect.mtu);
if (mtu < ENET_PROTOCOL_MINIMUM_MTU) then
mtu := ENET_PROTOCOL_MINIMUM_MTU
else
if (mtu > ENET_PROTOCOL_MAXIMUM_MTU) then
mtu := ENET_PROTOCOL_MAXIMUM_MTU;
currentPeer ^. mtu := mtu;
if (host ^. outgoingBandwidth = 0) and
(currentPeer ^. incomingBandwidth = 0) then
currentPeer ^. windowSize := ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE
else
if (host ^. outgoingBandwidth = 0) or
(currentPeer ^. incomingBandwidth = 0) then
currentPeer ^. windowSize := (ENET_MAX (host ^. outgoingBandwidth, currentPeer ^. incomingBandwidth) div
ENET_PEER_WINDOW_SIZE_SCALE) *
ENET_PROTOCOL_MINIMUM_WINDOW_SIZE
else
currentPeer ^. windowSize := (ENET_MIN (host ^. outgoingBandwidth, currentPeer ^. incomingBandwidth) div
ENET_PEER_WINDOW_SIZE_SCALE) *
ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
if (currentPeer ^. windowSize < ENET_PROTOCOL_MINIMUM_WINDOW_SIZE) then
currentPeer ^. windowSize := ENET_PROTOCOL_MINIMUM_WINDOW_SIZE
else
if (currentPeer ^. windowSize > ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE) then
currentPeer ^. windowSize := ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
if (host ^. incomingBandwidth = 0) then
windowSize := ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE
else
windowSize := (host ^. incomingBandwidth div ENET_PEER_WINDOW_SIZE_SCALE) *
ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
if (windowSize > ENET_NET_TO_HOST_32 (command ^. connect.windowSize)) then
windowSize := ENET_NET_TO_HOST_32 (command ^. connect.windowSize);
if (windowSize < ENET_PROTOCOL_MINIMUM_WINDOW_SIZE) then
windowSize := ENET_PROTOCOL_MINIMUM_WINDOW_SIZE
else
if (windowSize > ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE) then
windowSize := ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
verifyCommand.header.command := ENET_PROTOCOL_COMMAND_VERIFY_CONNECT or ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE;
verifyCommand.header.channelID := $FF;
verifyCommand.verifyConnect.outgoingPeerID := ENET_HOST_TO_NET_16 (currentPeer ^. incomingPeerID);
verifyCommand.verifyConnect.incomingSessionID := incomingSessionID;
verifyCommand.verifyConnect.outgoingSessionID := outgoingSessionID;
verifyCommand.verifyConnect.mtu := ENET_HOST_TO_NET_32 (currentPeer ^. mtu);
verifyCommand.verifyConnect.windowSize := ENET_HOST_TO_NET_32 (windowSize);
verifyCommand.verifyConnect.channelCount := ENET_HOST_TO_NET_32 (channelCount);
verifyCommand.verifyConnect.incomingBandwidth := ENET_HOST_TO_NET_32 (host ^. incomingBandwidth);
verifyCommand.verifyConnect.outgoingBandwidth := ENET_HOST_TO_NET_32 (host ^. outgoingBandwidth);
verifyCommand.verifyConnect.packetThrottleInterval := ENET_HOST_TO_NET_32 (currentPeer ^. packetThrottleInterval);
verifyCommand.verifyConnect.packetThrottleAcceleration := ENET_HOST_TO_NET_32 (currentPeer ^. packetThrottleAcceleration);
verifyCommand.verifyConnect.packetThrottleDeceleration := ENET_HOST_TO_NET_32 (currentPeer ^. packetThrottleDeceleration);
verifyCommand.verifyConnect.connectID := currentPeer ^. connectID;
enet_peer_queue_outgoing_command (currentPeer, @ verifyCommand, nil, 0, 0);
result := currentPeer;
end;
function enet_protocol_handle_send_reliable (host : pENetHost; peer : pENetPeer; command : pENetProtocol; currentData : ppenet_uint8):integer;
var
packet : pENetPacket;
dataLength : enet_size_t;
begin
if (command ^. header.channelID >= peer ^. channelCount) or
((peer ^. state <> ENET_PEER_STATE_CONNECTED) and (peer ^. state <> ENET_PEER_STATE_DISCONNECT_LATER)) then
begin result := -1; exit; end;
dataLength := ENET_NET_TO_HOST_16 (command ^. sendReliable.dataLength);
Inc(PAnsiChar(currentData^) , dataLength);
if (dataLength > ENET_PROTOCOL_MAXIMUM_PACKET_SIZE) or
(currentData^ < host ^. receivedData) or
(currentData^ > @ penet_uint8array(host ^. receivedData)^[host ^. receivedDataLength]) then
begin result := -1; exit; end;
packet := enet_packet_create (PAnsiChar(command) + sizeof (ENetProtocolSendReliable),
dataLength,
ENET_PACKET_FLAG_RELIABLE);
if (packet = nil) or
(enet_peer_queue_incoming_command (peer, command, packet, 0) = nil) then
begin result := -1; exit; end;
result := 0;
end;
function enet_protocol_handle_send_unsequenced (host : pENetHost; peer : pENetPeer; command : pENetProtocol; currentData : ppenet_uint8):integer;
var
packet : pENetPacket;
unsequencedGroup, index : enet_uint32;
dataLength : enet_size_t;
begin
if (command ^. header.channelID >= peer ^. channelCount) or
((peer ^. state <> ENET_PEER_STATE_CONNECTED) and (peer ^. state <> ENET_PEER_STATE_DISCONNECT_LATER)) then
begin result := -1; exit; end;
dataLength := ENET_NET_TO_HOST_16 (command ^. sendUnsequenced.dataLength);
inc(PAnsiChar(currentData^) , dataLength);
if (dataLength > ENET_PROTOCOL_MAXIMUM_PACKET_SIZE) or
(currentData^ < host ^. receivedData) or
(currentData^ > @ penet_uint8array(host ^. receivedData)^[host ^. receivedDataLength]) then
begin result := -1; exit; end;
unsequencedGroup := ENET_NET_TO_HOST_16 (command ^. sendUnsequenced.unsequencedGroup);
index := unsequencedGroup mod ENET_PEER_UNSEQUENCED_WINDOW_SIZE;
if (unsequencedGroup < peer ^. incomingUnsequencedGroup) then
inc(unsequencedGroup , $10000);
if (unsequencedGroup >= enet_uint32(peer ^. incomingUnsequencedGroup + ENET_PEER_FREE_UNSEQUENCED_WINDOWS * ENET_PEER_UNSEQUENCED_WINDOW_SIZE)) then
begin result := 0; exit; end;
unsequencedGroup := unsequencedGroup and $FFFF;
if ((unsequencedGroup - index) <> peer ^. incomingUnsequencedGroup) then
begin
peer ^. incomingUnsequencedGroup := unsequencedGroup - index;
FillChar (peer ^. unsequencedWindow, sizeof (peer ^. unsequencedWindow), 0);
end
else
if 0<>(peer ^. unsequencedWindow [index div 32] and (1 shl (index mod 32))) then
begin result := 0; exit; end;
packet := enet_packet_create (PAnsiChar(command)+ sizeof (ENetProtocolSendUnsequenced),
dataLength,
ENET_PACKET_FLAG_UNSEQUENCED);
if (packet = nil) or
(enet_peer_queue_incoming_command (peer, command, packet, 0) = nil) then
begin result := -1; exit; end;
peer ^. unsequencedWindow [index div 32] := peer ^. unsequencedWindow [index div 32] or enet_uint32(1 shl (index mod 32));
result := 0;
end;
function enet_protocol_handle_send_unreliable (host : pENetHost; peer : pENetPeer;command : pENetProtocol;currentData : ppenet_uint8):integer;
var
packet : pENetPacket;
dataLength : enet_size_t;
begin
if (command ^. header.channelID >= peer ^. channelCount) or
((peer ^. state <> ENET_PEER_STATE_CONNECTED) and (peer ^. state <> ENET_PEER_STATE_DISCONNECT_LATER)) then
begin result := -1; exit; end;
dataLength := ENET_NET_TO_HOST_16 (command ^. sendUnreliable.dataLength);
inc(PAnsiChar(currentData^), dataLength);
if (dataLength > ENET_PROTOCOL_MAXIMUM_PACKET_SIZE) or
(currentData^ < host ^. receivedData) or
(currentData^ > @ penet_uint8array(host ^. receivedData)^[host ^. receivedDataLength]) then
begin result := -1; exit; end;
packet := enet_packet_create (PAnsiChar(command) + sizeof (ENetProtocolSendUnreliable),
dataLength,
0);
if (packet = nil) or
(enet_peer_queue_incoming_command (peer, command, packet, 0) = nil) then
begin result:=-1; exit; end;
result := 0;
end;
function enet_protocol_handle_send_fragment (host : pENetHost; peer : pENetPeer; command : pENetProtocol; currentData : ppenet_uint8):integer;
var
fragmentNumber , fragmentCount, fragmentOffset, fragmentLength,
startSequenceNumber, totalLength : enet_uint32;
channel : pENetChannel;
startWindow, currentWindow : enet_uint16;
currentCommand : ENetListIterator;
startCommand : pENetIncomingCommand;
packet : pENetPacket;
hostCommand : ENetProtocol;
incomingCommand : pENetIncomingCommand;
label continuework1;
begin
startCommand := nil;
if (command ^. header.channelID >= peer ^. channelCount) or
((peer ^. state <> ENET_PEER_STATE_CONNECTED) and (peer ^. state <> ENET_PEER_STATE_DISCONNECT_LATER)) then
begin result := -1; exit; end;
fragmentLength := ENET_NET_TO_HOST_16 (command ^. sendFragment.dataLength);
inc( currentData^ , fragmentLength);
if (fragmentLength > ENET_PROTOCOL_MAXIMUM_PACKET_SIZE) or
(currentData^ < host ^. receivedData) or
(currentData^ > @ penet_uint8array(host ^. receivedData)^[host ^. receivedDataLength]) then
begin result := -1; exit; end;
channel := @ pENetChannelArray(peer ^. channels)^[command ^. header.channelID];
startSequenceNumber := ENET_NET_TO_HOST_16 (command ^. sendFragment.startSequenceNumber);
startWindow := startSequenceNumber div ENET_PEER_RELIABLE_WINDOW_SIZE;
currentWindow := channel ^. incomingReliableSequenceNumber div ENET_PEER_RELIABLE_WINDOW_SIZE;
if (startSequenceNumber < channel ^. incomingReliableSequenceNumber) then
inc(startWindow , ENET_PEER_RELIABLE_WINDOWS);
if (startWindow < currentWindow) or (startWindow >= (currentWindow + ENET_PEER_FREE_RELIABLE_WINDOWS - 1)) then
begin result := 0; exit; end;
fragmentNumber := ENET_NET_TO_HOST_32 (command ^. sendFragment.fragmentNumber);
fragmentCount := ENET_NET_TO_HOST_32 (command ^. sendFragment.fragmentCount);
fragmentOffset := ENET_NET_TO_HOST_32 (command ^. sendFragment.fragmentOffset);
totalLength := ENET_NET_TO_HOST_32 (command ^. sendFragment.totalLength);
if (fragmentCount > ENET_PROTOCOL_MAXIMUM_FRAGMENT_COUNT) or
(fragmentNumber >= fragmentCount) or
(totalLength > ENET_PROTOCOL_MAXIMUM_PACKET_SIZE) or
(fragmentOffset >= totalLength) or
(fragmentLength > totalLength - fragmentOffset) then
begin result := -1; exit; end;
currentCommand := enet_list_previous (enet_list_end (@channel ^. incomingReliableCommands));
while PAnsiChar(currentCommand) <> PAnsiChar(enet_list_end (@ channel ^. incomingReliableCommands)) do
begin
incomingCommand := pENetIncomingCommand(currentCommand);
if (startSequenceNumber >= channel ^. incomingReliableSequenceNumber) then
begin
if (incomingCommand ^. reliableSequenceNumber < channel ^. incomingReliableSequenceNumber) then
goto continuework1;
end
else
if (incomingCommand ^. reliableSequenceNumber >= channel ^. incomingReliableSequenceNumber) then
break;
if (incomingCommand ^. reliableSequenceNumber <= startSequenceNumber) then
begin
if (incomingCommand ^. reliableSequenceNumber < startSequenceNumber) then
break;
if ((incomingCommand ^. command.header.command and ENET_PROTOCOL_COMMAND_MASK) <> ENET_PROTOCOL_COMMAND_SEND_FRAGMENT) or
(totalLength <> incomingCommand ^. packet ^. dataLength) or
(fragmentCount <> incomingCommand ^. fragmentCount) then
begin result := -1; exit; end;
startCommand := incomingCommand;
break;
end;
continuework1:
currentCommand := enet_list_previous (currentCommand);
end;
if (startCommand = nil) then
begin
hostCommand := command^;
packet := enet_packet_create (nil, totalLength, ENET_PACKET_FLAG_RELIABLE);
if (packet = nil) then
begin result := -1; exit; end;
hostCommand.header.reliableSequenceNumber := startSequenceNumber;
startCommand := enet_peer_queue_incoming_command (peer, @ hostCommand, packet, fragmentCount);
if (startCommand = nil) then
begin result := -1; exit; end;
end;
if enet_uint32(pIntegerArray(startCommand ^. fragments)^[fragmentNumber div 32]) and (1 shl (fragmentNumber mod 32)) = 0 then
begin
dec(startCommand ^. fragmentsRemaining);
PIntegerArray(startCommand ^. fragments)^[fragmentNumber div 32] := PIntegerArray(startCommand ^. fragments)^[fragmentNumber div 32] or (1 shl (fragmentNumber mod 32));
if (fragmentOffset + fragmentLength > startCommand ^. packet ^. dataLength) then
fragmentLength := startCommand ^. packet ^. dataLength - fragmentOffset;
system.Move ((PAnsiChar(command) + sizeof (ENetProtocolSendFragment))^,
(PAnsiChar(startCommand ^. packet ^. data) + fragmentOffset)^,
fragmentLength);
if (startCommand ^. fragmentsRemaining <= 0) then
enet_peer_dispatch_incoming_reliable_commands (peer, channel);
end;
result := 0;
end;
function enet_protocol_handle_send_unreliable_fragment (host:pENetHost; peer:pENetPeer; command : pENetProtocol; currentData : ppenet_uint8):Integer;
var
fragmentNumber,
fragmentCount,
fragmentOffset,
fragmentLength,
reliableSequenceNumber,
startSequenceNumber,
totalLength : enet_uint32;
reliableWindow, currentWindow : enet_uint16;
channel : pENetChannel;
currentCommand : ENetListIterator;
startCommand : pENetIncomingCommand;
incomingCommand : pENetIncomingCommand;
packet : pENetPacket;
label continue1;
begin
startCommand := nil;
if (command ^. header.channelID >= peer ^. channelCount) or
((peer ^. state <> ENET_PEER_STATE_CONNECTED) and (peer ^. state <> ENET_PEER_STATE_DISCONNECT_LATER))
then begin
Result:=-1; exit;
end;
fragmentLength := ENET_NET_TO_HOST_16 (command ^. sendFragment.dataLength);
Inc(currentData^, fragmentLength);
if (fragmentLength > ENET_PROTOCOL_MAXIMUM_PACKET_SIZE) or
(currentData^ < host ^. receivedData) or
(currentData^ > @ penet_uint8array(host ^. receivedData )^[host ^. receivedDataLength]) then
begin
Result:=-1; exit;
end;
channel := @ peer ^. channels [command ^. header.channelID];
reliableSequenceNumber := command ^. header.reliableSequenceNumber;
startSequenceNumber := ENET_NET_TO_HOST_16 (command ^. sendFragment.startSequenceNumber);
reliableWindow := reliableSequenceNumber div ENET_PEER_RELIABLE_WINDOW_SIZE;
currentWindow := channel ^. incomingReliableSequenceNumber div ENET_PEER_RELIABLE_WINDOW_SIZE;
if (reliableSequenceNumber < channel ^. incomingReliableSequenceNumber) then
Inc(reliableWindow , ENET_PEER_RELIABLE_WINDOWS);
if (reliableWindow < currentWindow) or (reliableWindow >= currentWindow + ENET_PEER_FREE_RELIABLE_WINDOWS - 1) then
begin
Result:=0; exit;
end;
if (reliableSequenceNumber = channel ^. incomingReliableSequenceNumber) and
(startSequenceNumber <= channel ^. incomingUnreliableSequenceNumber) then
begin
Result:=0; exit;
end;
fragmentNumber := ENET_NET_TO_HOST_32 (command ^. sendFragment.fragmentNumber);
fragmentCount := ENET_NET_TO_HOST_32 (command ^. sendFragment.fragmentCount);
fragmentOffset := ENET_NET_TO_HOST_32 (command ^. sendFragment.fragmentOffset);
totalLength := ENET_NET_TO_HOST_32 (command ^. sendFragment.totalLength);
if (fragmentCount > ENET_PROTOCOL_MAXIMUM_FRAGMENT_COUNT) or
(fragmentNumber >= fragmentCount) or
(totalLength > ENET_PROTOCOL_MAXIMUM_PACKET_SIZE) or
(fragmentOffset >= totalLength) or
(fragmentLength > totalLength - fragmentOffset) then
begin
Result:=-1; exit;
end;
currentCommand := enet_list_previous (enet_list_end (@ channel ^. incomingUnreliableCommands));
while (currentCommand <> enet_list_end (@ channel ^. incomingUnreliableCommands)) do
begin
incomingCommand := pENetIncomingCommand ( currentCommand );
if (reliableSequenceNumber >= channel ^. incomingReliableSequenceNumber) then
begin
if (incomingCommand ^. reliableSequenceNumber < channel ^. incomingReliableSequenceNumber) then
goto continue1;
end
else
if (incomingCommand ^. reliableSequenceNumber >= channel ^. incomingReliableSequenceNumber) then
break;
if (incomingCommand ^. reliableSequenceNumber < reliableSequenceNumber) then
break;
if (incomingCommand ^. reliableSequenceNumber > reliableSequenceNumber) then
goto continue1;
if (incomingCommand ^. unreliableSequenceNumber <= startSequenceNumber) then
begin
if (incomingCommand ^. unreliableSequenceNumber < startSequenceNumber) then
break;
if ((incomingCommand ^. command.header.command and ENET_PROTOCOL_COMMAND_MASK) <> ENET_PROTOCOL_COMMAND_SEND_UNRELIABLE_FRAGMENT) or
(totalLength <> incomingCommand ^. packet ^. dataLength) or
(fragmentCount <> incomingCommand ^. fragmentCount) then
begin
Result:=-1; exit;
end;
startCommand := incomingCommand;
break;
end;
continue1:
currentCommand := enet_list_previous (currentCommand);
end;
if (startCommand = nil) then
begin
packet := enet_packet_create (nil, totalLength, ENET_PACKET_FLAG_UNRELIABLE_FRAGMENT);
if (packet = nil) then
begin
Result:=-1; exit;
end;
startCommand := enet_peer_queue_incoming_command (peer, command, packet, fragmentCount);
if (startCommand = nil) then
begin
Result:=-1; exit;
end;
end;
if ((PIntegerArray(startCommand ^. fragments)^ [fragmentNumber div 32] and (1 shl (fragmentNumber mod 32))) = 0) then
begin
Dec( startCommand ^. fragmentsRemaining);
PIntegerArray(startCommand ^. fragments)^[fragmentNumber div 32] := PIntegerArray(startCommand ^. fragments)^[fragmentNumber div 32] or (1 shl (fragmentNumber mod 32));
if (fragmentOffset + fragmentLength > startCommand ^. packet ^. dataLength) then
fragmentLength := startCommand ^. packet ^. dataLength - fragmentOffset;
system.Move((PAnsiChar(command) + sizeof (ENetProtocolSendFragment))^,
(PAnsiChar(startCommand ^. packet ^. data) + fragmentOffset)^,
fragmentLength);
if (startCommand ^. fragmentsRemaining <= 0) then
enet_peer_dispatch_incoming_unreliable_commands (peer, channel);
end;
Result:=0;
end;
function enet_protocol_handle_ping (host : pENetHost; peer : pENetPeer; command : pENetProtocol):integer;
begin
if (peer ^. state <> ENET_PEER_STATE_CONNECTED) and (peer ^. state <> ENET_PEER_STATE_DISCONNECT_LATER) then
begin
Result:=-1; exit;
end;
result := 0;
end;
function enet_protocol_handle_bandwidth_limit (host : pENetHost; peer : pENetPeer; command : pENetProtocol):integer;
begin
if (peer ^. state <> ENET_PEER_STATE_CONNECTED) and (peer ^. state <> ENET_PEER_STATE_DISCONNECT_LATER) then
begin
Result:=-1; exit;
end;
peer ^. incomingBandwidth := ENET_NET_TO_HOST_32 (command ^. bandwidthLimit.incomingBandwidth);
peer ^. outgoingBandwidth := ENET_NET_TO_HOST_32 (command ^. bandwidthLimit.outgoingBandwidth);
if (peer ^. incomingBandwidth = 0) and (host ^. outgoingBandwidth = 0) then
peer ^. windowSize := ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE
else
peer ^. windowSize := (ENET_MIN (peer ^. incomingBandwidth, host ^. outgoingBandwidth) div
ENET_PEER_WINDOW_SIZE_SCALE) * ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
if (peer ^. windowSize < ENET_PROTOCOL_MINIMUM_WINDOW_SIZE) then
peer ^. windowSize := ENET_PROTOCOL_MINIMUM_WINDOW_SIZE
else
if (peer ^. windowSize > ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE) then
peer ^. windowSize := ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
result := 0;
end;
function enet_protocol_handle_throttle_configure (host : pENetHost; peer : pENetPeer; command : pENetProtocol):integer;
begin
if (peer ^. state <> ENET_PEER_STATE_CONNECTED) and (peer ^. state <> ENET_PEER_STATE_DISCONNECT_LATER) then
begin
Result:=-1; exit;
end;
peer ^. packetThrottleInterval := ENET_NET_TO_HOST_32 (command ^. throttleConfigure.packetThrottleInterval);
peer ^. packetThrottleAcceleration := ENET_NET_TO_HOST_32 (command ^. throttleConfigure.packetThrottleAcceleration);
peer ^. packetThrottleDeceleration := ENET_NET_TO_HOST_32 (command ^. throttleConfigure.packetThrottleDeceleration);
result := 0;
end;
function enet_protocol_handle_disconnect (host : pENetHost; peer : pENetPeer; command : pENetProtocol):integer;
begin
if (peer ^. state = ENET_PEER_STATE_DISCONNECTED) or (peer ^. state = ENET_PEER_STATE_ZOMBIE) or (peer ^. state = ENET_PEER_STATE_ACKNOWLEDGING_DISCONNECT) then
begin
Result:=0; exit;
end;
enet_peer_reset_queues (peer);
if (peer ^. state = ENET_PEER_STATE_CONNECTION_SUCCEEDED) or (peer ^. state = ENET_PEER_STATE_DISCONNECTING) then
enet_protocol_dispatch_state (host, peer, ENET_PEER_STATE_ZOMBIE)
else
if (peer ^. state <> ENET_PEER_STATE_CONNECTED) and (peer ^. state <> ENET_PEER_STATE_DISCONNECT_LATER) then
begin
if (peer ^. state = ENET_PEER_STATE_CONNECTION_PENDING) then host ^. recalculateBandwidthLimits := 1;
enet_peer_reset (peer);
end
else
if (command ^. header.command and ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE)<>0 then
peer ^. state := ENET_PEER_STATE_ACKNOWLEDGING_DISCONNECT
else
enet_protocol_dispatch_state (host, peer, ENET_PEER_STATE_ZOMBIE);
if (peer ^. state <> ENET_PEER_STATE_DISCONNECTED) then
peer ^. eventData := ENET_NET_TO_HOST_32 (command ^. disconnect.data);
result := 0;
end;
function enet_protocol_handle_acknowledge (host : pENetHost; event : pENetEvent; peer : pENetPeer; command : pENetProtocol):integer;
var
roundTripTime, receivedSentTime, receivedReliableSequenceNumber : enet_uint32;
commandNumber : ENetProtocolCommand;
begin
if (peer ^. state = ENET_PEER_STATE_DISCONNECTED) or (peer ^. state = ENET_PEER_STATE_ZOMBIE) then
begin
Result:=0; exit;
end;
receivedSentTime := ENET_NET_TO_HOST_16 (command ^. acknowledge.receivedSentTime);
receivedSentTime := receivedSentTime or (host ^. serviceTime and $FFFF0000);
if ((receivedSentTime and $8000) > (host ^. serviceTime and $8000)) then
dec(receivedSentTime , $10000);
if (ENET_TIME_LESS (host ^. serviceTime, receivedSentTime)) then
begin result := 0; exit; end;
peer ^. lastReceiveTime := host ^. serviceTime;
peer ^. earliestTimeout := 0;
roundTripTime := ENET_TIME_DIFFERENCE (host ^. serviceTime, receivedSentTime);
enet_peer_throttle (peer, roundTripTime);
dec(peer ^. roundTripTimeVariance , peer ^. roundTripTimeVariance div 4);
if (roundTripTime >= peer ^. roundTripTime) then
begin
inc(peer ^. roundTripTime , (roundTripTime - peer ^. roundTripTime) div 8);
inc(peer ^. roundTripTimeVariance , (roundTripTime - peer ^. roundTripTime) div 4);
end
else
begin
dec(peer ^. roundTripTime , (peer ^. roundTripTime - roundTripTime) div 8);
inc(peer ^. roundTripTimeVariance , (peer ^. roundTripTime - roundTripTime) div 4);
end;
if (peer ^. roundTripTime < peer ^. lowestRoundTripTime) then
peer ^. lowestRoundTripTime := peer ^. roundTripTime;
if (peer ^. roundTripTimeVariance > peer ^. highestRoundTripTimeVariance) then
peer ^. highestRoundTripTimeVariance := peer ^. roundTripTimeVariance;
if (peer ^. packetThrottleEpoch = 0) or
(ENET_TIME_DIFFERENCE (host ^. serviceTime, peer ^. packetThrottleEpoch) >= peer ^. packetThrottleInterval) then
begin
peer ^. lastRoundTripTime := peer ^. lowestRoundTripTime;
peer ^. lastRoundTripTimeVariance := peer ^. highestRoundTripTimeVariance;
peer ^. lowestRoundTripTime := peer ^. roundTripTime;
peer ^. highestRoundTripTimeVariance := peer ^. roundTripTimeVariance;
peer ^. packetThrottleEpoch := host ^. serviceTime;
end;
receivedReliableSequenceNumber := ENET_NET_TO_HOST_16 (command ^. acknowledge.receivedReliableSequenceNumber);
commandNumber := enet_protocol_remove_sent_reliable_command (peer, receivedReliableSequenceNumber, command ^. header.channelID);
case peer ^. state of
ENET_PEER_STATE_ACKNOWLEDGING_CONNECT: begin
if (commandNumber <> ENET_PROTOCOL_COMMAND_VERIFY_CONNECT) then
begin result := -1; exit; end;
enet_protocol_notify_connect (host, peer, event);
end; // break;
ENET_PEER_STATE_DISCONNECTING: begin
if (commandNumber <> ENET_PROTOCOL_COMMAND_DISCONNECT) then
begin result := -1; exit; end;
enet_protocol_notify_disconnect (host, peer, event);
end; // break;
ENET_PEER_STATE_DISCONNECT_LATER: begin
if (enet_list_empty (@ peer ^. outgoingReliableCommands) and
enet_list_empty (@ peer ^. outgoingUnreliableCommands) and
enet_list_empty (@ peer ^. sentReliableCommands)) then
enet_peer_disconnect (peer, peer ^. eventData);
end; // break
else ;
end;
result := 0;
end;
function enet_protocol_handle_verify_connect (host : pENetHost; event : pENetEvent; peer : pENetPeer; command : pENetProtocol):integer;
var
mtu, windowSize : enet_uint32;
channelCount : enet_size_t;
begin
if (peer ^. state <> ENET_PEER_STATE_CONNECTING) then
begin result := 0; exit; end;