Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ConnectionMapping
- {
- private readonly Dictionary _connections =
- new Dictionary();
- public int Count
- {
- get { return _connections.Count; }
- }
- public void Add(T key, string connectionId)
- {
- lock (_connections)
- {
- HashSet connections;
- if (!_connections.TryGetValue(key, out connections))
- {
- connections = new HashSet();
- _connections.Add(key, connections);
- }
- lock (connections)
- {
- connections.Add(connectionId);
- }
- }
- }
- public IEnumerable GetConnections(T key)
- {
- HashSet connections;
- if (_connections.TryGetValue(key, out connections))
- {
- return connections;
- }
- return Enumerable.Empty();
- }
- public void Remove(T key, string connectionId)
- {
- lock (_connections)
- {
- HashSet connections;
- if (!_connections.TryGetValue(key, out connections))
- {
- return;
- }
- lock (connections)
- {
- connections.Remove(connectionId);
- if (connections.Count == 0)
- {
- _connections.Remove(key);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement