@@ -33,6 +33,7 @@ public class AckSystem : IDisposable
33
33
34
34
// temp list for resending when processing sentqueue
35
35
private readonly HashSet < ReliablePacket > _toResend = new HashSet < ReliablePacket > ( ) ;
36
+ private readonly List < ReliablePacket > _resendRemoveList = new List < ReliablePacket > ( ) ;
36
37
private readonly IRawConnection _connection ;
37
38
private readonly ITime _time ;
38
39
private readonly Pool < ByteBuffer > _bufferPool ;
@@ -643,12 +644,35 @@ private void ReliableLost(ushort sequence, ReliablePacket reliablePacket)
643
644
644
645
private void ResendMessages ( )
645
646
{
647
+ if ( _toResend . Count == 0 )
648
+ return ;
649
+
650
+ _resendRemoveList . Clear ( ) ;
651
+
646
652
foreach ( var reliable in _toResend )
647
653
{
654
+ // exit early if we can't send any more packets
655
+ if ( _sentAckablePackets . Count >= _maxPacketsInSendBufferPerConnection )
656
+ break ;
657
+
648
658
_metrics ? . OnResend ( reliable . Length ) ;
649
659
SendReliablePacket ( reliable ) ;
660
+ _resendRemoveList . Add ( reliable ) ;
661
+ }
662
+
663
+ // if we sent all packets, we can just clear the set
664
+ // otherwise we need to remove the ones we did send
665
+ if ( _resendRemoveList . Count == _toResend . Count )
666
+ {
667
+ _toResend . Clear ( ) ;
668
+ }
669
+ else
670
+ {
671
+ foreach ( var reliable in _resendRemoveList )
672
+ {
673
+ _toResend . Remove ( reliable ) ;
674
+ }
650
675
}
651
- _toResend . Clear ( ) ;
652
676
}
653
677
654
678
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
0 commit comments