public void InvokeOneway(RemotingRequest request) { EnsureClientStatus(); request.Type = RemotingRequestType.Oneway; _clientSocket.QueueMessage(RemotingUtil.BuildRequestMessage(request)); }
public void InvokeOneway(RemotingRequest request) { EnsureClientStatus(); request.IsOneway = true; _tcpClient.SendAsync(RemotingUtil.BuildRequestMessage(request)); }
public void InvokeWithCallback(RemotingRequest request) { EnsureClientStatus(); request.Type = RemotingRequestType.Callback; _clientSocket.QueueMessage(RemotingUtil.BuildRequestMessage(request)); }
private void HandleReplyMessage() { var responseMessage = _replyMessageQueue.Take(); if (responseMessage == null) { return; } var remotingResponse = RemotingUtil.ParseResponse(responseMessage); if (remotingResponse.Type == RemotingRequestType.Callback) { IResponseHandler responseHandler; if (_responseHandlerDict.TryGetValue(remotingResponse.RequestCode, out responseHandler)) { responseHandler.HandleResponse(remotingResponse); } } else if (remotingResponse.Type == RemotingRequestType.Async) { ResponseFuture responseFuture; if (_responseFutureDict.TryRemove(remotingResponse.Sequence, out responseFuture)) { if (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); } else { _logger.ErrorFormat("Set remoting response failed, response:" + remotingResponse); } } } }
private void HandleRemotingRequest(ITcpConnection connection, byte[] message, Action <byte[]> sendReplyAction) { var remotingRequest = RemotingUtil.ParseRequest(message); var requestHandlerContext = new SocketRequestHandlerContext(connection, sendReplyAction); IRequestHandler requestHandler; if (!_requestHandlerDict.TryGetValue(remotingRequest.Code, out requestHandler)) { var errorMessage = string.Format("No request handler found for remoting request:{0}", remotingRequest); _logger.Error(errorMessage); requestHandlerContext.SendRemotingResponse(new RemotingResponse(remotingRequest.Code, -1, remotingRequest.Type, Encoding.UTF8.GetBytes(errorMessage), remotingRequest.Sequence)); return; } try { var remotingResponse = requestHandler.HandleRequest(requestHandlerContext, remotingRequest); if (remotingRequest.Type != RemotingRequestType.Oneway && remotingResponse != null) { requestHandlerContext.SendRemotingResponse(remotingResponse); } } catch (Exception ex) { var errorMessage = string.Format("Exception raised when handling remoting request:{0}.", remotingRequest); _logger.Error(errorMessage, ex); if (remotingRequest.Type != RemotingRequestType.Oneway) { requestHandlerContext.SendRemotingResponse(new RemotingResponse(remotingRequest.Code, -1, remotingRequest.Type, Encoding.UTF8.GetBytes(ex.Message), remotingRequest.Sequence)); } } }
public void InvokeOneway(RemotingRequest request, int timeoutMillis) { EnsureClientStatus(); request.IsOneway = true; _tcpClient.SendMessage(RemotingUtil.BuildRequestMessage(request)); }
public SocketRequestHandlerContext(ITcpConnection connection, Action <byte[]> sendReplyAction) { Connection = connection; SendRemotingResponse = remotingResponse => { sendReplyAction(RemotingUtil.BuildResponseMessage(remotingResponse)); }; }
/// <summary> /// 处理远程请求的事件 /// </summary> /// <param name="connection"></param> /// <param name="message"></param> /// <param name="sendReplyAction"></param> private void HandleRemotingRequest(ITcpConnection connection, byte[] message, Action <byte[]> sendReplyAction) { if (_isShuttingdown) { return; } var remotingRequest = RemotingUtil.ParseRequest(message); var requestHandlerContext = new SocketRequestHandlerContext(connection, sendReplyAction); if (!_requestHandlerDict.TryGetValue(remotingRequest.Code, out IRequestHandler requestHandler)) { var errorMessage = string.Format("No request handler found for remoting request, remotingServerName: {0}, remotingRequest: {1}", Name, remotingRequest); _logger.Error(errorMessage); if (remotingRequest.Type != RemotingRequestType.Oneway) { requestHandlerContext.SendRemotingResponse(new RemotingResponse( remotingRequest.Type, remotingRequest.Code, remotingRequest.Sequence, remotingRequest.CreatedTime, -1, Encoding.UTF8.GetBytes(errorMessage), DateTime.Now, remotingRequest.Header, null)); } return; } try { var remotingResponse = requestHandler.HandleRequest(requestHandlerContext, remotingRequest); if (remotingRequest.Type != RemotingRequestType.Oneway && remotingResponse != null) { requestHandlerContext.SendRemotingResponse(remotingResponse); } } catch (Exception ex) { var errorMessage = string.Format("Unknown exception raised when handling remoting request:{0}, name: {0}, request: {1}", Name, remotingRequest); _logger.Error(errorMessage, ex); if (remotingRequest.Type != RemotingRequestType.Oneway) { requestHandlerContext.SendRemotingResponse(new RemotingResponse( remotingRequest.Type, remotingRequest.Code, remotingRequest.Sequence, remotingRequest.CreatedTime, -1, Encoding.UTF8.GetBytes(ex.Message), DateTime.Now, remotingRequest.Header, null)); } } }
private static byte[] BuildRemotingServerMessage(RemotingResponse remotingResponse) { byte[] remotingResponseData = RemotingUtil.BuildResponseMessage(remotingResponse); var remotingServerMessage = new RemotingServerMessage( RemotingServerMessageType.RemotingResponse, 100, remotingResponseData, null); return(RemotingUtil.BuildRemotingServerMessage(remotingServerMessage)); }
public Task <RemotingResponse> InvokeAsync(RemotingRequest request, int timeoutMillis = 5000) { EnsureClientStatus(); var taskCompletionSource = new TaskCompletionSource <RemotingResponse>(); var responseFuture = new ResponseFuture(request, timeoutMillis, taskCompletionSource); if (!_responseFutureDict.TryAdd(request.Sequence, responseFuture)) { throw new ResponseFutureAddFailedException(request.Sequence); } _tcpClient.SendAsync(RemotingUtil.BuildRequestMessage(request)); return(taskCompletionSource.Task); }
private void HandleServerMessage(ITcpConnection connection, byte[] message) { if (message == null) { return; } var remotingServerMessage = RemotingUtil.ParseRemotingServerMessage(message); if (remotingServerMessage.Type == RemotingServerMessageType.RemotingResponse) { HandleResponseMessage(connection, remotingServerMessage.Body); } else if (remotingServerMessage.Type == RemotingServerMessageType.ServerMessage) { HandleServerPushMessage(connection, remotingServerMessage); } }
private void HandleReplyMessage() { var responseMessage = _replyMessageQueue.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); } }
private void HandleResponseMessage(ITcpConnection connection, byte[] message) { if (message == null) { return; } var remotingResponse = RemotingUtil.ParseResponse(message); if (remotingResponse.RequestType == RemotingRequestType.Callback) { if (_responseHandlerDict.TryGetValue(remotingResponse.RequestCode, out IResponseHandler responseHandler)) { responseHandler.HandleResponse(remotingResponse); } else { _logger.ErrorFormat("No response handler found for remoting response, name: {0}, response: {1}", Name, remotingResponse); } } else if (remotingResponse.RequestType == RemotingRequestType.Async) { if (_responseFutureDict.TryRemove(remotingResponse.RequestSequence, out ResponseFuture responseFuture)) { if (responseFuture.SetResponse(remotingResponse)) { if (_logger.IsDebugEnabled) { _logger.DebugFormat("Remoting response back, name: {0}, request code: {1}, requect sequence: {2}, time spent: {3}", Name, responseFuture.Request.Code, responseFuture.Request.Sequence, (DateTime.Now - responseFuture.BeginTime).TotalMilliseconds); } } else { _logger.ErrorFormat("Set remoting response failed, name: {0}, response: {1}", Name, remotingResponse); } } } }
public void PushMessageToAllConnections(RemotingServerMessage message) { var data = RemotingUtil.BuildRemotingServerMessage(message); ServerSocket.PushMessageToAllConnections(data); }