diff --git a/src/libs/H.Formatters.Inferno/H.Formatters.Inferno.csproj b/src/libs/H.Formatters.Inferno/H.Formatters.Inferno.csproj index d56338c..e8c07b0 100644 --- a/src/libs/H.Formatters.Inferno/H.Formatters.Inferno.csproj +++ b/src/libs/H.Formatters.Inferno/H.Formatters.Inferno.csproj @@ -29,6 +29,7 @@ + diff --git a/src/libs/H.Formatters.Inferno/PipeServerExtensions.cs b/src/libs/H.Formatters.Inferno/PipeServerExtensions.cs index a43794c..39b2b79 100644 --- a/src/libs/H.Formatters.Inferno/PipeServerExtensions.cs +++ b/src/libs/H.Formatters.Inferno/PipeServerExtensions.cs @@ -1,5 +1,7 @@ using System.Diagnostics; +using System.IO.Pipes; using H.Pipes; +using H.Pipes.AccessControl; using H.Pipes.Extensions; namespace H.Formatters; @@ -19,7 +21,8 @@ public static class PipeServerExtensions [System.Runtime.Versioning.SupportedOSPlatform("windows")] public static void EnableEncryption( this IPipeServer server, - Action? exceptionAction = null) + Action? exceptionAction = null, + PipeSecurity? pipeSecurity = null) { server = server ?? throw new ArgumentNullException(nameof(server)); server.ClientConnected += async (_, args) => @@ -31,6 +34,11 @@ public static void EnableEncryption( var pipeName = $"{args.Connection.PipeName}_Inferno"; var server = new SingleConnectionPipeServer(pipeName, args.Connection.Formatter); + + if (pipeSecurity != null) + { + server.SetPipeSecurity(pipeSecurity); + } server.ExceptionOccurred += (_, args) => { Debug.WriteLine($"{nameof(EnableEncryption)} server returns exception: {args.Exception}"); diff --git a/src/libs/H.Pipes.AccessControl/PipeServerExtensions.cs b/src/libs/H.Pipes.AccessControl/PipeServerExtensions.cs index fcee94b..ea03031 100644 --- a/src/libs/H.Pipes.AccessControl/PipeServerExtensions.cs +++ b/src/libs/H.Pipes.AccessControl/PipeServerExtensions.cs @@ -11,7 +11,7 @@ public static class PipeServerExtensions { /// /// Sets 's for each that will be created by
- /// Overrides + /// Overrides and ///
/// /// @@ -22,8 +22,9 @@ public static void SetPipeSecurity(this IPipeServer server, PipeSecurity p { server = server ?? throw new ArgumentNullException(nameof(server)); pipeSecurity = pipeSecurity ?? throw new ArgumentNullException(nameof(pipeSecurity)); - - server.CreatePipeStreamFunc = pipeName => NamedPipeServerStreamConstructors.New( +#if NET6_0_OR_GREATER + server.CreatePipeStreamFunc = pipeName => + NamedPipeServerStreamAcl.Create( pipeName: pipeName, direction: PipeDirection.InOut, maxNumberOfServerInstances: 1, @@ -31,7 +32,21 @@ public static void SetPipeSecurity(this IPipeServer server, PipeSecurity p options: PipeOptions.Asynchronous | PipeOptions.WriteThrough, inBufferSize: 0, outBufferSize: 0, - pipeSecurity: pipeSecurity); + pipeSecurity); + + //server.c +#else + server.CreatePipeStreamFunc = pipeName => + NamedPipeServerStreamConstructors.New( + pipeName: pipeName, + direction: PipeDirection.InOut, + maxNumberOfServerInstances: 1, + transmissionMode: PipeTransmissionMode.Byte, + options: PipeOptions.Asynchronous | PipeOptions.WriteThrough, + inBufferSize: 0, + outBufferSize: 0, + pipeSecurity: pipeSecurity); +#endif } /// diff --git a/src/libs/H.Pipes/Factories/PipeServerFactory.cs b/src/libs/H.Pipes/Factories/PipeServerFactory.cs index 252afe4..3e94157 100644 --- a/src/libs/H.Pipes/Factories/PipeServerFactory.cs +++ b/src/libs/H.Pipes/Factories/PipeServerFactory.cs @@ -1,4 +1,6 @@ using System.IO.Pipes; +using System.Security.AccessControl; +using System.Security.Principal; namespace H.Pipes.Factories; @@ -44,7 +46,23 @@ public static async Task CreateAndWaitAsync(string pipeNa /// public static NamedPipeServerStream Create(string pipeName) { - return new NamedPipeServerStream( +#if NET6_0_OR_GREATER + +#pragma warning disable CA1416 + return NamedPipeServerStreamAcl.Create( + pipeName: pipeName, + direction: PipeDirection.InOut, + maxNumberOfServerInstances: 1, + transmissionMode: PipeTransmissionMode.Byte, + options: PipeOptions.Asynchronous | PipeOptions.WriteThrough, + inBufferSize: 0, + outBufferSize: 0, + null, + HandleInheritability.None, + (PipeAccessRights)0); +#pragma warning restore CA1416 +#else + return new NamedPipeServerStream( pipeName: pipeName, direction: PipeDirection.InOut, maxNumberOfServerInstances: 1, @@ -52,5 +70,7 @@ public static NamedPipeServerStream Create(string pipeName) options: PipeOptions.Asynchronous | PipeOptions.WriteThrough, inBufferSize: 0, outBufferSize: 0); +#endif + } } diff --git a/src/libs/H.Pipes/IO/PipeStreamWriter.cs b/src/libs/H.Pipes/IO/PipeStreamWriter.cs index 33c2dbb..e880c0c 100644 --- a/src/libs/H.Pipes/IO/PipeStreamWriter.cs +++ b/src/libs/H.Pipes/IO/PipeStreamWriter.cs @@ -20,7 +20,7 @@ public sealed class PipeStreamWriter : IDisposable /// Gets the underlying PipeStream object. /// private PipeStream BaseStream { get; } - private SemaphoreSlim SemaphoreSlim { get; } = new SemaphoreSlim(1, 1); + private SemaphoreSlim SemaphoreSlim { get; } = new(1, 1); #endregion