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