Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Add TrapSignals and UntrapSignals to Connection.cs #23

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
42 changes: 42 additions & 0 deletions src/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,25 @@ internal virtual uint Send (Message msg)
//temporary hack
internal void DispatchSignals ()
{
lock (delayed_signals)
lock (delayed_signals_recycle) {
if (trap_signals_flush) {
delayed_signals.Clear ();
delayed_signals_recycle.Clear ();
trap_signals_flush = false;
} else {
while (delayed_signals.Count != 0) {
Message msg = delayed_signals.Dequeue ();
HandleSignal (msg);
}

while (delayed_signals_recycle.Count != 0) {
delayed_signals.Enqueue (delayed_signals_recycle.Dequeue ());
}
}
}


lock (Inbound) {
while (Inbound.Count != 0) {
Message msg = Inbound.Dequeue ();
Expand Down Expand Up @@ -268,6 +287,23 @@ internal virtual void HandleMessage (Message msg)

Dictionary<uint,PendingCall> pendingCalls = new Dictionary<uint,PendingCall> ();

private Queue<Message> delayed_signals = new Queue<Message> ();
private Queue<Message> delayed_signals_recycle = new Queue<Message> ();
private int trap_signals_ref;
private bool trap_signals_flush;

public void TrapSignals ()
{
Interlocked.Increment (ref trap_signals_ref);
}

public void UntrapSignals ()
{
if (Interlocked.Decrement (ref trap_signals_ref) == 0) {
trap_signals_flush = true;
}
}

//this might need reworking with MulticastDelegate
internal void HandleSignal (Message msg)
{
Expand Down Expand Up @@ -304,6 +340,12 @@ internal void HandleSignal (Message msg)
//TODO: how should we handle this condition? sending an Error may not be appropriate in this case
if (Protocol.Verbose)
Console.Error.WriteLine ("Warning: No signal handler for " + signal.Member);

if (trap_signals_ref > 0) {
lock (delayed_signals_recycle) {
delayed_signals_recycle.Enqueue (msg);
}
}
}
}

Expand Down