| | | 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; |
| | | 5 | | using System.Threading; |
| | | 6 | | using System.Threading.Tasks; |
| | | 7 | | using CoreWCF.Channels.Framing; |
| | | 8 | | using Microsoft.AspNetCore.Connections; |
| | | 9 | | using Microsoft.Extensions.Logging; |
| | | 10 | | |
| | | 11 | | namespace CoreWCF.Channels |
| | | 12 | | { |
| | | 13 | | internal class NamedPipeConnection |
| | | 14 | | { |
| | 0 | 15 | | private static WaitCallback s_ThreadPoolCallback = ThreadPoolCallback; |
| | | 16 | | |
| | | 17 | | private ConnectionDelegate _connectionDelegate; |
| | 0 | 18 | | private readonly CancellationTokenSource _connectionClosingCts = new CancellationTokenSource(); |
| | 0 | 19 | | private readonly TaskCompletionSource<object> _completionTcs = new TaskCompletionSource<object>(TaskCreationOpti |
| | | 20 | | protected readonly long _id; |
| | | 21 | | protected readonly TransportConnectionManager _transportConnectionManager; |
| | | 22 | | |
| | 0 | 23 | | public NamedPipeConnection(long id, NamedPipeConnectionContext connectionContext, ConnectionDelegate connectionD |
| | | 24 | | { |
| | 0 | 25 | | _id = id; |
| | 0 | 26 | | ConnectionContext = connectionContext; |
| | 0 | 27 | | _transportConnectionManager = transportConnectionManager; |
| | 0 | 28 | | _connectionDelegate = connectionDelegate; |
| | 0 | 29 | | Logger = logger; |
| | 0 | 30 | | ConnectionClosedRequested = _connectionClosingCts.Token; |
| | 0 | 31 | | ConfigureContextLogging(); |
| | 0 | 32 | | } |
| | | 33 | | |
| | | 34 | | private void ConfigureContextLogging() |
| | | 35 | | { |
| | | 36 | | #if DEBUG |
| | | 37 | | ConnectionContext.Transport = new NamedPipeExceptionConvertingDuplexPipe(new LoggingDuplexPipe(ConnectionCon |
| | | 38 | | #else |
| | 0 | 39 | | ConnectionContext.Transport = new NamedPipeExceptionConvertingDuplexPipe(ConnectionContext.Transport); |
| | | 40 | | #endif |
| | 0 | 41 | | } |
| | | 42 | | |
| | 0 | 43 | | public Task ExecutionTask => _completionTcs.Task; |
| | | 44 | | |
| | 0 | 45 | | public CancellationToken ConnectionClosedRequested { get; set; } |
| | | 46 | | |
| | 0 | 47 | | public NamedPipeConnectionContext ConnectionContext { get; } |
| | | 48 | | |
| | 0 | 49 | | private NetNamedPipeTrace Logger { get; } |
| | | 50 | | |
| | | 51 | | internal void StartDispatching() |
| | | 52 | | { |
| | 0 | 53 | | ThreadPool.UnsafeQueueUserWorkItem(s_ThreadPoolCallback, this); |
| | 0 | 54 | | } |
| | | 55 | | |
| | | 56 | | internal static void ThreadPoolCallback(object state) |
| | | 57 | | { |
| | 0 | 58 | | _ = ((NamedPipeConnection)state).ExecuteAsync(); |
| | 0 | 59 | | } |
| | | 60 | | |
| | | 61 | | internal async Task ExecuteAsync() |
| | | 62 | | { |
| | 0 | 63 | | var connectionContext = ConnectionContext; |
| | | 64 | | |
| | | 65 | | try |
| | | 66 | | { |
| | 0 | 67 | | connectionContext.Start(); |
| | 0 | 68 | | Logger.ConnectionStart(connectionContext.ConnectionId); |
| | | 69 | | |
| | 0 | 70 | | using (BeginConnectionScope()) |
| | | 71 | | { |
| | | 72 | | try |
| | | 73 | | { |
| | 0 | 74 | | await _connectionDelegate(connectionContext); |
| | 0 | 75 | | } |
| | 0 | 76 | | catch (Exception ex) |
| | | 77 | | { |
| | 0 | 78 | | Logger.LogError(0, ex, "Unhandled exception while processing {ConnectionId}.", connectionContext |
| | 0 | 79 | | } |
| | 0 | 80 | | } |
| | | 81 | | } |
| | | 82 | | finally |
| | | 83 | | { |
| | 0 | 84 | | Logger.ConnectionStop(connectionContext.ConnectionId); |
| | | 85 | | |
| | | 86 | | // Dispose the transport connection, this needs to happen before removing it from the |
| | | 87 | | // connection manager so that we only signal completion of this connection after the transport |
| | | 88 | | // is properly torn down. |
| | 0 | 89 | | await connectionContext.DisposeAsync(); |
| | | 90 | | |
| | 0 | 91 | | _transportConnectionManager.RemoveConnection(_id); |
| | | 92 | | } |
| | 0 | 93 | | } |
| | | 94 | | |
| | | 95 | | protected IDisposable BeginConnectionScope() |
| | | 96 | | { |
| | 0 | 97 | | if (Logger.IsEnabled(LogLevel.Critical)) |
| | | 98 | | { |
| | 0 | 99 | | return Logger.BeginScope(new ConnectionLogScope(ConnectionContext.ConnectionId)); |
| | | 100 | | } |
| | | 101 | | |
| | 0 | 102 | | return null; |
| | | 103 | | } |
| | | 104 | | |
| | | 105 | | public void RequestClose() |
| | | 106 | | { |
| | | 107 | | try |
| | | 108 | | { |
| | 0 | 109 | | _ = CloseAsync(); |
| | 0 | 110 | | _connectionClosingCts.Cancel(); |
| | 0 | 111 | | } |
| | 0 | 112 | | catch (ObjectDisposedException) |
| | | 113 | | { |
| | | 114 | | // There's a race where the token could be disposed |
| | | 115 | | // swallow the exception and no-op |
| | 0 | 116 | | } |
| | 0 | 117 | | } |
| | | 118 | | |
| | | 119 | | private async Task CloseAsync() |
| | | 120 | | { |
| | | 121 | | try |
| | | 122 | | { |
| | 0 | 123 | | await ConnectionContext.DisposeAsync(); |
| | 0 | 124 | | } |
| | 0 | 125 | | catch (Exception ex) |
| | | 126 | | { |
| | 0 | 127 | | Logger.LogError(0, ex, "Error while closing connection {ConnectionId}.", ConnectionContext.ConnectionId) |
| | 0 | 128 | | } |
| | | 129 | | finally |
| | | 130 | | { |
| | 0 | 131 | | Complete(); |
| | | 132 | | } |
| | 0 | 133 | | } |
| | | 134 | | |
| | | 135 | | public void Complete() |
| | | 136 | | { |
| | 0 | 137 | | _completionTcs.TrySetResult(null); |
| | | 138 | | |
| | 0 | 139 | | _connectionClosingCts.Dispose(); |
| | 0 | 140 | | } |
| | | 141 | | } |
| | | 142 | | } |