1414 public class Handler
1515 {
1616 #region Members
17-
17+ private const string Name_of_JSONRPCEXCEPTION = "JsonRpcException&" ;
1818 private static int _sessionHandlerMasterVersion = 1 ;
1919 [ ThreadStatic ]
20+ private static Dictionary < string , Handler > _sessionHandlersLocal ;
21+ [ ThreadStatic ]
2022 private static int _sessionHandlerLocalVersion = 0 ;
2123 private static ConcurrentDictionary < string , Handler > _sessionHandlersMaster ;
22- [ ThreadStatic ]
23- private static Dictionary < string , Handler > _sessionHandlersLocal ;
24+
2425 private static volatile string _defaultSessionId ;
2526 #endregion
2627
@@ -212,13 +213,10 @@ public JsonResponse Handle(JsonRequest Rpc, Object RpcContext = null)
212213
213214 SMDService metadata = null ;
214215 Delegate handle = null ;
215- var haveMetadata = this . MetaData . Services . TryGetValue ( Rpc . Method , out metadata ) ;
216- if ( haveMetadata )
216+ if ( this . MetaData . Services . TryGetValue ( Rpc . Method , out metadata ) )
217217 {
218218 handle = metadata . dele ;
219- }
220-
221- if ( haveMetadata == false || metadata == null )
219+ } else if ( metadata == null )
222220 {
223221 JsonResponse response = new JsonResponse ( )
224222 {
@@ -229,53 +227,45 @@ public JsonResponse Handle(JsonRequest Rpc, Object RpcContext = null)
229227 return PostProcess ( Rpc , response , RpcContext ) ;
230228 }
231229
232- bool isJObject = Rpc . Params is Newtonsoft . Json . Linq . JObject ;
233- bool isJArray = Rpc . Params is Newtonsoft . Json . Linq . JArray ;
234230 object [ ] parameters = null ;
235231 bool expectsRefException = false ;
236232 var metaDataParamCount = metadata . parameters . Count ( x => x != null ) ;
237233
238- var getCount = Rpc . Params as ICollection ;
234+
239235 var loopCt = 0 ;
240-
236+ var getCount = Rpc . Params as ICollection ;
241237 if ( getCount != null )
242238 {
243239 loopCt = getCount . Count ;
244240 }
245241
246242 var paramCount = loopCt ;
247- if ( paramCount == metaDataParamCount - 1 && metadata . parameters [ metaDataParamCount - 1 ] . ObjectType . Name . Contains ( typeof ( JsonRpcException ) . Name ) )
243+ if ( paramCount == metaDataParamCount - 1 && metadata . parameters [ metaDataParamCount - 1 ] . ObjectType . Name . Equals ( Name_of_JSONRPCEXCEPTION ) )
248244 {
249245 paramCount ++ ;
250246 expectsRefException = true ;
251247 }
252248 parameters = new object [ paramCount ] ;
253249
254- if ( isJArray )
250+ if ( Rpc . Params is Newtonsoft . Json . Linq . JArray )
255251 {
256252 var jarr = ( ( Newtonsoft . Json . Linq . JArray ) Rpc . Params ) ;
257- //var loopCt = jarr.Count;
258- //var pCount = loopCt;
259- //if (pCount == metaDataParamCount - 1 && metadata.parameters[metaDataParamCount].GetType() == typeof(JsonRpcException))
260- // pCount++;
261- //parameters = new object[pCount];
262253 for ( int i = 0 ; i < loopCt ; i ++ )
263254 {
264255 parameters [ i ] = CleanUpParameter ( jarr [ i ] , metadata . parameters [ i ] ) ;
265256 }
266257 }
267- else if ( isJObject )
258+ else if ( Rpc . Params is Newtonsoft . Json . Linq . JObject )
268259 {
269- var jo = Rpc . Params as Newtonsoft . Json . Linq . JObject ;
270- //var loopCt = jo.Count;
271- //var pCount = loopCt;
272- //if (pCount == metaDataParamCount - 1 && metadata.parameters[metaDataParamCount].GetType() == typeof(JsonRpcException))
273- // pCount++;
274- //parameters = new object[pCount];
275- var asDict = jo as IDictionary < string , Newtonsoft . Json . Linq . JToken > ;
260+ var asDict = Rpc . Params as IDictionary < string , Newtonsoft . Json . Linq . JToken > ;
276261 for ( int i = 0 ; i < loopCt && i < metadata . parameters . Length ; i ++ )
277262 {
278- if ( asDict . ContainsKey ( metadata . parameters [ i ] . Name ) == false )
263+ if ( asDict . ContainsKey ( metadata . parameters [ i ] . Name ) == true )
264+ {
265+ parameters [ i ] = CleanUpParameter ( asDict [ metadata . parameters [ i ] . Name ] , metadata . parameters [ i ] ) ;
266+ continue ;
267+ }
268+ else
279269 {
280270 JsonResponse response = new JsonResponse ( )
281271 {
@@ -289,7 +279,6 @@ public JsonResponse Handle(JsonRequest Rpc, Object RpcContext = null)
289279 } ;
290280 return PostProcess ( Rpc , response , RpcContext ) ;
291281 }
292- parameters [ i ] = CleanUpParameter ( jo [ metadata . parameters [ i ] . Name ] , metadata . parameters [ i ] ) ;
293282 }
294283 }
295284
@@ -348,17 +337,20 @@ public JsonResponse Handle(JsonRequest Rpc, Object RpcContext = null)
348337
349338 var last = parameters . LastOrDefault ( ) ;
350339 var contextException = RpcGetAndRemoveRpcException ( ) ;
340+ JsonResponse response = null ;
351341 if ( contextException != null )
352342 {
353- JsonResponse response = new JsonResponse ( ) { Error = ProcessException ( Rpc , contextException ) , Id = Rpc . Id } ;
354- return PostProcess ( Rpc , response , RpcContext ) ;
343+ response = new JsonResponse ( ) { Error = ProcessException ( Rpc , contextException ) , Id = Rpc . Id } ;
355344 }
356- if ( expectsRefException && last != null && last is JsonRpcException )
345+ else if ( expectsRefException && last != null && last is JsonRpcException )
357346 {
358- JsonResponse response = new JsonResponse ( ) { Error = ProcessException ( Rpc , last as JsonRpcException ) , Id = Rpc . Id } ;
359- return PostProcess ( Rpc , response , RpcContext ) ;
347+ response = new JsonResponse ( ) { Error = ProcessException ( Rpc , last as JsonRpcException ) , Id = Rpc . Id } ;
348+ }
349+ else
350+ {
351+ response = new JsonResponse ( ) { Result = results } ;
360352 }
361- return PostProcess ( Rpc , new JsonResponse ( ) { Result = results } , RpcContext ) ;
353+ return PostProcess ( Rpc , response , RpcContext ) ;
362354 }
363355 catch ( Exception ex )
364356 {
@@ -492,9 +484,7 @@ private object CleanUpParameter(object p, SMDAdditionalParameters metaData)
492484
493485 private JsonRpcException PreProcess ( JsonRequest request , object context )
494486 {
495- if ( externalPreProcessingHandler == null )
496- return null ;
497- return externalPreProcessingHandler ( request , context ) ;
487+ return externalPreProcessingHandler == null ? null : externalPreProcessingHandler ( request , context ) ;
498488 }
499489
500490 private JsonResponse PostProcess ( JsonRequest request , JsonResponse response , object context )
0 commit comments