static void Main() { Process pipeClient = new Process(); pipeClient.StartInfo.FileName = "AnonymousPipeClient.exe"; using (AnonymousPipeServerStream pipeServer = new AnonymousPipeServerStream(PipeDirection.Out, HandleInheritability.Inheritable)) { // Show that anonymous pipes do not support Message mode. try { Console.WriteLine("[SERVER] Setting ReadMode to \"Message\"."); pipeServer.ReadMode = PipeTransmissionMode.Message; } catch (NotSupportedException e) { Console.WriteLine("[SERVER] Exception:\n {0}", e.Message); } Console.WriteLine("[SERVER] Current TransmissionMode: {0}.", pipeServer.TransmissionMode); // Pass the client process a handle to the server. pipeClient.StartInfo.Arguments = pipeServer.GetClientHandleAsString(); pipeClient.StartInfo.UseShellExecute = false; pipeClient.Start(); pipeServer.DisposeLocalCopyOfClientHandle(); try { // Read user input and send that to the client process. using (StreamWriter sw = new StreamWriter(pipeServer)) { sw.AutoFlush = true; // Send a 'sync message' and wait for client to receive it. sw.WriteLine("SYNC"); pipeServer.WaitForPipeDrain(); // Send the console input to the client process. Console.Write("[SERVER] Enter text: "); sw.WriteLine(Console.ReadLine()); } } // Catch the IOException that is raised if the pipe is broken // or disconnected. catch (IOException e) { Console.WriteLine("[SERVER] Error: {0}", e.Message); } } pipeClient.WaitForExit(); pipeClient.Close(); Console.WriteLine("[SERVER] Client quit. Server terminating."); }
public static void Main() { Process pipeClient = new Process(); pipeClient.StartInfo.FileName = "PipeClient.exe"; using (var pipeServer = new AnonymousPipeServerStream(PipeDirection.Out, HandleInheritability.Inheritable)) { try { Console.Write("[SERVER] Setting ReadMode to \"Message\"."); pipeServer.ReadMode = PipeTransmissionMode.Message; } catch (NotSupportedException e) { Console.WriteLine("[SERVER] Exception:\n {0}", e.Message); } Console.WriteLine("[SERVER] Current TransmissionMode: {0}.", pipeServer.TransmissionMode); pipeClient.StartInfo.Arguments = pipeServer.GetClientHandleAsString(); pipeClient.StartInfo.UseShellExecute = false; pipeClient.Start(); pipeServer.DisposeLocalCopyOfClientHandle(); try { using (var sw = new StreamWriter(pipeServer)) { sw.AutoFlush = true; sw.WriteLine("SYNC"); // Send a 'sync message' and wait for client to receive it. pipeServer.WaitForPipeDrain(); Console.Write("[SERVER] Enter text: "); sw.WriteLine(Console.ReadLine()); } } catch (IOException e) // if the pipe is broken or disconnected. { Console.WriteLine("[SERVER] Error: {0}", e.Message); } } pipeClient.WaitForExit(); pipeClient.Close(); Console.WriteLine("[SERVER] Client quit. Server terminating."); }
static void Relaunch() { var tempdir = Path.Combine( Path.GetTempPath(), Path.GetRandomFileName() ); Directory.CreateDirectory( tempdir ); var xcopy_args = String.Format( "/E \"{0}\" \"{1}\\\"", Path.GetDirectoryName(OriginalPath).Replace("file:\\",""), tempdir ); var pcopy = Process.Start( "xcopy", xcopy_args ); if (!pcopy.WaitForExit(10000)) throw new InvalidOperationException("Relaunch copy hanged!"); Win32.MoveFileEx( tempdir, null, Win32.MoveFileFlags.MOVEFILE_DELAY_UNTIL_REBOOT) ; var newexe = Path.Combine( tempdir, Path.GetFileName(OriginalPath) ); if ( Connection != null && Connection.Socket.Connected ) { var pipe = new AnonymousPipeServerStream( PipeDirection.Out, HandleInheritability.Inheritable ); var pipename = pipe.GetClientHandleAsString(); var pcloneinfo = new ProcessStartInfo( newexe, string.Format( "--original={0} --pipe={1}", OriginalPath, pipename ) ); pcloneinfo.UseShellExecute = false; var pclone = Process.Start(pcloneinfo); var sockinfo = Connection.Socket.DuplicateAndClose(pclone.Id).ProtocolInformation; pipe.Write( sockinfo, 0, sockinfo.Length ); pipe.WaitForPipeDrain(); pipe.Close(); } else { var pclone = Process.Start( newexe, string.Format( "--original={0}", OriginalPath ) ); } }
public Relay(Options options) { log.Info("creating relay host"); log.Debug("create interprocess input pipe"); pipeIn = new AnonymousPipeServerStream(PipeDirection.In, HandleInheritability.Inheritable); pipeReader = new StreamReader(pipeIn); log.Debug("create interprocess output pipe"); pipeOut = new AnonymousPipeServerStream(PipeDirection.Out, HandleInheritability.Inheritable); pipeWriter = new StreamWriter(pipeOut); log.Debug("create processor host"); processor = new Process(); processor.StartInfo.FileName = System.Reflection.Assembly.GetExecutingAssembly().Location; log.DebugFormat("StartInfo.FileName={0}", processor.StartInfo.FileName); processor.StartInfo.Arguments = String.Format("--pipe-in={0} --pipe-out={1} process", pipeOut.GetClientHandleAsString(), pipeIn.GetClientHandleAsString()); log.DebugFormat("StartInfo.Arguments={0}", processor.StartInfo.Arguments); processor.StartInfo.UseShellExecute = false; log.Debug("start processor host"); try { processor.Start(); } catch (Exception ex) { log.Fatal("Exception while starting processor host.", ex); throw ex; } log.DebugFormat("processor host process id : {0}", processor.Id); log.Debug("join processes into a job so processor host dies together with relay host"); job = new Job(); job.AddProcess(Process.GetCurrentProcess().Id); job.AddProcess(processor.Id); log.Debug("create native messaging ports"); portA = new Port(); portB = new Port(pipeIn, pipeOut); portsExceptions = new List<Exception>(); log.Debug("create stop event"); stop = new ManualResetEvent(false); log.Debug("synchronize processes"); string sync = "SYNC"; pipeWriter.WriteLine(sync); pipeWriter.Flush(); log.DebugFormat("sent {0}", sync); pipeOut.WaitForPipeDrain(); sync = pipeReader.ReadLine(); log.DebugFormat("received {0}", sync); log.Info("created relay host"); }
private void DoJITTest(TestAgent test, uint milestoneCount, uint iterationCount) { var outPipeServer = new AnonymousPipeServerStream(PipeDirection.Out, HandleInheritability.Inheritable); var inPipeServer = new AnonymousPipeServerStream(PipeDirection.In, HandleInheritability.Inheritable); var outClientHandler = outPipeServer.GetClientHandleAsString(); var inClientHandler = inPipeServer.GetClientHandleAsString(); var workerProcess = new Process(); workerProcess.StartInfo.FileName = _jitesterPath; workerProcess.StartInfo.UseShellExecute = false; workerProcess.StartInfo.Arguments = String.Concat( outClientHandler, " ", inClientHandler, " ", milestoneCount, " ", iterationCount, " ", test.ContainerType.Assembly.Location); workerProcess.Start(); outPipeServer.DisposeLocalCopyOfClientHandle(); inPipeServer.DisposeLocalCopyOfClientHandle(); var binFormatter = new BinaryFormatter(); binFormatter.Serialize(outPipeServer, test); outPipeServer.WaitForPipeDrain(); var resObj = binFormatter.Deserialize(inPipeServer); var results = resObj as List<PassResult>; foreach (var result in results) { _resultHandler.AddResult(result); _progressHandler.PortionDone(); } }
public static void ServerPInvokeChecks() { // calling every API related to server and client to detect any bad PInvokes using (AnonymousPipeServerStream server = new AnonymousPipeServerStream(PipeDirection.Out)) { Task clientTask = Task.Run(() => StartClient(PipeDirection.In, server.ClientSafePipeHandle)); Assert.False(server.CanRead); Assert.False(server.CanSeek); Assert.False(server.CanTimeout); Assert.True(server.CanWrite); Assert.False(string.IsNullOrWhiteSpace(server.GetClientHandleAsString())); Assert.False(server.IsAsync); Assert.True(server.IsConnected); if (Interop.IsWindows) { Assert.Equal(0, server.OutBufferSize); } else { Assert.Throws<PlatformNotSupportedException>(() => server.OutBufferSize); } Assert.Equal(PipeTransmissionMode.Byte, server.ReadMode); Assert.NotNull(server.SafePipeHandle); Assert.Equal(PipeTransmissionMode.Byte, server.TransmissionMode); server.Write(new byte[] { 123 }, 0, 1); server.WriteAsync(new byte[] { 124 }, 0, 1).Wait(); server.Flush(); if (Interop.IsWindows) { server.WaitForPipeDrain(); } else { Assert.Throws<PlatformNotSupportedException>(() => server.WaitForPipeDrain()); } clientTask.Wait(); server.DisposeLocalCopyOfClientHandle(); } using (AnonymousPipeServerStream server = new AnonymousPipeServerStream(PipeDirection.In)) { Task clientTask = Task.Run(() => StartClient(PipeDirection.Out, server.ClientSafePipeHandle)); if (Interop.IsWindows) { Assert.Equal(4096, server.InBufferSize); } else { Assert.Throws<PlatformNotSupportedException>(() => server.InBufferSize); } byte[] readData = new byte[] { 0, 1 }; Assert.Equal(1, server.Read(readData, 0, 1)); Assert.Equal(1, server.ReadAsync(readData, 1, 1).Result); Assert.Equal(123, readData[0]); Assert.Equal(124, readData[1]); clientTask.Wait(); } }
public static void ServerPInvokeChecks() { // calling every API related to server and client to detect any bad PInvokes using (AnonymousPipeServerStream server = new AnonymousPipeServerStream(PipeDirection.Out)) { Task clientTask = StartClientAsync(PipeDirection.In, server.ClientSafePipeHandle); Console.WriteLine("server.CanRead = {0}", server.CanRead); Console.WriteLine("server.CanSeek = {0}", server.CanSeek); Console.WriteLine("server.CanTimeout = {0}", server.CanTimeout); Console.WriteLine("server.CanWrite = {0}", server.CanWrite); Console.WriteLine("server.GetClientHandleAsString() = {0}", server.GetClientHandleAsString()); Console.WriteLine("server.IsAsync = {0}", server.IsAsync); Console.WriteLine("server.IsConnected = {0}", server.IsConnected); Console.WriteLine("server.OutBufferSize = {0}", server.OutBufferSize); Console.WriteLine("server.ReadMode = {0}", server.ReadMode); Console.WriteLine("server.SafePipeHandle = {0}", server.SafePipeHandle); Console.WriteLine("server.TransmissionMode = {0}", server.TransmissionMode); server.Write(new byte[] { 123 }, 0, 1); server.WriteAsync(new byte[] { 124 }, 0, 1).Wait(); server.Flush(); Console.WriteLine("Waiting for Pipe Drain."); server.WaitForPipeDrain(); clientTask.Wait(); server.DisposeLocalCopyOfClientHandle(); } using (AnonymousPipeServerStream server = new AnonymousPipeServerStream(PipeDirection.In)) { Task clientTask = StartClientAsync(PipeDirection.Out, server.ClientSafePipeHandle); Console.WriteLine("server.InBufferSize = {0}", server.InBufferSize); byte[] readData = new byte[] { 0, 1 }; server.Read(readData, 0, 1); server.ReadAsync(readData, 1, 1).Wait(); Assert.Equal(123, readData[0]); Assert.Equal(124, readData[1]); } }
public static void ServerReadOnlyThrows() { using (AnonymousPipeServerStream server = new AnonymousPipeServerStream(PipeDirection.In)) { Assert.Throws<NotSupportedException>(() => server.Write(new byte[5], 0, 5)); Assert.Throws<NotSupportedException>(() => server.WriteByte(123)); Assert.Throws<NotSupportedException>(() => server.Flush()); Assert.Throws<NotSupportedException>(() => server.OutBufferSize); Assert.Throws<NotSupportedException>(() => server.WaitForPipeDrain()); Assert.ThrowsAsync<NotSupportedException>(() => server.WriteAsync(new byte[5], 0, 5)); } }
private static void SendMessage(AnonymousPipeServerStream pipeServer, StreamWriter sw, string message) { sw.WriteLine(message); LogItLine(message); pipeServer.WaitForPipeDrain(); Thread.Sleep(1000); }