/// <summary> /// Send an async request to the client and await later for the response or error /// </summary> public Task <ResponseResultOrError> SendRequest(RequestType requestType, object parameters) { JObject jsonMessage = new JObject(); PrepareJsonPRCMessage(jsonMessage); // Generate a unique id for the request int id = Interlocked.Increment(ref sequenceNumber); string requestId = id.ToString(); jsonMessage["id"] = requestId; jsonMessage["method"] = requestType.Method; if (parameters != null) { jsonMessage["params"] = JToken.FromObject(parameters); } // Send text message messageServer.SendMessage(jsonMessage.ToString(Formatting.None)); // Remember all elements which will be needed to handle correctly the response to the request TaskCompletionSource <ResponseResultOrError> taskCompletionSource = new TaskCompletionSource <ResponseResultOrError>(); ResponseWaitState responseWaitState = new ResponseWaitState(requestType, requestId, taskCompletionSource); responsesExpected.Add(requestId, responseWaitState); // The completion of the task will be signaled later, when the response arrives return(taskCompletionSource.Task); }
private void HandleResponse(string requestId, JToken result, JToken error) { ResponseWaitState responseWaitState = null; responsesExpected.TryGetValue(requestId, out responseWaitState); if (responseWaitState == null) { WriteServerLog(String.Format("No response was expected for request id \"{0}\"", requestId)); } else { RequestType requestType = responseWaitState.RequestType; object objResult = null; if (result != null && requestType.ResultType != null) { objResult = result.ToObject(requestType.ResultType); } object objErrorData = null; if (error != null && error["data"] != null && requestType.ErrorDataType != null) { objErrorData = error["data"].ToObject(requestType.ErrorDataType); } ResponseResultOrError resultOrError = new ResponseResultOrError(); resultOrError.result = objResult; if (error != null && error["code"] != null) { resultOrError.code = (int)error["code"]; resultOrError.message = (string)error["message"]; resultOrError.data = objErrorData; } try { responseWaitState.TaskCompletionSource.SetResult(resultOrError); } catch (Exception e) { WriteServerLog(String.Format("Task completion for the response expected by request {0} of type {1} failed : {1}", requestId, requestType.GetType().Name, e.Message)); } } }
/// <summary> /// Send an async request to the client and await later for the response or error /// </summary> public Task<ResponseResultOrError> SendRequest(RequestType requestType, object parameters) { JObject jsonMessage = new JObject(); PrepareJsonPRCMessage(jsonMessage); // Generate a unique id for the request int id = Interlocked.Increment(ref sequenceNumber); string requestId = id.ToString(); jsonMessage["id"] = requestId; jsonMessage["method"] = requestType.Method; if (parameters != null) { jsonMessage["params"] = JToken.FromObject(parameters); } // Send text message messageServer.SendMessage(jsonMessage.ToString(Formatting.None)); // Remember all elements which will be needed to handle correctly the response to the request TaskCompletionSource<ResponseResultOrError> taskCompletionSource = new TaskCompletionSource<ResponseResultOrError>(); ResponseWaitState responseWaitState = new ResponseWaitState(requestType, requestId, taskCompletionSource); responsesExpected.Add(requestId, responseWaitState); // The completion of the task will be signaled later, when the response arrives return taskCompletionSource.Task; }