@@ -21,12 +21,12 @@ public partial class ConnectionManager
21
21
22
22
internal Task ? ReceivedPacketsHandlerTask { get ; set ; }
23
23
24
- internal Task ? ConnectionMonitorTask { get ; set ; }
24
+ internal Thread ? ConnectionMonitorThread { get ; set ; }
25
25
26
26
/// <summary>
27
27
/// Health check method to assure that tasks haven't faulted unexpectedly.
28
28
/// </summary>
29
- private async Task RunTaskHealthCheckAsync ( Task ? task , string taskName )
29
+ private void RunTaskHealthCheck ( Task ? task , string taskName )
30
30
{
31
31
if ( task is null )
32
32
{
@@ -38,85 +38,74 @@ private async Task RunTaskHealthCheckAsync(Task? task, string taskName)
38
38
{
39
39
Logger . Error ( $ "{ this . Client . Options . ClientId } -(CM)- { taskName } Faulted: { task . Exception } ") ;
40
40
Logger . Error ( $ "{ this . Client . Options . ClientId } -(CM)- { taskName } died. Disconnecting.") ;
41
- await this . HandleDisconnectionAsync ( false ) . ConfigureAwait ( false ) ;
41
+ _ = Task . Run ( async ( ) => await this . HandleDisconnectionAsync ( false ) . ConfigureAwait ( false ) ) ;
42
42
}
43
43
}
44
44
}
45
45
46
+ private Thread LaunchConnectionMonitorThread ( )
47
+ {
48
+ var thread = new Thread ( this . ConnectionMonitor ) ;
49
+ thread . Start ( ) ;
50
+ return thread ;
51
+ }
52
+
46
53
/// <summary>
47
54
/// Asynchronous background task that monitors the connection state and sends PingReq packets when
48
55
/// necessary.
49
56
/// </summary>
50
- /// <param name="cancellationToken">The cancellation token.</param>
51
- private Task ConnectionMonitorAsync ( CancellationToken cancellationToken ) => Task . Run (
52
- async ( ) =>
57
+ private void ConnectionMonitor ( )
58
+ {
59
+ Logger . Trace ( $ "{ this . Client . Options . ClientId } -(CM)- Starting...{ this . State } ") ;
60
+ if ( this . Client . Options . KeepAlive == 0 )
53
61
{
54
- Logger . Trace ( $ "{ this . Client . Options . ClientId } -(CM)- Starting...{ this . State } ") ;
55
- if ( this . Client . Options . KeepAlive == 0 )
56
- {
57
- Logger . Debug ( $ "{ this . Client . Options . ClientId } -(CM)- KeepAlive is 0. No pings will be sent.") ;
58
- }
62
+ Logger . Debug ( $ "{ this . Client . Options . ClientId } -(CM)- KeepAlive is 0. No pings will be sent.") ;
63
+ }
59
64
60
- var keepAlivePeriod = this . Client . Options . KeepAlive ;
61
- this . lastCommunicationTimer . Start ( ) ;
65
+ var keepAlivePeriod = this . Client . Options . KeepAlive ;
66
+ this . lastCommunicationTimer . Start ( ) ;
62
67
63
- while ( true )
68
+ while ( true )
69
+ {
70
+ try
64
71
{
65
- try
72
+ // If connected and no recent packets have been sent, send a ping
73
+ if ( this . State == ConnectState . Connected )
66
74
{
67
- // If connected and no recent packets have been sent, send a ping
68
- if ( this . State == ConnectState . Connected )
75
+ if ( this . Client . Options . KeepAlive > 0 && this . lastCommunicationTimer . Elapsed > TimeSpan . FromSeconds ( keepAlivePeriod ) )
69
76
{
70
- if ( this . Client . Options . KeepAlive > 0 && this . lastCommunicationTimer . Elapsed > TimeSpan . FromSeconds ( keepAlivePeriod ) )
71
- {
72
- // Send PingReq
73
- Logger . Trace ( $ "{ this . Client . Options . ClientId } -(CM)- --> PingReq") ;
74
- this . SendQueue . Enqueue ( new PingReqPacket ( ) ) ;
75
- }
76
- }
77
-
78
- // Dumping Client State
79
- Logger . Debug ( $ "{ this . Client . Options . ClientId } -(CM)- { this . State } : last communications { this . lastCommunicationTimer . Elapsed } ago") ;
80
- Logger . Debug ( $ "{ this . Client . Options . ClientId } -(CM)- SendQueue:...............{ this . SendQueue . Count } ") ;
81
- Logger . Debug ( $ "{ this . Client . Options . ClientId } -(CM)- ReceivedQueue:...........{ this . ReceivedQueue . Count } ") ;
82
- Logger . Debug ( $ "{ this . Client . Options . ClientId } -(CM)- OutgoingPublishQueue:....{ this . OutgoingPublishQueue . Count } ") ;
83
- Logger . Debug ( $ "{ this . Client . Options . ClientId } -(CM)- OPubTransactionQueue:....{ this . OPubTransactionQueue . Count } /{ this . OPubTransactionQueue . Capacity } ") ;
84
- Logger . Debug ( $ "{ this . Client . Options . ClientId } -(CM)- IPubTransactionQueue:....{ this . IPubTransactionQueue . Count } /{ this . IPubTransactionQueue . Capacity } ") ;
85
- Logger . Debug ( $ "{ this . Client . Options . ClientId } -(CM)- # of Subscriptions:......{ this . Client . Subscriptions . Count } ") ;
86
- Logger . Debug ( $ "{ this . Client . Options . ClientId } -(CM)- PacketIDsInUse:..........{ this . PacketIDManager . Count } ") ;
87
-
88
- // Background Tasks Health Check
89
- await this . RunTaskHealthCheckAsync ( this . ConnectionWriterTask , "ConnectionWriter" ) . ConfigureAwait ( false ) ;
90
- await this . RunTaskHealthCheckAsync ( this . ConnectionReaderTask , "ConnectionReader" ) . ConfigureAwait ( false ) ;
91
- await this . RunTaskHealthCheckAsync ( this . ConnectionPublishWriterTask , "ConnectionPublishWriter" ) . ConfigureAwait ( false ) ;
92
- await this . RunTaskHealthCheckAsync ( this . ReceivedPacketsHandlerTask , "ReceivedPacketsHandler" ) . ConfigureAwait ( false ) ;
93
-
94
- // Sleep cycle
95
- await Task . Delay ( 2000 , cancellationToken ) . ConfigureAwait ( false ) ;
96
-
97
- // Check for cancellation
98
- if ( cancellationToken . IsCancellationRequested )
99
- {
100
- Logger . Trace ( $ "{ this . Client . Options . ClientId } -(CM)- Canceled & exiting...") ;
101
- break ;
102
- }
103
- }
104
- catch ( Exception ex )
105
- {
106
- if ( ex is TaskCanceledException || cancellationToken . IsCancellationRequested )
107
- {
108
- break ;
109
- }
110
- else
111
- {
112
- Logger . Error ( $ "{ this . Client . Options . ClientId } -(CM)- Exception: { ex } ") ;
113
- throw ;
77
+ // Send PingReq
78
+ Logger . Trace ( $ "{ this . Client . Options . ClientId } -(CM)- --> PingReq") ;
79
+ this . SendQueue . Enqueue ( new PingReqPacket ( ) ) ;
114
80
}
115
81
}
116
- }
117
82
118
- Logger . Debug ( $ "{ this . Client . Options . ClientId } -(CM)- Exiting...{ this . State } , cancellationRequested={ cancellationToken . IsCancellationRequested } ") ;
119
- } , cancellationToken ) ;
83
+ // Dumping Client State
84
+ Logger . Debug ( $ "{ this . Client . Options . ClientId } -(CM)- { this . State } : last communications { this . lastCommunicationTimer . Elapsed } ago") ;
85
+ Logger . Debug ( $ "{ this . Client . Options . ClientId } -(CM)- SendQueue:...............{ this . SendQueue . Count } ") ;
86
+ Logger . Debug ( $ "{ this . Client . Options . ClientId } -(CM)- ReceivedQueue:...........{ this . ReceivedQueue . Count } ") ;
87
+ Logger . Debug ( $ "{ this . Client . Options . ClientId } -(CM)- OutgoingPublishQueue:....{ this . OutgoingPublishQueue . Count } ") ;
88
+ Logger . Debug ( $ "{ this . Client . Options . ClientId } -(CM)- OPubTransactionQueue:....{ this . OPubTransactionQueue . Count } /{ this . OPubTransactionQueue . Capacity } ") ;
89
+ Logger . Debug ( $ "{ this . Client . Options . ClientId } -(CM)- IPubTransactionQueue:....{ this . IPubTransactionQueue . Count } /{ this . IPubTransactionQueue . Capacity } ") ;
90
+ Logger . Debug ( $ "{ this . Client . Options . ClientId } -(CM)- # of Subscriptions:......{ this . Client . Subscriptions . Count } ") ;
91
+ Logger . Debug ( $ "{ this . Client . Options . ClientId } -(CM)- PacketIDsInUse:..........{ this . PacketIDManager . Count } ") ;
92
+
93
+ // Background Tasks Health Check
94
+ this . RunTaskHealthCheck ( this . ConnectionWriterTask , "ConnectionWriter" ) ;
95
+ this . RunTaskHealthCheck ( this . ConnectionReaderTask , "ConnectionReader" ) ;
96
+ this . RunTaskHealthCheck ( this . ConnectionPublishWriterTask , "ConnectionPublishWriter" ) ;
97
+ this . RunTaskHealthCheck ( this . ReceivedPacketsHandlerTask , "ReceivedPacketsHandler" ) ;
98
+
99
+ // Sleep cycle
100
+ Thread . Sleep ( 2000 ) ;
101
+ }
102
+ catch ( Exception ex )
103
+ {
104
+ Logger . Error ( $ "{ this . Client . Options . ClientId } -(CM)- Exception: { ex } ") ;
105
+ throw ;
106
+ }
107
+ } // while (true)
108
+ }
120
109
121
110
/// <summary>
122
111
/// Asynchronous background task that handles the outgoing publish packets queued in OutgoingPublishQueue.
0 commit comments