|
1 | 1 | using System; |
| 2 | +using System.Collections.Concurrent; |
2 | 3 | using System.Collections.Generic; |
3 | 4 | using System.Linq; |
4 | 5 | using System.Net.WebSockets; |
@@ -29,17 +30,17 @@ internal Connection(string url, int delay, WebSocket ws, ILoggerFactory loggerFa |
29 | 30 |
|
30 | 31 | _logger = LoggerFactory.CreateLogger<Connection>(); |
31 | 32 | _socketQueue = new TaskQueue(); |
32 | | - _callbacks = new Dictionary<int, MessageTask>(); |
33 | | - _sessions = new Dictionary<string, CDPSession>(); |
| 33 | + _callbacks = new ConcurrentDictionary<int, MessageTask>( ); |
| 34 | + _sessions = new ConcurrentDictionary<string, CDPSession>(); |
34 | 35 | _websocketReaderCancellationSource = new CancellationTokenSource(); |
35 | 36 |
|
36 | 37 | Task.Factory.StartNew(GetResponseAsync); |
37 | 38 | } |
38 | 39 |
|
39 | 40 | #region Private Members |
40 | 41 | private int _lastId; |
41 | | - private readonly Dictionary<int, MessageTask> _callbacks; |
42 | | - private readonly Dictionary<string, CDPSession> _sessions; |
| 42 | + private readonly ConcurrentDictionary<int, MessageTask> _callbacks; |
| 43 | + private readonly ConcurrentDictionary<string, CDPSession> _sessions; |
43 | 44 | private readonly TaskQueue _socketQueue; |
44 | 45 | private readonly CancellationTokenSource _websocketReaderCancellationSource; |
45 | 46 | #endregion |
@@ -91,7 +92,7 @@ internal async Task<JObject> SendAsync(string method, dynamic args = null, bool |
91 | 92 | throw new TargetClosedException($"Protocol error({method}): Target closed."); |
92 | 93 | } |
93 | 94 |
|
94 | | - var id = ++_lastId; |
| 95 | + var id = Interlocked.Increment(ref _lastId); |
95 | 96 | var message = JsonConvert.SerializeObject(new Dictionary<string, object> |
96 | 97 | { |
97 | 98 | { MessageKeys.Id, id }, |
@@ -133,7 +134,7 @@ internal async Task<CDPSession> CreateSessionAsync(TargetInfo targetInfo) |
133 | 134 | targetId = targetInfo.TargetId |
134 | 135 | }).ConfigureAwait(false))[MessageKeys.SessionId].AsString(); |
135 | 136 | var session = new CDPSession(this, targetInfo.Type, sessionId); |
136 | | - _sessions.Add(sessionId, session); |
| 137 | + _sessions.TryAdd(sessionId, session); |
137 | 138 | return session; |
138 | 139 | } |
139 | 140 |
|
@@ -263,7 +264,7 @@ private void ProcessResponse(string response) |
263 | 264 | { |
264 | 265 | //If we get the object we are waiting for we return if |
265 | 266 | //if not we add this to the list, sooner or later some one will come for it |
266 | | - if (_callbacks.TryGetValue(id.Value, out var callback) && _callbacks.Remove(id.Value)) |
| 267 | + if (_callbacks.TryRemove(id.Value, out var callback)) |
267 | 268 | { |
268 | 269 | if (obj[MessageKeys.Error] != null) |
269 | 270 | { |
@@ -291,7 +292,7 @@ private void ProcessResponse(string response) |
291 | 292 | else if (method == "Target.detachedFromTarget") |
292 | 293 | { |
293 | 294 | var sessionId = param[MessageKeys.SessionId].AsString(); |
294 | | - if (_sessions.TryGetValue(sessionId, out var session) && _sessions.Remove(sessionId) && !session.IsClosed) |
| 295 | + if (_sessions.TryRemove(sessionId, out var session) && !session.IsClosed) |
295 | 296 | { |
296 | 297 | session.OnClosed(); |
297 | 298 | } |
|
0 commit comments