Example #1
0
        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));
                }
            }
        }
Example #2
0
        /// <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));
                }
            }
        }