2024-02-11 14:30:14 +13:00
|
|
|
|
#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
|
|
|
|
|
{
|
2024-03-14 15:34:25 +13:00
|
|
|
|
public event Action<string[]> ArgumentsReceived;
|
2024-02-11 14:30:14 +13:00
|
|
|
|
|
|
|
|
|
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}";
|
2024-02-21 18:01:38 +13:00
|
|
|
|
private const int MaxArgumentsLength = 100;
|
2024-02-11 14:30:14 +13:00
|
|
|
|
|
|
|
|
|
private readonly Mutex mutex;
|
2024-02-12 12:50:51 +13:00
|
|
|
|
private CancellationTokenSource cts;
|
2024-02-11 14:30:14 +13:00
|
|
|
|
|
2024-03-14 15:34:25 +13:00
|
|
|
|
public SingleInstanceManager(string[] args) : this(true, args)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-11 14:30:14 +13:00
|
|
|
|
public SingleInstanceManager(bool isSingleInstance, string[] args)
|
|
|
|
|
{
|
|
|
|
|
IsSingleInstance = isSingleInstance;
|
|
|
|
|
|
|
|
|
|
mutex = new Mutex(false, MutexName);
|
|
|
|
|
|
2024-03-14 15:34:25 +13:00
|
|
|
|
try
|
2024-02-11 14:30:14 +13:00
|
|
|
|
{
|
2024-03-14 15:34:25 +13:00
|
|
|
|
IsFirstInstance = mutex.WaitOne(100, false);
|
2024-02-11 14:30:14 +13:00
|
|
|
|
|
2024-03-14 15:34:25 +13:00
|
|
|
|
if (IsSingleInstance)
|
|
|
|
|
{
|
2024-02-11 14:30:14 +13:00
|
|
|
|
if (IsFirstInstance)
|
|
|
|
|
{
|
2024-02-12 12:50:51 +13:00
|
|
|
|
cts = new CancellationTokenSource();
|
|
|
|
|
|
2024-02-28 03:39:20 +13:00
|
|
|
|
Task.Run(ListenForConnectionsAsync, cts.Token);
|
2024-02-11 14:30:14 +13:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
RedirectArgumentsToFirstInstance(args);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-14 15:34:25 +13:00
|
|
|
|
catch (AbandonedMutexException)
|
2024-02-11 14:30:14 +13:00
|
|
|
|
{
|
2024-03-14 15:34:25 +13:00
|
|
|
|
DebugHelper.WriteLine("Single instance mutex found abandoned from another process.");
|
|
|
|
|
|
2024-02-11 14:30:14 +13:00
|
|
|
|
IsFirstInstance = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual void OnArgumentsReceived(string[] arguments)
|
|
|
|
|
{
|
2024-03-14 15:34:25 +13:00
|
|
|
|
ArgumentsReceived?.Invoke(arguments);
|
2024-02-11 14:30:14 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task ListenForConnectionsAsync()
|
|
|
|
|
{
|
2024-02-12 12:50:51 +13:00
|
|
|
|
while (!cts.IsCancellationRequested)
|
2024-02-11 14:30:14 +13:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using (NamedPipeServerStream serverPipe = new NamedPipeServerStream(PipeName, PipeDirection.InOut, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous))
|
|
|
|
|
{
|
2024-02-12 12:50:51 +13:00
|
|
|
|
await serverPipe.WaitForConnectionAsync(cts.Token);
|
2024-02-11 14:30:14 +13:00
|
|
|
|
|
|
|
|
|
using (BinaryReader reader = new BinaryReader(serverPipe, Encoding.UTF8))
|
|
|
|
|
{
|
|
|
|
|
int length = reader.ReadInt32();
|
|
|
|
|
|
2024-02-21 18:01:38 +13:00
|
|
|
|
if (length < 0 || length > MaxArgumentsLength)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("Invalid length: " + length);
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-11 14:30:14 +13:00
|
|
|
|
string[] args = new string[length];
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < length; i++)
|
|
|
|
|
{
|
|
|
|
|
args[i] = reader.ReadString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
OnArgumentsReceived(args);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-12 12:50:51 +13:00
|
|
|
|
catch when (cts.IsCancellationRequested)
|
2024-02-11 14:30:14 +13:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2024-03-11 15:11:00 +13:00
|
|
|
|
DebugHelper.WriteException(e);
|
2024-02-11 14:30:14 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void RedirectArgumentsToFirstInstance(string[] args)
|
|
|
|
|
{
|
2024-03-11 15:11:00 +13:00
|
|
|
|
try
|
2024-02-11 14:30:14 +13:00
|
|
|
|
{
|
2024-03-11 15:11:00 +13:00
|
|
|
|
using (NamedPipeClientStream clientPipe = new NamedPipeClientStream(".", PipeName, PipeDirection.Out))
|
2024-02-11 14:30:14 +13:00
|
|
|
|
{
|
2024-03-11 15:11:00 +13:00
|
|
|
|
clientPipe.Connect();
|
2024-02-11 14:30:14 +13:00
|
|
|
|
|
2024-03-11 15:11:00 +13:00
|
|
|
|
using (BinaryWriter writer = new BinaryWriter(clientPipe, Encoding.UTF8))
|
2024-02-11 14:30:14 +13:00
|
|
|
|
{
|
2024-03-11 15:11:00 +13:00
|
|
|
|
writer.Write(args.Length);
|
|
|
|
|
|
|
|
|
|
foreach (string argument in args)
|
|
|
|
|
{
|
|
|
|
|
writer.Write(argument);
|
|
|
|
|
}
|
2024-02-11 14:30:14 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-11 15:11:00 +13:00
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
DebugHelper.WriteException(e);
|
|
|
|
|
}
|
2024-02-11 14:30:14 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
2024-03-14 15:34:25 +13:00
|
|
|
|
if (cts != null)
|
|
|
|
|
{
|
|
|
|
|
cts.Cancel();
|
|
|
|
|
cts.Dispose();
|
|
|
|
|
}
|
2024-02-11 14:30:14 +13:00
|
|
|
|
|
2024-03-14 15:34:25 +13:00
|
|
|
|
if (mutex != null)
|
|
|
|
|
{
|
|
|
|
|
if (IsFirstInstance)
|
|
|
|
|
{
|
|
|
|
|
mutex.ReleaseMutex();
|
|
|
|
|
}
|
2024-02-11 14:30:14 +13:00
|
|
|
|
|
2024-03-14 15:34:25 +13:00
|
|
|
|
mutex.Dispose();
|
|
|
|
|
}
|
2024-02-11 14:30:14 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|