コード例 #1
0
        public MqttClient(
            MqttConnectPacket connectPacket,
            IMqttChannelAdapter channelAdapter,
            MqttSession session,
            MqttServerOptions serverOptions,
            MqttServerEventContainer eventContainer,
            MqttClientSessionsManager sessionsManager,
            IMqttNetLogger logger)
        {
            _serverOptions   = serverOptions ?? throw new ArgumentNullException(nameof(serverOptions));
            _eventContainer  = eventContainer;
            _sessionsManager = sessionsManager ?? throw new ArgumentNullException(nameof(sessionsManager));

            ChannelAdapter = channelAdapter ?? throw new ArgumentNullException(nameof(channelAdapter));
            Endpoint       = channelAdapter.Endpoint;

            Session        = session ?? throw new ArgumentNullException(nameof(session));
            _connectPacket = connectPacket ?? throw new ArgumentNullException(nameof(connectPacket));

            if (logger == null)
            {
                throw new ArgumentNullException(nameof(logger));
            }
            _logger = logger.WithSource(nameof(MqttClient));
        }
コード例 #2
0
        async Task <List <string> > PrepareTopicHashSubscriptions(TopicHashSelector selector)
        {
            Dictionary <string, List <string> > topicsByPublisher;
            Dictionary <string, List <string> > singleWildcardTopicsByPublisher;
            Dictionary <string, List <string> > multiWildcardTopicsByPublisher;

            const int NumPublishers         = 1;
            const int NumTopicsPerPublisher = 10000;

            TopicGenerator.Generate(NumPublishers, NumTopicsPerPublisher, out topicsByPublisher, out singleWildcardTopicsByPublisher, out multiWildcardTopicsByPublisher);

            var topics = topicsByPublisher.FirstOrDefault().Value;
            var singleWildcardTopics = singleWildcardTopicsByPublisher.FirstOrDefault().Value;
            var multiWildcardTopics  = multiWildcardTopicsByPublisher.FirstOrDefault().Value;

            const string ClientId                = "Client1";
            var          logger                  = new Mockups.TestLogger();
            var          serverOptions           = new MQTTnet.Server.MqttServerOptions();
            var          eventContainer          = new MQTTnet.Server.MqttServerEventContainer();
            var          retainedMessagesManager = new MqttRetainedMessagesManager(eventContainer, logger);
            var          sessionManager          = new MqttClientSessionsManager(serverOptions, retainedMessagesManager, eventContainer, logger);

            _clientSession = new MQTTnet.Server.MqttSession(
                ClientId,
                false,
                new Dictionary <object, object>(),
                serverOptions,
                eventContainer,
                retainedMessagesManager,
                sessionManager
                );

            List <string> topicsToSubscribe;

            switch (selector)
            {
            case TopicHashSelector.SingleWildcard:
                topicsToSubscribe = singleWildcardTopics;
                break;

            case TopicHashSelector.MultiWildcard:
                topicsToSubscribe = multiWildcardTopics;
                break;

            default:
                topicsToSubscribe = topics;
                break;
            }
            foreach (var t in topicsToSubscribe)
            {
                var subPacket = new Packets.MqttSubscribePacket();
                var filter    = new Packets.MqttTopicFilter();
                filter.Topic = t;
                subPacket.TopicFilters.Add(filter);
                await _clientSession.SubscriptionsManager.Subscribe(subPacket, default(CancellationToken));
            }

            return(topics);
        }
コード例 #3
0
 public MqttClientSubscriptionsManager(
     MqttSession session,
     MqttServerEventContainer eventContainer,
     MqttRetainedMessagesManager retainedMessagesManager,
     ISubscriptionChangedNotification subscriptionChangedNotification)
 {
     _session                         = session ?? throw new ArgumentNullException(nameof(session));
     _eventContainer                  = eventContainer ?? throw new ArgumentNullException(nameof(eventContainer));
     _retainedMessagesManager         = retainedMessagesManager ?? throw new ArgumentNullException(nameof(retainedMessagesManager));
     _subscriptionChangedNotification = subscriptionChangedNotification;
 }
コード例 #4
0
        public void OnSubscriptionsRemoved(MqttSession clientSession, List <string> subscriptionTopics)
        {
            lock (_sessionsManagementLock)
            {
                foreach (var subscriptionTopic in subscriptionTopics)
                {
                    clientSession.RemoveSubscribedTopic(subscriptionTopic);
                }

                if (!clientSession.HasSubscribedTopics)
                {
                    // last subscription removed
                    _subscriberSessions.Remove(clientSession);
                }
            }
        }
コード例 #5
0
        public void OnSubscriptionsAdded(MqttSession clientSession, List <string> topics)
        {
            lock (_sessionsManagementLock)
            {
                if (!clientSession.HasSubscribedTopics)
                {
                    // first subscribed topic
                    _subscriberSessions.Add(clientSession);
                }

                foreach (var topic in topics)
                {
                    clientSession.AddSubscribedTopic(topic);
                }
            }
        }
コード例 #6
0
        int CheckTopicSubscriptions(MQTTnet.Server.MqttSession clientSession, List <string> topicsToFind, string subject)
        {
            var matchCount = 0;

            {
                int resultCount = 0;

                var stopWatch = new System.Diagnostics.Stopwatch();
                stopWatch.Start();
                var countUp   = 0;
                var countDown = topicsToFind.Count - 1;
                for (; countUp < topicsToFind.Count; ++countUp, --countDown)
                {
                    var topicToFind = topicsToFind[countDown];

                    UInt64 topicHash;
                    UInt64 hashMask;
                    bool   hasWildcard;
                    MQTTnet.Server.MqttSubscription.CalculateTopicHash((string)topicToFind, out topicHash, out hashMask, out hasWildcard);

                    var result = clientSession.SubscriptionsManager.CheckSubscriptions((string)topicToFind, topicHash, MqttQualityOfServiceLevel.AtMostOnce, "OtherClient");
                    if (result.IsSubscribed)
                    {
                        ++matchCount;
                    }
                    ++resultCount;
                }

                stopWatch.Stop();

                Console.Write("Match count: " + matchCount + "; ");

                Console.WriteLine(subject + " lookup milliseconds: " + stopWatch.ElapsedMilliseconds);
            }

            return(matchCount);
        }
コード例 #7
0
 MqttClient CreateConnection(MqttConnectPacket connectPacket, IMqttChannelAdapter channelAdapter, MqttSession session)
 {
     return(new MqttClient(connectPacket, channelAdapter, session, _options, _eventContainer, this, _rootLogger));
 }
コード例 #8
0
 public MqttSessionStatus(MqttSession session)
 {
     _session = session ?? throw new ArgumentNullException(nameof(session));
 }