Advertisement
Guest User

Untitled

a guest
Feb 21st, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. public class ConnectionMapping
  2. {
  3. private readonly Dictionary _connections =
  4. new Dictionary();
  5.  
  6. public int Count
  7. {
  8. get { return _connections.Count; }
  9. }
  10.  
  11. public void Add(T key, string connectionId)
  12. {
  13. lock (_connections)
  14. {
  15. HashSet connections;
  16. if (!_connections.TryGetValue(key, out connections))
  17. {
  18. connections = new HashSet();
  19. _connections.Add(key, connections);
  20. }
  21.  
  22. lock (connections)
  23. {
  24. connections.Add(connectionId);
  25. }
  26. }
  27. }
  28.  
  29. public IEnumerable GetConnections(T key)
  30. {
  31. HashSet connections;
  32. if (_connections.TryGetValue(key, out connections))
  33. {
  34. return connections;
  35. }
  36. return Enumerable.Empty();
  37. }
  38.  
  39. public void Remove(T key, string connectionId)
  40. {
  41. lock (_connections)
  42. {
  43. HashSet connections;
  44. if (!_connections.TryGetValue(key, out connections))
  45. {
  46. return;
  47. }
  48. lock (connections)
  49. {
  50. connections.Remove(connectionId);
  51. if (connections.Count == 0)
  52. {
  53. _connections.Remove(key);
  54. }
  55. }
  56. }
  57. }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement