public async Task StartAsync(MqttServerOptions options, IMqttNetLogger logger)
        {
            if (_listener != null)
            {
                throw new InvalidOperationException("Server is already started.");
            }

            if (logger is null)
            {
                throw new ArgumentNullException(nameof(logger));
            }
            _rootLogger = logger;
            _logger     = logger.WithSource(nameof(MqttTcpServerAdapter));

            _options = options ?? throw new ArgumentNullException(nameof(options));

            if (options.DefaultEndpointOptions.IsEnabled)
            {
                _listener = new StreamSocketListener();

                // This also affects the client sockets.
                _listener.Control.NoDelay          = options.DefaultEndpointOptions.NoDelay;
                _listener.Control.KeepAlive        = true;
                _listener.Control.QualityOfService = SocketQualityOfService.LowLatency;
                _listener.ConnectionReceived      += OnConnectionReceivedAsync;

                await _listener.BindServiceNameAsync(options.DefaultEndpointOptions.Port.ToString(), SocketProtectionLevel.PlainSocket);
            }

            if (options.TlsEndpointOptions.IsEnabled)
            {
                throw new NotSupportedException("TLS servers are not supported when using 'uap10.0'.");
            }
        }
예제 #2
0
        public LowLevelMqttClient(IMqttClientAdapterFactory clientAdapterFactory, IMqttNetLogger logger)
        {
            _clientAdapterFactory = clientAdapterFactory ?? throw new ArgumentNullException(nameof(clientAdapterFactory));

            _rootLogger = logger ?? throw new ArgumentNullException(nameof(logger));
            _logger     = logger.WithSource(nameof(LowLevelMqttClient));
        }
예제 #3
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));
        }
예제 #4
0
        public MqttServerEventDispatcher(IMqttNetLogger logger)
        {
            if (logger is null)
            {
                throw new ArgumentNullException(nameof(logger));
            }

            _logger = logger.WithSource(nameof(MqttServerEventDispatcher));
        }
예제 #5
0
        public MqttRetainedMessagesManager(MqttServerEventContainer eventContainer, IMqttNetLogger logger)
        {
            _eventContainer = eventContainer ?? throw new ArgumentNullException(nameof(eventContainer));

            if (logger == null)
            {
                throw new ArgumentNullException(nameof(logger));
            }
            _logger = logger.WithSource(nameof(MqttRetainedMessagesManager));
        }
예제 #6
0
        public MqttClient(IMqttClientAdapterFactory channelFactory, IMqttNetLogger logger)
        {
            if (logger == null)
            {
                throw new ArgumentNullException(nameof(logger));
            }

            _adapterFactory = channelFactory ?? throw new ArgumentNullException(nameof(channelFactory));
            _logger         = logger.WithSource(nameof(MqttClient));
        }
        public MqttPacketInspector(AsyncEvent <InspectMqttPacketEventArgs> asyncEvent, IMqttNetLogger logger)
        {
            _asyncEvent = asyncEvent ?? throw new ArgumentNullException(nameof(asyncEvent));

            if (logger == null)
            {
                throw new ArgumentNullException(nameof(logger));
            }
            _logger = logger.WithSource(nameof(MqttPacketInspector));
        }
예제 #8
0
        public ManagedMqttClient(IMqttClient mqttClient, IMqttNetLogger logger)
        {
            InternalClient = mqttClient ?? throw new ArgumentNullException(nameof(mqttClient));

            if (logger == null)
            {
                throw new ArgumentNullException(nameof(logger));
            }
            _logger = logger.WithSource(nameof(ManagedMqttClient));
        }
예제 #9
0
        // TODO: Get rid of the logger here!
        public Task Start(IMqttServerOptions options, IMqttNetLogger logger)
        {
            if (logger == null)
            {
                throw new ArgumentNullException(nameof(logger));
            }
            _logger = logger.WithSource(nameof(MqttRetainedMessagesManager));

            _options = options ?? throw new ArgumentNullException(nameof(options));
            return(PlatformAbstractionLayer.CompletedTask);
        }
예제 #10
0
        public MqttServerKeepAliveMonitor(IMqttServerOptions options, MqttClientSessionsManager sessionsManager, IMqttNetLogger logger)
        {
            _options         = options ?? throw new ArgumentNullException(nameof(options));
            _sessionsManager = sessionsManager ?? throw new ArgumentNullException(nameof(sessionsManager));

            if (logger == null)
            {
                throw new ArgumentNullException(nameof(logger));
            }
            _logger = logger.WithSource(nameof(MqttServerKeepAliveMonitor));
        }
