本文整理汇总了C#中NamedPipeServerStream.WaitForConnectionAsync方法的典型用法代码示例。如果您正苦于以下问题:C# NamedPipeServerStream.WaitForConnectionAsync方法的具体用法?C# NamedPipeServerStream.WaitForConnectionAsync怎么用?C# NamedPipeServerStream.WaitForConnectionAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类NamedPipeServerStream
的用法示例。
在下文中一共展示了NamedPipeServerStream.WaitForConnectionAsync方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: PingPong_Async
public async Task PingPong_Async()
{
// Create names for two pipes
string outName = Path.GetRandomFileName();
string inName = Path.GetRandomFileName();
// Create the two named pipes, one for each direction, then create
// another process with which to communicate
using (var outbound = new NamedPipeServerStream(outName, PipeDirection.Out))
using (var inbound = new NamedPipeClientStream(".", inName, PipeDirection.In))
using (RemoteInvoke(PingPong_OtherProcess, outName, inName))
{
// Wait for both pipes to be connected
await Task.WhenAll(outbound.WaitForConnectionAsync(), inbound.ConnectAsync());
// Repeatedly write then read a byte to and from the other process
var data = new byte[1];
for (byte i = 0; i < 10; i++)
{
data[0] = i;
await outbound.WriteAsync(data, 0, data.Length);
data[0] = 0;
int received = await inbound.ReadAsync(data, 0, data.Length);
Assert.Equal(1, received);
Assert.Equal(i, data[0]);
}
}
}
示例2: RunAsClient_Windows
public async Task RunAsClient_Windows()
{
string pipeName = Path.GetRandomFileName();
using (var server = new NamedPipeServerStream(pipeName))
using (var client = new NamedPipeClientStream(".", pipeName, PipeDirection.InOut, PipeOptions.None, TokenImpersonationLevel.Impersonation))
{
Task serverTask = server.WaitForConnectionAsync();
client.Connect();
await serverTask;
bool ran = false;
server.RunAsClient(() => ran = true);
Assert.True(ran, "Expected delegate to have been invoked");
}
}
示例3: PingPong_OtherProcess
private static int PingPong_OtherProcess(string inName, string outName)
{
// Create pipes with the supplied names
using (var inbound = new NamedPipeClientStream(".", inName, PipeDirection.In))
using (var outbound = new NamedPipeServerStream(outName, PipeDirection.Out))
{
// Wait for the connections to be established
Task.WaitAll(inbound.ConnectAsync(), outbound.WaitForConnectionAsync());
// Repeatedly read then write bytes from and to the other process
for (int i = 0; i < 10; i++)
{
int b = inbound.ReadByte();
outbound.WriteByte((byte)b);
}
}
return SuccessExitCode;
}
示例4: PingPong_Sync
public void PingPong_Sync()
{
// Create names for two pipes
string outName = Path.GetRandomFileName();
string inName = Path.GetRandomFileName();
// Create the two named pipes, one for each direction, then create
// another process with which to communicate
using (var outbound = new NamedPipeServerStream(outName, PipeDirection.Out))
using (var inbound = new NamedPipeClientStream(".", inName, PipeDirection.In))
using (RemoteInvoke(PingPong_OtherProcess, outName, inName))
{
// Wait for both pipes to be connected
Task.WaitAll(outbound.WaitForConnectionAsync(), inbound.ConnectAsync());
// Repeatedly write then read a byte to and from the other process
for (byte i = 0; i < 10; i++)
{
outbound.WriteByte(i);
int received = inbound.ReadByte();
Assert.Equal(i, received);
}
}
}
示例5: NameTooLong_MaxLengthPerPlatform
public void NameTooLong_MaxLengthPerPlatform()
{
// Increase a name's length until it fails
ArgumentOutOfRangeException e = null;
string name = Path.GetRandomFileName();
for (int i = 0; ; i++)
{
try
{
name += 'c';
using (var s = new NamedPipeServerStream(name))
using (var c = new NamedPipeClientStream(name))
{
Task t = s.WaitForConnectionAsync();
c.Connect();
t.GetAwaiter().GetResult();
}
}
catch (ArgumentOutOfRangeException exc)
{
e = exc;
break;
}
}
Assert.NotNull(e);
Assert.NotNull(e.ActualValue);
// Validate the length was expected
string path = (string)e.ActualValue;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
Assert.Equal(108, path.Length);
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
Assert.Equal(104, path.Length);
}
else
{
Assert.InRange(path.Length, 92, int.MaxValue);
}
}
示例6: Unix_GetImpersonationUserName_Succeed
public async Task Unix_GetImpersonationUserName_Succeed()
{
string pipeName = GetUniquePipeName();
using (var server = new NamedPipeServerStream(pipeName))
using (var client = new NamedPipeClientStream(".", pipeName, PipeDirection.InOut, PipeOptions.None, TokenImpersonationLevel.Impersonation))
{
Task serverTask = server.WaitForConnectionAsync();
client.Connect();
await serverTask;
string name = server.GetImpersonationUserName();
Assert.NotNull(name);
Assert.False(string.IsNullOrWhiteSpace(name));
}
}
示例7: Windows_GetImpersonationUserName_Succeed
[PlatformSpecific(PlatformID.Windows)] // Win32 P/Invokes to verify the user name
public async Task Windows_GetImpersonationUserName_Succeed()
{
string pipeName = GetUniquePipeName();
using (var server = new NamedPipeServerStream(pipeName))
{
using (var client = new NamedPipeClientStream(".", pipeName, PipeDirection.InOut, PipeOptions.None, TokenImpersonationLevel.Impersonation))
{
string expectedUserName;
Task serverTask = server.WaitForConnectionAsync();
client.Connect();
await serverTask;
Assert.True(Interop.TryGetImpersonationUserName(server.SafePipeHandle, out expectedUserName), "GetNamedPipeHandleState failed");
Assert.Equal(expectedUserName, server.GetImpersonationUserName());
}
}
}
示例8: Unix_MultipleServerDisposal_DoesntDeletePipe
public void Unix_MultipleServerDisposal_DoesntDeletePipe()
{
// Test for when multiple servers are created and linked to the same FIFO. The original ServerStream
// that created the FIFO is disposed. The other servers should still be valid and useable.
string serverName = GetUniquePipeName();
var server1 = new NamedPipeServerStream(serverName, PipeDirection.In); // Creates the FIFO
var server2 = new NamedPipeServerStream(serverName, PipeDirection.In);
var client1 = new NamedPipeClientStream(".", serverName, PipeDirection.Out);
var client2 = new NamedPipeClientStream(".", serverName, PipeDirection.Out);
Task server1Task = server1.WaitForConnectionAsync(); // Opens a handle to the FIFO
Task server2Task = server2.WaitForConnectionAsync(); // Opens a handle to the same FIFO as server1
Task client1Task = client1.ConnectAsync();
Task.WaitAll(server1Task, server2Task, client1Task); // client1 connects to server1 AND server2
Assert.True(server1.IsConnected);
Assert.True(server2.IsConnected);
// Get rid of client1/server1 and make sure server2 isn't connected (so that it can connect to client2)
server1.Dispose();
client1.Dispose();
server2.Disconnect();
Assert.False(server2.IsConnected);
server2Task = server2.WaitForConnectionAsync(); // Opens a handle to the same FIFO
Task client2Task = client2.ConnectAsync();
Task.WaitAll(server2Task, client2Task); // Should not block!
Assert.True(server2.IsConnected);
}
示例9: ServerWaitForConnectThrows
public static async Task ServerWaitForConnectThrows()
{
using (NamedPipeServerStream server = new NamedPipeServerStream("unique3", PipeDirection.InOut, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous, 0, 0))
{
var ctx = new CancellationTokenSource();
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) // [ActiveIssue(812, PlatformID.AnyUnix)] - cancellation token after the operation has been initiated
{
Task serverWaitTimeout = server.WaitForConnectionAsync(ctx.Token);
ctx.Cancel();
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => serverWaitTimeout);
}
ctx.Cancel();
Assert.True(server.WaitForConnectionAsync(ctx.Token).IsCanceled);
}
}
示例10: ServerAfterDisconnectThrows
public static async Task ServerAfterDisconnectThrows()
{
using (NamedPipeServerStream server = new NamedPipeServerStream("unique3", PipeDirection.Out, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous))
using (NamedPipeClientStream client = new NamedPipeClientStream(".", "unique3", PipeDirection.In))
{
Task clientConnect = client.ConnectAsync();
server.WaitForConnection();
await clientConnect;
Assert.Throws<InvalidOperationException>(() => server.IsMessageComplete);
Assert.Throws<InvalidOperationException>(() => server.WaitForConnection());
await Assert.ThrowsAsync<InvalidOperationException>(() => server.WaitForConnectionAsync());
server.Disconnect();
Assert.Throws<InvalidOperationException>(() => server.Disconnect()); // double disconnect
AfterDisconnectWriteOnlyPipeThrows(server);
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) // on Unix, InOut doesn't result in the same Disconnect-based errors due to allowing for other connections
{
using (NamedPipeServerStream server = new NamedPipeServerStream("unique3", PipeDirection.InOut, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous))
using (NamedPipeClientStream client = new NamedPipeClientStream("unique3"))
{
Task clientConnect = client.ConnectAsync();
server.WaitForConnection();
await clientConnect;
Assert.Throws<InvalidOperationException>(() => server.IsMessageComplete);
Assert.Throws<InvalidOperationException>(() => server.WaitForConnection());
await Assert.ThrowsAsync<InvalidOperationException>(() => server.WaitForConnectionAsync());
server.Disconnect();
Assert.Throws<InvalidOperationException>(() => server.Disconnect()); // double disconnect
AfterDisconnectReadWritePipeThrows(server);
}
}
}