using System; using System.Collections.Generic; using System.Threading; namespace Luticate2.Utils.Hubs { public class LuHubConnectionTracker { protected IDictionary> ConnectionIds { get; set; } protected Mutex Mutex; public LuHubConnectionTracker() { Mutex = new Mutex(); ConnectionIds = new Dictionary>(); } public static string GetNameFromType(Type type) { return type.FullName; } public void Add(string name, string id) { Mutex.WaitOne(); GetReal(name).Add(id); Mutex.ReleaseMutex(); } public void Add(Type type, string id) { Add(GetNameFromType(type), id); } public void Add(object obj, string id) { Add(obj.GetType(), id); } public void Add(string id) { Add(typeof(T), id); } public void Remove(string name, string id) { Mutex.WaitOne(); GetReal(name).Remove(id); Mutex.ReleaseMutex(); } public void Remove(Type type, string id) { Remove(GetNameFromType(type), id); } public void Remove(object obj, string id) { Remove(obj.GetType(), id); } public void Remove(string id) { Remove(typeof(T), id); } public IList GetReal(string name) { if (!ConnectionIds.ContainsKey(name)) { ConnectionIds.Add(name, new List()); } return ConnectionIds[name]; } public IList Get(string name) { Mutex.WaitOne(); var copy = new List(GetReal(name)); Mutex.ReleaseMutex(); return copy; } public IList Get(Type type) { return Get(GetNameFromType(type)); } public IList Get(object obj) { return Get(GetNameFromType(obj.GetType())); } public IList Get() { return Get(GetNameFromType(typeof(T))); } } }