public static JsonRpcResponse CreateJsonRpcResponse(JsonRpcRequest request, Response response) { if (response.IsError) { return CreateSuccessfulJsonRpcResponse(request, response.Content); } return CreateErrorJsonRpcResponse(request, response.Error); }
private async void HandleReceivedJObject(JObject jObject) { if (jObject.IsJsonRpcRequest()) { JsonRpcRequest jsonRpcRequest = jObject.ToObject<JsonRpcRequest>(); String method = jsonRpcRequest.Method; if (jsonRpcRequest.IsNotification) { if (_subscribedNotificationCallbacks.ContainsKey(method)) { _subscribedNotificationCallbacks[method].Invoke(jsonRpcRequest.Params); } } else { if (_subscribedRequestCallbacks.ContainsKey(method)) { Response response = await _subscribedRequestCallbacks[method].Invoke(jsonRpcRequest.Params); JsonRpcResponse jsonRpcResponse = JsonRpcResponse.Factory.CreateJsonRpcResponse(jsonRpcRequest, response); StreamCommand responseCommand = JObjectStreamCommand.Factory.Create(JObject.FromObject(jsonRpcResponse)); _sendToClientQueue.Enqueue(responseCommand); } } } else if (jObject.IsJsonRpcResponse()) { JsonRpcResponse jsonRpcResponse = jObject.ToObject<JsonRpcResponse>(); TaskCompletionSource<Response> requestTaskCompletionSource; if (_incompleteJsonRpcRequests.TryGetValue(jsonRpcResponse.Id, out requestTaskCompletionSource)) { Response response = new Response(); if (jObject.IsErrorJsonRpcResponse()) { response.Error = jsonRpcResponse.Error.Message; } else { response.Content = jsonRpcResponse.Result; } requestTaskCompletionSource.SetResult(response); } } }