public void InvokeOneway(RemotingRequest request)
        {
            EnsureClientStatus();

            request.Type = RemotingRequestType.Oneway;
            _clientSocket.QueueMessage(RemotingUtil.BuildRequestMessage(request));
        }
        public void InvokeWithCallback(RemotingRequest request)
        {
            EnsureClientStatus();

            request.Type = RemotingRequestType.Callback;
            _clientSocket.QueueMessage(RemotingUtil.BuildRequestMessage(request));
        }
        private void HandleRemotingRequest(ITcpConnection connection, byte[] message, Action <byte[]> sendReplyAction)
        {
            if (_isShuttingdown)
            {
                return;
            }

            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, null);
                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}.", 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));
                }
            }
        }
示例#4
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));
        }
        public Task <RemotingResponse> InvokeAsync(RemotingRequest request, int timeoutMillis)
        {
            EnsureClientStatus();

            request.Type = RemotingRequestType.Async;
            var taskCompletionSource = new TaskCompletionSource <RemotingResponse>();
            var responseFuture       = new ResponseFuture(request, timeoutMillis, taskCompletionSource);

            if (!_responseFutureDict.TryAdd(request.Sequence, responseFuture))
            {
                throw new ResponseFutureAddFailedException(request.Sequence);
            }

            _clientSocket.QueueMessage(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 HandleResponseMessage(ITcpConnection connection, byte[] message)
        {
            if (message == null)
            {
                return;
            }

            var remotingResponse = RemotingUtil.ParseResponse(message);

            if (remotingResponse.RequestType == RemotingRequestType.Callback)
            {
                IResponseHandler responseHandler;
                if (_responseHandlerDict.TryGetValue(remotingResponse.RequestCode, out responseHandler))
                {
                    responseHandler.HandleResponse(remotingResponse);
                }
                else
                {
                    _logger.ErrorFormat("No response handler found for remoting response:{0}", remotingResponse);
                }
            }
            else if (remotingResponse.RequestType == RemotingRequestType.Async)
            {
                ResponseFuture responseFuture;
                if (_responseFutureDict.TryRemove(remotingResponse.RequestSequence, out responseFuture))
                {
                    if (responseFuture.SetResponse(remotingResponse))
                    {
                        _logger.Debug("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);
                    }
                }
            }
        }
        public void PushMessageToAllConnections(RemotingServerMessage message)
        {
            var data = RemotingUtil.BuildRemotingServerMessage(message);

            _serverSocket.PushMessageToAllConnections(data);
        }