From 99c6e5dae6e30cdd1d2a96851dbd6e6cc644592a Mon Sep 17 00:00:00 2001 From: Nixtar Date: Wed, 19 Dec 2018 09:59:01 +1000 Subject: [PATCH] Support custom Pipe Name Updated to support custom Pipe Name via constructor parameter. Misc c# naming convention --- InstanceManager.sln.DotSettings | 57 ++++++++++++++++++++++++++ InstanceManager/InstanceManager.cs | 50 +++++++++++++--------- InstanceManager/InstanceManager.csproj | 4 +- 3 files changed, 90 insertions(+), 21 deletions(-) create mode 100644 InstanceManager.sln.DotSettings diff --git a/InstanceManager.sln.DotSettings b/InstanceManager.sln.DotSettings new file mode 100644 index 0000000..a736f26 --- /dev/null +++ b/InstanceManager.sln.DotSettings @@ -0,0 +1,57 @@ + + 3 + False + <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> + <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> + <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> + <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> + <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /> + <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> + <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /> + <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> + <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /> + <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> + <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> + <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> + <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /> + <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /> + <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /> + <Policy Inspect="True" Prefix="I" Suffix="" Style="AaBb" /> + <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /> + <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /> + <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> + <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> + <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> + <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> + <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> + <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> + <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> + <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> + <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> + <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> + <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> + <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> + <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> + <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> + <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /> + <Policy Inspect="True" Prefix="T" Suffix="" Style="AaBb" /> + <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /> + <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /> + <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /> + <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /> + <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> + <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /> + <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /> + LIVE_MONITOR + LIVE_MONITOR + DO_NOTHING + LIVE_MONITOR + LIVE_MONITOR + LIVE_MONITOR + LIVE_MONITOR + LIVE_MONITOR + LIVE_MONITOR + LIVE_MONITOR + LIVE_MONITOR + DO_NOTHING + LIVE_MONITOR \ No newline at end of file diff --git a/InstanceManager/InstanceManager.cs b/InstanceManager/InstanceManager.cs index a860d33..eb8ab54 100644 --- a/InstanceManager/InstanceManager.cs +++ b/InstanceManager/InstanceManager.cs @@ -23,36 +23,46 @@ namespace InstanceManager { public class InstanceManager { - static string pipeName = System.Reflection.Assembly.GetExecutingAssembly().GetName() + "-instance-lock"; - PipeStream pipeStream; + private static string _pipeName = System.Reflection.Assembly.GetExecutingAssembly().GetName() + "-instance-lock"; + private static string[] _args; + PipeStream _pipeStream; + public InstanceManager(string[] args) { - try - { - StartServer(); - } - catch (IOException) - { - pipeStream = new NamedPipeClientStream(".", pipeName, PipeDirection.Out, PipeOptions.WriteThrough); - (pipeStream as NamedPipeClientStream).Connect(); - DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(string[])); - ser.WriteObject(pipeStream, args); - pipeStream.WaitForPipeDrain(); + _args = args; + StartServer(); + } - throw new Exception("Already running"); - } + public InstanceManager(string[] args, string pipeName) + { + _pipeName = pipeName; + _args = args; + StartServer(); } ~InstanceManager() { - pipeStream.Dispose(); + _pipeStream.Dispose(); } private void StartServer() { - // TODO Add security. - //PipeSecurity ps = new PipeSecurity(); - pipeStream = new NamedPipeServerStream(pipeName, PipeDirection.In, 1, PipeTransmissionMode.Message, PipeOptions.None, 1024 * 128, 1024); + try + { + // TODO Add security. + //PipeSecurity ps = new PipeSecurity(); + _pipeStream = new NamedPipeServerStream(_pipeName, PipeDirection.In, 1, PipeTransmissionMode.Message, PipeOptions.None, 1024 * 128, 1024); + } + catch (IOException) + { + _pipeStream = new NamedPipeClientStream(".", _pipeName, PipeDirection.Out, PipeOptions.WriteThrough); + (_pipeStream as NamedPipeClientStream).Connect(); + DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(string[])); + ser.WriteObject(_pipeStream, _args); + _pipeStream.WaitForPipeDrain(); + + throw new Exception("Already running"); + } } // TODO Add cancel token. @@ -60,7 +70,7 @@ public async void InstanceStartup(Action onStartupNextInstance) { while (true) { - NamedPipeServerStream server = pipeStream as NamedPipeServerStream; + NamedPipeServerStream server = _pipeStream as NamedPipeServerStream; await server.WaitForConnectionAsync(); DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(string[])); diff --git a/InstanceManager/InstanceManager.csproj b/InstanceManager/InstanceManager.csproj index f87cc59..2ca1f04 100644 --- a/InstanceManager/InstanceManager.csproj +++ b/InstanceManager/InstanceManager.csproj @@ -4,7 +4,7 @@ netstandard2.0 netstandard2.0;net46 true - 1.0.4 + 1.0.5 https://github.com/Bear21/InstanceManager/blob/master/LICENSE https://github.com/Bear21/InstanceManager.git https://github.com/Bear21/InstanceManager @@ -12,6 +12,8 @@ Stephen Wheeler - 8bitbear.com 8bitbear.InstanceManager Stephen Wheeler - 8bitbear.com + 1.0.5.0 + 1.0.5.0