Beispiel #1
0
 public RoutedMessage(NetworkMessageHeader header, int len, byte[] msg, MessageConsumerDelegate consumer, MessageDecoded listener)
 {
     Header       = header;
     HeaderLength = len;
     Message      = msg;
     Consumer     = consumer;
     Listener     = listener;
 }
        public void Subscribe(int opcode, MessageDecoded listener)
        {
            if (!_listeners.ContainsKey(opcode))
            {
                _listeners.Add(opcode, null);
            }

            if (listener != null)
            {
                _listeners[opcode] += listener;
            }
        }
 public void Start()
 {
     if (m_MessageQueue == null)
     {
         InvalidOperationException ex =
             new InvalidOperationException("This object has not been initialized with a ConcurrentQueue<MqttMessage> messageQueue. "
                                           + "Before you can call Start() you have to provide the message queue."
                                           );
         Logger.Error("No message queue available. Unable to start.", ex);
         throw ex;
     }
     m_Stop = false;
     while (!m_Stop)
     {
         MqttMessage msg;
         if (m_MessageQueue.TryDequeue(out msg))
         {
             if (Logger.IsDebugEnabled)
             {
                 Logger.Debug("Starting to decode message...");
             }
             Logger.Info($"Topic: {msg.Topic}");
             NetworkMessage decodedMessage = null;
             try
             {
                 decodedMessage = ParseBinaryMessage(msg.Payload);
             }
             catch (Exception e)
             {
                 if (Debugger.IsAttached)
                 {
                     Debugger.Break();
                 }
                 Logger.Error($"Error while decoding message: {e}");
                 Console.WriteLine($"Error while decoding message: {e}");
             }
             if (decodedMessage != null)
             {
                 MessageDecoded?.Invoke(this, new MessageDecodedEventArgs(decodedMessage, msg.Topic));
             }
         }
         else
         {
             Thread.Sleep(10);
         }
     }
 }
        public void Unsubscribe(int opcode, MessageDecoded listener)
        {
            if (!_listeners.ContainsKey(opcode))
            {
                return;
            }

            if (listener != null)
            {
                _listeners[opcode] -= listener;
            }

            if (_listeners[opcode] == null)
            {
                _listeners.Remove(opcode);
            }
        }
Beispiel #5
0
 protected virtual void OnMessageDecoded(MessageEventArgs args)
 {
     MessageDecoded?.Invoke(this, args);
 }
 public void Unsubscribe(int opcode, MessageDecoded listener)
 {
     _dispatcher.Unsubscribe(opcode, listener);
 }