Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 26 additions & 19 deletions Signal-Windows.Lib/Events/SignalMessageEventArgs.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Signal_Windows.Models;

namespace Signal_Windows.Lib.Events
{
public class SignalMessageEventArgs : EventArgs
{
public SignalMessage Message { get; private set; }

public SignalMessageEventArgs(SignalMessage message)
{
Message = message;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Signal_Windows.Models;

namespace Signal_Windows.Lib.Events
{
public enum SignalMessageType
{
NormalMessage,
PipeEmptyMessage
}
public class SignalMessageEventArgs : EventArgs
{
public SignalMessage Message { get; private set; }
public SignalMessageType MessageType { get; private set; }

public SignalMessageEventArgs(SignalMessage message, SignalMessageType type)
{
Message = message;
MessageType = type;
}
}
}
4 changes: 4 additions & 0 deletions Signal-Windows.Lib/IncomingMessages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ public void OnMessage(SignalServiceMessagePipeMessage message)
Logger.LogWarning("OnMessage() could not handle unknown message type {0}", envelope.getType());
}
}
else if (message is SignalServiceMessagePipeEmptyMessage)
{
Handle.DispatchPipeEmptyMessage();
}
}
finally
{
Expand Down
85 changes: 78 additions & 7 deletions Signal-Windows.Lib/LibUtils.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using libsignalservice.push;
using libsignalservice;
using libsignalservice.push;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -58,24 +60,93 @@ await dispatcher.RunAsync(priority, () =>

public class LibUtils
{
public const string GlobalSemaphoreName = "SignalWindowsPrivateMessenger_Mutex";
private static readonly ILogger Logger = LibsignalLogging.CreateLogger<LibUtils>();
public const string GlobalMutexName = "SignalWindowsPrivateMessenger_Mutex";
public const string GlobalEventWaitHandleName = "SignalWindowsPrivateMessenger_EventWaitHandle";
public static string URL = "https://textsecure-service.whispersystems.org";
public static SignalServiceUrl[] ServiceUrls = new SignalServiceUrl[] { new SignalServiceUrl(URL, null) };
public static bool MainPageActive = false;
public static string USER_AGENT = "Signal-Windows";
public static uint PREKEY_BATCH_SIZE = 100;
public static bool WindowActive = false;
public static Semaphore GlobalSemaphore;
public static Mutex GlobalLock;
private static SynchronizationContext GlobalLockContext;

internal static void Lock()
{
GlobalSemaphore = new Semaphore(1, 1, GlobalSemaphoreName, out bool b);
GlobalSemaphore.WaitOne();
Logger.LogTrace("System lock locking, sync context = {0}", SynchronizationContext.Current);
GlobalLock = new Mutex(false, GlobalMutexName, out bool createdNew);
GlobalLockContext = SynchronizationContext.Current;
try
{
GlobalLock.WaitOne();
}
catch (AbandonedMutexException e)
{
Logger.LogWarning("System lock was abandoned! {0}", e.Message);
}
Logger.LogTrace("System lock locked");
}

public static bool Lock(int timeout)
{
GlobalLock = new Mutex(false, GlobalMutexName, out bool createdNew);
GlobalLockContext = SynchronizationContext.Current;
Logger.LogTrace("System lock locking with timeout, sync context = {0}", SynchronizationContext.Current);
bool success = false;
try
{
success = GlobalLock.WaitOne(timeout);
}
catch(AbandonedMutexException e)
{
Logger.LogWarning("System lock was abandoned! {0}", e.Message);
success = true;
}
Logger.LogTrace("System lock locked = {}", success);
return success;
}

public static void Unlock()
{
Logger.LogTrace("System lock releasing, sync context = {0}", SynchronizationContext.Current);
try
{
if(GlobalLockContext != null)
{
GlobalLockContext.Post((a) =>
{
GlobalLock.ReleaseMutex();
}, null);
}
else
{
GlobalLock.ReleaseMutex();
}
}
catch(Exception e)
{
Logger.LogWarning("System lock failed to unlock! {0}\n{1}", e.Message, e.StackTrace);
}
Logger.LogTrace("System lock released");
}

public static EventWaitHandle OpenResetEventSet()
{
Logger.LogTrace("OpenResetEventSet()");
var handle = new EventWaitHandle(true, EventResetMode.ManualReset, GlobalEventWaitHandleName, out bool createdNew);
if(!createdNew)
{
Logger.LogTrace("OpenResetEventSet() setting old event");
handle.Set();
}
return handle;
}

internal static void Unlock()
public static EventWaitHandle OpenResetEventUnset()
{
GlobalSemaphore.Release();
Logger.LogTrace("OpenResetEventUnset()");
return new EventWaitHandle(false, EventResetMode.ManualReset, GlobalEventWaitHandleName, out bool createdNew);
}
}
}
16 changes: 14 additions & 2 deletions Signal-Windows.Lib/SignalLibHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class SignalLibHandle
private SignalServiceMessageSender MessageSender;
private SignalServiceMessageReceiver MessageReceiver;
public BlockingCollection<SignalMessage> OutgoingQueue = new BlockingCollection<SignalMessage>(new ConcurrentQueue<SignalMessage>());
private EventWaitHandle GlobalResetEvent;

public event EventHandler<SignalMessageEventArgs> SignalMessageEvent;

Expand Down Expand Up @@ -99,7 +100,9 @@ public async Task Acquire(CoreDispatcher d, ISignalFrontend w) //TODO wrap tryca
Logger.LogTrace("Acquire() locking");
CancelSource = new CancellationTokenSource();
SemaphoreSlim.Wait(CancelSource.Token);
GlobalResetEvent = LibUtils.OpenResetEventSet();
LibUtils.Lock();
GlobalResetEvent.Reset();
var getConversationsTask = Task.Run(() =>
{
return GetConversations(); // we want to display the conversations asap!
Expand Down Expand Up @@ -146,7 +149,9 @@ public async Task Reacquire()
Logger.LogTrace("Reacquire() locking");
CancelSource = new CancellationTokenSource();
SemaphoreSlim.Wait(CancelSource.Token);
GlobalResetEvent = LibUtils.OpenResetEventSet();
LibUtils.Lock();
GlobalResetEvent.Reset();
LibsignalDBContext.ClearSessionCache();
Instance = this;
await Task.Run(() =>
Expand Down Expand Up @@ -178,9 +183,11 @@ public void Release()
IncomingMessagesTask?.Wait();
OutgoingMessagesTask?.Wait();
Instance = null;
Logger.LogTrace("Release() releasing (global and local)");
Logger.LogTrace("Release() releasing global)");
LibUtils.Unlock();
Logger.LogTrace("Release() releasing local)");
SemaphoreSlim.Release();
Logger.LogTrace("Release() released");
}

public void BackgroundRelease()
Expand Down Expand Up @@ -277,10 +284,15 @@ internal void DispatchHandleMessage(SignalMessage message, SignalConversation co
Frames[dispatcher].HandleMessage(message, conversation);
}));
}
SignalMessageEvent?.Invoke(this, new SignalMessageEventArgs(message));
SignalMessageEvent?.Invoke(this, new SignalMessageEventArgs(message, Events.SignalMessageType.NormalMessage));
Task.WaitAll(operations.ToArray());
}

internal void DispatchPipeEmptyMessage()
{
SignalMessageEvent?.Invoke(this, new SignalMessageEventArgs(null, Events.SignalMessageType.PipeEmptyMessage));
}

internal void HandleMessageSentLocked(SignalMessage msg)
{
Logger.LogTrace("HandleMessageSentLocked() locking");
Expand Down
Loading