@@ -166,12 +166,23 @@ private void ProcessMessage(IDictionary<string, object> message)
166
166
}
167
167
}
168
168
169
+ private bool ValidateClientMessage ( IDictionary < string , object > message , out int requestId )
170
+ {
171
+ requestId = 0 ;
172
+
173
+ if ( ! ( message . TryGetValue ( "clientId" , out object clientIdObj ) &&
174
+ clientIdObj is string clientId && clientId == ClientId ) )
175
+ return false ;
176
+
177
+ return message . TryGetValue ( "requestId" , out object requestIdObj ) &&
178
+ Int32 . TryParse ( requestIdObj ? . ToString ( ) , out requestId ) ;
179
+ }
180
+
169
181
void ProcessDeleteEventMessage ( IDictionary < string , object > message )
170
182
{
171
- string clientId = message [ "clientId" ] as string ;
172
- if ( clientId != ClientId )
183
+ if ( ! ValidateClientMessage ( message , out int requestId ) )
173
184
return ;
174
- int requestId = Convert . ToInt32 ( message [ "requestId" ] ) ;
185
+
175
186
if ( Subscriptions . TryGetValue ( requestId , out IParseLiveQuerySubscription subscription ) )
176
187
{
177
188
subscription . OnDelete ( ParseObjectCoder . Instance . Decode (
@@ -183,10 +194,9 @@ void ProcessDeleteEventMessage(IDictionary<string, object> message)
183
194
184
195
void ProcessLeaveEventMessage ( IDictionary < string , object > message )
185
196
{
186
- string clientId = message [ "clientId" ] as string ;
187
- if ( clientId != ClientId )
197
+ if ( ! ValidateClientMessage ( message , out int requestId ) )
188
198
return ;
189
- int requestId = Convert . ToInt32 ( message [ "requestId" ] ) ;
199
+
190
200
if ( Subscriptions . TryGetValue ( requestId , out IParseLiveQuerySubscription subscription ) )
191
201
{
192
202
subscription . OnLeave (
@@ -203,10 +213,9 @@ void ProcessLeaveEventMessage(IDictionary<string, object> message)
203
213
204
214
void ProcessUpdateEventMessage ( IDictionary < string , object > message )
205
215
{
206
- string clientId = message [ "clientId" ] as string ;
207
- if ( clientId != ClientId )
216
+ if ( ! ValidateClientMessage ( message , out int requestId ) )
208
217
return ;
209
- int requestId = Convert . ToInt32 ( message [ "requestId" ] ) ;
218
+
210
219
if ( Subscriptions . TryGetValue ( requestId , out IParseLiveQuerySubscription subscription ) )
211
220
{
212
221
subscription . OnUpdate (
@@ -223,10 +232,9 @@ void ProcessUpdateEventMessage(IDictionary<string, object> message)
223
232
224
233
void ProcessEnterEventMessage ( IDictionary < string , object > message )
225
234
{
226
- string clientId = message [ "clientId" ] as string ;
227
- if ( clientId != ClientId )
235
+ if ( ! ValidateClientMessage ( message , out int requestId ) )
228
236
return ;
229
- int requestId = Convert . ToInt32 ( message [ "requestId" ] ) ;
237
+
230
238
if ( Subscriptions . TryGetValue ( requestId , out IParseLiveQuerySubscription subscription ) )
231
239
{
232
240
subscription . OnEnter (
@@ -243,10 +251,9 @@ void ProcessEnterEventMessage(IDictionary<string, object> message)
243
251
244
252
void ProcessCreateEventMessage ( IDictionary < string , object > message )
245
253
{
246
- string clientId = message [ "clientId" ] as string ;
247
- if ( clientId != ClientId )
254
+ if ( ! ValidateClientMessage ( message , out int requestId ) )
248
255
return ;
249
- int requestId = Convert . ToInt32 ( message [ "requestId" ] ) ;
256
+
250
257
if ( Subscriptions . TryGetValue ( requestId , out IParseLiveQuerySubscription subscription ) )
251
258
{
252
259
subscription . OnCreate ( ParseObjectCoder . Instance . Decode (
@@ -258,19 +265,27 @@ void ProcessCreateEventMessage(IDictionary<string, object> message)
258
265
259
266
void ProcessErrorMessage ( IDictionary < string , object > message )
260
267
{
261
- ParseLiveQueryErrorEventArgs errorArgs = new ParseLiveQueryErrorEventArgs (
262
- Convert . ToInt32 ( message [ "code" ] ) ,
263
- message [ "error" ] as string ,
264
- Convert . ToBoolean ( message [ "reconnect" ] ) ) ;
268
+ if ( ! ( message . TryGetValue ( "code" , out object codeObj ) &&
269
+ Int32 . TryParse ( codeObj ? . ToString ( ) , out int code ) ) )
270
+ return ;
271
+
272
+ if ( ! ( message . TryGetValue ( "error" , out object errorObj ) &&
273
+ errorObj is string error ) )
274
+ return ;
275
+
276
+ if ( ! ( message . TryGetValue ( "reconnect" , out object reconnectObj ) &&
277
+ Boolean . TryParse ( reconnectObj ? . ToString ( ) , out bool reconnect ) ) )
278
+ return ;
279
+
280
+ ParseLiveQueryErrorEventArgs errorArgs = new ParseLiveQueryErrorEventArgs ( code , error , reconnect ) ;
265
281
Error ? . Invoke ( this , errorArgs ) ;
266
282
}
267
283
268
284
void ProcessUnsubscriptionMessage ( IDictionary < string , object > message )
269
285
{
270
- string clientId = message [ "clientId" ] as string ;
271
- if ( clientId != ClientId )
286
+ if ( ! ValidateClientMessage ( message , out int requestId ) )
272
287
return ;
273
- int requestId = Convert . ToInt32 ( message [ "requestId" ] ) ;
288
+
274
289
if ( UnsubscriptionSignals . TryGetValue ( requestId , out TaskCompletionSource unsubscriptionSign ) )
275
290
{
276
291
unsubscriptionSign ? . TrySetResult ( ) ;
@@ -279,10 +294,9 @@ void ProcessUnsubscriptionMessage(IDictionary<string, object> message)
279
294
280
295
void ProcessSubscriptionMessage ( IDictionary < string , object > message )
281
296
{
282
- string clientId = message [ "clientId" ] as string ;
283
- if ( clientId != ClientId )
297
+ if ( ! ValidateClientMessage ( message , out int requestId ) )
284
298
return ;
285
- int requestId = Convert . ToInt32 ( message [ "requestId" ] ) ;
299
+
286
300
if ( SubscriptionSignals . TryGetValue ( requestId , out TaskCompletionSource subscriptionSignal ) )
287
301
{
288
302
subscriptionSignal ? . TrySetResult ( ) ;
@@ -291,8 +305,12 @@ void ProcessSubscriptionMessage(IDictionary<string, object> message)
291
305
292
306
void ProcessConnectionMessage ( IDictionary < string , object > message )
293
307
{
308
+ if ( ! ( message . TryGetValue ( "clientId" , out object clientIdObj ) &&
309
+ clientIdObj is string clientId ) )
310
+ return ;
311
+
312
+ ClientId = clientId ;
294
313
_state = ParseLiveQueryState . Connected ;
295
- ClientId = message [ "clientId" ] as string ;
296
314
ConnectionSignal . TrySetResult ( ) ;
297
315
}
298
316
0 commit comments