Esempio n. 1
0
        private static void AsyncProcessInternal(string sessionId, string jsonRpc, object jsonRpcContext, Action <string> callback)
        {
            Handler handler = Handler.GetSessionHandler(sessionId);

            Tuple <JsonRequest>[] batch = null;
            if (isSingleRpc(jsonRpc))
            {
                batch = new[] { Tuple.Create(JsonConvert.DeserializeObject <JsonRequest>(jsonRpc)) };
            }
            else
            {
                batch = JsonConvert.DeserializeObject <JsonRequest[]>(jsonRpc)
                        .Select(request => new Tuple <JsonRequest>(request))
                        .ToArray();
            }

            if (batch.Length == 0)
            {
                callback.Invoke(JsonConvert.SerializeObject(new JsonResponse
                {
                    Error = handler.ProcessParseException(jsonRpc,
                                                          new JsonRpcException(3200, "Invalid Request", "Batch of calls was empty."))
                }));
            }

            foreach (var tuple in batch)
            {
                JsonRequest  jsonRequest  = tuple.Item1;
                JsonResponse jsonResponse = new JsonResponse();

                if (jsonRequest == null)
                {
                    jsonResponse.Error = handler.ProcessParseException(jsonRpc,
                                                                       new JsonRpcException(-32700, "Parse error",
                                                                                            "Invalid JSON was received by the server. An error occurred on the server while parsing the JSON text."));
                }
                else
                {
                    jsonResponse.Id = jsonRequest.Id;

                    if (jsonRequest.Method == null)
                    {
                        jsonResponse.Error = handler.ProcessParseException(jsonRpc,
                                                                           new JsonRpcException(-32600, "Invalid Request", "Missing property 'method'"));
                    }
                    else
                    {
                        handler.Handle(jsonRequest, jsonRpcContext,
                                       delegate(JsonResponse a)
                        {
                            a.Id = jsonRequest.Id;
                            if (a.Id != null || a.Error != null)
                            {
                                callback.Invoke(JsonConvert.SerializeObject(a));
                            }
                        }
                                       );
                    }
                }
            }
        }