/// <summary>
        /// Internal function for removing listeners outside of the core
        /// processing logic. We do this to cleanly remove listeners and keep
        /// performance up.
        /// </summary>
        /// <param name="rListener"></param>
        private static void RemoveListener(MessageListenerDefinition rListener)
        {
            if (mMessageHandlers.ContainsKey(rListener.MessageType))
            {
                if (mMessageHandlers[rListener.MessageType].ContainsKey(rListener.Filter))
                {
                    // Test if we have listeners assigned
                    if (mMessageHandlers[rListener.MessageType][rListener.Filter] != null && rListener.Handler != null)
                    {
                        mMessageHandlers[rListener.MessageType][rListener.Filter] -= rListener.Handler;
                    }

                    // TT 2/18 - If removing the handler leaves no handlers left, remove the filter
                    if (mMessageHandlers[rListener.MessageType][rListener.Filter] == null)
                    {
                        mMessageHandlers[rListener.MessageType].Remove(rListener.Filter);
                    }

                    // TT 2/18 - If removing the filter leaves no filters left, remove the message type
                    if (mMessageHandlers[rListener.MessageType].Count == 0)
                    {
                        mMessageHandlers.Remove(rListener.MessageType);
                    }
                }
            }

            // Release the definition
            MessageListenerDefinition.Release(rListener);
        }
        /// <summary>
        /// Internal function for removing listeners outside of the core
        /// processing logic. We do this to cleanly remove listeners and keep
        /// performance up.
        /// </summary>
        /// <param name="rListener"></param>
        private static void AddListener(MessageListenerDefinition rListener)
        {
            Dictionary <string, MessageHandler> lRecipientDictionary = null;

            // First check if we know about the message type
            if (mMessageHandlers.ContainsKey(rListener.MessageType))
            {
                lRecipientDictionary = mMessageHandlers[rListener.MessageType];
            }
            // If we don't know about the message type, add it
            else if (!mMessageHandlers.ContainsKey(rListener.MessageType))
            {
                lRecipientDictionary = new Dictionary <string, MessageHandler>();
                mMessageHandlers.Add(rListener.MessageType, lRecipientDictionary);
            }

            // Check if we know about the owner, then add the handler
            if (!lRecipientDictionary.ContainsKey(rListener.Filter))
            {
                lRecipientDictionary.Add(rListener.Filter, null);
            }
            lRecipientDictionary[rListener.Filter] += rListener.Handler;

            // Release the definition
            MessageListenerDefinition.Release(rListener);
        }
        /// <summary>
        /// Returns an element back to the pool.
        /// </summary>
        /// <param name="rEdge"></param>
        public static void Release(MessageListenerDefinition rInstance)
        {
            if (rInstance == null)
            {
                return;
            }

            // We should never release an instance unless we're
            // sure we're done with it. So clearing here is fine
            rInstance.MessageType = "";
            rInstance.Filter      = "";
            rInstance.Handler     = null;

            // Make it available to others.
            sPool.Release(rInstance);
        }
        /// <summary>
        /// Pulls an object from the pool.
        /// </summary>
        /// <returns></returns>
        public static MessageListenerDefinition Allocate()
        {
            // Grab the next available object
            MessageListenerDefinition lInstance = sPool.Allocate();

            lInstance.MessageType = "";
            lInstance.Filter      = "";
            lInstance.Handler     = null;

            // For this type, guarentee we have something
            // to hand back tot he caller
            if (lInstance == null)
            {
                lInstance = new MessageListenerDefinition();
            }
            return(lInstance);
        }
        /// <summary>
        /// Tie the handler to the specified recipient and message type.
        /// This way it will be raised when a message of this type comes in.
        /// </summary>
        /// <param name="rMessageType">Message we want to listen for</param>
        /// <param name="rFilter">Filter tag to determine who gets the message (recipient name or a tag)</param>
        /// <param name="rHandler">Hander to handle the message</param>
        /// <param name="rImmediate">Determines if the function ignores the cache and forces the listener into the list now</param>
        public static void AddListener(string rMessageType, string rFilter, MessageHandler rHandler, bool rImmediate)
        {
            MessageListenerDefinition lListener = MessageListenerDefinition.Allocate();

            lListener.MessageType = rMessageType;
            lListener.Filter      = rFilter;
            lListener.Handler     = rHandler;

            if (rImmediate)
            {
                AddListener(lListener);
                MessageListenerDefinition.Release(lListener);
            }
            else
            {
                mListenerAdds.Add(lListener);
            }
        }
        /// <summary>
        /// Stop listening for messages for the specified type and to the specified recipient.
        /// </summary>
        /// <param name="rMessageType">Message we want to listen for</param>
        /// <param name="rFilter">Filter used to determine who gets the message (recipient name or tag)</param>
        /// <param name="rHandler">Hander to handle the message</param>
        /// <param name="rImmediate">Determines if the function ignores the cache and forcibly removes the listener from the list now</param>
        public static void RemoveListener(string rMessageType, string rFilter, MessageHandler rHandler, bool rImmediate)
        {
            // Post for clean up outside of any processing loops
            MessageListenerDefinition lListener = MessageListenerDefinition.Allocate();

            lListener.MessageType = rMessageType;
            lListener.Filter      = rFilter;
            lListener.Handler     = rHandler;

            if (rImmediate)
            {
                RemoveListener(lListener);
                MessageListenerDefinition.Release(lListener);
            }
            else
            {
                mListenerRemoves.Add(lListener);
            }
        }