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 HandleRemotingRequest(ReceiveContext receiveContext) { var remotingRequest = RemotingUtil.ParseRequest(receiveContext.ReceivedMessage); var requestHandlerContext = new SocketRequestHandlerContext(receiveContext, remotingRequest, _logger); IRequestHandler requestHandler; if (!_requestHandlerDict.TryGetValue(remotingRequest.Code, out requestHandler)) { var errorMessage = string.Format("No request handler found for remoting request, request code:{0}", remotingRequest.Code); _logger.Error(errorMessage); requestHandlerContext.SendRemotingResponse(new RemotingResponse(-1, remotingRequest.Sequence, Encoding.UTF8.GetBytes(errorMessage))); return; } try { _logger.DebugFormat("Handling remoting request, request code:{0}, request sequence:{1}", remotingRequest.Code, remotingRequest.Sequence); var remotingResponse = requestHandler.HandleRequest(requestHandlerContext, remotingRequest); if (!remotingRequest.IsOneway && remotingResponse != null) { requestHandlerContext.SendRemotingResponse(remotingResponse); } } catch (Exception ex) { var errorMessage = string.Format("Exception raised when handling remoting request, request code:{0}.", remotingRequest.Code); _logger.Error(errorMessage, ex); if (!remotingRequest.IsOneway) { requestHandlerContext.SendRemotingResponse(new RemotingResponse(-1, remotingRequest.Sequence, Encoding.UTF8.GetBytes(ex.Message))); } } }
public SocketRequestHandlerContext(ReceiveContext receiveContext, RemotingRequest remotingRequest, ILogger logger) { Channel = new SocketChannel(receiveContext.ReplySocketInfo); SendRemotingResponse = remotingResponse => { receiveContext.ReplyMessage = RemotingUtil.BuildResponseMessage(remotingResponse); receiveContext.ReplySentCallback = sendResult => { if (!sendResult.Success && sendResult.Exception != null) { var errorMessage = "[" + sendResult.Exception.GetType() .Name + "]"; var socketException = sendResult.Exception as SocketException; if (socketException != null) { errorMessage = "[" + sendResult.Exception.GetType() .Name + ", ErrorCode:" + socketException.SocketErrorCode + "]"; } logger.DebugFormat( "Remoting request handled, reply sent status:{0}, errorMessage:{1}, request code:{2}, request sequence:{3}, response code:{4}, response sequence:{5}", sendResult.Success, errorMessage, remotingRequest.Code, remotingRequest.Sequence, remotingResponse.Code, remotingResponse.Sequence); } else { logger.DebugFormat( "Remoting request handled, reply sent status:{0}, request code:{1}, request sequence:{2}, response code:{3}, response sequence:{4}", sendResult.Success, remotingRequest.Code, remotingRequest.Sequence, remotingResponse.Code, remotingResponse.Sequence); } }; logger.DebugFormat("Begin to send reply, request code:{0}, request sequence:{1}, response code:{2}, response sequence:{3}", remotingRequest.Code, remotingRequest.Sequence, remotingResponse.Code, remotingResponse.Sequence); receiveContext.MessageHandledCallback(receiveContext); }; }
public void InvokeOneway(RemotingRequest request, int timeoutMillis) { EnsureServerAvailable(); request.IsOneway = true; _clientSocket.SendMessage(RemotingUtil.BuildRequestMessage(request), sendResult => { if (!sendResult.Success) { _logger.ErrorFormat("Send request {0} to channel <{1}> failed, exception:{2}", request, _address, sendResult.Exception); } }); }
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); }
private void HandleMessage() { var responseMessage = _messageQueue.Take(); if (responseMessage == null) { return; } var remotingResponse = RemotingUtil.ParseResponse(responseMessage); ResponseFuture responseFuture; if (_responseFutureDict.TryRemove(remotingResponse.Sequence, out responseFuture)) { responseFuture.SetResponse(remotingResponse); _logger.DebugFormat("Remoting response back, request code:{0}, requect sequence:{1}, time spent:{2}", responseFuture.Request.Code, responseFuture.Request.Sequence, (DateTime.Now - responseFuture.BeginTime).TotalMilliseconds); } }