@@ -107,6 +107,9 @@ public enum ConnectionState {
107
107
private ConnectionState ddpConnectionState ;
108
108
private string sessionId ;
109
109
110
+ private object subscriptionsLock = new object ( ) ;
111
+ private object methodCallsLock = new object ( ) ;
112
+
110
113
private Dictionary < string , Subscription > subscriptions = new Dictionary < string , Subscription > ( ) ;
111
114
private Dictionary < string , MethodCall > methodCalls = new Dictionary < string , MethodCall > ( ) ;
112
115
@@ -155,14 +158,18 @@ public void OnWebSocketOpen()
155
158
{
156
159
OnDebugMessage ? . Invoke ( "Websocket open" ) ;
157
160
Send ( GetConnectMessage ( ) ) ;
158
- foreach ( Subscription subscription in subscriptions . Values )
159
- {
160
- Send ( GetSubscriptionMessage ( subscription ) ) ;
161
- }
162
- foreach ( MethodCall methodCall in methodCalls . Values )
163
- {
164
- Send ( GetMethodCallMessage ( methodCall ) ) ;
165
- }
161
+ lock ( subscriptionsLock ) {
162
+ foreach ( Subscription subscription in subscriptions . Values )
163
+ {
164
+ Send ( GetSubscriptionMessage ( subscription ) ) ;
165
+ }
166
+ }
167
+ lock ( methodCallsLock ) {
168
+ foreach ( MethodCall methodCall in methodCalls . Values )
169
+ {
170
+ Send ( GetMethodCallMessage ( methodCall ) ) ;
171
+ }
172
+ }
166
173
}
167
174
168
175
private void OnWebSocketError ( string reason ) {
@@ -177,8 +184,14 @@ private void OnWebSocketClose(bool wasClean) {
177
184
if ( wasClean ) {
178
185
ddpConnectionState = ConnectionState . CLOSED ;
179
186
sessionId = null ;
180
- subscriptions . Clear ( ) ;
181
- methodCalls . Clear ( ) ;
187
+ lock ( subscriptionsLock )
188
+ {
189
+ subscriptions . Clear ( ) ;
190
+ }
191
+ lock ( methodCallsLock )
192
+ {
193
+ methodCalls . Clear ( ) ;
194
+ }
182
195
OnDisconnected ? . Invoke ( this ) ;
183
196
} else {
184
197
ddpConnectionState = ConnectionState . DISCONNECTED ;
@@ -230,7 +243,10 @@ private void HandleMessage(JSONObject message) {
230
243
231
244
case MessageType . NOSUB : {
232
245
string subscriptionId = message [ Field . ID ] . str ;
233
- subscriptions . Remove ( subscriptionId ) ;
246
+ lock ( subscriptionsLock )
247
+ {
248
+ subscriptions . Remove ( subscriptionId ) ;
249
+ }
234
250
235
251
if ( message . HasField ( Field . ERROR ) ) {
236
252
OnError ? . Invoke ( GetError ( message [ Field . ERROR ] ) ) ;
@@ -266,7 +282,11 @@ private void HandleMessage(JSONObject message) {
266
282
string [ ] subscriptionIds = ToStringArray ( message [ Field . SUBS ] ) ;
267
283
268
284
foreach ( string subscriptionId in subscriptionIds ) {
269
- Subscription subscription = subscriptions [ subscriptionId ] ;
285
+ Subscription subscription ;
286
+ lock ( subscriptionsLock )
287
+ {
288
+ subscription = subscriptions [ subscriptionId ] ;
289
+ }
270
290
if ( subscription != null ) {
271
291
subscription . isReady = true ;
272
292
subscription . OnReady ? . Invoke ( subscription ) ;
@@ -294,14 +314,21 @@ private void HandleMessage(JSONObject message) {
294
314
295
315
case MessageType . RESULT : {
296
316
string methodCallId = message [ Field . ID ] . str ;
297
- MethodCall methodCall = methodCalls [ methodCallId ] ;
317
+ MethodCall methodCall ;
318
+ lock ( methodCallsLock )
319
+ {
320
+ methodCall = methodCalls [ methodCallId ] ;
321
+ }
298
322
if ( methodCall != null ) {
299
323
if ( message . HasField ( Field . ERROR ) ) {
300
324
methodCall . error = GetError ( message [ Field . ERROR ] ) ;
301
325
}
302
326
methodCall . result = message [ Field . RESULT ] ;
303
327
if ( methodCall . hasUpdated ) {
304
- methodCalls . Remove ( methodCallId ) ;
328
+ lock ( methodCallsLock )
329
+ {
330
+ methodCalls . Remove ( methodCallId ) ;
331
+ }
305
332
}
306
333
methodCall . hasResult = true ;
307
334
methodCall . OnResult ? . Invoke ( methodCall ) ;
@@ -312,10 +339,17 @@ private void HandleMessage(JSONObject message) {
312
339
case MessageType . UPDATED : {
313
340
string [ ] methodCallIds = ToStringArray ( message [ Field . METHODS ] ) ;
314
341
foreach ( string methodCallId in methodCallIds ) {
315
- MethodCall methodCall = methodCalls [ methodCallId ] ;
342
+ MethodCall methodCall ;
343
+ lock ( methodCallsLock )
344
+ {
345
+ methodCall = methodCalls [ methodCallId ] ;
346
+ }
316
347
if ( methodCall != null ) {
317
348
if ( methodCall . hasResult ) {
318
- methodCalls . Remove ( methodCallId ) ;
349
+ lock ( methodCallsLock )
350
+ {
351
+ methodCalls . Remove ( methodCallId ) ;
352
+ }
319
353
}
320
354
methodCall . hasUpdated = true ;
321
355
methodCall . OnUpdated ? . Invoke ( methodCall ) ;
@@ -503,7 +537,10 @@ public async Task<MethodCall> CallAsync(string methodName, params JSONObject[] i
503
537
methodName = methodName ,
504
538
items = items
505
539
} ;
506
- methodCalls [ methodCall . id ] = methodCall ;
540
+ lock ( methodCallsLock )
541
+ {
542
+ methodCalls [ methodCall . id ] = methodCall ;
543
+ }
507
544
await SendAsync ( GetMethodCallMessage ( methodCall ) ) ;
508
545
return methodCall ;
509
546
}
@@ -514,7 +551,10 @@ public MethodCall Call(string methodName, params JSONObject[] items) {
514
551
methodName = methodName ,
515
552
items = items
516
553
} ;
517
- methodCalls [ methodCall . id ] = methodCall ;
554
+ lock ( methodCallsLock )
555
+ {
556
+ methodCalls [ methodCall . id ] = methodCall ;
557
+ }
518
558
Send ( GetMethodCallMessage ( methodCall ) ) ;
519
559
return methodCall ;
520
560
}
0 commit comments