Пример #1
0
        private Task <AsyncTaskResult> DispatchMessages(IEnumerable <IMessage> messages)
        {
            var messageCount = messages.Count();

            if (messageCount == 0)
            {
                return(Task.FromResult <AsyncTaskResult>(AsyncTaskResult.Success));
            }
            var rootDispatching = new RootDisptaching();

            //先对每个事件调用其Handler
            var queueMessageDispatching = new QueueMessageDisptaching(this, rootDispatching, messages);

            DispatchSingleMessage(queueMessageDispatching.DequeueMessage(), queueMessageDispatching);

            //如果有至少两个事件,则尝试调用针对两个事件的Handler
            if (messageCount >= 2)
            {
                var twoMessageHandlers = _twoMessageHandlerProvider.GetHandlers(messages.Select(x => x.GetType()));
                if (twoMessageHandlers.IsNotEmpty())
                {
                    DispatchMultiMessage(messages, twoMessageHandlers, rootDispatching, DispatchTwoMessageToHandlerAsync);
                }
            }
            //如果有至少三个事件,则尝试调用针对三个事件的Handler
            if (messageCount >= 3)
            {
                var threeMessageHandlers = _threeMessageHandlerProvider.GetHandlers(messages.Select(x => x.GetType()));
                if (threeMessageHandlers.IsNotEmpty())
                {
                    DispatchMultiMessage(messages, threeMessageHandlers, rootDispatching, DispatchThreeMessageToHandlerAsync);
                }
            }
            return(rootDispatching.Task);
        }
Пример #2
0
        private void DispatchSingleMessage(IMessage message, QueueMessageDisptaching queueMessageDispatching)
        {
            var messageHandlerDataList = _handlerProvider.GetHandlers(message.GetType());

            if (!messageHandlerDataList.Any())
            {
                queueMessageDispatching.OnMessageHandled(message);
                return;
            }

            foreach (var messageHandlerData in messageHandlerDataList)
            {
                var singleMessageDispatching = new SingleMessageDisptaching(message, queueMessageDispatching, messageHandlerData.AllHandlers, _typeNameProvider);

                if (messageHandlerData.ListHandlers != null && messageHandlerData.ListHandlers.IsNotEmpty())
                {
                    foreach (var handler in messageHandlerData.ListHandlers)
                    {
                        DispatchSingleMessageToHandlerAsync(singleMessageDispatching, handler, null, 0);
                    }
                }
                if (messageHandlerData.QueuedHandlers != null && messageHandlerData.QueuedHandlers.IsNotEmpty())
                {
                    var queueHandler = new QueuedHandler <IMessageHandlerProxy1>(messageHandlerData.QueuedHandlers, (queuedHandler, nextHandler) => DispatchSingleMessageToHandlerAsync(singleMessageDispatching, nextHandler, queuedHandler, 0));
                    DispatchSingleMessageToHandlerAsync(singleMessageDispatching, queueHandler.DequeueHandler(), queueHandler, 0);
                }
            }
        }
Пример #3
0
 public SingleMessageDisptaching(IMessage message, QueueMessageDisptaching queueMessageDispatching, IEnumerable <IObjectProxy> handlers, ITypeNameProvider typeNameProvider)
 {
     Message = message;
     _queueMessageDispatching = queueMessageDispatching;
     _handlerDict             = new ConcurrentDictionary <string, IObjectProxy>();
     handlers.ForEach(x => _handlerDict.TryAdd(typeNameProvider.GetTypeName(x.GetInnerObject().GetType()), x));
 }