Exemple #1
0
        /// <summary>
        /// Add the specified message to the processing queue for sending.
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool QueueMessage(BaseMessage msg)
        {
            // if no listeners then just return.
            if (!_listeners.ContainsKey(msg.Name))
            {
                MessageLogHandler.AddLogEntry(LogEntryType.Send, msg.Name, msg.ToString(), "No listeners are setup. Discarding message!");
                return(false);
            }

            _messageQueue.Enqueue(msg);
            return(true);
        }
Exemple #2
0
        /// <summary>
        /// Remove the listener from the specified message type.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="handler"></param>
        /// <returns></returns>
        public void RemoveListener <T>(MessageListenerDelegate handler) where T : BaseMessage
        {
            var    messageType = typeof(T);
            string messageName = messageType.Name;

            Assert.IsTrue(_listeners.ContainsKey(messageName), "You are trying to remove a handler that a message type that isn't registered for " + messageName);

            List <MessageListenerDelegate> listenerList = _listeners[messageName];

            Assert.IsTrue(listenerList.Contains(handler), "You are trying to remove a handler that isn't registered for " + messageName);
            listenerList.Remove(handler);

            MessageLogHandler.AddLogEntry(LogEntryType.RemoveListener, messageName);
        }
Exemple #3
0
        /// <summary>
        /// Add a listener for the specified message type. Be sure to call RemoveListener when you are done.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="handler"></param>
        /// <returns></returns>
        public void AddListener <T>(MessageListenerDelegate handler) where T : BaseMessage
        {
            var    messageType = typeof(T);
            string messageName = messageType.Name;

            if (!_listeners.ContainsKey(messageName))
            {
                _listeners.Add(messageName, new List <MessageListenerDelegate>());
            }

            List <MessageListenerDelegate> listenerList = _listeners[messageName];

            Assert.IsFalse(listenerList.Contains(handler), "You should not add the same listener multiple times for " + messageName);
            listenerList.Add(handler);

            MessageLogHandler.AddLogEntry(LogEntryType.AddListener, messageName);
        }
Exemple #4
0
        /// <summary>
        /// Immediately send the specified message to all listeners.
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool TriggerMessage(BaseMessage msg)
        {
            if (!_listeners.ContainsKey(msg.Name))
            {
                MessageLogHandler.AddLogEntry(LogEntryType.Send, msg.Name, msg.ToString(), "No listeners are setup. Discarding message!");
                return(false);
            }

            List <MessageListenerDelegate> listenerList = _listeners[msg.Name];

            for (int i = 0; i < listenerList.Count; ++i)
            {
                var sent = listenerList[i](msg);

                if (msg.SendMode == BaseMessage.SendModeType.SendToFirst && sent)
                {
                    MessageLogHandler.AddLogEntry(LogEntryType.Send, msg.Name, msg.ToString(), "Sent to first listener.");
                    return(true);
                }
            }
            MessageLogHandler.AddLogEntry(LogEntryType.Send, msg.Name, msg.ToString(), "Sent to " + listenerList.Count + " listeners.");
            return(true);
        }