@@ -44,13 +44,32 @@ public partial class HiveMQClient : IDisposable, IHiveMQClient
44
44
45
45
private readonly Stopwatch lastCommunicationTimer = new ( ) ;
46
46
47
+ /// <summary>
48
+ /// Health check method to assure that tasks haven't faulted unexpectedly.
49
+ /// </summary>
50
+ private async Task RunTaskHealthCheckAsync ( Task ? task , string taskName )
51
+ {
52
+ if ( task is null )
53
+ {
54
+ Logger . Info ( $ "{ this . Options . ClientId } -(CM)- { taskName } is not running.") ;
55
+ }
56
+ else
57
+ {
58
+ if ( task . IsFaulted )
59
+ {
60
+ Logger . Error ( $ "{ this . Options . ClientId } -(CM)- { taskName } Faulted: { task . Exception } ") ;
61
+ Logger . Error ( $ "{ this . Options . ClientId } -(CM)- { taskName } died. Disconnecting.") ;
62
+ _ = await this . HandleDisconnectionAsync ( false ) . ConfigureAwait ( false ) ;
63
+ }
64
+ }
65
+ }
66
+
47
67
/// <summary>
48
68
/// Asynchronous background task that monitors the connection state and sends PingReq packets when
49
69
/// necessary.
50
70
/// </summary>
51
71
/// <param name="cancellationToken">The cancellation token.</param>
52
- /// <returns>A boolean return indicating exit state.</returns>
53
- private Task < bool > ConnectionMonitorAsync ( CancellationToken cancellationToken ) => Task . Run (
72
+ private Task ConnectionMonitorAsync ( CancellationToken cancellationToken ) => Task . Run (
54
73
async ( ) =>
55
74
{
56
75
var keepAlivePeriod = this . Options . KeepAlive / 2 ;
@@ -77,34 +96,35 @@ private Task<bool> ConnectionMonitorAsync(CancellationToken cancellationToken) =
77
96
78
97
// Dumping Client State
79
98
Logger . Trace ( $ "{ this . Options . ClientId } -(CM)- { this . ConnectState } lastCommunicationTimer:{ this . lastCommunicationTimer . Elapsed } ") ;
80
- Logger . Trace ( $ "{ this . Options . ClientId } -(CM)- SendQueue:{ this . SendQueue . Count } ReceivedQueue:{ this . ReceivedQueue . Count } OutgoingPublishQueue:{ this . OutgoingPublishQueue . Count } ") ;
81
- Logger . Trace ( $ "{ this . Options . ClientId } -(CM)- TransactionQueue:{ this . TransactionQueue . Count } ") ;
82
- Logger . Trace ( $ "{ this . Options . ClientId } -(CM)- - ConnectionMonitor:{ this . ConnectionMonitorTask ? . Status } ") ;
83
- Logger . Trace ( $ "{ this . Options . ClientId } -(CM)- - ConnectionPublishWriter:{ this . ConnectionPublishWriterTask ? . Status } ") ;
84
- Logger . Trace ( $ "{ this . Options . ClientId } -(CM)- - ConnectionWriter:{ this . ConnectionWriterTask ? . Status } ") ;
85
- Logger . Trace ( $ "{ this . Options . ClientId } -(CM)- - ConnectionReader:{ this . ConnectionReaderTask ? . Status } ") ;
86
- Logger . Trace ( $ "{ this . Options . ClientId } -(CM)- - ReceivedPacketsHandler:{ this . ReceivedPacketsHandlerTask ? . Status } ") ;
99
+ Logger . Trace ( $ "{ this . Options . ClientId } -(CM)- SendQueue:............{ this . SendQueue . Count } ") ;
100
+ Logger . Trace ( $ "{ this . Options . ClientId } -(CM)- ReceivedQueue:........{ this . ReceivedQueue . Count } ") ;
101
+ Logger . Trace ( $ "{ this . Options . ClientId } -(CM)- OutgoingPublishQueue:.{ this . OutgoingPublishQueue . Count } ") ;
102
+ Logger . Trace ( $ "{ this . Options . ClientId } -(CM)- TransactionQueue:.....{ this . TransactionQueue . Count } ") ;
103
+ Logger . Trace ( $ "{ this . Options . ClientId } -(CM)- # of Subscriptions:...{ this . Subscriptions . Count } ") ;
104
+
105
+ await this . RunTaskHealthCheckAsync ( this . ConnectionWriterTask , "ConnectionWriter" ) . ConfigureAwait ( false ) ;
106
+ await this . RunTaskHealthCheckAsync ( this . ConnectionReaderTask , "ConnectionReader" ) . ConfigureAwait ( false ) ;
107
+ await this . RunTaskHealthCheckAsync ( this . ConnectionPublishWriterTask , "ConnectionPublishWriter" ) . ConfigureAwait ( false ) ;
108
+ await this . RunTaskHealthCheckAsync ( this . ReceivedPacketsHandlerTask , "ReceivedPacketsHandler" ) . ConfigureAwait ( false ) ;
87
109
88
110
try
89
111
{
90
112
await Task . Delay ( 2000 , cancellationToken ) . ConfigureAwait ( false ) ;
91
113
}
92
114
catch ( TaskCanceledException )
93
115
{
94
- Logger . Trace ( $ "{ this . Options . ClientId } -(CM)- Cancelled") ;
95
- break ;
116
+ Logger . Info ( $ "{ this . Options . ClientId } -(CM)- Cancelled") ;
117
+ return ;
96
118
}
97
119
}
98
120
99
121
Logger . Trace ( $ "{ this . Options . ClientId } -(CM)- Exiting...{ this . ConnectState } ") ;
100
-
101
- return true ;
102
122
} , cancellationToken ) ;
103
123
104
124
/// <summary>
105
125
/// Asynchronous background task that handles the outgoing publish packets queued in OutgoingPublishQueue.
106
126
/// </summary>
107
- private Task < bool > ConnectionPublishWriterAsync ( CancellationToken cancellationToken ) => Task . Run (
127
+ private Task ConnectionPublishWriterAsync ( CancellationToken cancellationToken ) => Task . Run (
108
128
async ( ) =>
109
129
{
110
130
this . lastCommunicationTimer . Start ( ) ;
@@ -168,13 +188,12 @@ private Task<bool> ConnectionPublishWriterAsync(CancellationToken cancellationTo
168
188
} // while(true)
169
189
170
190
Logger . Trace ( $ "{ this . Options . ClientId } -(PW)- ConnectionPublishWriter Exiting...{ this . ConnectState } ") ;
171
- return true ;
172
191
} , cancellationToken ) ;
173
192
174
193
/// <summary>
175
194
/// Asynchronous background task that handles the outgoing traffic of packets queued in the sendQueue.
176
195
/// </summary>
177
- private Task < bool > ConnectionWriterAsync ( CancellationToken cancellationToken ) => Task . Run (
196
+ private Task ConnectionWriterAsync ( CancellationToken cancellationToken ) => Task . Run (
178
197
async ( ) =>
179
198
{
180
199
this . lastCommunicationTimer . Start ( ) ;
@@ -280,7 +299,7 @@ private Task<bool> ConnectionWriterAsync(CancellationToken cancellationToken) =>
280
299
} // while(true)
281
300
282
301
Logger . Trace ( $ "{ this . Options . ClientId } -(W)- ConnectionWriter Exiting...{ this . ConnectState } ") ;
283
- return true ;
302
+ return ;
284
303
} , cancellationToken ) ;
285
304
286
305
/// <summary>
@@ -383,8 +402,7 @@ private Task<bool> ConnectionReaderAsync(CancellationToken cancellationToken) =>
383
402
/// Continually processes the packets queued in the receivedQueue.
384
403
/// </summary>
385
404
/// <param name="cancellationToken">The cancellation token to stop the task.</param>
386
- /// <returns>A fairly worthless boolean.</returns>
387
- private Task < bool > ReceivedPacketsHandlerAsync ( CancellationToken cancellationToken ) => Task . Run (
405
+ private Task ReceivedPacketsHandlerAsync ( CancellationToken cancellationToken ) => Task . Run (
388
406
async ( ) =>
389
407
{
390
408
Logger . Trace ( $ "{ this . Options . ClientId } -(RPH)- Starting...{ this . ConnectState } ") ;
@@ -412,7 +430,7 @@ private Task<bool> ReceivedPacketsHandlerAsync(CancellationToken cancellationTok
412
430
ReasonString = "Packet Too Large" ,
413
431
} ;
414
432
await this . DisconnectAsync ( opts ) . ConfigureAwait ( false ) ;
415
- return false ;
433
+ return ;
416
434
}
417
435
}
418
436
@@ -462,7 +480,7 @@ private Task<bool> ReceivedPacketsHandlerAsync(CancellationToken cancellationTok
462
480
} // while (true)
463
481
464
482
Logger . Trace ( $ "{ this . Options . ClientId } -(RPH)- ReceivedPacketsHandler Exiting...{ this . ConnectState } ") ;
465
- return true ;
483
+ return ;
466
484
} , cancellationToken ) ;
467
485
468
486
/// <summary>
0 commit comments