Revert ApplicationInstanceManager for testing issue

This commit is contained in:
Jaex 2020-06-17 01:37:16 +03:00
parent 7b302567f0
commit 3293de0703

View file

@ -23,11 +23,11 @@ You should have received a copy of the GNU General Public License
#endregion License Information (GPL v3) #endregion License Information (GPL v3)
using Newtonsoft.Json;
using System; using System;
using System.IO; using System.Runtime.Remoting;
using System.IO.Pipes; using System.Runtime.Remoting.Channels;
using System.Text; using System.Runtime.Remoting.Channels.Ipc;
using System.Security.Permissions;
using System.Threading; using System.Threading;
namespace ShareX.HelpersLib namespace ShareX.HelpersLib
@ -36,15 +36,15 @@ public class ApplicationInstanceManager : IDisposable
{ {
private static readonly string MutexName = "82E6AC09-0FEF-4390-AD9F-0DD3F5561EFC"; private static readonly string MutexName = "82E6AC09-0FEF-4390-AD9F-0DD3F5561EFC";
private static readonly string AppName = "ShareX"; private static readonly string AppName = "ShareX";
private static readonly string PipeName = $"{Environment.MachineName}-{Environment.UserName}-{AppName}"; private static readonly string EventName = string.Format("{0}-{1}-{2}", Environment.MachineName, Environment.UserName, AppName);
private static readonly string SemaphoreName = PipeName + "Semaphore"; private static readonly string SemaphoreName = string.Format("{0}{1}", EventName, "Semaphore");
public bool IsSingleInstance { get; private set; } public bool IsSingleInstance { get; private set; }
public bool IsFirstInstance { get; private set; } public bool IsFirstInstance { get; private set; }
private Mutex mutex; private Mutex mutex;
private Semaphore semaphore; private Semaphore semaphore;
private NamedPipeServerStream pipeServer; private IpcServerChannel serverChannel;
public ApplicationInstanceManager(bool isSingleInstance, string[] args, EventHandler<InstanceCallbackEventArgs> callback) public ApplicationInstanceManager(bool isSingleInstance, string[] args, EventHandler<InstanceCallbackEventArgs> callback)
{ {
@ -74,29 +74,45 @@ public ApplicationInstanceManager(bool isSingleInstance, string[] args, EventHan
public void Dispose() public void Dispose()
{ {
if (IsFirstInstance) if (IsFirstInstance)
{
if (mutex != null)
{ {
mutex.ReleaseMutex(); mutex.ReleaseMutex();
} }
mutex.Dispose(); if (serverChannel != null)
semaphore?.Dispose(); {
pipeServer?.Dispose(); ChannelServices.UnregisterChannel(serverChannel);
}
if (semaphore != null)
{
semaphore.Close();
}
}
} }
private void CreateFirstInstance(EventHandler<InstanceCallbackEventArgs> callback) private void CreateFirstInstance(EventHandler<InstanceCallbackEventArgs> callback)
{ {
try try
{ {
semaphore = new Semaphore(1, 1, SemaphoreName, out var createdNew); bool createdNew;
using (EventWaitHandle eventWaitHandle = new EventWaitHandle(false, EventResetMode.AutoReset, EventName, out createdNew))
{
// Mixing single instance and multi instance (via command line parameter) copies of the program can // Mixing single instance and multi instance (via command line parameter) copies of the program can
// result in CreateFirstInstance being called if it isn't really the first one. Make sure this is // result in CreateFirstInstance being called if it isn't really the first one. Make sure this is
// really first instance by detecting if the semaphore was created // really first instance by detecting if EventWaitHandle was created
if (!createdNew) if (!createdNew)
{ {
return; return;
} }
CreateServer(callback); semaphore = new Semaphore(1, 1, SemaphoreName);
ThreadPool.RegisterWaitForSingleObject(eventWaitHandle, WaitOrTimerCallback, callback, Timeout.Infinite, false);
RegisterRemoteType(AppName);
}
} }
catch (Exception e) catch (Exception e)
{ {
@ -108,11 +124,19 @@ private void CreateMultipleInstance(string[] args)
{ {
try try
{ {
semaphore = Semaphore.OpenExisting(SemaphoreName); InstanceProxy.CommandLineArgs = args;
// Wait until the server is ready to accept data using (EventWaitHandle eventWaitHandle = EventWaitHandle.OpenExisting(EventName))
{
semaphore = Semaphore.OpenExisting(SemaphoreName);
semaphore.WaitOne(); semaphore.WaitOne();
SendDataToServer(args); UpdateRemoteObject(AppName);
if (eventWaitHandle != null)
{
eventWaitHandle.Set();
}
}
} }
catch (Exception e) catch (Exception e)
{ {
@ -122,67 +146,69 @@ private void CreateMultipleInstance(string[] args)
Environment.Exit(0); Environment.Exit(0);
} }
private void SendDataToServer(string[] args) private void UpdateRemoteObject(string uri)
{ {
using (var pipeClient = new NamedPipeClientStream(".", PipeName, PipeDirection.Out)) IpcClientChannel clientChannel = new IpcClientChannel();
{ ChannelServices.RegisterChannel(clientChannel, true);
pipeClient.Connect();
var pipeData = new InstanceCallbackEventArgs InstanceProxy proxy = Activator.GetObject(typeof(InstanceProxy), string.Format("ipc://{0}{1}{2}/{2}", Environment.MachineName, Environment.UserName, uri)) as InstanceProxy;
{
CommandLineArgs = args
};
var bytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(pipeData)); if (proxy != null)
pipeClient.Write(bytes, 0, bytes.Length); {
} proxy.SetCommandLineArgs(InstanceProxy.CommandLineArgs);
} }
private void CreateServer(EventHandler<InstanceCallbackEventArgs> callback) ChannelServices.UnregisterChannel(clientChannel);
{
pipeServer = new NamedPipeServerStream(PipeName, PipeDirection.In, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);
pipeServer.BeginWaitForConnection(ConnectionCallback, callback);
} }
private void ConnectionCallback(IAsyncResult ar) private void RegisterRemoteType(string uri)
{ {
try serverChannel = new IpcServerChannel(Environment.MachineName + Environment.UserName + uri);
{ ChannelServices.RegisterChannel(serverChannel, true);
pipeServer.EndWaitForConnection(ar);
} RemotingConfiguration.RegisterWellKnownServiceType(typeof(InstanceProxy), uri, WellKnownObjectMode.Singleton);
catch (ObjectDisposedException)
{
// Operation got aborted as part of program exit.
return;
} }
var callback = ar.AsyncState as EventHandler<InstanceCallbackEventArgs>; private void WaitOrTimerCallback(object state, bool timedOut)
var sr = new StreamReader(pipeServer, Encoding.UTF8);
try
{ {
EventHandler<InstanceCallbackEventArgs> callback = state as EventHandler<InstanceCallbackEventArgs>;
if (callback != null) if (callback != null)
{ {
var data = sr.ReadToEnd(); try
callback(this, JsonConvert.DeserializeObject<InstanceCallbackEventArgs>(data)); {
} callback(state, new InstanceCallbackEventArgs(InstanceProxy.CommandLineArgs));
} }
finally finally
{ {
// Close the existing server if (semaphore != null)
sr.Dispose(); {
// Create a new server
CreateServer(callback);
// Signal that we are ready to accept a new connection
semaphore.Release(); semaphore.Release();
} }
} }
} }
}
}
[Serializable]
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
internal class InstanceProxy : MarshalByRefObject
{
public static string[] CommandLineArgs { get; internal set; }
public void SetCommandLineArgs(string[] commandLineArgs)
{
CommandLineArgs = commandLineArgs;
}
}
public class InstanceCallbackEventArgs : EventArgs public class InstanceCallbackEventArgs : EventArgs
{ {
[JsonProperty] public string[] CommandLineArgs { get; private set; }
public string[] CommandLineArgs { get; internal set; }
internal InstanceCallbackEventArgs(string[] commandLineArgs)
{
CommandLineArgs = commandLineArgs;
}
} }
} }