public ProtobufProtocolSerializer(IProtocolMessageFactory messageFactory)
 {
     _messageFactory = messageFactory;
     _clientToBrokerRequestSerializer = new ClientToBrokerRequestHandler <IPooledBuffer, Nothing>(Serialize, Serialize, Serialize);
     _brokerToClientRequestSerializer = new BrokerToClientRequestHandler <IPooledBuffer, Nothing>(Serialize);
     _invocationMessageSerializer     = new InvocationMessageHandler <IPooledBuffer, Nothing>(Serialize, Serialize, Serialize);
     _setInvocationTargetHandler      = new InvocationTargetHandler <IDisposable, InvocationStartRequest>(SetTarget, SetTarget);
 }
 public InvocationReceiveProcessor(
     UniqueId id,
     IReadableChannel <TransportMessageFrame> transport,
     IProtocolImplementation protocol,
     IMarshaller <TResponse> marshaller,
     IWritableChannel <IInvocationMessage> sender,
     InvocationState invocationState)
 {
     _log             = LogManager.GetLogger <InvocationReceiveProcessor <TResponse> >(id.ToString());
     _transport       = transport;
     _protocol        = protocol;
     _marshaller      = marshaller;
     _sender          = sender;
     _invocationState = invocationState;
     _incomingHandler = new InvocationMessageHandler <Nothing, Nothing>(
         HandleIncomingMessageHeader,
         HandleIncomingConfirmation,
         HandleIncomingCompletion);
 }
 public T Handle <T, TArgs>(InvocationMessageHandler <T, TArgs> handler, TArgs args)
 {
     return(handler.Handle(this, args));
 }
Exemplo n.º 4
0
        /// <summary>
        /// 处理消息
        /// </summary>
        /// <param name="hubMessage">转换后的消息对象</param>
        private void ProcessMessages(HubMessage hubMessage)
        {
            if (hubMessage == null)
            {
                Log.Debug("为获取到可处理的消息");
                throw new ArgumentNullException($"为获取到可处理的消息.{nameof(hubMessage)}");
            }
            switch (hubMessage)
            {
            case InvocationMessage invocation:
            {
                var handler = new InvocationMessageHandler(_handlers, (r) =>
                    {
                        if (!string.IsNullOrEmpty(invocation.InvocationId))
                        {
                            var completionMessage = new CompletionMessage(invocation.InvocationId, null, r, r != null);
                            SendHubMessage(completionMessage);
                        }
                    });
                try
                {
                    handler.Handler(invocation);
                }
                catch (Exception e)
                {
                    Log.Error(null, e);
                    if (invocation != null && !string.IsNullOrEmpty(invocation.InvocationId))
                    {
                        SendHubMessage(new CompletionMessage(invocation.InvocationId, e.Message, null, false));
                    }
                }
            }
            break;

            case StreamItemMessage streamItem:
            {
                Log.Info($"客户端暂不支持 {nameof(StreamItemMessage)}");
                if (streamItem != null && !string.IsNullOrEmpty(streamItem.InvocationId))
                {
                    SendHubMessage(new CompletionMessage(streamItem.InvocationId, $"客户端暂不支持 {nameof(StreamItemMessage)}", null, false));
                }
            }
            break;

            case StreamInvocationMessage streamInvocation:
            {
                Log.Info($"客户端暂不支持 {nameof(StreamInvocationMessage)}");
                if (streamInvocation != null && !string.IsNullOrEmpty(streamInvocation.InvocationId))
                {
                    SendHubMessage(new CompletionMessage(streamInvocation.InvocationId, $"客户端暂不支持 {nameof(StreamInvocationMessage)}", null, false));
                }
            }
            break;

            case CancelInvocationMessage cancelInvocation:
            {
                Log.Info($"客户端暂不支持 {nameof(CancelInvocationMessage)}");
            }
            break;

            case CompletionMessage completion:
            {
                var handler = new CompletionMessageHandler(_sendedMessageCallBacks);
                handler.Handler(completion);
                // 清除当前 InvocationId 对应的回调
                if (_sendedMessageCallBacks.ContainsKey(completion.InvocationId))
                {
                    lock (_sendedMessageCallBacks)
                    {
                        _sendedMessageCallBacks.Remove(completion.InvocationId);
                    }
                }
            }
            break;

            case CloseMessage close:
            {
                if (string.IsNullOrEmpty(close.Error))
                {
                    Log.Debug("服务器将关闭连接,客户端主动断开连接");
                }
                else
                {
                    Log.Error("服务器将关闭连接,客户端主动断开连接", new Exception(close.Error));
                }
                StopAsync();
            }
            break;

            case PingMessage _:
                Log.Debug("接收到服务器端 Ping");
                break;

            default:
                throw new InvalidOperationException($"未知的消息类型: {hubMessage.GetType().FullName}");
            }
        }