diff --git a/source2/Debug/Cosmos.Debug.VSDebugEngine/DebugWindows.cs b/source2/Debug/Cosmos.Debug.VSDebugEngine/DebugWindows.cs index 10f160991..f1de15f1a 100644 --- a/source2/Debug/Cosmos.Debug.VSDebugEngine/DebugWindows.cs +++ b/source2/Debug/Cosmos.Debug.VSDebugEngine/DebugWindows.cs @@ -9,41 +9,13 @@ using System.Windows.Forms; namespace Cosmos.Debug.VSDebugEngine { static public class DebugWindows { - static private NamedPipeClientStream mPipe; - static private StreamWriter mWriter; + static Cosmos.Compiler.Debug.PipeClient mPipe; static public void SendCommand(byte aCmd, byte[] aData) { if (mPipe == null) { - // User might run mult instances of VS, so we need to make sure the pipe name - // is unique but also predictable since the pipe is the only way to talk - // between the debugger and ToolWindows project. - int xPID = System.Diagnostics.Process.GetCurrentProcess().Id; - mPipe = new NamedPipeClientStream(".", Cosmos.Compiler.Debug.Pipes.DownName, PipeDirection.Out); - try { - // For now we assume its there or not from the first call. - // If we don't find the server, we disable it to avoid causing lag. - // TODO: In future - try this instead: - // String[] listOfPipes = System.IO.Directory.GetFiles(@"\.\pipe\"); - - mPipe.Connect(500); - } catch (TimeoutException ex) { - mPipe.Close(); - mPipe = null; - return; - } - mWriter = new StreamWriter(mPipe); + mPipe = new Cosmos.Compiler.Debug.PipeClient(Cosmos.Compiler.Debug.Pipes.DownName); } - - mPipe.WriteByte(aCmd); - - int xLength = Math.Min(aData.Length, 32000); - mPipe.WriteByte((byte)(xLength >> 8)); - mPipe.WriteByte((byte)(xLength & 0xFF)); - if (xLength > 0) { - mPipe.Write(aData, 0, xLength); - } - - mPipe.Flush(); + mPipe.SendCommand(aCmd, aData); } } diff --git a/source2/IL2CPU/Cosmos.IL2CPU.Debug/Consts.cs b/source2/IL2CPU/Cosmos.IL2CPU.Debug/Consts.cs index 789cbe9b1..2ed9eea7e 100644 --- a/source2/IL2CPU/Cosmos.IL2CPU.Debug/Consts.cs +++ b/source2/IL2CPU/Cosmos.IL2CPU.Debug/Consts.cs @@ -59,6 +59,9 @@ namespace Cosmos.Compiler.Debug { public static readonly string UpName; static Pipes() { + // User might run mult instances of VS, so we need to make sure the pipe name + // is unique but also predictable since the pipe is the only way to talk + // between the debugger and ToolWindows project. int xPID = System.Diagnostics.Process.GetCurrentProcess().Id; DownName = @"Cosmos\DebugDown-" + xPID; UpName = @"Cosmos\DebugUp-" + xPID; diff --git a/source2/IL2CPU/Cosmos.IL2CPU.Debug/PipeClient.cs b/source2/IL2CPU/Cosmos.IL2CPU.Debug/PipeClient.cs index 63467574e..34c2374ab 100644 --- a/source2/IL2CPU/Cosmos.IL2CPU.Debug/PipeClient.cs +++ b/source2/IL2CPU/Cosmos.IL2CPU.Debug/PipeClient.cs @@ -1,9 +1,51 @@ using System; using System.Collections.Generic; +using System.IO; +using System.IO.Pipes; using System.Linq; using System.Text; namespace Cosmos.Compiler.Debug { public class PipeClient { + private string mPipeName; + private NamedPipeClientStream mPipe; + private StreamWriter mWriter; + + public PipeClient(string aPipeName) { + mPipeName = aPipeName; + } + + public void SendCommand(byte aCmd, byte[] aData) { + // We need to delay creation and connect until its used, so we guarantee + // that the server side is active and ready. + if (mPipe == null) { + mPipe = new NamedPipeClientStream(".", Cosmos.Compiler.Debug.Pipes.DownName, PipeDirection.Out); + try { + // For now we assume its there or not from the first call. + // If we don't find the server, we disable it to avoid causing lag. + // TODO: In future - try this instead: + // String[] listOfPipes = System.IO.Directory.GetFiles(@"\.\pipe\"); + + mPipe.Connect(500); + } catch (TimeoutException ex) { + mPipe.Close(); + mPipe = null; + return; + } + mWriter = new StreamWriter(mPipe); + } + + mPipe.WriteByte(aCmd); + + int xLength = Math.Min(aData.Length, 32000); + mPipe.WriteByte((byte)(xLength >> 8)); + mPipe.WriteByte((byte)(xLength & 0xFF)); + if (xLength > 0) { + mPipe.Write(aData, 0, xLength); + } + + mPipe.Flush(); + } + } }