@@ -51,7 +51,7 @@ internal abstract class AsyncRpcContinuation<T> : IRpcContinuation
51
51
52
52
private bool _disposedValue ;
53
53
54
- public AsyncRpcContinuation ( TimeSpan continuationTimeout , CancellationToken cancellationToken )
54
+ public AsyncRpcContinuation ( TimeSpan continuationTimeout , CancellationToken rpcCancellationToken )
55
55
{
56
56
/*
57
57
* Note: we can't use an ObjectPool for these because the netstandard2.0
@@ -89,7 +89,7 @@ public AsyncRpcContinuation(TimeSpan continuationTimeout, CancellationToken canc
89
89
_tcsConfiguredTaskAwaitable = _tcs . Task . ConfigureAwait ( false ) ;
90
90
91
91
_linkedCancellationTokenSource = CancellationTokenSource . CreateLinkedTokenSource (
92
- _continuationTimeoutCancellationTokenSource . Token , cancellationToken ) ;
92
+ _continuationTimeoutCancellationTokenSource . Token , rpcCancellationToken ) ;
93
93
}
94
94
95
95
public CancellationToken CancellationToken
@@ -105,7 +105,27 @@ public ConfiguredTaskAwaitable<T>.ConfiguredTaskAwaiter GetAwaiter()
105
105
return _tcsConfiguredTaskAwaitable . GetAwaiter ( ) ;
106
106
}
107
107
108
- public abstract Task HandleCommandAsync ( IncomingCommand cmd ) ;
108
+ public async Task HandleCommandAsync ( IncomingCommand cmd )
109
+ {
110
+ try
111
+ {
112
+ await DoHandleCommandAsync ( cmd )
113
+ . ConfigureAwait ( false ) ;
114
+ }
115
+ catch ( OperationCanceledException )
116
+ {
117
+ if ( CancellationToken . IsCancellationRequested )
118
+ {
119
+ _tcs . SetCanceled ( ) ;
120
+ }
121
+ else
122
+ {
123
+ throw ;
124
+ }
125
+ }
126
+ }
127
+
128
+ protected abstract Task DoHandleCommandAsync ( IncomingCommand cmd ) ;
109
129
110
130
public virtual void HandleChannelShutdown ( ShutdownEventArgs reason )
111
131
{
@@ -141,17 +161,17 @@ public ConnectionSecureOrTuneAsyncRpcContinuation(TimeSpan continuationTimeout,
141
161
{
142
162
}
143
163
144
- public override Task HandleCommandAsync ( IncomingCommand cmd )
164
+ protected override Task DoHandleCommandAsync ( IncomingCommand cmd )
145
165
{
146
166
if ( cmd . CommandId == ProtocolCommandId . ConnectionSecure )
147
167
{
148
168
var secure = new ConnectionSecure ( cmd . MethodSpan ) ;
149
- _tcs . TrySetResult ( new ConnectionSecureOrTune ( secure . _challenge , default ) ) ;
169
+ _tcs . SetResult ( new ConnectionSecureOrTune ( secure . _challenge , default ) ) ;
150
170
}
151
171
else if ( cmd . CommandId == ProtocolCommandId . ConnectionTune )
152
172
{
153
173
var tune = new ConnectionTune ( cmd . MethodSpan ) ;
154
- _tcs . TrySetResult ( new ConnectionSecureOrTune ( default , new ConnectionTuneDetails
174
+ _tcs . SetResult ( new ConnectionSecureOrTune ( default , new ConnectionTuneDetails
155
175
{
156
176
m_channelMax = tune . _channelMax ,
157
177
m_frameMax = tune . _frameMax ,
@@ -178,11 +198,11 @@ public SimpleAsyncRpcContinuation(ProtocolCommandId expectedCommandId, TimeSpan
178
198
_expectedCommandId = expectedCommandId ;
179
199
}
180
200
181
- public override Task HandleCommandAsync ( IncomingCommand cmd )
201
+ protected override Task DoHandleCommandAsync ( IncomingCommand cmd )
182
202
{
183
203
if ( cmd . CommandId == _expectedCommandId )
184
204
{
185
- _tcs . TrySetResult ( true ) ;
205
+ _tcs . SetResult ( true ) ;
186
206
}
187
207
else
188
208
{
@@ -206,14 +226,14 @@ public BasicCancelAsyncRpcContinuation(string consumerTag, IConsumerDispatcher c
206
226
_consumerDispatcher = consumerDispatcher ;
207
227
}
208
228
209
- public override async Task HandleCommandAsync ( IncomingCommand cmd )
229
+ protected override async Task DoHandleCommandAsync ( IncomingCommand cmd )
210
230
{
211
231
if ( cmd . CommandId == ProtocolCommandId . BasicCancelOk )
212
232
{
213
- _tcs . TrySetResult ( true ) ;
214
233
Debug . Assert ( _consumerTag == new BasicCancelOk ( cmd . MethodSpan ) . _consumerTag ) ;
215
234
await _consumerDispatcher . HandleBasicCancelOkAsync ( _consumerTag , CancellationToken )
216
235
. ConfigureAwait ( false ) ;
236
+ _tcs . SetResult ( true ) ;
217
237
}
218
238
else
219
239
{
@@ -235,14 +255,16 @@ public BasicConsumeAsyncRpcContinuation(IAsyncBasicConsumer consumer, IConsumerD
235
255
_consumerDispatcher = consumerDispatcher ;
236
256
}
237
257
238
- public override async Task HandleCommandAsync ( IncomingCommand cmd )
258
+ protected override async Task DoHandleCommandAsync ( IncomingCommand cmd )
239
259
{
240
260
if ( cmd . CommandId == ProtocolCommandId . BasicConsumeOk )
241
261
{
242
262
var method = new BasicConsumeOk ( cmd . MethodSpan ) ;
243
- _tcs . TrySetResult ( method . _consumerTag ) ;
263
+
244
264
await _consumerDispatcher . HandleBasicConsumeOkAsync ( _consumer , method . _consumerTag , CancellationToken )
245
265
. ConfigureAwait ( false ) ;
266
+
267
+ _tcs . SetResult ( method . _consumerTag ) ;
246
268
}
247
269
else
248
270
{
@@ -264,7 +286,7 @@ public BasicGetAsyncRpcContinuation(Func<ulong, ulong> adjustDeliveryTag,
264
286
265
287
internal DateTime StartTime { get ; } = DateTime . UtcNow ;
266
288
267
- public override Task HandleCommandAsync ( IncomingCommand cmd )
289
+ protected override Task DoHandleCommandAsync ( IncomingCommand cmd )
268
290
{
269
291
if ( cmd . CommandId == ProtocolCommandId . BasicGetOk )
270
292
{
@@ -280,11 +302,11 @@ public override Task HandleCommandAsync(IncomingCommand cmd)
280
302
header ,
281
303
cmd . Body . ToArray ( ) ) ;
282
304
283
- _tcs . TrySetResult ( result ) ;
305
+ _tcs . SetResult ( result ) ;
284
306
}
285
307
else if ( cmd . CommandId == ProtocolCommandId . BasicGetEmpty )
286
308
{
287
- _tcs . TrySetResult ( null ) ;
309
+ _tcs . SetResult ( null ) ;
288
310
}
289
311
else
290
312
{
@@ -325,7 +347,7 @@ public override void HandleChannelShutdown(ShutdownEventArgs reason)
325
347
326
348
public Task OnConnectionShutdownAsync ( object ? sender , ShutdownEventArgs reason )
327
349
{
328
- _tcs . TrySetResult ( true ) ;
350
+ _tcs . SetResult ( true ) ;
329
351
return Task . CompletedTask ;
330
352
}
331
353
}
@@ -377,13 +399,13 @@ public QueueDeclareAsyncRpcContinuation(TimeSpan continuationTimeout, Cancellati
377
399
{
378
400
}
379
401
380
- public override Task HandleCommandAsync ( IncomingCommand cmd )
402
+ protected override Task DoHandleCommandAsync ( IncomingCommand cmd )
381
403
{
382
404
if ( cmd . CommandId == ProtocolCommandId . QueueDeclareOk )
383
405
{
384
406
var method = new Client . Framing . QueueDeclareOk ( cmd . MethodSpan ) ;
385
407
var result = new QueueDeclareOk ( method . _queue , method . _messageCount , method . _consumerCount ) ;
386
- _tcs . TrySetResult ( result ) ;
408
+ _tcs . SetResult ( result ) ;
387
409
}
388
410
else
389
411
{
@@ -417,12 +439,12 @@ public QueueDeleteAsyncRpcContinuation(TimeSpan continuationTimeout, Cancellatio
417
439
{
418
440
}
419
441
420
- public override Task HandleCommandAsync ( IncomingCommand cmd )
442
+ protected override Task DoHandleCommandAsync ( IncomingCommand cmd )
421
443
{
422
444
if ( cmd . CommandId == ProtocolCommandId . QueueDeleteOk )
423
445
{
424
446
var method = new QueueDeleteOk ( cmd . MethodSpan ) ;
425
- _tcs . TrySetResult ( method . _messageCount ) ;
447
+ _tcs . SetResult ( method . _messageCount ) ;
426
448
}
427
449
else
428
450
{
@@ -440,12 +462,12 @@ public QueuePurgeAsyncRpcContinuation(TimeSpan continuationTimeout, Cancellation
440
462
{
441
463
}
442
464
443
- public override Task HandleCommandAsync ( IncomingCommand cmd )
465
+ protected override Task DoHandleCommandAsync ( IncomingCommand cmd )
444
466
{
445
467
if ( cmd . CommandId == ProtocolCommandId . QueuePurgeOk )
446
468
{
447
469
var method = new QueuePurgeOk ( cmd . MethodSpan ) ;
448
- _tcs . TrySetResult ( method . _messageCount ) ;
470
+ _tcs . SetResult ( method . _messageCount ) ;
449
471
}
450
472
else
451
473
{
0 commit comments