예제 #11
0
        public MqttChannelAdapter(IMqttChannel channel, MqttPacketFormatterAdapter packetFormatterAdapter, IMqttPacketInspector packetInspector, IMqttNetLogger logger)
        {
            _channel = channel ?? throw new ArgumentNullException(nameof(channel));
            PacketFormatterAdapter = packetFormatterAdapter ?? throw new ArgumentNullException(nameof(packetFormatterAdapter));

            _packetInspectorHandler = new MqttPacketInspectorHandler(packetInspector, logger);

            if (logger == null)
            {
                throw new ArgumentNullException(nameof(logger));
            }
            _logger = logger.WithSource(nameof(MqttChannelAdapter));
        }
예제 #12
0
        public MqttPacketInspectorHandler(IMqttPacketInspector packetInspector, IMqttNetLogger logger)
        {
            _packetInspector = packetInspector;

            if (packetInspector != null)
            {
                _receivedPacketBuffer = new MemoryStream();
            }

            if (logger == null)
            {
                throw new ArgumentNullException(nameof(logger));
            }
            _logger = logger.WithSource(nameof(MqttPacketInspectorHandler));
        }
예제 #13
0
        public MqttServer(IEnumerable <IMqttServerAdapter> adapters, IMqttNetLogger logger)
        {
            if (adapters == null)
            {
                throw new ArgumentNullException(nameof(adapters));
            }
            _adapters = adapters.ToList();

            if (logger == null)
            {
                throw new ArgumentNullException(nameof(logger));
            }
            _logger     = logger.WithSource(nameof(MqttServer));
            _rootLogger = logger;

            _eventDispatcher = new MqttServerEventDispatcher(logger);
        }
예제 #14
0
        public MqttTcpServerListener(
            AddressFamily addressFamily,
            MqttServerTcpEndpointBaseOptions options,
            X509Certificate2 tlsCertificate,
            IMqttNetLogger logger)
        {
            _addressFamily  = addressFamily;
            _options        = options;
            _tlsCertificate = tlsCertificate;
            _rootLogger     = logger;
            _logger         = logger.WithSource(nameof(MqttTcpServerListener));

            if (_options is MqttServerTlsTcpEndpointOptions tlsOptions)
            {
                _tlsOptions = tlsOptions;
            }
        }
        public MqttClientSessionsManager(
            MqttServerOptions options,
            MqttRetainedMessagesManager retainedMessagesManager,
            MqttServerEventContainer eventContainer,
            IMqttNetLogger logger)
        {
            if (logger == null)
            {
                throw new ArgumentNullException(nameof(logger));
            }

            _logger     = logger.WithSource(nameof(MqttClientSessionsManager));
            _rootLogger = logger;

            _options = options ?? throw new ArgumentNullException(nameof(options));
            _retainedMessagesManager = retainedMessagesManager ?? throw new ArgumentNullException(nameof(retainedMessagesManager));
            _eventContainer          = eventContainer ?? throw new ArgumentNullException(nameof(eventContainer));
        }
예제 #16
0
        public MqttServer(MqttServerOptions options, IEnumerable <IMqttServerAdapter> adapters, IMqttNetLogger logger)
        {
            _options = options ?? throw new ArgumentNullException(nameof(options));

            if (adapters == null)
            {
                throw new ArgumentNullException(nameof(adapters));
            }

            _adapters = adapters.ToList();

            _rootLogger = logger ?? throw new ArgumentNullException(nameof(logger));
            _logger     = logger.WithSource(nameof(MqttServer));

            _retainedMessagesManager = new MqttRetainedMessagesManager(_eventContainer, _rootLogger);
            _clientSessionsManager   = new MqttClientSessionsManager(options, _retainedMessagesManager, _eventContainer, _rootLogger);
            _keepAliveMonitor        = new MqttServerKeepAliveMonitor(options, _clientSessionsManager, _rootLogger);
        }
예제 #17
0
        public MqttTcpServerListener(
            AddressFamily addressFamily,
            MqttServerOptions serverOptions,
            MqttServerTcpEndpointBaseOptions tcpEndpointOptions,
            X509Certificate2 tlsCertificate,
            IMqttNetLogger logger)
        {
            _addressFamily  = addressFamily;
            _serverOptions  = serverOptions ?? throw new ArgumentNullException(nameof(serverOptions));
            _options        = tcpEndpointOptions ?? throw new ArgumentNullException(nameof(tcpEndpointOptions));
            _tlsCertificate = tlsCertificate;
            _rootLogger     = logger;
            _logger         = logger.WithSource(nameof(MqttTcpServerListener));

            if (_options is MqttServerTlsTcpEndpointOptions tlsOptions)
            {
                _tlsOptions = tlsOptions;
            }
        }
