Esempio n. 1
0
        /// <summary>
        /// Occurs when a message is received.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private Task OnMessageReceived(MqttApplicationMessageReceivedEventArgs e)
        {
            return(Task.Run(() =>
            {
                try
                {
                    if (!e.ApplicationMessage.Topic.StartsWith("emitter"))
                    {
                        var handlers = this.Trie.Match(e.ApplicationMessage.Topic);
                        // Invoke every handler matching the channel
                        foreach (MessageHandler handler in handlers)
                        {
                            handler(e.ApplicationMessage.Topic, e.ApplicationMessage.Payload);
                        }

                        if (handlers.Count == 0)
                        {
                            DefaultMessageHandler?.Invoke(e.ApplicationMessage.Topic, e.ApplicationMessage.Payload);
                        }
                        return;
                    }

                    // Did we receive a keygen response?
                    if (e.ApplicationMessage.Topic == "emitter/keygen/")
                    {
                        // Deserialize the response
                        var response = KeygenResponse.FromBinary(e.ApplicationMessage.Payload);
                        if (response == null || response.Status != 200)
                        {
                            this.InvokeError(response.Status);
                            return;
                        }

                        // Call the response handler we have registered previously
                        // TODO: get rid of the handler afterwards, or refac keygen
                        if (this.KeygenHandlers.ContainsKey(response.Channel))
                        {
                            ((KeygenHandler)this.KeygenHandlers[response.Channel])(response);
                        }
                        return;
                    }

                    if (e.ApplicationMessage.Topic == "emitter/presence/")
                    {
                        var presenceEvent = PresenceEvent.FromBinary(e.ApplicationMessage.Payload);

                        var handlers = this.PresenceTrie.Match(presenceEvent.Channel);
                        // Invoke every handler matching the channel
                        foreach (PresenceHandler handler in handlers)
                        {
                            handler(presenceEvent);
                        }

                        if (handlers.Count == 0)
                        {
                            DefaultPresenceHandler?.Invoke(presenceEvent);
                        }

                        return;
                    }

                    if (e.ApplicationMessage.Topic == "emitter/error/")
                    {
                        var errorEvent = ErrorEvent.FromBinary(e.ApplicationMessage.Payload);
                        var emitterException = new EmitterException((EmitterEventCode)errorEvent.Status, errorEvent.Message);

                        InvokeError(emitterException);
                        return;
                    }

                    if (e.ApplicationMessage.Topic == "emitter/me/")
                    {
                        var meResponse = MeResponse.FromBinary(e.ApplicationMessage.Payload);
                        Me?.Invoke(meResponse);

                        return;
                    }
                    return;
                }
                catch (Exception ex)
                {
                    this.InvokeError(ex);
                    return;
                }
            }));
        }