static void Main(string[] args) { using (AnonymousPipeClientStream pipeClient = new AnonymousPipeClientStream(PipeDirection.Out, args[0])) { using (StreamWriter pipeWriter = new StreamWriter(pipeClient)) { pipeWriter.WriteLine("HELLO"); pipeWriter.Flush(); Thread.Sleep(750); pipeWriter.WriteLine("FROM"); pipeWriter.Flush(); Thread.Sleep(750); pipeWriter.WriteLine("PIPE"); pipeWriter.Flush(); Thread.Sleep(750); pipeWriter.WriteLine("QUIT"); pipeWriter.Flush(); pipeClient.WaitForPipeDrain(); } } }
public static void ClientPInvokeChecks() { using (AnonymousPipeServerStream server = new AnonymousPipeServerStream(PipeDirection.In)) { using (AnonymousPipeClientStream client = new AnonymousPipeClientStream(PipeDirection.Out, server.ClientSafePipeHandle)) { Task serverTask = Task.Run(() => DoStreamOperations(server)); Assert.False(client.CanRead); Assert.False(client.CanSeek); Assert.False(client.CanTimeout); Assert.True(client.CanWrite); Assert.False(client.IsAsync); Assert.True(client.IsConnected); if (Interop.IsWindows) { Assert.Equal(0, client.OutBufferSize); } else { Assert.Throws<PlatformNotSupportedException>(() => client.OutBufferSize); } Assert.Equal(PipeTransmissionMode.Byte, client.ReadMode); Assert.NotNull(client.SafePipeHandle); Assert.Equal(PipeTransmissionMode.Byte, client.TransmissionMode); client.Write(new byte[] { 123 }, 0, 1); client.WriteAsync(new byte[] { 124 }, 0, 1).Wait(); if (Interop.IsWindows) { client.WaitForPipeDrain(); } else { Assert.Throws<PlatformNotSupportedException>(() => client.WaitForPipeDrain()); } client.Flush(); serverTask.Wait(); } } using (AnonymousPipeServerStream server = new AnonymousPipeServerStream(PipeDirection.Out)) { using (AnonymousPipeClientStream client = new AnonymousPipeClientStream(PipeDirection.In, server.ClientSafePipeHandle)) { Task serverTask = Task.Run(() => DoStreamOperations(server)); if (Interop.IsWindows) { Assert.Equal(4096, client.InBufferSize); } else { Assert.Throws<PlatformNotSupportedException>(() => client.InBufferSize); } byte[] readData = new byte[] { 0, 1 }; Assert.Equal(1, client.Read(readData, 0, 1)); Assert.Equal(1, client.ReadAsync(readData, 1, 1).Result); Assert.Equal(123, readData[0]); Assert.Equal(124, readData[1]); serverTask.Wait(); } } }
public static void ClientPInvokeChecks() { using (AnonymousPipeServerStream server = new AnonymousPipeServerStream(PipeDirection.In)) { using (AnonymousPipeClientStream client = new AnonymousPipeClientStream(PipeDirection.Out, server.ClientSafePipeHandle)) { Task serverTask = DoServerOperationsAsync(server); Console.WriteLine("client.CanRead = {0}", client.CanRead); Console.WriteLine("client.CanSeek = {0}", client.CanSeek); Console.WriteLine("client.CanTimeout = {0}", client.CanTimeout); Console.WriteLine("client.CanWrite = {0}", client.CanWrite); Console.WriteLine("client.IsAsync = {0}", client.IsAsync); Console.WriteLine("client.IsConnected = {0}", client.IsConnected); Console.WriteLine("client.OutBufferSize = {0}", client.OutBufferSize); Console.WriteLine("client.ReadMode = {0}", client.ReadMode); Console.WriteLine("client.SafePipeHandle = {0}", client.SafePipeHandle); Console.WriteLine("client.TransmissionMode = {0}", client.TransmissionMode); client.Write(new byte[] { 123 }, 0, 1); client.WriteAsync(new byte[] { 124 }, 0, 1).Wait(); client.WaitForPipeDrain(); client.Flush(); serverTask.Wait(); } } using (AnonymousPipeServerStream server = new AnonymousPipeServerStream(PipeDirection.Out)) { using (AnonymousPipeClientStream client = new AnonymousPipeClientStream(PipeDirection.In, server.ClientSafePipeHandle)) { Task serverTask = DoServerOperationsAsync(server); Console.WriteLine("client.InBufferSize = {0}", client.InBufferSize); byte[] readData = new byte[] { 0, 1 }; client.Read(readData, 0, 1); client.ReadAsync(readData, 1, 1).Wait(); Assert.Equal(123, readData[0]); Assert.Equal(124, readData[1]); serverTask.Wait(); } } }
public static void Main(string[] args) { //todo: handle errors //Console.ReadKey(); //for debug purpose only if (args.Length != 5) { ShowErrorMessage(); return; } AnonymousPipeClientStream inPipeClient, outPipeClient; Int32 milestoneIndex, iterationIndex; String containerAssemblyPath; try { inPipeClient = new AnonymousPipeClientStream(PipeDirection.In, args[0]); outPipeClient = new AnonymousPipeClientStream(PipeDirection.Out, args[1]); milestoneIndex = Int32.Parse(args[2]); iterationIndex = Int32.Parse(args[3]); //todo: check existence & correctness containerAssemblyPath = args[4]; } catch (ArgumentException) { ShowErrorMessage(); return; } catch (FormatException) { ShowErrorMessage(); return; } AppDomain.CurrentDomain.AssemblyResolve += (s, a) => { //todo: investigate tested assembly necessity return Assembly.LoadFile(containerAssemblyPath); }; var binFormatter = new BinaryFormatter(); var obj = binFormatter.Deserialize(inPipeClient); var agent = obj as TestAgent; var results = new List<PassResult>(); var ctor = agent.ContainerType.GetConstructor(Type.EmptyTypes); var container = ctor.Invoke(null); //Load all missed referenced assemblies var referencedAssemblies = AppDomain.CurrentDomain.GetAssemblies(); var containerReferencedAssemplies = agent.ContainerType.Assembly.GetReferencedAssemblies(); foreach (var containerReferencedAssemply in containerReferencedAssemplies) { if (!containerReferencedAssemply.Name.Equals("mscorlib")) { var missed = true; foreach (var referencedAssembly in referencedAssemblies) { if (referencedAssembly.FullName.Equals(containerReferencedAssemply.FullName)) { missed = false; break; } } if (missed) { Assembly.Load(containerReferencedAssemply.FullName); } } } Object testedObject = RunAssistant.InvokeTestPreparing(agent, container); RunAssistant.InvokeMilestonePreparing(agent, container, ref testedObject, milestoneIndex); RunAssistant.InvokeIterationPreparing(agent, container, ref testedObject, iterationIndex); foreach (var testUnit in agent.TestUnits) { if (testUnit.Marker) { var result = DoJITTest(agent, testUnit, container, testedObject, iterationIndex); result.LineId = testUnit.Id; result.IterationIndex = iterationIndex; results.Add(result); } } RunAssistant.InvokeIterationFinalizing(agent, container, ref testedObject); RunAssistant.InvokeMilestoneFinalizing(agent, container, ref testedObject); RunAssistant.InvokeTestFinalizing(agent, container, testedObject); binFormatter.Serialize(outPipeClient, results); outPipeClient.WaitForPipeDrain(); }
/// <summary> /// 方法 匿名パイプを使用してローカル プロセス間の通信を行う /// http://msdn.microsoft.com/ja-jp/library/bb546102.aspx /// の親プロセス側実装(入出力に対応させた) /// ・AnonymousPipeServerStream クラス (System.IO.Pipes) /// http://msdn.microsoft.com/ja-jp/library/system.io.pipes.anonymouspipeserverstream.aspx /// ・AnonymousPipeClientStream クラス (System.IO.Pipes) /// http://msdn.microsoft.com/ja-jp/library/system.io.pipes.anonymouspipeclientstream.aspx /// </summary> public static void Main(string[] args) { if (args.Length > 0) { using (PipeStream pipeClientIn = new AnonymousPipeClientStream(PipeDirection.In, args[0])) { using (PipeStream pipeClientOut = new AnonymousPipeClientStream(PipeDirection.Out, args[1])) { // 匿名パイプでは、Message送信モードはサポートされません。 // http://msdn.microsoft.com/ja-jp/library/system.io.pipes.pipetransmissionmode.aspx // 匿名パイプの送信モードを取得:Byteになっている筈(デバッグ) Debug.WriteLine(string.Format( "[Child] pipeClientIn.TransmissionMode: {0}.", pipeClientIn.TransmissionMode.ToString())); Debug.WriteLine(string.Format( "[Child] pipeClientOut.TransmissionMode: {0}.", pipeClientOut.TransmissionMode.ToString())); using (StreamReader sr = new StreamReader(pipeClientIn)) { using (StreamWriter sw = new StreamWriter(pipeClientOut)) { string temp; sw.AutoFlush = true; while ((temp = sr.ReadLine()) != null) { Debug.WriteLine("[Child] Debug: " + temp); // 前・子プロセスへの書き込みが // 全て読み取られるまで待つ。 pipeClientOut.WaitForPipeDrain(); if (temp.ToUpper().IndexOf("EXIT") != -1) { // 終了する場合 Debug.WriteLine("[Child] Debug EXIT"); sw.WriteLine("[Child] EXIT"); sw.Flush(); break; } else { // 継続する場合 // 入力を反転して出力する。 Debug.WriteLine("[Child] Debug Reversal: " + Program.Reverse(temp)); sw.WriteLine("[Child] Reversal: " + Program.Reverse(temp)); sw.Flush(); } } } } } } } }