You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LuHubConnectionTracker.cs 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. namespace Luticate2.Utils.Hubs
  5. {
  6. public class LuHubConnectionTracker
  7. {
  8. protected IDictionary<string, IList<string>> ConnectionIds { get; set; }
  9. protected Mutex Mutex;
  10. public LuHubConnectionTracker()
  11. {
  12. Mutex = new Mutex();
  13. ConnectionIds = new Dictionary<string, IList<string>>();
  14. }
  15. public static string GetNameFromType(Type type)
  16. {
  17. return type.FullName;
  18. }
  19. public void Add(string name, string id)
  20. {
  21. Mutex.WaitOne();
  22. GetReal(name).Add(id);
  23. Mutex.ReleaseMutex();
  24. }
  25. public void Add(Type type, string id)
  26. {
  27. Add(GetNameFromType(type), id);
  28. }
  29. public void Add(object obj, string id)
  30. {
  31. Add(obj.GetType(), id);
  32. }
  33. public void Add<T>(string id)
  34. {
  35. Add(typeof(T), id);
  36. }
  37. public void Remove(string name, string id)
  38. {
  39. Mutex.WaitOne();
  40. GetReal(name).Remove(id);
  41. Mutex.ReleaseMutex();
  42. }
  43. public void Remove(Type type, string id)
  44. {
  45. Remove(GetNameFromType(type), id);
  46. }
  47. public void Remove(object obj, string id)
  48. {
  49. Remove(obj.GetType(), id);
  50. }
  51. public void Remove<T>(string id)
  52. {
  53. Remove(typeof(T), id);
  54. }
  55. public IList<string> GetReal(string name)
  56. {
  57. if (!ConnectionIds.ContainsKey(name))
  58. {
  59. ConnectionIds.Add(name, new List<string>());
  60. }
  61. return ConnectionIds[name];
  62. }
  63. public IList<string> Get(string name)
  64. {
  65. Mutex.WaitOne();
  66. var copy = new List<string>(GetReal(name));
  67. Mutex.ReleaseMutex();
  68. return copy;
  69. }
  70. public IList<string> Get(Type type)
  71. {
  72. return Get(GetNameFromType(type));
  73. }
  74. public IList<string> Get(object obj)
  75. {
  76. return Get(GetNameFromType(obj.GetType()));
  77. }
  78. public IList<string> Get<T>()
  79. {
  80. return Get(GetNameFromType(typeof(T)));
  81. }
  82. }
  83. }