This repository has been archived by the owner on Sep 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
94 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System; | ||
|
||
namespace IEC60870.Util | ||
{ | ||
static public class PubSubExtensions | ||
{ | ||
static private readonly PubSubHub hub = new PubSubHub(); | ||
|
||
static public void Publish<T>(this object obj, string topic, T data) | ||
{ | ||
hub.Publish(obj, topic, data); | ||
} | ||
|
||
static public void Subscribe<T>(this object obj, string topic, Action<T> handler) | ||
{ | ||
hub.Subscribe(obj, topic, handler); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace IEC60870.Util | ||
{ | ||
public class PubSubHub | ||
{ | ||
internal class Handler | ||
{ | ||
public Delegate Action { get; set; } | ||
public WeakReference Sender { get; set; } | ||
public Type Type { get; set; } | ||
} | ||
|
||
internal object locker = new object(); | ||
internal Dictionary<string, List<Handler>> handlers = new Dictionary<string, List<Handler>>(); | ||
|
||
public void Publish<T>(object sender, string topic, T data) | ||
{ | ||
if (handlers.ContainsKey(topic)) | ||
{ | ||
var listHandler = handlers[topic]; | ||
listHandler.ForEach(handler => | ||
{ | ||
if (handler.Sender.IsAlive) | ||
{ | ||
((Action<T>)handler.Action)(data); | ||
} | ||
else | ||
{ | ||
lock (locker) | ||
{ | ||
handlers[topic].Remove(handler); | ||
} | ||
} | ||
}); | ||
} | ||
} | ||
|
||
public void Subscribe<T>(object sender, string topic, Action<T> handler) | ||
{ | ||
var item = new Handler | ||
{ | ||
Action = handler, | ||
Sender = new WeakReference(sender), | ||
Type = typeof(T) | ||
}; | ||
|
||
if (handlers.ContainsKey(topic)) | ||
{ | ||
lock (locker) | ||
{ | ||
handlers[topic].Add(item); | ||
} | ||
} | ||
else | ||
{ | ||
lock (locker) | ||
{ | ||
handlers[topic] = new List<Handler>(); | ||
handlers[topic].Add(item); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.