Implement single instance support using named pipes instead of ipc

This commit is contained in:
Jaex 2024-02-11 04:30:14 +03:00
parent b699edcd1c
commit bedd13c597
5 changed files with 169 additions and 214 deletions

View File

@ -1,208 +0,0 @@
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
Copyright (c) 2007-2024 ShareX Team
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Ipc;
using System.Security.Permissions;
using System.Threading;
namespace ShareX.HelpersLib
{
public class ApplicationInstanceManager : IDisposable
{
private static readonly string MutexName = "82E6AC09-0FEF-4390-AD9F-0DD3F5561EFC";
private static readonly string AppName = "ShareX";
private static readonly string EventName = string.Format("{0}-{1}-{2}", Environment.MachineName, Environment.UserName, AppName);
private static readonly string SemaphoreName = string.Format("{0}{1}", EventName, "Semaphore");
public bool IsSingleInstance { get; private set; }
public bool IsFirstInstance { get; private set; }
private Mutex mutex;
private Semaphore semaphore;
private IpcServerChannel serverChannel;
public ApplicationInstanceManager(bool isSingleInstance, string[] args, EventHandler<InstanceCallbackEventArgs> callback)
{
IsSingleInstance = isSingleInstance;
mutex = new Mutex(false, MutexName);
try
{
IsFirstInstance = mutex.WaitOne(100, false);
if (IsSingleInstance && !IsFirstInstance)
{
CreateMultipleInstance(args);
}
}
catch (AbandonedMutexException)
{
// Log the mutex was abandoned in another process, it will still get acquired
DebugHelper.WriteLine("Single instance mutex found abandoned from another process.");
IsFirstInstance = true;
}
CreateFirstInstance(callback);
}
public void Dispose()
{
if (IsFirstInstance)
{
if (mutex != null)
{
mutex.ReleaseMutex();
}
if (serverChannel != null)
{
ChannelServices.UnregisterChannel(serverChannel);
}
if (semaphore != null)
{
semaphore.Close();
}
}
}
private void CreateFirstInstance(EventHandler<InstanceCallbackEventArgs> callback)
{
try
{
using (EventWaitHandle eventWaitHandle = new EventWaitHandle(false, EventResetMode.AutoReset, EventName, out bool createdNew))
{
// 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
// really first instance by detecting if EventWaitHandle was created
if (!createdNew)
{
return;
}
semaphore = new Semaphore(1, 1, SemaphoreName);
ThreadPool.RegisterWaitForSingleObject(eventWaitHandle, WaitOrTimerCallback, callback, Timeout.Infinite, false);
RegisterRemoteType(AppName);
}
}
catch (Exception e)
{
DebugHelper.WriteException(e);
}
}
private void CreateMultipleInstance(string[] args)
{
try
{
InstanceProxy.CommandLineArgs = args;
using (EventWaitHandle eventWaitHandle = EventWaitHandle.OpenExisting(EventName))
{
semaphore = Semaphore.OpenExisting(SemaphoreName);
semaphore.WaitOne();
UpdateRemoteObject(AppName);
if (eventWaitHandle != null)
{
eventWaitHandle.Set();
}
}
}
catch (Exception e)
{
DebugHelper.WriteException(e);
}
Environment.Exit(0);
}
private void UpdateRemoteObject(string uri)
{
IpcClientChannel clientChannel = new IpcClientChannel();
ChannelServices.RegisterChannel(clientChannel, true);
if (Activator.GetObject(typeof(InstanceProxy), string.Format("ipc://{0}{1}{2}/{2}", Environment.MachineName, Environment.UserName, uri)) is InstanceProxy proxy)
{
proxy.SetCommandLineArgs(InstanceProxy.CommandLineArgs);
}
ChannelServices.UnregisterChannel(clientChannel);
}
private void RegisterRemoteType(string uri)
{
serverChannel = new IpcServerChannel(Environment.MachineName + Environment.UserName + uri);
ChannelServices.RegisterChannel(serverChannel, true);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(InstanceProxy), uri, WellKnownObjectMode.Singleton);
}
private void WaitOrTimerCallback(object state, bool timedOut)
{
if (state is EventHandler<InstanceCallbackEventArgs> callback)
{
try
{
callback(state, new InstanceCallbackEventArgs(InstanceProxy.CommandLineArgs));
}
finally
{
if (semaphore != null)
{
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 string[] CommandLineArgs { get; private set; }
internal InstanceCallbackEventArgs(string[] commandLineArgs)
{
CommandLineArgs = commandLineArgs;
}
}
}

View File

@ -13,7 +13,6 @@
<Reference Include="System.IO.Compression" />
<Reference Include="System.Management" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Runtime.Remoting" />
<Reference Include="System.Security" />
<Reference Include="System.Web" />
</ItemGroup>

View File

@ -0,0 +1,161 @@
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
Copyright (c) 2007-2024 ShareX Team
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
using System;
using System.IO;
using System.IO.Pipes;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ShareX.HelpersLib
{
public class SingleInstanceManager : IDisposable
{
public event EventHandler<ArgumentsReceivedEventArgs> ArgumentsReceived;
public bool IsSingleInstance { get; private set; }
public bool IsFirstInstance { get; private set; }
private const string MutexName = "82E6AC09-0FEF-4390-AD9F-0DD3F5561EFC";
private const string AppName = "ShareX";
private static readonly string PipeName = $"{Environment.MachineName}-{Environment.UserName}-{AppName}";
private readonly Mutex mutex;
private CancellationTokenSource cancellationSource;
public SingleInstanceManager(bool isSingleInstance, string[] args)
{
IsSingleInstance = isSingleInstance;
mutex = new Mutex(false, MutexName);
if (IsSingleInstance)
{
try
{
IsFirstInstance = mutex.WaitOne(100, false);
if (IsFirstInstance)
{
Task.Run(() => ListenForConnectionsAsync());
}
else
{
RedirectArgumentsToFirstInstance(args);
Environment.Exit(0);
}
}
catch (AbandonedMutexException)
{
DebugHelper.WriteLine("Single instance mutex found abandoned from another process.");
IsFirstInstance = true;
}
}
else
{
IsFirstInstance = true;
}
}
protected virtual void OnArgumentsReceived(string[] arguments)
{
ArgumentsReceived?.Invoke(this, new ArgumentsReceivedEventArgs(arguments));
}
private async Task ListenForConnectionsAsync()
{
cancellationSource = new CancellationTokenSource();
while (!cancellationSource.IsCancellationRequested)
{
try
{
using (NamedPipeServerStream serverPipe = new NamedPipeServerStream(PipeName, PipeDirection.InOut, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous))
{
await serverPipe.WaitForConnectionAsync(cancellationSource.Token);
using (BinaryReader reader = new BinaryReader(serverPipe, Encoding.UTF8))
{
int length = reader.ReadInt32();
string[] args = new string[length];
for (int i = 0; i < length; i++)
{
args[i] = reader.ReadString();
}
OnArgumentsReceived(args);
}
}
}
catch (Exception e) when (cancellationSource.IsCancellationRequested)
{
}
catch (Exception e)
{
DebugHelper.WriteLine("Error in named pipe communication: {0}", e.Message);
}
}
}
private void RedirectArgumentsToFirstInstance(string[] args)
{
using (NamedPipeClientStream clientPipe = new NamedPipeClientStream(".", PipeName, PipeDirection.Out))
{
clientPipe.Connect();
using (BinaryWriter writer = new BinaryWriter(clientPipe, Encoding.UTF8))
{
writer.Write(args.Length);
foreach (string argument in args)
{
writer.Write(argument);
}
}
}
}
public void Dispose()
{
cancellationSource?.Cancel();
mutex?.ReleaseMutex();
}
}
public class ArgumentsReceivedEventArgs : EventArgs
{
public string[] Arguments { get; private set; }
public ArgumentsReceivedEventArgs(string[] arguments)
{
Arguments = arguments;
}
}
}

View File

@ -31,6 +31,7 @@ using System.Collections.Generic;
using System.Collections.Specialized;
using System.Drawing;
using System.IO;
using System.Web;
using System.Windows.Forms;
namespace ShareX.UploadersLib.FileUploaders
@ -453,8 +454,8 @@ namespace ShareX.UploadersLib.FileUploaders
if (CreateShareableURLRaw)
{
var uriBuilder = new UriBuilder(result.URL);
var query = System.Web.HttpUtility.ParseQueryString(uriBuilder.Query);
UriBuilder uriBuilder = new UriBuilder(result.URL);
NameValueCollection query = HttpUtility.ParseQueryString(uriBuilder.Query);
query["raw"] = "1";
uriBuilder.Query = query.ToString();
result.URL = $"{uriBuilder.Scheme}://{uriBuilder.Host}{uriBuilder.Path}{uriBuilder.Query}";

View File

@ -297,9 +297,11 @@ namespace ShareX
MultiInstance = CLI.IsCommandExist("multi", "m");
using (ApplicationInstanceManager instanceManager = new ApplicationInstanceManager(!MultiInstance, args, SingleInstanceCallback))
using (SingleInstanceManager singleInstanceManager = new SingleInstanceManager(!MultiInstance, args))
using (TimerResolutionManager timerResolutionManager = new TimerResolutionManager())
{
singleInstanceManager.ArgumentsReceived += SingleInstanceManager_ArgumentsReceived;
Run();
}
@ -392,13 +394,13 @@ namespace ShareX
Application.Exit();
}
private static void SingleInstanceCallback(object sender, InstanceCallbackEventArgs args)
private static void SingleInstanceManager_ArgumentsReceived(object sender, ArgumentsReceivedEventArgs e)
{
if (WaitFormLoad(5000))
{
MainForm.InvokeSafe(async () =>
{
await UseCommandLineArgs(args.CommandLineArgs);
await UseCommandLineArgs(e.Arguments);
});
}
}