예제 #1
0
        private static void SyncBulkHandleMessages(NodeWithMessagesCollection distributedMessages, List <NodeWithMessages> unhandledNodes)
        {
            var waiters = new List <Action>();

            for (int i = 0; i < distributedMessages.Count; i++)
            {
                NodeWithMessages nodeWithMessages = distributedMessages[i];
                NodeWithInfo     nodeWithInfo     = nodeWithMessages.NodeWithInfo;
                if (nodeWithMessages.Messages.InMessageCount > 0)
                {
                    bool inMessagesHandled = nodeWithInfo.Node.HandleInMessages(nodeWithMessages.Messages.InMessages, nodeWithInfo.SyncInMessages, nodeWithInfo.SkipErrorQueueForSync);
                    if (!inMessagesHandled)
                    {
                        unhandledNodes.Add(nodeWithMessages);
                    }
                }
                if (nodeWithMessages.Messages.OutMessageCount > 0)
                {
                    var node = nodeWithInfo.Node;
                    var ar   = node.BeginHandleOutMessages(nodeWithMessages.Messages.OutMessages, null, null);
                    waiters.Add(() => node.EndHandleOutMessages(ar));
                }
            }

            foreach (var waiter in waiters)
            {
                waiter();
            }
        }
예제 #2
0
 internal NodeWithMessages(NodeWithInfo node)
 {
     NodeWithInfo = node;
 }
예제 #3
0
        private static void AsyncBulkHandleMessages(NodeWithMessagesCollection distributedMessages, List <NodeWithMessages> unhandledNodes)
        {
            if (log.IsDebugEnabled)
            {
                log.Debug("Starting asyncbulk handle messages");
            }

            AutoResetEvent  resetEvent   = null;
            HandleWithCount finishedLock = null;

            int numberOfGets = 0;
            int getsLefts    = 0;

            for (int i = 0; i < distributedMessages.Count; i++)
            {
                if (distributedMessages[i].Messages.OutMessageCount > 0)
                {
                    numberOfGets++;
                }
            }

            if (numberOfGets > 0)
            {
                resetEvent   = OutMessageWaitHandle;
                finishedLock = new HandleWithCount(resetEvent, numberOfGets);
                getsLefts    = numberOfGets;              //use this to determine when we're at the last get so we use this thread to process it
            }

            for (int i = 0; i < distributedMessages.Count; i++)
            {
                NodeWithMessages nodeWithMessages = distributedMessages[i];
                NodeWithInfo     nodeWithInfo     = nodeWithMessages.NodeWithInfo;
                if (nodeWithMessages.Messages.InMessageCount > 0)
                {
                    bool inMessagesHandled = nodeWithInfo.Node.HandleInMessages(nodeWithMessages.Messages.InMessages,
                                                                                nodeWithInfo.SyncInMessages, nodeWithInfo.SkipErrorQueueForSync);
                    if (!inMessagesHandled)
                    {
                        unhandledNodes.Add(nodeWithMessages);
                    }
                }
                if (nodeWithMessages.Messages.OutMessageCount > 0)
                {
                    if (--getsLefts > 0)                     //post all the last group to be handled async
                    {
                        nodeWithInfo.Node.PostOutMessages(
                            new MessagesWithLock(nodeWithMessages.Messages.OutMessages, finishedLock));
                    }
                    else
                    {
                        nodeWithInfo.Node.SendOutMessages(nodeWithMessages.Messages.OutMessages);                         //do the last set on this thread rather than punting the duty
                        finishedLock.Decrement();
                    }
                }
            }

            if (numberOfGets > 0)
            {
                resetEvent.WaitOne();                 //wait for all operations to complete. the event will be signaled with lock.decrement hits 0
            }
        }