@@ -27,7 +27,7 @@ public class LogicalConnectionManager : ILogicalConnection
2727
2828 private const int connectionTimeout = 1000 ;
2929
30- private const int connectionPingInterval = 1000 ;
30+ private const int pingInterval = 100 ;
3131
3232 public LogicalConnectionManager ( ClientOptions options )
3333 {
@@ -42,17 +42,7 @@ public void Dispose()
4242 }
4343
4444 Interlocked . Exchange ( ref _droppableLogicalConnection , null ) ? . Dispose ( ) ;
45-
46- var savedTimer = Interlocked . Exchange ( ref _timer , null ) ;
47- if ( savedTimer != null )
48- {
49- using ( var timerDisposedEvent = new ManualResetEvent ( false ) )
50- {
51- savedTimer . Dispose ( timerDisposedEvent ) ;
52- // this will guarantee that all callbacks finished
53- timerDisposedEvent . WaitOne ( ) ;
54- }
55- }
45+ Interlocked . Exchange ( ref _timer , null ) ? . Dispose ( ) ;
5646 }
5747
5848 public async Task Connect ( )
@@ -69,37 +59,36 @@ public async Task Connect()
6959
7060 _clientOptions . LogWriter ? . WriteLine ( $ "{ nameof ( LogicalConnectionManager ) } : Connected...") ;
7161
72- _timer = new Timer ( x => CheckPing ( ) , null , connectionPingInterval , Timeout . Infinite ) ;
62+ _timer = new Timer ( x => CheckPing ( ) , null , pingInterval , Timeout . Infinite ) ;
7363 }
7464
75- private static readonly PingRequest pingRequest = new PingRequest ( ) ;
65+ private static readonly PingRequest _pingRequest = new PingRequest ( ) ;
7666
7767 private void CheckPing ( )
7868 {
7969 LogicalConnection savedConnection = _droppableLogicalConnection ;
8070
81- if ( savedConnection == null )
82- {
83- return ;
84- }
85-
8671 try
8772 {
88- Task task = savedConnection . SendRequestWithEmptyResponse ( pingRequest ) ;
89- if ( ! task . Wait ( connectionTimeout ) || task . Status != TaskStatus . RanToCompletion )
73+ if ( savedConnection == null || ! savedConnection . IsConnected ( ) )
9074 {
91- savedConnection . Dispose ( ) ;
75+ return ;
76+ }
77+
78+ using ( Task task = savedConnection . SendRequestWithEmptyResponse ( _pingRequest ) )
79+ {
80+ if ( Task . WaitAny ( new [ ] { task } , connectionTimeout ) != 0 || task . Status != TaskStatus . RanToCompletion )
81+ {
82+ _clientOptions . LogWriter ? . WriteLine ( $ "{ nameof ( LogicalConnectionManager ) } : Ping failed, dropping logical conection...") ;
83+ savedConnection . Dispose ( ) ;
84+ }
9285 }
93- }
94- catch ( AggregateException ae )
95- {
96- savedConnection . Dispose ( ) ;
9786 }
9887 finally
9988 {
10089 if ( _disposing == 0 )
10190 {
102- _timer ? . Change ( connectionPingInterval , Timeout . Infinite ) ;
91+ _timer ? . Change ( pingInterval , Timeout . Infinite ) ;
10392 }
10493 }
10594 }
0 commit comments