| | | 1 | | using System; |
| | | 2 | | using CoreWCF.Channels; |
| | | 3 | | using CoreWCF.Channels.Framing; |
| | | 4 | | using CoreWCF.Configuration; |
| | | 5 | | using Microsoft.AspNetCore.Server.Kestrel.Core; |
| | | 6 | | using Microsoft.Extensions.Logging; |
| | | 7 | | using Microsoft.Extensions.Options; |
| | | 8 | | using System.Collections.Generic; |
| | | 9 | | using Microsoft.AspNetCore.Connections; |
| | | 10 | | using System.IO; |
| | | 11 | | |
| | | 12 | | namespace CoreWCF.Channels |
| | | 13 | | { |
| | | 14 | | internal class UnixDomainSocketFramingOptionsSetup : IConfigureOptions<KestrelServerOptions> |
| | | 15 | | { |
| | | 16 | | private readonly ILogger<UnixDomainSocketOptions> _logger; |
| | | 17 | | private readonly ILogger<FramingConnection> _framingConnectionLogger; |
| | | 18 | | private readonly IServiceProvider _serviceProvider; |
| | | 19 | | private readonly UnixDomainSocketOptions _options; |
| | | 20 | | private readonly IServiceBuilder _serviceBuilder; |
| | | 21 | | |
| | 3 | 22 | | public UnixDomainSocketFramingOptionsSetup(IOptions<UnixDomainSocketOptions> options, IServiceBuilder serviceBui |
| | | 23 | | { |
| | 3 | 24 | | _options = options.Value ?? new UnixDomainSocketOptions(); |
| | 3 | 25 | | _serviceBuilder = serviceBuilder; |
| | 3 | 26 | | _logger = logger; |
| | 3 | 27 | | _framingConnectionLogger = framingConnectionLogger; |
| | 3 | 28 | | _serviceProvider = serviceProvider; |
| | 3 | 29 | | } |
| | | 30 | | |
| | 12 | 31 | | public List<ListenOptions> ListenOptions { get; } = new List<ListenOptions>(); |
| | | 32 | | |
| | 6 | 33 | | public bool AttachUDS { get; set; } |
| | | 34 | | |
| | | 35 | | public void Configure(KestrelServerOptions options) |
| | | 36 | | { |
| | 3 | 37 | | if (AttachUDS) |
| | | 38 | | { |
| | 3 | 39 | | options.ApplicationServices = _serviceProvider; |
| | 12 | 40 | | foreach (UnixDomainSocketListenOptions listenOption in _options.CodeBackedListenOptions) |
| | | 41 | | { |
| | 3 | 42 | | string filePath = listenOption.FilePath; |
| | | 43 | | try |
| | | 44 | | { |
| | 3 | 45 | | if (File.Exists(filePath)) |
| | | 46 | | { |
| | 0 | 47 | | File.Delete(filePath); |
| | | 48 | | } |
| | 3 | 49 | | } |
| | 0 | 50 | | catch(Exception ex) |
| | | 51 | | { |
| | 0 | 52 | | throw; |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | |
| | | 56 | | //Provide Path here |
| | 3 | 57 | | options.ListenUnixSocket(filePath, builder => |
| | 3 | 58 | | { |
| | 3 | 59 | | // builder.Use(ConvertExceptionsAndAddLogging); |
| | 3 | 60 | | builder.UseConnectionHandler<NetMessageFramingConnectionHandler>(); |
| | 3 | 61 | | // Save the ListenOptions to be able to get final port number for adding BaseAddresses later |
| | 3 | 62 | | ListenOptions.Add(builder); |
| | 6 | 63 | | }); |
| | | 64 | | } |
| | | 65 | | |
| | 3 | 66 | | _serviceBuilder.Opening += OnServiceBuilderOpening; |
| | | 67 | | } |
| | 3 | 68 | | } |
| | | 69 | | |
| | | 70 | | |
| | | 71 | | private ConnectionDelegate ConvertExceptionsAndAddLogging(ConnectionDelegate next) |
| | | 72 | | { |
| | 0 | 73 | | return (ConnectionContext context) => |
| | 0 | 74 | | { |
| | 0 | 75 | | var logger = new ConnectionIdWrappingLogger(_framingConnectionLogger, context.ConnectionId); |
| | 0 | 76 | | context.Features.Set<ILogger>(logger); |
| | 0 | 77 | | |
| | 0 | 78 | | //TODO: Add a public api mechanism to enable connection logging in RELEASE build |
| | 0 | 79 | | #if DEBUG |
| | 0 | 80 | | context.Transport = new UnixDomainSocketExceptionConvertingDuplexPipe(new LoggingDuplexPipe(context.Tran |
| | 0 | 81 | | #else |
| | 0 | 82 | | context.Transport = new UnixDomainSocketExceptionConvertingDuplexPipe(context.Transport); |
| | 0 | 83 | | #endif |
| | 0 | 84 | | return next(context); |
| | 0 | 85 | | }; |
| | | 86 | | } |
| | | 87 | | |
| | | 88 | | private void OnServiceBuilderOpening(object sender, EventArgs e) |
| | | 89 | | { |
| | 3 | 90 | | UpdateServiceBuilderBaseAddresses(); |
| | 3 | 91 | | } |
| | | 92 | | |
| | | 93 | | internal void UpdateServiceBuilderBaseAddresses() |
| | | 94 | | { |
| | 12 | 95 | | foreach (ListenOptions listenOptions in ListenOptions) |
| | | 96 | | { |
| | 3 | 97 | | string address = listenOptions.SocketPath; |
| | 3 | 98 | | var baseAddress = new Uri($"net.uds://{address}/"); |
| | 3 | 99 | | _logger.LogDebug("Adding base address {baseAddress} to ServiceBuilderOptions", baseAddress); |
| | 3 | 100 | | _serviceBuilder.BaseAddresses.Add(baseAddress); |
| | | 101 | | } |
| | | 102 | | |
| | 3 | 103 | | ListenOptions.Clear(); |
| | 3 | 104 | | } |
| | | 105 | | } |
| | | 106 | | } |
| | | 107 | | |