public async Task AnonymousPipeWriteViaFileStream(bool asyncWrites) { using (var server = new AnonymousPipeServerStream(PipeDirection.In)) { Task serverTask = Task.Run(() => { for (int i = 0; i < 6; i++) Assert.Equal(i, server.ReadByte()); }); using (var client = new FileStream(new SafeFileHandle(server.ClientSafePipeHandle.DangerousGetHandle(), false), FileAccess.Write, bufferSize: 3)) { var data = new[] { new byte[] { 0, 1 }, new byte[] { 2, 3 }, new byte[] { 4, 5 } }; foreach (byte[] arr in data) { if (asyncWrites) await client.WriteAsync(arr, 0, arr.Length); else client.Write(arr, 0, arr.Length); } } await serverTask; } }
public void PingPong() { // Create two anonymous pipes, one for each direction of communication. // Then spawn another process to communicate with. using (var outbound = new AnonymousPipeServerStream(PipeDirection.Out, HandleInheritability.Inheritable)) using (var inbound = new AnonymousPipeServerStream(PipeDirection.In, HandleInheritability.Inheritable)) using (var remote = RemoteInvoke(PingPong_OtherProcess, outbound.GetClientHandleAsString(), inbound.GetClientHandleAsString())) { // Close our local copies of the handles now that we've passed them of to the other process outbound.DisposeLocalCopyOfClientHandle(); inbound.DisposeLocalCopyOfClientHandle(); // Ping-pong back and forth by writing then reading bytes one at a time. for (byte i = 0; i < 10; i++) { outbound.WriteByte(i); int received = inbound.ReadByte(); Assert.Equal(i, received); } } }
public void TestIPCProcess() { Process p = CreateProcess("ipc"); using (var outbound = new AnonymousPipeServerStream(PipeDirection.Out, HandleInheritability.Inheritable)) using (var inbound = new AnonymousPipeServerStream(PipeDirection.In, HandleInheritability.Inheritable)) { p.StartInfo.Arguments += " " + outbound.GetClientHandleAsString() + " " + inbound.GetClientHandleAsString(); p.Start(); outbound.DisposeLocalCopyOfClientHandle(); inbound.DisposeLocalCopyOfClientHandle(); for (byte i = 0; i < 10; i++) { outbound.WriteByte(i); int received = inbound.ReadByte(); Assert.Equal(i, received); } Assert.True(p.WaitForExit(WaitInMS)); Assert.Equal(SuccessExitCode, p.ExitCode); } }
public static void ServerWriteOnlyThrows() { using (AnonymousPipeServerStream server = new AnonymousPipeServerStream(PipeDirection.Out)) { Assert.Throws<NotSupportedException>(() => server.Read(new byte[5], 0, 5)); Assert.Throws<NotSupportedException>(() => server.ReadByte()); Assert.Throws<NotSupportedException>(() => server.InBufferSize); Assert.ThrowsAsync<NotSupportedException>(() => server.ReadAsync(new byte[5], 0, 5)); } }