示例#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 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);
        }
示例#2
0
        /// <summary>
        /// Send the message object as needed. In this instance, the caller needs to
        /// release the message.
        /// </summary>
        /// <param name="rMessage"></param>
        public static void SendMessage(IMessage rMessage)
        {
            //   util.Log.log("SendMessage0" + rMessage.Type);

            bool lReportMissingRecipient = true;

            // Hold the message for the delay or the next frame (< 0)
            if (rMessage.Delay > 0 || rMessage.Delay < 0)
            {
                // util.Log.log("SendMessage1");
                if (!mMessages.Contains(rMessage))
                {
                    mMessages.Add(rMessage);
                }
                lReportMissingRecipient = false;
            }
            // Send the message now if there are handlers
            else if (mMessageHandlers.ContainsKey(rMessage.Type))
            {
                // util.Log.log("find the rMessage.Recipient is" + rMessage.Recipient);
                // Get handlers for the message type
                SecurityDictionary <int, MessageHandler> lHandlers = mMessageHandlers[rMessage.Type];
                foreach (var lFilter in lHandlers.Keys)
                {
                    // If there are no listeners left, flag the entries for removal
                    if (lHandlers[lFilter] == null)
                    {
                        RemoveListener(rMessage.Type, lFilter, null);
                        continue;
                    }

                    // Send to anyone listening to all filters
                    if (lFilter == Message.FilterTypeNothing)
                    {
                        //  util.Log.log("SendMessage2");


                        // lHandlers[lFilter](rMessage);
                        mWorkQueue.Enqueue(new SafeWorkItemClass(rMessage, lHandlers[lFilter]));
                        lReportMissingRecipient = false;
                    }
                    // Check if we are filtering by name (with an object)
                    //else if (mRecipientType == EnumMessageRecipientType.NAME && rMessage.Recipient is UnityEngine.Object)
                    //{
                    //    if (lFilter.ToLower() == ((UnityEngine.Object)rMessage.Recipient).name.ToLower())
                    //    {
                    //        rMessage.IsSent = true;
                    //        lHandlers[lFilter](rMessage);

                    //        lReportMissingRecipient = false;
                    //    }
                    //}
                    //// Check if we are filtering by tag (with an object)
                    //else if (mRecipientType == EnumMessageRecipientType.TAG && rMessage.Recipient is UnityEngine.GameObject)
                    //{
                    //    if (lFilter.ToLower() == ((UnityEngine.GameObject)rMessage.Recipient).tag.ToLower())
                    //    {
                    //        rMessage.IsSent = true;
                    //        lHandlers[lFilter](rMessage);

                    //        lReportMissingRecipient = false;
                    //    }
                    //}
                    // If we have a string as the recipient, just test it
                    else
                    {
                        //util.Log.log("SendMessage3");
                        if (lFilter == rMessage.Recipient)
                        {
                            // util.Log.log("捕捉到》》 >>rMessage.Type" + rMessage.Type + "  rMessage.Recipient>>" +rMessage.Recipient);
                            //lHandlers[lFilter](rMessage);

                            mWorkQueue.Enqueue(new SafeWorkItemClass(rMessage, lHandlers[lFilter]));
                            lReportMissingRecipient = false;
                        }
                        else
                        {
                            // util.Log.LogError("警告,这个mess 没有人handle);
                        }
                    }
                }
            }

            // If we were unable to send the message, we may need to report it
            if (lReportMissingRecipient)
            {
                if (MessageNotHandled == null)
                {
                    // Debug.LogWarning("MessageDispatcher: Unhandled Message of type " + rMessage.Type);
                }
                else
                {
                    //util.Log.log("SendMessage3");
                    MessageNotHandled(rMessage);
                }
                // util.Log.log("SendMessage4");
                // Flag the message as handled so we can remove it
                // rMessage.IsHandled = true;
            }
        }