@@ -83,14 +83,13 @@ internal sealed class Connection : IConnection
8383 //
8484 // Heartbeats
8585 //
86-
8786 private TimeSpan _heartbeat = TimeSpan . Zero ;
8887 private TimeSpan _heartbeatTimeSpan = TimeSpan . FromSeconds ( 0 ) ;
89- private int _missedHeartbeats = 0 ;
88+ private int _missedHeartbeats ;
89+ private bool _heartbeatDetected ;
9090
9191 private Timer _heartbeatWriteTimer ;
9292 private Timer _heartbeatReadTimer ;
93- private readonly AutoResetEvent _heartbeatRead = new AutoResetEvent ( false ) ;
9493
9594 private Task _mainLoopTask ;
9695
@@ -615,12 +614,9 @@ public void MainLoopIteration()
615614 }
616615 }
617616
618- public void NotifyHeartbeatListener ( )
617+ private void NotifyHeartbeatListener ( )
619618 {
620- if ( _heartbeat != TimeSpan . Zero )
621- {
622- _heartbeatRead . Set ( ) ;
623- }
619+ _heartbeatDetected = true ;
624620 }
625621
626622 public void NotifyReceivedCloseOk ( )
@@ -847,13 +843,14 @@ public void HeartbeatReadTimerCallback(object state)
847843 {
848844 if ( ! _closed )
849845 {
850- if ( ! _heartbeatRead . WaitOne ( 0 ) )
846+ if ( _heartbeatDetected )
851847 {
852- _missedHeartbeats ++ ;
848+ _heartbeatDetected = false ;
849+ _missedHeartbeats = 0 ;
853850 }
854851 else
855852 {
856- _missedHeartbeats = 0 ;
853+ _missedHeartbeats ++ ;
857854 }
858855
859856 // We check against 8 = 2 * 4 because we need to wait for at
@@ -921,7 +918,7 @@ public void HeartbeatWriteTimerCallback(object state)
921918 }
922919 }
923920
924- void MaybeStopHeartbeatTimers ( )
921+ private void MaybeStopHeartbeatTimers ( )
925922 {
926923 NotifyHeartbeatListener ( ) ;
927924 _heartbeatReadTimer ? . Dispose ( ) ;
0 commit comments