Exemplo n.º 1
0
        public void InvokeOneway(RemotingRequest request)
        {
            EnsureClientStatus();

            request.Type = RemotingRequestType.Oneway;
            _clientSocket.QueueMessage(RemotingUtil.BuildRequestMessage(request));
        }
Exemplo n.º 2
0
        public void InvokeOneway(RemotingRequest request)
        {
            EnsureClientStatus();

            request.IsOneway = true;
            _tcpClient.SendAsync(RemotingUtil.BuildRequestMessage(request));
        }
Exemplo n.º 3
0
        public void InvokeWithCallback(RemotingRequest request)
        {
            EnsureClientStatus();

            request.Type = RemotingRequestType.Callback;
            _clientSocket.QueueMessage(RemotingUtil.BuildRequestMessage(request));
        }
Exemplo n.º 4
0
        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);
                    }
                }
            }
        }
Exemplo n.º 5
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));
                }
            }
        }
Exemplo n.º 6
0
        public void InvokeOneway(RemotingRequest request, int timeoutMillis)
        {
            EnsureClientStatus();

            request.IsOneway = true;
            _tcpClient.SendMessage(RemotingUtil.BuildRequestMessage(request));
        }
Exemplo n.º 7
0
 public SocketRequestHandlerContext(ITcpConnection connection, Action <byte[]> sendReplyAction)
 {
     Connection           = connection;
     SendRemotingResponse = remotingResponse =>
     {
         sendReplyAction(RemotingUtil.BuildResponseMessage(remotingResponse));
     };
 }
Exemplo n.º 8
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));
                }
            }
        }
Exemplo n.º 9
0
        private static byte[] BuildRemotingServerMessage(RemotingResponse remotingResponse)
        {
            byte[] remotingResponseData  = RemotingUtil.BuildResponseMessage(remotingResponse);
            var    remotingServerMessage = new RemotingServerMessage(
                RemotingServerMessageType.RemotingResponse,
                100,
                remotingResponseData,
                null);

            return(RemotingUtil.BuildRemotingServerMessage(remotingServerMessage));
        }
Exemplo n.º 10
0
        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);
        }
Exemplo n.º 11
0
        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);
            }
        }
Exemplo n.º 12
0
        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);
                    }
                }
            }
        }
Exemplo n.º 14
0
        public void PushMessageToAllConnections(RemotingServerMessage message)
        {
            var data = RemotingUtil.BuildRemotingServerMessage(message);

            ServerSocket.PushMessageToAllConnections(data);
        }