Ejemplo n.º 1
0
        /// <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);
        }
Ejemplo n.º 2
0
        /// <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)
        {
            SecurityDictionary <int, 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 SecurityDictionary <int, 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);
        }
        // ******************************** OBJECT POOL ********************************

        /// <summary>
        /// Allows us to reuse objects without having to reallocate them over and over
        /// </summary>
        // private static ObjectPool<MessageListenerDefinition> sPool = new ObjectPool<MessageListenerDefinition>(40, 10);

        /// <summary>
        /// Pulls an object from the pool.
        /// </summary>
        /// <returns></returns>
        public static MessageListenerDefinition Allocate()
        {
            // Grab the next available object
            MessageListenerDefinition lInstance = new MessageListenerDefinition();

            //sPool.Allocate();
            lInstance.MessageType = Message.FilterTypeNothing;
            lInstance.Filter      = Message.FilterTypeNothing;
            lInstance.Handler     = null;

            // For this type, guarentee we have something
            // to hand back tot he caller
            if (lInstance == null)
            {
                lInstance = new MessageListenerDefinition();
            }
            return(lInstance);
        }
Ejemplo n.º 4
0
        /// <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(int rMessageType, int 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);
            }
        }
Ejemplo n.º 5
0
        /// <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(int rMessageType, int rFilter, MessageHandler rHandler, bool rImmediate)
        {
            // util.Log.log("AddListener>>rMessageType" + rMessageType + " rFilter>" + rFilter + " MessageHandler" + rHandler);
            MessageListenerDefinition lListener = MessageListenerDefinition.Allocate();

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

            if (rImmediate)
            {
                AddListener(lListener);
                //  MessageListenerDefinition.Release(lListener);
            }
            else
            {
                mListenerAdds.Add(lListener);
            }
        }