static int Main(string[] args) { if (args.Length > 0) { if (args[0].Equals("infinite")) { // To avoid potential issues with orphaned processes (say, the test exits before // exiting the process), we'll say "infinite" is actually 100 seconds. Sleep(100 * 1000); } else if (args[0].Equals("error")) { Console.Error.WriteLine(Name + " started error stream"); Console.Error.WriteLine(Name + " closed error stream"); Sleep(); } else if (args[0].Equals("input")) { string str = Console.ReadLine(); } else if (args[0].Equals("stream")) { Console.WriteLine(Name + " started"); Console.WriteLine(Name + " closed"); Sleep(); } else if (args[0].Equals("byteAtATime")) { var stdout = Console.OpenStandardOutput(); var bytes = new byte[] { 97, 0 }; //Encoding.Unicode.GetBytes("a"); for (int i = 0; i != bytes.Length; ++i) { stdout.WriteByte(bytes[i]); stdout.Flush(); Sleep(100); } } else if (args[0].Equals("ipc")) { using (var inbound = new AnonymousPipeClientStream(PipeDirection.In, args[1])) using (var outbound = new AnonymousPipeClientStream(PipeDirection.Out, args[2])) { // Echo 10 bytes from inbound to outbound for (int i = 0; i < 10; i++) { int b = inbound.ReadByte(); outbound.WriteByte((byte)b); } } } else { Console.WriteLine(string.Join(" ", args)); } } return(100); }
/// <summary> /// Sends a goodbye message (a zero byte) and closes this side of the pipe. /// </summary> public void Dispose() { if (!_disposed) { _disposed = true; _client.WriteByte(0); _writer.Dispose(); _client.Dispose(); } }
private static int PingPong_OtherProcess(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(SuccessExitCode); }
private static int PingPong_OtherProcess(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 SuccessExitCode; }
private static void AnonymousPipeClientStreamEg(string[] args) { /* * Check the server side of this example similar to NamedPipeServer. * */ /* * AnonymousPipe is similar to NamedPipe, which allows two processes * to communicate with each other, with difference being that * * NamedPipes are bidirectional and AnonymousPipe are unidirectional, * were the server sends a request to the client, and it seems like * the client process is a dependent process. * */ /* * NamedPipe works by looking for a process with the same matching the * assigned name, were as the AnonPipe uses the ID (PID) to find the * process. * */ try { var outId = args[0]; var inId = args[1]; Console.WriteLine(outId); Console.WriteLine(inId); using (var inPipe = new AnonymousPipeClientStream(PipeDirection.In, inId)) using (var outPipe = new AnonymousPipeClientStream(PipeDirection.Out, outId)) { for (int i = 0; i < 1000; i++) { Console.WriteLine("R: " + inPipe.ReadByte()); var number = new Random().Next(0, 150); Console.WriteLine("S: " + number); inPipe.WriteByte((byte)number); } } } catch (Exception exception) { Console.WriteLine(exception); } }
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); } }