public RemotingResponse InvokeSync(RemotingRequest request, int timeoutMillis) { EnsureServerAvailable(); var message = RemotingUtil.BuildRequestMessage(request); var taskCompletionSource = new TaskCompletionSource <RemotingResponse>(); var responseFuture = new ResponseFuture(request, timeoutMillis, taskCompletionSource); if (!_responseFutureDict.TryAdd(request.Sequence, responseFuture)) { throw new Exception(string.Format("Try to add response future failed. request sequence:{0}", request.Sequence)); } _clientSocket.SendMessage(message, sendResult => SendMessageCallback(responseFuture, request, _address, sendResult)); var task = taskCompletionSource.Task; var response = task.WaitResult(timeoutMillis); if (response == null) { if (!task.IsCompleted) { throw new RemotingTimeoutException(_address, request, timeoutMillis); } if (task.IsFaulted) { throw new RemotingRequestException(_address, request, task.Exception); } throw new RemotingRequestException(_address, request, "Send remoting request successfully, but the remoting response is null."); } return(response); }
private void SendMessageCallback(ResponseFuture responseFuture, RemotingRequest request, string address, SendResult sendResult) { if (!sendResult.Success) { _logger.ErrorFormat("Send request {0} to channel <{1}> failed, exception:{2}", request, address, sendResult.Exception); responseFuture.SetException(new RemotingRequestException(address, request, sendResult.Exception)); _responseFutureDict.Remove(request.Sequence); } }
public Task <RemotingResponse> InvokeAsync(RemotingRequest request, int timeoutMillis) { EnsureServerAvailable(); var message = RemotingUtil.BuildRequestMessage(request); var taskCompletionSource = new TaskCompletionSource <RemotingResponse>(); var responseFuture = new ResponseFuture(request, timeoutMillis, taskCompletionSource); if (!_responseFutureDict.TryAdd(request.Sequence, responseFuture)) { throw new Exception(string.Format("Try to add response future failed. request sequence:{0}", request.Sequence)); } _clientSocket.SendMessage(message, sendResult => SendMessageCallback(responseFuture, request, _address, sendResult)); return(taskCompletionSource.Task); }