mirror of
https://github.com/ShareX/ShareX.git
synced 2024-07-01 04:30:49 +12:00
Merge pull request #4758 from sylveon/master
Replace System.Remoting by named pipe, remove RIPEMD-160 support
This commit is contained in:
commit
315f5f8f94
|
@ -23,11 +23,11 @@
|
|||
|
||||
#endregion License Information (GPL v3)
|
||||
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Runtime.Remoting;
|
||||
using System.Runtime.Remoting.Channels;
|
||||
using System.Runtime.Remoting.Channels.Ipc;
|
||||
using System.Security.Permissions;
|
||||
using System.IO;
|
||||
using System.IO.Pipes;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
|
||||
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 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");
|
||||
private static readonly string PipeName = $"{Environment.MachineName}-{Environment.UserName}-{AppName}";
|
||||
private static readonly string SemaphoreName = PipeName + "Semaphore";
|
||||
|
||||
public bool IsSingleInstance { get; private set; }
|
||||
public bool IsFirstInstance { get; private set; }
|
||||
|
||||
private Mutex mutex;
|
||||
private Semaphore semaphore;
|
||||
private IpcServerChannel serverChannel;
|
||||
private NamedPipeServerStream pipeServer;
|
||||
|
||||
public ApplicationInstanceManager(bool isSingleInstance, string[] args, EventHandler<InstanceCallbackEventArgs> callback)
|
||||
{
|
||||
|
@ -75,44 +75,28 @@ public void Dispose()
|
|||
{
|
||||
if (IsFirstInstance)
|
||||
{
|
||||
if (mutex != null)
|
||||
{
|
||||
mutex.ReleaseMutex();
|
||||
}
|
||||
|
||||
if (serverChannel != null)
|
||||
{
|
||||
ChannelServices.UnregisterChannel(serverChannel);
|
||||
}
|
||||
|
||||
if (semaphore != null)
|
||||
{
|
||||
semaphore.Close();
|
||||
}
|
||||
mutex.ReleaseMutex();
|
||||
}
|
||||
|
||||
mutex.Dispose();
|
||||
semaphore?.Dispose();
|
||||
pipeServer?.Dispose();
|
||||
}
|
||||
|
||||
private void CreateFirstInstance(EventHandler<InstanceCallbackEventArgs> callback)
|
||||
{
|
||||
try
|
||||
{
|
||||
bool createdNew;
|
||||
|
||||
using (EventWaitHandle eventWaitHandle = new EventWaitHandle(false, EventResetMode.AutoReset, EventName, out createdNew))
|
||||
semaphore = new Semaphore(1, 1, SemaphoreName, out var 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 the semaphore was created
|
||||
if (!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);
|
||||
return;
|
||||
}
|
||||
|
||||
CreateServer(callback);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
|
@ -124,19 +108,11 @@ private void CreateMultipleInstance(string[] args)
|
|||
{
|
||||
try
|
||||
{
|
||||
InstanceProxy.CommandLineArgs = args;
|
||||
semaphore = Semaphore.OpenExisting(SemaphoreName);
|
||||
|
||||
using (EventWaitHandle eventWaitHandle = EventWaitHandle.OpenExisting(EventName))
|
||||
{
|
||||
semaphore = Semaphore.OpenExisting(SemaphoreName);
|
||||
semaphore.WaitOne();
|
||||
UpdateRemoteObject(AppName);
|
||||
|
||||
if (eventWaitHandle != null)
|
||||
{
|
||||
eventWaitHandle.Set();
|
||||
}
|
||||
}
|
||||
// Wait until the server is ready to accept data
|
||||
semaphore.WaitOne();
|
||||
SendDataToServer(args);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
|
@ -146,69 +122,59 @@ private void CreateMultipleInstance(string[] args)
|
|||
Environment.Exit(0);
|
||||
}
|
||||
|
||||
private void UpdateRemoteObject(string uri)
|
||||
private void SendDataToServer(string[] args)
|
||||
{
|
||||
IpcClientChannel clientChannel = new IpcClientChannel();
|
||||
ChannelServices.RegisterChannel(clientChannel, true);
|
||||
|
||||
InstanceProxy proxy = Activator.GetObject(typeof(InstanceProxy), string.Format("ipc://{0}{1}{2}/{2}", Environment.MachineName, Environment.UserName, uri)) as InstanceProxy;
|
||||
|
||||
if (proxy != null)
|
||||
using (var pipeClient = new NamedPipeClientStream(".", PipeName, PipeDirection.Out))
|
||||
{
|
||||
proxy.SetCommandLineArgs(InstanceProxy.CommandLineArgs);
|
||||
}
|
||||
pipeClient.Connect();
|
||||
|
||||
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)
|
||||
{
|
||||
EventHandler<InstanceCallbackEventArgs> callback = state as EventHandler<InstanceCallbackEventArgs>;
|
||||
|
||||
if (callback != null)
|
||||
{
|
||||
try
|
||||
var pipeData = new InstanceCallbackEventArgs
|
||||
{
|
||||
callback(state, new InstanceCallbackEventArgs(InstanceProxy.CommandLineArgs));
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (semaphore != null)
|
||||
{
|
||||
semaphore.Release();
|
||||
}
|
||||
}
|
||||
CommandLineArgs = args
|
||||
};
|
||||
|
||||
var bytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(pipeData));
|
||||
pipeClient.Write(bytes, 0, bytes.Length);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
|
||||
internal class InstanceProxy : MarshalByRefObject
|
||||
{
|
||||
public static string[] CommandLineArgs { get; internal set; }
|
||||
|
||||
public void SetCommandLineArgs(string[] commandLineArgs)
|
||||
private void CreateServer(EventHandler<InstanceCallbackEventArgs> callback)
|
||||
{
|
||||
CommandLineArgs = commandLineArgs;
|
||||
pipeServer = new NamedPipeServerStream(PipeName, PipeDirection.In, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);
|
||||
pipeServer.BeginWaitForConnection(ConnectionCallback, callback);
|
||||
}
|
||||
|
||||
private void ConnectionCallback(IAsyncResult ar)
|
||||
{
|
||||
var callback = ar.AsyncState as EventHandler<InstanceCallbackEventArgs>;
|
||||
var sr = new StreamReader(pipeServer, Encoding.UTF8);
|
||||
try
|
||||
{
|
||||
pipeServer.EndWaitForConnection(ar);
|
||||
|
||||
if (callback != null)
|
||||
{
|
||||
var data = sr.ReadToEnd();
|
||||
callback(this, JsonConvert.DeserializeObject<InstanceCallbackEventArgs>(data));
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
// Close the existing server
|
||||
sr.Dispose();
|
||||
|
||||
// Create a new server
|
||||
CreateServer(callback);
|
||||
|
||||
// Signal that we are ready to accept a new connection
|
||||
semaphore.Release();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class InstanceCallbackEventArgs : EventArgs
|
||||
{
|
||||
public string[] CommandLineArgs { get; private set; }
|
||||
|
||||
internal InstanceCallbackEventArgs(string[] commandLineArgs)
|
||||
{
|
||||
CommandLineArgs = commandLineArgs;
|
||||
}
|
||||
[JsonProperty]
|
||||
public string[] CommandLineArgs { get; internal set; }
|
||||
}
|
||||
}
|
|
@ -154,8 +154,6 @@ public static HashAlgorithm GetHashAlgorithm(HashType hashType)
|
|||
return new SHA384CryptoServiceProvider();
|
||||
case HashType.SHA512:
|
||||
return new SHA512CryptoServiceProvider();
|
||||
case HashType.RIPEMD160:
|
||||
return new RIPEMD160Managed();
|
||||
}
|
||||
|
||||
return null;
|
||||
|
|
|
@ -97,12 +97,9 @@ public string ASCIIText
|
|||
public string SHA384 { get; private set; }
|
||||
public string SHA512 { get; private set; }
|
||||
|
||||
// http://en.wikipedia.org/wiki/RIPEMD
|
||||
public string RIPEMD160 { get; private set; }
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
Text = Base64 = CRC32 = MD5 = SHA1 = SHA256 = SHA384 = SHA512 = RIPEMD160 = null;
|
||||
Text = Base64 = CRC32 = MD5 = SHA1 = SHA256 = SHA384 = SHA512 = null;
|
||||
Binary = null;
|
||||
Hexadecimal = null;
|
||||
ASCII = null;
|
||||
|
@ -127,7 +124,6 @@ public bool EncodeText(string text)
|
|||
SHA256 = TranslatorHelper.TextToHash(text, HashType.SHA256, true);
|
||||
SHA384 = TranslatorHelper.TextToHash(text, HashType.SHA384, true);
|
||||
SHA512 = TranslatorHelper.TextToHash(text, HashType.SHA512, true);
|
||||
RIPEMD160 = TranslatorHelper.TextToHash(text, HashType.RIPEMD160, true);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -207,7 +203,6 @@ public string HashToString()
|
|||
sb.AppendLine($"SHA-256: {SHA256}");
|
||||
sb.AppendLine($"SHA-384: {SHA384}");
|
||||
sb.AppendLine($"SHA-512: {SHA512}");
|
||||
sb.Append($"RIPEMD-160: {RIPEMD160}");
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
|
|
|
@ -87,9 +87,7 @@ public enum HashType
|
|||
[Description("SHA-384")]
|
||||
SHA384,
|
||||
[Description("SHA-512")]
|
||||
SHA512,
|
||||
[Description("RIPEMD-160")]
|
||||
RIPEMD160
|
||||
SHA512
|
||||
}
|
||||
|
||||
public enum BorderType
|
||||
|
|
|
@ -105,7 +105,6 @@
|
|||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Design" />
|
||||
<Reference Include="System.Drawing" />
|
||||
<Reference Include="System.Speech" />
|
||||
<Reference Include="System.Web" />
|
||||
<Reference Include="System.Windows.Forms" />
|
||||
<Reference Include="System.Xml" />
|
||||
|
|
|
@ -40,7 +40,6 @@
|
|||
using System.Drawing.Imaging;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Speech.Synthesis;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
@ -1347,11 +1346,7 @@ public static void PlayCaptureSound(TaskSettings taskSettings)
|
|||
{
|
||||
if (taskSettings == null) taskSettings = TaskSettings.GetDefaultTaskSettings();
|
||||
|
||||
if (!string.IsNullOrEmpty(taskSettings.AdvancedSettings.SpeechCapture))
|
||||
{
|
||||
TextToSpeechAsync(taskSettings.AdvancedSettings.SpeechCapture);
|
||||
}
|
||||
else if (taskSettings.AdvancedSettings.UseCustomCaptureSound && !string.IsNullOrEmpty(taskSettings.AdvancedSettings.CustomCaptureSoundPath))
|
||||
if (taskSettings.AdvancedSettings.UseCustomCaptureSound && !string.IsNullOrEmpty(taskSettings.AdvancedSettings.CustomCaptureSoundPath))
|
||||
{
|
||||
Helpers.PlaySoundAsync(taskSettings.AdvancedSettings.CustomCaptureSoundPath);
|
||||
}
|
||||
|
@ -1365,11 +1360,7 @@ public static void PlayTaskCompleteSound(TaskSettings taskSettings)
|
|||
{
|
||||
if (taskSettings == null) taskSettings = TaskSettings.GetDefaultTaskSettings();
|
||||
|
||||
if (!string.IsNullOrEmpty(taskSettings.AdvancedSettings.SpeechTaskCompleted))
|
||||
{
|
||||
TextToSpeechAsync(taskSettings.AdvancedSettings.SpeechTaskCompleted);
|
||||
}
|
||||
else if (taskSettings.AdvancedSettings.UseCustomTaskCompletedSound && !string.IsNullOrEmpty(taskSettings.AdvancedSettings.CustomTaskCompletedSoundPath))
|
||||
if (taskSettings.AdvancedSettings.UseCustomTaskCompletedSound && !string.IsNullOrEmpty(taskSettings.AdvancedSettings.CustomTaskCompletedSoundPath))
|
||||
{
|
||||
Helpers.PlaySoundAsync(taskSettings.AdvancedSettings.CustomTaskCompletedSoundPath);
|
||||
}
|
||||
|
@ -1393,17 +1384,6 @@ public static void PlayErrorSound(TaskSettings taskSettings)
|
|||
}
|
||||
}
|
||||
|
||||
public static void TextToSpeechAsync(string text)
|
||||
{
|
||||
Task.Run(() =>
|
||||
{
|
||||
using (SpeechSynthesizer speaker = new SpeechSynthesizer())
|
||||
{
|
||||
speaker.Speak(text);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static void OpenUploadersConfigWindow(IUploaderService uploaderService = null)
|
||||
{
|
||||
SettingManager.WaitUploadersConfig();
|
||||
|
|
|
@ -429,9 +429,6 @@ public class TaskSettingsAdvanced
|
|||
Editor(typeof(WavFileNameEditor), typeof(UITypeEditor))]
|
||||
public string CustomCaptureSoundPath { get; set; }
|
||||
|
||||
[Category("Sound"), DefaultValue(""), Description("If this text is not empty then when the screen is captured text to speech engine will say the phrase entered instead of playing the default sound.")]
|
||||
public string SpeechCapture { get; set; }
|
||||
|
||||
[Category("Sound"), DefaultValue(false), Description("Enable/disable custom task complete sound.")]
|
||||
public bool UseCustomTaskCompletedSound { get; set; }
|
||||
|
||||
|
@ -439,9 +436,6 @@ public class TaskSettingsAdvanced
|
|||
Editor(typeof(WavFileNameEditor), typeof(UITypeEditor))]
|
||||
public string CustomTaskCompletedSoundPath { get; set; }
|
||||
|
||||
[Category("Sound"), DefaultValue(""), Description("If this text is not empty then when a task is completed text to speech engine will say the phrase entered instead of playing the default sound.")]
|
||||
public string SpeechTaskCompleted { get; set; }
|
||||
|
||||
[Category("Sound"), DefaultValue(false), Description("Enable/disable custom error sound.")]
|
||||
public bool UseCustomErrorSound { get; set; }
|
||||
|
||||
|
|
Loading…
Reference in a new issue