Skip to content

Commit

Permalink
feat: Added PipeClient.CreatePipeStreamForConnectionFunc.
Browse files Browse the repository at this point in the history
  • Loading branch information
HavenDV committed Dec 8, 2023
1 parent caf0005 commit eae9191
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 35 deletions.
4 changes: 2 additions & 2 deletions src/libs/H.Pipes/Factories/PipeClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ public static async Task<NamedPipeClientStream> CreateAndConnectAsync(
public static NamedPipeClientStream Create(string pipeName, string serverName)
{
return new NamedPipeClientStream(
serverName,
pipeName,
serverName: serverName,
pipeName: pipeName,
direction: PipeDirection.InOut,
options: PipeOptions.Asynchronous | PipeOptions.WriteThrough);
}
Expand Down
12 changes: 10 additions & 2 deletions src/libs/H.Pipes/PipeClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ public bool IsConnecting

/// <inheritdoc/>
public Func<string, string, NamedPipeClientStream>? CreatePipeStreamFunc { get; set; }

/// <summary>
/// If set, used instead of CreatePipeStreamFunc for connections
/// </summary>
public Func<string, string, NamedPipeClientStream>? CreatePipeStreamForConnectionFunc { get; set; }

/// <inheritdoc/>
public string PipeName { get; }
Expand Down Expand Up @@ -177,8 +182,11 @@ public async Task ConnectAsync(CancellationToken cancellationToken = default)

// Connect to the actual data pipe
#pragma warning disable CA2000 // Dispose objects before losing scope
var dataPipe = await PipeClientFactory
.CreateAndConnectAsync(connectionPipeName, ServerName, CreatePipeStreamFunc, cancellationToken)
var dataPipe = await PipeClientFactory.CreateAndConnectAsync(
pipeName: connectionPipeName,
serverName: ServerName,
func: CreatePipeStreamForConnectionFunc ?? CreatePipeStreamFunc,
cancellationToken: cancellationToken)
#pragma warning restore CA2000 // Dispose objects before losing scope
.ConfigureAwait(false);

Expand Down
45 changes: 14 additions & 31 deletions src/tests/H.Pipes.Tests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,37 +143,14 @@ public async Task WriteOnlyServer()
Console.WriteLine($"PipeName: {pipeName}");

await using var server = new PipeServer<byte[]>(pipeName);
if (OperatingSystem.IsWindows())
{
var pipeSecurity = new PipeSecurity();
pipeSecurity.AddAccessRule(new PipeAccessRule(
new SecurityIdentifier(WellKnownSidType.WorldSid, null),
PipeAccessRights.ReadWrite,
AccessControlType.Allow));

#pragma warning disable CA1416
server.CreatePipeStreamFunc = name => NamedPipeServerStreamAcl.Create(
pipeName: name,
direction: PipeDirection.Out,
maxNumberOfServerInstances: 1,
transmissionMode: PipeTransmissionMode.Byte,
options: PipeOptions.Asynchronous | PipeOptions.WriteThrough,
inBufferSize: 0,
outBufferSize: 0,
pipeSecurity: pipeSecurity);
#pragma warning restore CA1416
}
else
{
server.CreatePipeStreamFunc = static pipeName => new NamedPipeServerStream(
pipeName: pipeName,
direction: PipeDirection.Out,
maxNumberOfServerInstances: 1,
transmissionMode: PipeTransmissionMode.Byte,
options: PipeOptions.Asynchronous | PipeOptions.WriteThrough,
inBufferSize: 0,
outBufferSize: 0);
}
server.CreatePipeStreamFunc = static pipeName => new NamedPipeServerStream(
pipeName: pipeName,
direction: PipeDirection.Out,
maxNumberOfServerInstances: 1,
transmissionMode: PipeTransmissionMode.Byte,
options: PipeOptions.Asynchronous | PipeOptions.WriteThrough,
inBufferSize: 0,
outBufferSize: 0);

server.ClientConnected += async (_, args) =>
{
Expand Down Expand Up @@ -237,6 +214,12 @@ await server.WriteAsync(new byte[]

var isClientReceivedMessage = new TaskCompletionSource<bool>();
await using var client = new PipeClient<byte[]>(pipeName);
client.CreatePipeStreamForConnectionFunc = static (pipeName, serverName) => new NamedPipeClientStream(
serverName: serverName,
pipeName: pipeName,
direction: PipeDirection.In,
options: PipeOptions.Asynchronous | PipeOptions.WriteThrough);

client.MessageReceived += (_, _) =>
{
_ = isClientReceivedMessage.TrySetResult(true);
Expand Down

0 comments on commit eae9191

Please sign in to comment.