Пример #1
0
        public void InvokeOneway(RemotingRequest request)
        {
            EnsureClientStatus();

            request.Type = RemotingRequestType.Oneway;
            _clientSocket.QueueMessage(RemotingUtil.BuildRequestMessage(request));
        }
Пример #2
0
        public void InvokeWithCallback(RemotingRequest request)
        {
            EnsureClientStatus();

            request.Type = RemotingRequestType.Callback;
            _clientSocket.QueueMessage(RemotingUtil.BuildRequestMessage(request));
        }
Пример #3
0
        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);
                LogUtil.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}.", remotingRequest);
                LogUtil.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));
        }
Пример #5
0
        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);
        }
Пример #6
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);
            }
        }
Пример #7
0
        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
                {
                    LogUtil.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))
                    {
                        LogUtil.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
                    {
                        LogUtil.ErrorFormat("Set remoting response failed, response:" + remotingResponse);
                    }
                }
            }
        }
Пример #8
0
        public void PushMessageToAllConnections(RemotingServerMessage message)
        {
            var data = RemotingUtil.BuildRemotingServerMessage(message);

            _serverSocket.PushMessageToAllConnections(data);
        }