| | | 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.Collections.Generic; |
| | | 6 | | using System.Runtime.Versioning; |
| | | 7 | | using System.Threading; |
| | | 8 | | using System.Threading.Tasks; |
| | | 9 | | using CoreWCF.Runtime; |
| | | 10 | | using Microsoft.Extensions.Logging; |
| | | 11 | | |
| | | 12 | | namespace CoreWCF.Channels |
| | | 13 | | { |
| | | 14 | | [SupportedOSPlatform("windows")] |
| | | 15 | | internal class NamedPipeTransportManager |
| | | 16 | | { |
| | | 17 | | private readonly ILogger<NamedPipeTransportManager> _logger; |
| | | 18 | | private readonly ILoggerFactory _loggerFactory; |
| | | 19 | | private readonly Dictionary<Uri, NamedPipeListener> _listeners; |
| | | 20 | | |
| | 0 | 21 | | public NamedPipeTransportManager(ILoggerFactory loggerFactory) |
| | | 22 | | { |
| | 0 | 23 | | _loggerFactory = loggerFactory; |
| | 0 | 24 | | _logger = _loggerFactory.CreateLogger<NamedPipeTransportManager>(); |
| | 0 | 25 | | _listeners = new Dictionary<Uri, NamedPipeListener>(); |
| | 0 | 26 | | } |
| | | 27 | | |
| | | 28 | | internal async Task BindAsync(List<NamedPipeListenOptions> listenOptions, CancellationToken cancellationToken) |
| | | 29 | | { |
| | | 30 | | Fx.Assert(listenOptions.Count > 0, "We must have at least one listen option to bind to"); |
| | 0 | 31 | | foreach (var listenOption in listenOptions) |
| | | 32 | | { |
| | 0 | 33 | | var listener = new NamedPipeListener(listenOption); |
| | 0 | 34 | | await listener.BindAsync(cancellationToken); |
| | 0 | 35 | | listener.StartAccepting(); |
| | 0 | 36 | | _logger.LogInformation("Started accepting new NetNamedPipe connections for uri {baseUri}", listenOption. |
| | 0 | 37 | | _listeners.Add(listenOption.BaseAddress, listener); |
| | 0 | 38 | | } |
| | 0 | 39 | | } |
| | | 40 | | |
| | | 41 | | internal async Task StopAsync(CancellationToken cancellationToken) |
| | | 42 | | { |
| | 0 | 43 | | foreach(var pair in _listeners) |
| | | 44 | | { |
| | 0 | 45 | | var listenerBaseAddress = pair.Key; |
| | 0 | 46 | | var listener = pair.Value; |
| | 0 | 47 | | _logger.LogInformation("Stopping NetNamedPipe for uri {baseUri}", listenerBaseAddress); |
| | | 48 | | // Might want to consider not awaiting, but collecting Task's in array and then calling |
| | | 49 | | // Task.WhenAll as this currently stops them sequentially. This is probably fine as |
| | | 50 | | // stopping should be a quick action. |
| | 0 | 51 | | await listener.StopAsync(cancellationToken); |
| | | 52 | | } |
| | 0 | 53 | | } |
| | | 54 | | } |
| | | 55 | | } |