private void PublishThread() { while (this.isRunning) { try { var publish = (MqttMsgPublish)this.publishQueue.Take(); if (publish != null) { Interlocked.Increment(ref numberOfMessagesPublished); // get all subscriptions for a topic var subscriptions = MqttSubscriberManager.GetSubscriptionsByTopic(publish.Topic); foreach (var subscription in subscriptions) { var qosLevel = (subscription.QosLevel < publish.QosLevel) ? subscription.QosLevel : publish.QosLevel; // send PUBLISH message to the current subscriber MqttMessageToClientConnectionManager.Publish(subscription.ClientConnection, publish.Topic, publish.Message, qosLevel, publish.Retain); } } } catch (Exception exception) { logger.LogException(this, exception); } } }
private static void SubscribersForRetainedThread() { while (isRunning) { try { var subscription = SubscribersForRetained.Take(); var query = from p in RetainedMessages where (new Regex(subscription.Topic)).IsMatch(p.Key) // check for topics based also on wildcard with regex select p.Value; foreach (MqttMsgPublish retained in query) { var qosLevel = (subscription.QosLevel < retained.QosLevel) ? subscription.QosLevel : retained.QosLevel; // send PUBLISH message to the current subscriber MqttMessageToClientConnectionManager.Publish(subscription.ClientConnection, retained.Topic, retained.Message, qosLevel, retained.Retain); } } catch (Exception) { throw; } } }
private void ClientsForSessionThread() { while (isRunning) { try { string clientId = this.clientsForSession.Take(); MqttBrokerSession session = MqttSessionManager.GetSession(clientId); MqttMsgPublish outgoingMsg; while (session.OutgoingMessages.TryDequeue(out outgoingMsg)) { var query = from s in session.Subscriptions where (new Regex(s.Topic)).IsMatch(outgoingMsg.Topic) // check for topics based also on wildcard with regex select s; MqttSubscription subscription = query.FirstOrDefault(); if (subscription != null) { var qosLevel = (subscription.QosLevel < outgoingMsg.QosLevel) ? subscription.QosLevel : outgoingMsg.QosLevel; MqttMessageToClientConnectionManager.Publish(subscription.ClientConnection, outgoingMsg.Topic, outgoingMsg.Message, qosLevel, outgoingMsg.Retain); } } } catch (Exception exception) { logger.LogException(this, exception); } } }