| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Threading; |
| | | 5 | | using System.Threading.Tasks; |
| | | 6 | | using CoreWCF.Runtime; |
| | | 7 | | |
| | | 8 | | namespace CoreWCF.Channels |
| | | 9 | | { |
| | | 10 | | internal class DuplexRequestContext : RequestContextBase |
| | | 11 | | { |
| | | 12 | | private readonly IDuplexChannel _channel; |
| | | 13 | | private TaskCompletionSource<object> _dispatchInvokedTcs; |
| | | 14 | | private Task _dispatchInvokedTask; |
| | | 15 | | |
| | | 16 | | internal DuplexRequestContext(IDuplexChannel channel, Message request, IDefaultCommunicationTimeouts timeouts) |
| | 0 | 17 | | : base(request, timeouts.CloseTimeout, timeouts.SendTimeout) |
| | | 18 | | { |
| | 0 | 19 | | _channel = channel; |
| | 0 | 20 | | } |
| | | 21 | | |
| | | 22 | | protected override void OnAbort() |
| | | 23 | | { |
| | 0 | 24 | | } |
| | | 25 | | |
| | | 26 | | protected override Task OnCloseAsync(CancellationToken token) |
| | | 27 | | { |
| | 0 | 28 | | return Task.CompletedTask; |
| | | 29 | | } |
| | | 30 | | |
| | | 31 | | protected override Task OnReplyAsync(Message message, CancellationToken token) |
| | | 32 | | { |
| | 0 | 33 | | if (message != null) |
| | | 34 | | { |
| | 0 | 35 | | return _channel.SendAsync(message, token); |
| | | 36 | | } |
| | | 37 | | |
| | 0 | 38 | | return Task.CompletedTask; |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | public override void OnOperationInvoke() |
| | | 42 | | { |
| | 0 | 43 | | if (_dispatchInvokedTask == null) |
| | | 44 | | { |
| | 0 | 45 | | if (Interlocked.CompareExchange(ref _dispatchInvokedTask, Task.CompletedTask, null) == null) |
| | | 46 | | { |
| | 0 | 47 | | return; |
| | | 48 | | } |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | Fx.Assert(_dispatchInvokedTcs != null, "A non-null Task should have the associated TCS set"); |
| | 0 | 52 | | _dispatchInvokedTcs.TrySetResult(null); |
| | 0 | 53 | | } |
| | | 54 | | |
| | | 55 | | // TODO: Switch to ValueTask as in many scenarios this will be completed before being requested; |
| | | 56 | | public Task OperationDispatching |
| | | 57 | | { |
| | | 58 | | get |
| | | 59 | | { |
| | 0 | 60 | | if (_dispatchInvokedTask != null) |
| | | 61 | | { |
| | 0 | 62 | | return _dispatchInvokedTask; |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | // There's a small race here where we could create a TCS and dispatchInvokedTask is set via OnOperationI |
| | | 66 | | // In this case, we won't use our TCS and it will simply be an unnecessary allocation. As dispatchInvoke |
| | | 67 | | // always set using Interlocked.CompareExchance, we guarantee returning the correct Task; |
| | | 68 | | // Creating with RunContinuationsAsynchronously otherwise the method awaiting the Task will continue exe |
| | | 69 | | // the thread which calls OnOperationInvoke; |
| | 0 | 70 | | _dispatchInvokedTcs = new TaskCompletionSource<object>(TaskCreationOptions.RunContinuationsAsynchronousl |
| | 0 | 71 | | if (Interlocked.CompareExchange(ref _dispatchInvokedTask, _dispatchInvokedTcs.Task, null) == null) |
| | | 72 | | { |
| | 0 | 73 | | return _dispatchInvokedTcs.Task; |
| | | 74 | | } |
| | | 75 | | else |
| | | 76 | | { |
| | 0 | 77 | | _dispatchInvokedTcs = null; |
| | 0 | 78 | | return _dispatchInvokedTask; |
| | | 79 | | } |
| | | 80 | | } |
| | | 81 | | } |
| | | 82 | | } |
| | | 83 | | } |