예제 #18
0
        /// <summary>
        /// Create a rx mqtt client based on a <see cref="IManagedMqttClient"/>.
        /// </summary>
        /// <param name="managedMqttClient">The manged mqtt client.</param>
        /// <param name="logger">The mqtt net logger.</param>
        /// <remarks>
        /// Use the <see cref="MqttFactoryExtensions.CreateRxMqttClient(IMqttFactory)"/> or
        /// <see cref="MqttFactoryExtensions.CreateRxMqttClient(IMqttFactory, IMqttNetLogger)"/>
        /// factory methods to crate the client.
        /// </remarks>
        /// <exception cref="ArgumentNullException"></exception>
        public RxMqttClient(IManagedMqttClient managedMqttClient, IMqttNetLogger logger)
        {
            InternalClient = managedMqttClient ?? throw new ArgumentNullException(nameof(managedMqttClient));
            if (logger == null)
            {
                throw new ArgumentNullException(nameof(logger));
            }

            this.logger            = logger.WithSource(nameof(RxMqttClient));
            topicSubscriptionCache = new Dictionary <string, IObservable <MqttApplicationMessageReceivedEventArgs> >();

            var cancelationSubject = new Subject <Unit>();

            ConnectedEvent = CrateFromHandler <MqttClientConnectedEventArgs>(observer =>
            {
                managedMqttClient.UseConnectedHandler(args => observer.OnNext(args));
                return(Disposable.Create(() => managedMqttClient.ConnectedHandler = null));
            });

            DisconnectedEvent = CrateFromHandler <MqttClientDisconnectedEventArgs>(observer =>
            {
                managedMqttClient.UseDisconnectedHandler(args => observer.OnNext(args));
                return(Disposable.Create(() => managedMqttClient.DisconnectedHandler = null));
            });

            ConnectingFailedEvent = CrateFromHandler <ManagedProcessFailedEventArgs>(observer =>
            {
                managedMqttClient.ConnectingFailedHandler = new ConnectingFailedHandlerDelegate(args => observer.OnNext(args));
                return(Disposable.Create(() => managedMqttClient.ConnectingFailedHandler = null));
            });

            SynchronizingSubscriptionsFailedEvent = CrateFromHandler <ManagedProcessFailedEventArgs>(observer =>
            {
                managedMqttClient.SynchronizingSubscriptionsFailedHandler = new SynchronizingSubscriptionsFailedHandlerDelegate(args => observer.OnNext(args));
                return(Disposable.Create(() => managedMqttClient.SynchronizingSubscriptionsFailedHandler = null));
            });

            ApplicationMessageProcessedEvent = CrateFromHandler <ApplicationMessageProcessedEventArgs>(observer =>
            {
                managedMqttClient.ApplicationMessageProcessedHandler = new ApplicationMessageProcessedHandlerDelegate(args => observer.OnNext(args));
                return(Disposable.Create(() => managedMqttClient.ApplicationMessageReceivedHandler = null));
            });

            ApplicationMessageSkippedEvent = CrateFromHandler <ApplicationMessageSkippedEventArgs>(observer =>
            {
                managedMqttClient.ApplicationMessageSkippedHandler = new ApplicationMessageSkippedHandlerDelegate(args => observer.OnNext(args));
                return(Disposable.Create(() => managedMqttClient.ApplicationMessageReceivedHandler = null));
            });

            Connected = Observable
                        .Create <bool>(observer =>
            {
                var connected    = ConnectedEvent.Subscribe(_ => observer.OnNext(true));
                var disconnected = DisconnectedEvent.Subscribe(_ => observer.OnNext(false));
                return(new CompositeDisposable(connected, disconnected));
            })
                        .TakeUntil(cancelationSubject) // complete on dispose
                        .Prepend(IsConnected)          // start with current state
                        .Append(false)                 // finish with false
                        .Replay(1)                     // replay last state on subscribe
                        .RefCount();                   // count subscriptions and dispose source observable when no subscription

            applicationMessageReceived = CrateFromHandler <MqttApplicationMessageReceivedEventArgs>(observer =>
            {
                managedMqttClient.UseApplicationMessageReceivedHandler(args => observer.OnNext(args));
                return(Disposable.Create(() => managedMqttClient.ApplicationMessageReceivedHandler = null));
            });

            IObservable <T> CrateFromHandler <T>(Func <IObserver <T>, IDisposable> func)
            {
                return(Observable.Create(func)
                       .TakeUntil(cancelationSubject) // complete on dispose
                       .Publish()                     // publish from on source observable
                       .RefCount());                  // count subscriptions and dispose source observable when no subscription
            }

            cleanUp = Disposable.Create(() =>
            {
                cancelationSubject.OnNext(Unit.Default);    // complete all observers
                cancelationSubject.Dispose();
                try { managedMqttClient.Dispose(); }
                catch { }
            });
        }
예제 #19
0
 public MqttTcpServerAdapter(IMqttNetLogger logger)
 {
     _rootLogger = logger ?? throw new ArgumentNullException(nameof(logger));
     _logger     = logger.WithSource(nameof(MqttTcpServerAdapter));
 }