Beispiel #1
0
        private OuterCommandsManager()
        {
            m_outerCommands     = new Dictionary <(Int32, String), OuterCommand>();
            m_calledCommands    = new Dictionary <Int32, OuterCommandCall>();
            m_worker            = new CommandMessageWorker();
            m_repliesBuffer     = new BufferBlock <OuterCommandCall>();
            m_commandInitAction = new ActionBlock <(Int32 Identity, Byte[][] Msg)>(
                msg => AddOuterCommand(msg.Identity, msg.Msg));
            m_commandSendAction = new ActionBlock <(Int32 Identity, Byte[][] Msg)>(
                msg => m_worker.SendMessage(msg.Identity, msg.Msg));
            m_commandReplyAction = new ActionBlock <OuterCommandCall>(
                cmd => cmd.NotifyCommandPerformed(cmd.Reply));
            m_decodeTransform = new TransformBlock <(Int32 Identity, Byte[][] Msg), OuterCommandCall>(
                msg =>
            {
                var reply         = CommandCodec.DecodeReplyIds(msg.Identity, msg.Msg);
                var calledCommand = m_calledCommands[reply.CallId];
                m_calledCommands.Remove(reply.CallId);
                calledCommand.Reply = CommandCodec.DecodeReplyFully(reply, calledCommand, msg.Msg);
                return(calledCommand);
            });
            m_encodeTransform = new TransformBlock <OuterCommandCall, (Int32 Identity, Byte[][] Msg)>(
                call => CommandCodec.EncodeCommandCall(call));

            m_worker.CommandInitMessagesBuffer.LinkTo(m_commandInitAction);
            m_worker.CommandOuterMessageBuffer.LinkTo(m_decodeTransform);
            m_decodeTransform.LinkTo(m_commandReplyAction);
            m_encodeTransform.LinkTo(m_commandSendAction);
        }
        public MessageWorker() :
            base(TCPMonitorAdress, UDPMonitorAdress, ZSocketType.ROUTER, MessagingCoreSettings.Default.CoreSocketReceivePollRate)
        {
            m_cmdWorker = new CommandMessageWorker();
            m_processor = new MessageProcessor();

            m_decodeTransform = new TransformBlock <(TransportProtocol Protocol, ZMessage Message), MessageData>
                                    ((msg) =>
            {
                var decodedMsg      = MessageCodec.DecodeMessage(msg.Message);
                decodedMsg.Protocol = msg.Protocol;
                msg.Message.Dispose();
                return(decodedMsg);
            });

            m_decodedCommandMessageProcessingAction = new ActionBlock <MessageData>(msg => m_processor.ProcessCommandMessage(msg));
            m_decodedMessageProcessingAction        = new ActionBlock <MessageData>(msg => m_processor.ProcessMessage(msg));

            m_encodeTransform = new TransformBlock <(Node Node, IList <Byte[]> Msg), (TransportProtocol Protocol, ZMessage Msg)>
                                    (msg => (TransportProtocol.TCP, MessageCodec.EncodeMessage(msg.Node.Identity, msg.Msg)));
            m_encodeSignalTransform = new TransformBlock <(Node Node, IList <Byte[]> Msg), (TransportProtocol Protocol, ZMessage Msg)>
                                          (msg => (TransportProtocol.TCP, MessageCodec.EncodeNodeSignalMessage(msg.Node.Identity, msg.Msg)));

            m_sendAction = new ActionBlock <(TransportProtocol Protocol, ZMessage Msg)>(msg =>
            {
                if (msg.Protocol == TransportProtocol.TCP)
                {
                    m_tcpSocketSendAction.Post(msg.Msg);
                }
                //else
                //    m_udpSocketSendAction.Post(msg.Msg);
            });

            m_receiveBuffer.LinkTo(m_decodeTransform);
            m_decodeTransform.LinkTo(m_decodedMessageProcessingAction);
            m_cmdWorker.RecvBuffer.LinkTo(m_decodedCommandMessageProcessingAction);

            m_processor.SendMessageBuffer.LinkTo(m_encodeTransform);
            m_processor.SendSignalBuffer.LinkTo(m_encodeSignalTransform);
            m_processor.SendCommandBuffer.LinkTo(m_cmdWorker.SendBuffer);

            m_encodeTransform.LinkTo(m_sendAction);
            m_encodeSignalTransform.LinkTo(m_sendAction);
        }