Example #1
0
        /// <summary>
        /// Forwards a message to a set of subscriptions
        /// </summary>
        private static void ForwardToServers(int contract, string channel, ArraySegment <byte> message, IEnumerable <Subscription> subscriptions)
        {
            // Send the message out
            using (var filter = Filter <MessageQueue> .Acquire())
            {
                // Iterate through all of subscriptions matched
                foreach (var subscription in subscriptions)
                {
                    // We need to hold on to a copy of the array we are going to iterate through
                    // as to to this would not break the loop, since the array can/will be changed
                    // while we iterate!
                    var servers = subscription.Servers;
                    if (servers == null || servers.Length == 0)
                    {
                        continue;
                    }

                    // Add the server to the set
                    for (int i = 0; i < servers.Length; ++i)
                    {
                        // Set the bloom filter value for the server we found.
                        var mq = servers[i].Session as MessageQueue;
                        if (mq == null)
                        {
                            servers[i].Session = mq = new MessageQueue();
                        }
                        filter.Add(mq);
                    }
                }

                // Send a message to every subscribed server
                foreach (var server in Service.Mesh.Members)
                {
                    // Get the message queue and check if we have it in our filter
                    var mq = server.Session as MessageQueue;
                    if (mq == null || !filter.Contains(mq))
                    {
                        continue;
                    }

                    // Enqueue to the message queue
                    mq.Enqueue(contract, channel, message);
                }
            }
        }