public void AddOuterCommand(Int32 identity, Byte[][] message) { var commandInfo = CommandCodec.DecodeCommandInit(identity, message); commandInfo.MessageWrapper = CreateWrapper($"c_{commandInfo.Name}{commandInfo.Node}_msg", commandInfo.RequestDescription, $"c_{commandInfo.Name}{commandInfo.Node}_repl", commandInfo.ReplyDescription); m_outerCommands.Add((commandInfo.Node, commandInfo.Name), commandInfo); CommandAdded?.Invoke(this, commandInfo); IMessageCreator CreateWrapper(String msgName, IList <MessagePartDescription> msgDesc, String repName, IList <MessagePartDescription> replDesc) { var msg = (msgDesc is null) ? typeof(MockMessage) : (msgDesc[0].Type == MessageValueType.Raw) ? typeof(RawMessage) : MessageTypeCreator.Instance.CreateMessageType(msgName, msgDesc); var repl = (replDesc is null) ? typeof(MockMessage) : (replDesc[0].Type == MessageValueType.Raw) ? typeof(RawMessage) : MessageTypeCreator.Instance.CreateMessageType(repName, replDesc); return((IMessageCreator)Activator.CreateInstance( typeof(MessageCreator <,>).MakeGenericType(msg, repl))); } }
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); }