public void ClientClosesPipe_ServerReceivesEof() { using (var pipe = new AnonymousPipeServerStream(PipeDirection.In, HandleInheritability.Inheritable)) using (var remote = RemoteExecutor.Invoke(new Action <string>(ChildFunc), pipe.GetClientHandleAsString(), new RemoteInvokeOptions { CheckExitCode = false })) { pipe.DisposeLocalCopyOfClientHandle(); for (int i = 1; i <= 5; i++) { Assert.Equal(i, pipe.ReadByte()); } Assert.Equal(-1, pipe.ReadByte()); remote.Process.Kill(); } void ChildFunc(string clientHandle) { using (var pipe = new AnonymousPipeClientStream(PipeDirection.Out, clientHandle)) { pipe.Write(new byte[] { 1, 2, 3, 4, 5 }, 0, 5); } Thread.CurrentThread.Join(); } }
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 SignalsProcessTree(Signal signal) { using var stdOutputPipe = new AnonymousPipeServerStream(PipeDirection.In); using var sut = CreateProcessTree(stdOutputPipe); switch (signal) { case Signal.Interrupt: sut.SignalInterrupt(); break; case Signal.Termination: sut.SignalTermination(); break; case Signal.Kill: sut.Kill(); break; default: throw new ArgumentException("should never be reached", nameof(signal)); } sut.WaitForExit(); // Check that the grand child has been killed. // If the grand child is still running, this read will block and eventually read 'E'. Assert.Equal(-1, stdOutputPipe.ReadByte()); Assert.NotEqual(0, sut.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); } }
public void DisposeTerminatesProcessTree() { using var stdOutputPipe = new AnonymousPipeServerStream(PipeDirection.In); using var sut = CreateProcessTree(stdOutputPipe); sut.Dispose(); // Check that the grand child has been killed. // If the grand child is still running, this read will block and eventually read 'E'. Assert.Equal(-1, stdOutputPipe.ReadByte()); }
public static void Main(string[] args) { try { // Debug. Console.WriteLine("Anonymous Pipes Server Begin: {0}", System.DateTime.Now.ToString()); // Path to client executable. string clientPath = @"C:\Source\AV\CSharp\Streams\AnonymousPipes\AnonymousPipesClient.exe"; HandleInheritability inherit = HandleInheritability.Inheritable; using (var svr = new AnonymousPipeServerStream(PipeDirection.Out, inherit)) using (var clt = new AnonymousPipeServerStream(PipeDirection.In, inherit)) { string svrID = svr.GetClientHandleAsString(); string cltID = clt.GetClientHandleAsString(); ProcessStartInfo prc = new ProcessStartInfo { FileName = clientPath, Arguments = svrID + " " + cltID, UseShellExecute = false }; Process.Start(prc); // var startInfo = new ProcessStartInfo(clientPath, svrID + " " + cltID); // startInfo.UseShellExecute = false; // Process p = Process.Start(startInfo); svr.DisposeLocalCopyOfClientHandle(); clt.DisposeLocalCopyOfClientHandle(); svr.WriteByte(101); Console.WriteLine($"Transmission received: {clt.ReadByte()}"); //prc.WaitForExit(); } } catch (Exception ex) { Console.WriteLine(ex.Message + Environment.NewLine + ex.StackTrace + Environment.NewLine + ex.Source); } // Debug. Console.WriteLine("Anonymous Pipes Client End: {0}", System.DateTime.Now.ToString()); // Await user input. Console.ReadLine(); }
public static void Main(string[] args) { try { // Debug. Console.WriteLine("Anonymous Pipes Client Begin: {0}", System.DateTime.Now.ToString()); //string cltID = args[0]; //string svrID = args[1]; // Debug. Console.WriteLine($"args.Length: {args.Length}"); // Debug. if (args.Length > 0) { for (int i = 0; i < args.Length; i++) { Console.WriteLine($"args[{i}]: {args[i].ToString()}"); } } // Debug. Console.WriteLine("Line #40"); using (var clt = new AnonymousPipeServerStream(PipeDirection.In, inherit)) using (var svr = new AnonymousPipeServerStream(PipeDirection.Out, inherit)) { // Debug. Console.WriteLine("AnonymousPipesClient.cs :: Line #46"); Console.WriteLine($"Transmission received: {clt.ReadByte()}"); svr.WriteByte(200); } } catch (Exception ex) { Console.WriteLine(ex.Message + Environment.NewLine + ex.StackTrace + Environment.NewLine + ex.Source); } // Debug. Console.WriteLine("Anonymous Pipes Client End: {0}", System.DateTime.Now.ToString()); }
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 Process_IPC() { 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 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 = RemoteExecutor.Invoke(new Func <string, string, int>(ChildFunc), 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); } } int ChildFunc(string inHandle, string outHandle) { // Create the clients associated with the supplied handles using (var inbound = new AnonymousPipeClientStream(PipeDirection.In, inHandle)) using (var outbound = new AnonymousPipeClientStream(PipeDirection.Out, outHandle)) { // Repeatedly read then write a byte from and to the server for (int i = 0; i < 10; i++) { int b = inbound.ReadByte(); outbound.WriteByte((byte)b); } } return(RemoteExecutor.SuccessExitCode); } }
private static IChildProcess CreateProcessTree(AnonymousPipeServerStream stdOutputPipe) { var si = new ChildProcessStartInfo( TestUtil.DotnetCommandName, TestUtil.TestChildPath, "SpawnAndWait", TestUtil.DotnetCommandName, TestUtil.TestChildPath, "EchoAndSleepAndEcho", "S", SleepMilliseconds, "Exited") { StdInputRedirection = InputRedirection.NullDevice, StdOutputRedirection = OutputRedirection.Handle, StdOutputHandle = stdOutputPipe.ClientSafePipeHandle, StdErrorRedirection = OutputRedirection.NullDevice, }; var p = ChildProcess.Start(si); try { stdOutputPipe.DisposeLocalCopyOfClientHandle(); // Wait for the grand child to echo. Assert.Equal((byte)'S', stdOutputPipe.ReadByte()); } catch { p.Dispose(); throw; } return(p); }
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.Throws<NotSupportedException>(() => NotReachable(server.ReadAsync(new byte[5], 0, 5))); } }