-
Notifications
You must be signed in to change notification settings - Fork 1
/
ENetPeer.cs
1303 lines (1052 loc) · 52.8 KB
/
ENetPeer.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using ENetDotNet.Internal;
using ENetDotNet.Protocol;
using static ENetDotNet.Internal.Constants;
namespace ENetDotNet
{
internal sealed class ENetAcknowledge
{
public PooledLinkedList<ENetAcknowledge>.Node? AcknowledgementListNode;
public uint SentTime;
public ENetProtocolAcknowledge Command;
public void Reset()
{
AcknowledgementListNode?.Remove();
SentTime = default;
Command = default;
}
}
internal sealed class ENetIncomingCommand
{
public PooledLinkedList<ENetIncomingCommand>.Node? IncomingCommandListNode;
public ushort reliableSequenceNumber;
public ushort unreliableSequenceNumber;
public ENetProtocol command;
public readonly List<uint> fragments = new();
public uint fragmentsRemaining;
public ENetPacket? packet;
public void Reset()
{
packet?.Dispose();
packet = null;
IncomingCommandListNode?.Remove();
fragments.Clear();
reliableSequenceNumber = default;
unreliableSequenceNumber = default;
fragmentsRemaining = default;
}
}
internal sealed class ENetOutgoingCommand
{
public PooledLinkedList<ENetOutgoingCommand>.Node? OutgoingCommandListNode;
public ushort reliableSequenceNumber;
public ushort unreliableSequenceNumber;
public uint sentTime;
public uint roundTripTimeout;
public uint roundTripTimeoutLimit;
public uint fragmentOffset;
public ushort fragmentLength;
public ushort sendAttempts;
public ENetProtocol command;
public ENetPacket? packet;
public void Reset()
{
packet?.Dispose();
packet = null;
OutgoingCommandListNode?.Remove();
reliableSequenceNumber = default;
unreliableSequenceNumber = default;
sentTime = default;
roundTripTimeout = default;
roundTripTimeoutLimit = default;
fragmentOffset = default;
fragmentLength = default;
sendAttempts = default;
}
}
internal sealed class ENetChannel
{
public readonly int Id;
public ushort outgoingReliableSequenceNumber;
public ushort outgoingUnreliableSequenceNumber;
public ushort usedReliableWindows;
public readonly ushort[] reliableWindows = new ushort[ENET_PEER_RELIABLE_WINDOWS];
public ushort incomingReliableSequenceNumber;
public ushort incomingUnreliableSequenceNumber;
public readonly PooledLinkedList<ENetIncomingCommand> incomingReliableCommands = new();
public readonly PooledLinkedList<ENetIncomingCommand> incomingUnreliableCommands = new();
public ENetChannel(int id)
{
Id = id;
}
}
public sealed class ENetPeer
{
internal readonly PooledLinkedList<ENetAcknowledge> m_AcknowledgementList = new();
internal readonly List<ENetChannel> m_ChannelList = new();
internal readonly PooledLinkedList<ENetOutgoingCommand> m_OutgoingCommands = new();
internal readonly PooledLinkedList<ENetOutgoingCommand> m_SentReliableCommands = new();
internal ushort m_PeerId;
internal ushort incomingPeerId;
internal ushort outgoingPeerId = ENET_PROTOCOL_MAXIMUM_PEER_ID;
internal byte incomingSessionId, outgoingSessionId;
internal uint m_ConnectId;
internal ulong m_IncomingDataTotal;
internal ENetPeerState m_State = ENetPeerState.Disconnected;
internal ulong lastRoundTripTime = ENET_PEER_DEFAULT_ROUND_TRIP_TIME;
internal ulong lastRoundTripTimeVariance;
internal ulong packetThrottleLimit = ENET_PEER_PACKET_THROTTLE_SCALE;
internal ulong packetThrottle = ENET_PEER_DEFAULT_PACKET_THROTTLE;
internal uint packetThrottleAcceleration = ENET_PEER_PACKET_THROTTLE_ACCELERATION;
internal uint packetThrottleDeceleration = ENET_PEER_PACKET_THROTTLE_DECELERATION;
internal uint packetThrottleInterval = ENET_PEER_PACKET_THROTTLE_INTERVAL;
internal ulong lastReceiveTime, lastSendTime;
internal ulong roundTripTime = ENET_PEER_DEFAULT_ROUND_TRIP_TIME;
internal ulong highestRoundTripTimeVariance;
internal ulong roundTripTimeVariance;
internal ulong lowestRoundTripTime = ENET_PEER_DEFAULT_ROUND_TRIP_TIME;
internal ulong packetThrottleEpoch;
internal ulong reliableDataInTransit;
internal ulong timeoutLimit = ENET_PEER_TIMEOUT_LIMIT,
timeoutMinimum = ENET_PEER_TIMEOUT_MINIMUM,
timeoutMaximum = ENET_PEER_TIMEOUT_MAXIMUM;
internal ulong packetLossEpoch;
internal uint packetsSent;
internal uint packetsLost;
internal uint packetLoss;
internal uint packetLossVariance;
internal uint incomingBandwidth, outgoingBandwidth;
internal ulong pingInterval = ENET_PEER_PING_INTERVAL;
internal ulong incomingBandwidthThrottleEpoch, outgoingBandwidthThrottleEpoch;
internal int earliestTimeout;
internal ulong incomingDataTotal, outgoingDataTotal;
internal uint eventData;
internal uint nextTimeout;
internal uint windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
internal readonly uint[] unsequencedWindow = new uint[ENET_PEER_UNSEQUENCED_WINDOW_SIZE / 32];
internal uint mtu;
internal int m_Flags;
internal ushort incomingUnsequencedGroup, outgoingUnsequencedGroup, outgoingReliableSequenceNumber;
internal readonly Queue<ENetOutgoingCommand> m_SendFragmentsBuffer = new();
public ENetHost Host { get; }
public IPEndPoint RemoteEndPoint { get; }
public object? UserData { get; set; }
public int ChannelCount { get; internal set; }
public ENetPeerState State => m_State;
internal ENetPeer(ENetHost host, IPEndPoint remoteEndPoint)
{
Host = host;
RemoteEndPoint = remoteEndPoint;
}
public unsafe void Send(ENetPacket packet)
{
var channel = GetOrAddChannelById(packet.Channel);
var fragments = m_SendFragmentsBuffer;
if (m_State != ENetPeerState.Connected ||
packet.Channel >= this.ChannelCount ||
packet.DataLength > this.Host.maximumPacketSize)
throw new Exception(); //return -1;
var fragmentLength = this.mtu - sizeof(ENetProtocolHeader) - sizeof(ENetProtocolSendFragment);
if (this.Host.Checksum != null)
fragmentLength -= 4;
if (packet.DataLength > fragmentLength)
{
uint fragmentCount = (uint)((packet.DataLength + fragmentLength - 1) / fragmentLength),
fragmentNumber,
fragmentOffset;
byte commandNumber;
ushort startSequenceNumber;
ENetOutgoingCommand fragment;
if (fragmentCount > ENET_PROTOCOL_MAXIMUM_FRAGMENT_COUNT)
throw new Exception(); //return -1;
if ((packet.Flags & (ENetPacketFlags.Reliable | ENetPacketFlags.UnreliableFragments)) == ENetPacketFlags.UnreliableFragments &&
channel.outgoingUnreliableSequenceNumber < 0xFFFF)
{
commandNumber = ENET_PROTOCOL_COMMAND_SEND_UNRELIABLE_FRAGMENT;
startSequenceNumber = (ushort)(channel.outgoingUnreliableSequenceNumber + 1);
}
else
{
commandNumber = ENET_PROTOCOL_COMMAND_SEND_FRAGMENT | ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE;
startSequenceNumber = (ushort)(channel.outgoingReliableSequenceNumber + 1);
}
m_SendFragmentsBuffer.Clear();
for (fragmentNumber = 0,
fragmentOffset = 0;
fragmentOffset < packet.DataLength;
++fragmentNumber,
fragmentOffset = (uint)(fragmentOffset + fragmentLength))
{
if (packet.DataLength - fragmentOffset < fragmentLength)
fragmentLength = packet.DataLength - fragmentOffset;
fragment = Host.m_OutgoingCommandPool.Rent();
fragment.fragmentOffset = fragmentOffset;
fragment.fragmentLength = (ushort)fragmentLength;
fragment.packet = packet;
fragment.command.Header.Command = commandNumber;
fragment.command.Header.ChannelID = (byte)packet.Channel;
fragment.command.SendFragment.StartSequenceNumber = startSequenceNumber;
fragment.command.SendFragment.DataLength = (ushort)fragmentLength;
fragment.command.SendFragment.FragmentCount = fragmentCount;
fragment.command.SendFragment.FragmentNumber = fragmentNumber;
fragment.command.SendFragment.TotalLength = (uint)packet.DataLength;
fragment.command.SendFragment.FragmentOffset = fragmentOffset;
fragments.Enqueue(fragment);
}
packet.IncrementRefCount();
while (fragments.TryDequeue(out var fragmentCommand))
{
SetupOutgoingCommand(fragmentCommand);
}
//return 0;
}
}
public void Ping()
{
if (this.m_State != ENetPeerState.Connected)
return;
ENetProtocol command = default;
command.Header.Command = ENET_PROTOCOL_COMMAND_PING | ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE;
command.Header.ChannelID = 0xFF;
QueueOutgoingCommand(in command, packet: null, offset: 0, length: 0);
}
public void PingInterval(TimeSpan interval)
{
this.pingInterval = interval != TimeSpan.Zero ? (ulong)interval.TotalMilliseconds : ENET_PEER_PING_INTERVAL;
}
public void Timeout(TimeSpan timeoutLimit, TimeSpan timeoutMinimum, TimeSpan timeoutMaximum)
{
this.timeoutLimit = timeoutLimit != TimeSpan.Zero ? (ulong)timeoutLimit.TotalMilliseconds : ENET_PEER_TIMEOUT_LIMIT;
this.timeoutMinimum = timeoutMinimum != TimeSpan.Zero ? (ulong)timeoutMinimum.TotalMilliseconds : ENET_PEER_TIMEOUT_MINIMUM;
this.timeoutMaximum = timeoutMaximum != TimeSpan.Zero ? (ulong)timeoutMaximum.TotalMilliseconds : ENET_PEER_TIMEOUT_MAXIMUM;
}
public void ThrottleConfigure(TimeSpan interval, uint acceleration, uint deceleration)
{
ENetProtocol command = default;
this.packetThrottleInterval = (uint)interval.TotalMilliseconds;
this.packetThrottleAcceleration = acceleration;
this.packetThrottleDeceleration = deceleration;
command.Header.Command = ENET_PROTOCOL_COMMAND_THROTTLE_CONFIGURE | ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE;
command.Header.ChannelID = 0xFF;
command.ThrottleConfigure.PacketThrottleInterval = (uint)interval.TotalMilliseconds;
command.ThrottleConfigure.PacketThrottleAcceleration = acceleration;
command.ThrottleConfigure.PacketThrottleDeceleration = deceleration;
QueueOutgoingCommand(in command, packet: null, offset: 0, length: 0);
}
internal bool Unlist() => Host.m_Peers.TryRemovePeer(this);
public void DisconnectLater(uint data)
{
if ((this.m_State == ENetPeerState.Connected || this.m_State == ENetPeerState.DisconnectLater) &&
!(m_OutgoingCommands.IsEmpty &&
m_SentReliableCommands.IsEmpty))
{
this.m_State = ENetPeerState.DisconnectLater;
this.eventData = data;
}
else
Disconnect(data);
}
public void DisconnectNow(uint data)
{
if (this.m_State == ENetPeerState.Disconnected)
return;
if (this.m_State != ENetPeerState.Zombie &&
this.m_State != ENetPeerState.Disconnecting)
{
ENetProtocol command = default;
command.Header.Command = ENET_PROTOCOL_COMMAND_DISCONNECT | ENET_PROTOCOL_COMMAND_FLAG_UNSEQUENCED;
command.Header.ChannelID = 0xFF;
command.Disconnect.Data = data;
QueueOutgoingCommand(in command, null, 0, 0);
Host.Flush();
}
Unlist();
}
public void Disconnect(uint data)
{
if (m_State == ENetPeerState.Disconnecting ||
m_State == ENetPeerState.Disconnected ||
m_State == ENetPeerState.AcknowledgingDisconnect ||
m_State == ENetPeerState.Zombie)
return;
ENetProtocol command = default;
command.Header.Command = ENET_PROTOCOL_COMMAND_DISCONNECT;
command.Header.ChannelID = 0xFF;
command.Disconnect.Data = data;
if (this.m_State == ENetPeerState.Connected || this.m_State == ENetPeerState.DisconnectLater)
command.Header.Command |= ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE;
else
command.Header.Command |= ENET_PROTOCOL_COMMAND_FLAG_UNSEQUENCED;
QueueOutgoingCommand(in command, packet: null, offset: 0, length: 0);
if (this.m_State == ENetPeerState.Connected || this.m_State == ENetPeerState.DisconnectLater)
{
OnDisconnect();
m_State = ENetPeerState.Disconnecting;
}
else
{
Host.Flush();
Unlist();
}
}
internal ENetChannel GetOrAddChannelById(int channelId)
{
for (int i = 0; i < m_ChannelList.Count; i++)
{
if (m_ChannelList[i].Id == channelId)
return m_ChannelList[i];
}
var channel = new ENetChannel(channelId);
m_ChannelList.Add(channel);
return channel;
}
internal ENetAcknowledge? QueueAcknowledgement(in ENetProtocolAcknowledge command, ushort sentTime)
{
if (command.Header.ChannelID < ChannelCount)
{
var channel = GetOrAddChannelById(command.Header.ChannelID);
var reliableWindow = command.Header.ReliableSequenceNumber / ENET_PEER_RELIABLE_WINDOW_SIZE;
var currentWindow = channel.incomingReliableSequenceNumber / ENET_PEER_RELIABLE_WINDOW_SIZE;
if (command.Header.ReliableSequenceNumber < channel.incomingReliableSequenceNumber)
reliableWindow += ENET_PEER_RELIABLE_WINDOWS;
if (reliableWindow >= currentWindow + ENET_PEER_FREE_RELIABLE_WINDOWS - 1 && reliableWindow <= currentWindow + ENET_PEER_FREE_RELIABLE_WINDOWS)
return null;
}
unsafe
{
this.outgoingDataTotal += (ulong)sizeof(ENetProtocolAcknowledge);
}
var ack = Host.m_AcknowledgePool.Rent();
ack.SentTime = sentTime;
ack.Command = command;
ack.AcknowledgementListNode = m_AcknowledgementList.AddLast(ack);
return ack;
}
internal void Throttle(ulong rtt)
{
if (lastRoundTripTime <= lastRoundTripTimeVariance)
{
packetThrottle = packetThrottleLimit;
}
else if (rtt <= lastRoundTripTime)
{
packetThrottle += packetThrottleAcceleration;
if (packetThrottle > packetThrottleLimit)
packetThrottle = packetThrottleLimit;
return; //return 1;
}
else if (rtt > lastRoundTripTime + 2 * lastRoundTripTimeVariance)
{
if (packetThrottle > packetThrottleDeceleration)
packetThrottle -= packetThrottleDeceleration;
else
packetThrottle = 0;
return; //return -1;
}
//return 0;
}
internal int RemoveSentReliableCommand(uint reliableSequenceNumber, int channelID)
{
var wasSent = true;
ENetOutgoingCommand? outgoingCommand = null;
foreach (var currentOutgoingCommand in m_SentReliableCommands)
{
if (currentOutgoingCommand.reliableSequenceNumber == reliableSequenceNumber &&
currentOutgoingCommand.command.Header.ChannelID == channelID)
{
outgoingCommand = currentOutgoingCommand;
break;
}
}
if (outgoingCommand == null)
{
foreach (var currentOutgoingCommand in m_OutgoingCommands)
{
if ((currentOutgoingCommand.command.Header.Command & ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE) == 0)
continue;
if (currentOutgoingCommand.sendAttempts < 1)
return ENET_PROTOCOL_COMMAND_NONE;
if (currentOutgoingCommand.reliableSequenceNumber == reliableSequenceNumber &&
currentOutgoingCommand.command.Header.ChannelID == channelID)
{
outgoingCommand = currentOutgoingCommand;
break;
}
}
if (outgoingCommand == null)
return ENET_PROTOCOL_COMMAND_NONE;
wasSent = false;
}
if (channelID < ChannelCount)
{
var channel = GetOrAddChannelById(channelID);
var reliableWindow = reliableSequenceNumber / ENET_PEER_RELIABLE_WINDOW_SIZE;
if (channel.reliableWindows[reliableWindow] > 0)
{
--channel.reliableWindows[reliableWindow];
if (channel.reliableWindows[reliableWindow] == 0)
channel.usedReliableWindows &= unchecked((ushort)~(1 << (int)reliableWindow));
}
}
var commandNumber = outgoingCommand.command.Header.Command & ENET_PROTOCOL_COMMAND_MASK;
outgoingCommand.OutgoingCommandListNode!.Remove();
outgoingCommand.OutgoingCommandListNode = null;
if (outgoingCommand.packet != null)
{
if (wasSent)
reliableDataInTransit -= outgoingCommand.fragmentLength;
outgoingCommand.packet.Dispose();
outgoingCommand.packet = null;
}
Host.m_OutgoingCommandPool.Return(outgoingCommand);
if (!m_SentReliableCommands.IsEmpty)
{
outgoingCommand = m_SentReliableCommands.FirstNode.Value;
nextTimeout = outgoingCommand.sentTime + outgoingCommand.roundTripTimeout;
}
return commandNumber;
}
internal void OnDisconnect()
{
if (m_State == ENetPeerState.Connected || m_State == ENetPeerState.DisconnectLater)
{
if (incomingBandwidth != 0)
--Host.bandwidthLimitedPeers;
}
}
internal void OnConnect()
{
if (m_State != ENetPeerState.Connected && m_State != ENetPeerState.DisconnectLater)
{
if (incomingBandwidth != 0)
++Host.bandwidthLimitedPeers;
}
}
internal void ChangeState(ENetPeerState state)
{
if (state == ENetPeerState.Connected || state == ENetPeerState.DisconnectLater)
OnConnect();
else
OnDisconnect();
m_State = state;
}
internal void NotifyConnect() => NotifyConnect(eventData);
internal void NotifyConnect(uint data)
{
Host.recalculateBandwidthLimits = true;
ChangeState(m_State == ENetPeerState.Connecting ? ENetPeerState.ConnectionSucceeded : ENetPeerState.ConnectionPending);
Host.InvokeOnEvent(ENetEvent.CreateConnect(this, data));
}
internal void NotifyDisconnect(uint data = 0)
{
if (m_State >= ENetPeerState.ConnectionPending)
Host.recalculateBandwidthLimits = true;
ChangeState(ENetPeerState.Disconnected);
Host.InvokeOnEvent(ENetEvent.CreateDisconnect(this, data));
Unlist();
}
internal void SetupOutgoingCommand(ENetOutgoingCommand outgoingCommand)
{
var channel = GetOrAddChannelById(outgoingCommand.command.Header.ChannelID);
if (outgoingCommand.command.Header.ChannelID == 0xFF)
{
++this.outgoingReliableSequenceNumber;
outgoingCommand.reliableSequenceNumber = this.outgoingReliableSequenceNumber;
outgoingCommand.unreliableSequenceNumber = 0;
}
else if ((outgoingCommand.command.Header.Command & ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE) != 0)
{
++channel.outgoingReliableSequenceNumber;
channel.outgoingUnreliableSequenceNumber = 0;
outgoingCommand.reliableSequenceNumber = channel.outgoingReliableSequenceNumber;
outgoingCommand.unreliableSequenceNumber = 0;
}
else if ((outgoingCommand.command.Header.Command & ENET_PROTOCOL_COMMAND_FLAG_UNSEQUENCED) != 0)
{
++this.outgoingUnsequencedGroup;
outgoingCommand.reliableSequenceNumber = 0;
outgoingCommand.unreliableSequenceNumber = 0;
}
else
{
if (outgoingCommand.fragmentOffset == 0)
++channel.outgoingUnreliableSequenceNumber;
outgoingCommand.reliableSequenceNumber = channel.outgoingReliableSequenceNumber;
outgoingCommand.unreliableSequenceNumber = channel.outgoingUnreliableSequenceNumber;
}
outgoingCommand.sendAttempts = 0;
outgoingCommand.sentTime = 0;
outgoingCommand.roundTripTimeout = 0;
outgoingCommand.roundTripTimeoutLimit = 0;
outgoingCommand.command.Header.ReliableSequenceNumber = outgoingCommand.reliableSequenceNumber;
switch (outgoingCommand.command.Header.Command & ENET_PROTOCOL_COMMAND_MASK)
{
case ENET_PROTOCOL_COMMAND_SEND_UNRELIABLE:
outgoingCommand.command.SendUnreliable.UnreliableSequenceNumber =
outgoingCommand.unreliableSequenceNumber;
break;
case ENET_PROTOCOL_COMMAND_SEND_UNSEQUENCED:
outgoingCommand.command.SendUnsequenced.UnsequencedGroup = this.outgoingUnsequencedGroup;
break;
default:
break;
}
outgoingCommand.OutgoingCommandListNode = m_OutgoingCommands.AddLast(outgoingCommand);
}
internal ENetOutgoingCommand QueueOutgoingCommand(in ENetProtocol command, ENetPacket? packet, uint offset, ushort length)
{
var outgoingCommand = Host.m_OutgoingCommandPool.Rent();
outgoingCommand.command = command; // unlike the original code, we are moving it
outgoingCommand.fragmentOffset = offset;
outgoingCommand.fragmentLength = length;
outgoingCommand.packet = packet;
if (packet != null)
{
packet.IncrementRefCount();
}
SetupOutgoingCommand(outgoingCommand);
return outgoingCommand;
}
internal ENetIncomingCommand? QueueIncomingCommand(in ENetProtocol command, ReadOnlySpan<byte> data, int dataLength, ENetPacketFlags flags, uint fragmentCount)
{
throw new NotImplementedException();
}
internal void DispatchIncomingReliableCommands(ENetChannel channel, ENetIncomingCommand? queuedCommand)
{
throw new NotImplementedException();
}
internal void DispatchIncomingUnreliableCommands(ENetChannel channel, ENetIncomingCommand? queuedCommand)
{
throw new NotImplementedException();
}
internal void SendAcknowledgements()
{
throw new NotImplementedException();
}
internal void RemoveSentUnreliableCommands()
{
throw new NotImplementedException();
}
internal void CheckTimeouts()
{
throw new NotImplementedException();
}
internal bool CheckOutgoingCommands()
{
throw new NotImplementedException();
}
internal void Reset()
{
ResetQueues();
Unlist();
}
internal void ResetQueues()
{
for (var current = m_AcknowledgementList.FirstNodeOrNull; current != null;)
{
var acknowledge = current.Value;
var next = current.Next;
current = next;
acknowledge.Reset();
Host.m_AcknowledgePool.Return(acknowledge);
}
for (var current = m_SentReliableCommands.FirstNodeOrNull; current != null;)
{
var outgoingCommand = current.Value;
var next = current.Next;
current = next;
outgoingCommand.Reset();
Host.m_OutgoingCommandPool.Return(outgoingCommand);
}
for (var current = m_OutgoingCommands.FirstNodeOrNull; current != null;)
{
var outgoingCommand = current.Value;
var next = current.Next;
current = next;
outgoingCommand.Reset();
Host.m_OutgoingCommandPool.Return(outgoingCommand);
}
foreach (var outgoingCommand in m_SendFragmentsBuffer)
{
outgoingCommand.Reset();
Host.m_OutgoingCommandPool.Return(outgoingCommand);
}
m_SendFragmentsBuffer.Clear();
foreach (var channel in m_ChannelList)
{
for (var current = channel.incomingReliableCommands.FirstNodeOrNull; current != null;)
{
var incomingCommand = current.Value;
var next = current.Next;
current = next;
incomingCommand.Reset();
Host.m_IncomingCommandPool.Return(incomingCommand);
}
for (var current = channel.incomingUnreliableCommands.FirstNodeOrNull; current != null;)
{
var incomingCommand = current.Value;
var next = current.Next;
current = next;
incomingCommand.Reset();
Host.m_IncomingCommandPool.Return(incomingCommand);
}
}
}
internal void HandleConnectCommand(in ENetProtocolConnect connect, ref ENetPacketReader dataReader)
{
if (m_State != ENetPeerState.Disconnected)
return;
if (connect.ChannelCount < ENET_PROTOCOL_MINIMUM_CHANNEL_COUNT ||
connect.ChannelCount > ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT)
return;
var duplicatePeers = 0;
foreach (var listedPeer in Host.m_Peers)
{
if (listedPeer.RemoteEndPoint.Address.Equals(RemoteEndPoint.Address))
{
duplicatePeers++;
}
}
if (duplicatePeers >= Host.duplicatePeers)
return;
this.ChannelCount = (int)connect.ChannelCount;
this.m_State = ENetPeerState.AcknowledgingConnect;
this.m_ConnectId = connect.ConnectID;
this.outgoingPeerId = connect.OutgoingPeerID;
this.incomingBandwidth = connect.IncomingBandwidth;
this.outgoingBandwidth = connect.OutgoingBandwidth;
this.packetThrottleInterval = connect.PacketThrottleInterval;
this.packetThrottleAcceleration = connect.PacketThrottleAcceleration;
this.packetThrottleDeceleration = connect.PacketThrottleDeceleration;
this.eventData = connect.Data;
unchecked
{
outgoingSessionId = Host.m_Random.NextByte();
incomingSessionId = Host.m_Random.NextByte();
if (incomingSessionId == outgoingSessionId)
incomingSessionId++;
}
mtu = Math.Clamp(connect.MTU, ENET_PROTOCOL_MINIMUM_MTU, ENET_PROTOCOL_MAXIMUM_MTU);
if (Host.outgoingBandwidth == 0 && this.incomingBandwidth == 0)
this.windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
else if (Host.outgoingBandwidth == 0 || this.incomingBandwidth == 0)
this.windowSize = Math.Max(Host.outgoingBandwidth, this.incomingBandwidth) /
ENET_PEER_WINDOW_SIZE_SCALE *
ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
else
this.windowSize = Math.Min(Host.outgoingBandwidth, this.incomingBandwidth) /
ENET_PEER_WINDOW_SIZE_SCALE *
ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
this.windowSize = Math.Clamp(this.windowSize,
ENET_PROTOCOL_MINIMUM_WINDOW_SIZE,
ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE);
var incomingWindowSize = 0u;
if (Host.incomingBandwidth == 0)
incomingWindowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
else
incomingWindowSize = Host.incomingBandwidth / ENET_PEER_WINDOW_SIZE_SCALE *
ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
if (incomingWindowSize > connect.WindowSize)
incomingWindowSize = connect.WindowSize;
incomingWindowSize = Math.Clamp(incomingWindowSize,
ENET_PROTOCOL_MINIMUM_WINDOW_SIZE,
ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE);
ENetProtocol verifyCommand = default;
verifyCommand.Header.Command = ENET_PROTOCOL_COMMAND_VERIFY_CONNECT | ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE;
verifyCommand.Header.ChannelID = 0xFF;
verifyCommand.VerifyConnect.OutgoingPeerID = this.incomingPeerId;
verifyCommand.VerifyConnect.IncomingSessionID = incomingSessionId;
verifyCommand.VerifyConnect.OutgoingSessionID = outgoingSessionId;
verifyCommand.VerifyConnect.MTU = this.mtu;
verifyCommand.VerifyConnect.WindowSize = windowSize;
verifyCommand.VerifyConnect.ChannelCount = unchecked((byte)ChannelCount);
verifyCommand.VerifyConnect.IncomingBandwidth = Host.incomingBandwidth;
verifyCommand.VerifyConnect.OutgoingBandwidth = Host.outgoingBandwidth;
verifyCommand.VerifyConnect.PacketThrottleInterval = this.packetThrottleInterval;
verifyCommand.VerifyConnect.PacketThrottleAcceleration = this.packetThrottleAcceleration;
verifyCommand.VerifyConnect.PacketThrottleDeceleration = this.packetThrottleDeceleration;
verifyCommand.VerifyConnect.ConnectID = this.m_ConnectId;
QueueOutgoingCommand(in verifyCommand, packet: null, offset: 0, length: 0);
}
internal void HandleVerifyConnectCommand(in ENetProtocolVerifyConnect verifyConnect, ref ENetPacketReader dataReader)
{
if (m_State != ENetPeerState.Connecting)
return;
var channelCount = unchecked((byte)verifyConnect.ChannelCount);
if (channelCount < ENET_PROTOCOL_MINIMUM_CHANNEL_COUNT || channelCount > ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT ||
verifyConnect.PacketThrottleInterval != this.packetThrottleInterval ||
verifyConnect.PacketThrottleAcceleration != this.packetThrottleAcceleration ||
verifyConnect.PacketThrottleDeceleration != this.packetThrottleDeceleration ||
verifyConnect.ConnectID != this.m_ConnectId)
{
NotifyDisconnect();
return;
}
RemoveSentReliableCommand(1, 0xFF);
if (channelCount < ChannelCount)
ChannelCount = channelCount;
this.outgoingPeerId = verifyConnect.OutgoingPeerID;
this.incomingSessionId = verifyConnect.IncomingSessionID;
this.outgoingSessionId = verifyConnect.OutgoingSessionID;
var connectMTU = Math.Clamp(verifyConnect.MTU,
ENET_PROTOCOL_MINIMUM_MTU,
ENET_PROTOCOL_MAXIMUM_MTU);
if (connectMTU < this.mtu)
this.mtu = connectMTU;
var outgoingWindowSize = Math.Clamp(verifyConnect.WindowSize,
ENET_PROTOCOL_MINIMUM_WINDOW_SIZE,
ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE);
if (outgoingWindowSize < this.windowSize)
this.windowSize = outgoingWindowSize;
this.incomingBandwidth = verifyConnect.IncomingBandwidth;
this.outgoingBandwidth = verifyConnect.OutgoingBandwidth;
NotifyConnect();
}
internal void HandleThrottleConfigureCommand(in ENetProtocolThrottleConfigure throttleConfigure, ref ENetPacketReader dataReader)
{
if (m_State != ENetPeerState.Connected && m_State != ENetPeerState.DisconnectLater)
return;
packetThrottleInterval = throttleConfigure.PacketThrottleInterval;
packetThrottleAcceleration = throttleConfigure.PacketThrottleAcceleration;
packetThrottleDeceleration = throttleConfigure.PacketThrottleDeceleration;
}
internal void HandleDisconnectCommand(in ENetProtocolDisconnect disconnect, ref ENetPacketReader dataReader)
{
if (this.m_State == ENetPeerState.Disconnected || this.m_State == ENetPeerState.Zombie || this.m_State == ENetPeerState.AcknowledgingDisconnect)
return;
ResetQueues();
if (this.m_State == ENetPeerState.ConnectionSucceeded || this.m_State == ENetPeerState.Disconnecting || this.m_State == ENetPeerState.Connecting)
NotifyDisconnect(disconnect.Data);
else
if (this.m_State != ENetPeerState.Connected && this.m_State != ENetPeerState.DisconnectLater)
{
if (this.m_State == ENetPeerState.ConnectionPending) Host.recalculateBandwidthLimits = true;
Reset();
}
else
if ((disconnect.Header.Command & ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE) != 0)
ChangeState(ENetPeerState.AcknowledgingDisconnect);
else
NotifyDisconnect(disconnect.Data);
if (this.m_State != ENetPeerState.Disconnected)
this.eventData = disconnect.Data;
}
internal void HandleBandwidthLimitCommand(in ENetProtocolBandwidthLimit bandwidthLimit, ref ENetPacketReader dataReader)
{
if (this.incomingBandwidth != 0)
--Host.bandwidthLimitedPeers;
this.incomingBandwidth = bandwidthLimit.IncomingBandwidth;
this.outgoingBandwidth = bandwidthLimit.OutgoingBandwidth;
if (this.incomingBandwidth != 0)
++Host.bandwidthLimitedPeers;
if (this.incomingBandwidth == 0 && Host.outgoingBandwidth == 0)
this.windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
else
if (this.incomingBandwidth == 0 || Host.outgoingBandwidth == 0)
this.windowSize = Math.Max(this.incomingBandwidth, Host.outgoingBandwidth) /
ENET_PEER_WINDOW_SIZE_SCALE * ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
else
this.windowSize = Math.Min(this.incomingBandwidth, Host.outgoingBandwidth) /
ENET_PEER_WINDOW_SIZE_SCALE * ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
if (this.windowSize < ENET_PROTOCOL_MINIMUM_WINDOW_SIZE)
this.windowSize = ENET_PROTOCOL_MINIMUM_WINDOW_SIZE;
else
if (this.windowSize > ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE)
this.windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE;
}
internal void HandlePingCommand(in ENetProtocolPing ping, ref ENetPacketReader dataReader)
{
/* no-op */
}
internal void HandleAcknowledgeCommand(in ENetProtocolAcknowledge acknowledge, ref ENetPacketReader dataReader)
{
if (m_State == ENetPeerState.Disconnected || m_State == ENetPeerState.Zombie)
return; //return 0;
ulong receivedSentTime = acknowledge.ReceivedSentTime;
receivedSentTime |= Host.m_ServiceTime & 0x7FFF_FFFF_FFFF_0000;
if ((receivedSentTime & 0x8000) > (Host.m_ServiceTime & 0x8000))
receivedSentTime -= 0x10000;
if (Host.m_ServiceTime > receivedSentTime)
return; // return 0;
ulong roundTripTime = Host.m_ServiceTime - receivedSentTime;
roundTripTime = Math.Max(roundTripTime, 1);
if (lastReceiveTime > 0)
{
Throttle(roundTripTime);
roundTripTimeVariance -= roundTripTimeVariance / 4;
if (roundTripTime >= this.roundTripTime)
{
var diff = roundTripTime - this.roundTripTime;
roundTripTimeVariance += diff / 4;
this.roundTripTime += diff / 8;
}
else
{
var diff = this.roundTripTime - roundTripTime;
roundTripTimeVariance += diff / 4;
this.roundTripTime -= diff / 8;
}
}
else
{
this.roundTripTime = roundTripTime;
roundTripTimeVariance = (roundTripTime + 1) / 2;
}
if (this.roundTripTime < lowestRoundTripTime)
lowestRoundTripTime = this.roundTripTime;
if (roundTripTimeVariance > highestRoundTripTimeVariance)
highestRoundTripTimeVariance = roundTripTimeVariance;
if (packetThrottleEpoch == 0 ||
Host.m_ServiceTime - packetThrottleEpoch >= packetThrottleInterval)
{
lastRoundTripTime = lowestRoundTripTime;
lastRoundTripTimeVariance = Math.Max(highestRoundTripTimeVariance, 1);
lowestRoundTripTime = this.roundTripTime;
highestRoundTripTimeVariance = roundTripTimeVariance;
packetThrottleEpoch = Host.m_ServiceTime;
}
lastReceiveTime = Math.Max(Host.m_ServiceTime, 1);
earliestTimeout = 0;
uint receivedReliableSequenceNumber = acknowledge.ReceivedReliableSequenceNumber;
var commandNumber = RemoveSentReliableCommand(receivedReliableSequenceNumber, acknowledge.Header.ChannelID);
switch (m_State)
{
case ENetPeerState.AcknowledgingConnect: