示例#1
0
        private IMqttServerOptions CreateMqttServerOptions()
        {
            var options = new MqttServerOptionsBuilder()
                          .WithMaxPendingMessagesPerClient(_settings.MaxPendingMessagesPerClient)
                          .WithDefaultCommunicationTimeout(TimeSpan.FromSeconds(_settings.CommunicationTimeout))
                          .WithConnectionValidator(_mqttConnectionValidator)
                          .WithStorage(_mqttServerStorage);

            // Configure unencrypted connections
            if (_settings.TcpEndPoint.Enabled)
            {
                options.WithDefaultEndpoint();

                if (_settings.TcpEndPoint.TryReadIPv4(out var address4))
                {
                    options.WithDefaultEndpointBoundIPAddress(address4);
                }

                if (_settings.TcpEndPoint.TryReadIPv6(out var address6))
                {
                    options.WithDefaultEndpointBoundIPV6Address(address6);
                }

                if (_settings.TcpEndPoint.Port > 0)
                {
                    options.WithDefaultEndpointPort(_settings.TcpEndPoint.Port);
                }
            }
            else
            {
                options.WithoutDefaultEndpoint();
            }

            if (_settings.ConnectionBacklog > 0)
            {
                options.WithConnectionBacklog(_settings.ConnectionBacklog);
            }

            if (_settings.EnablePersistentSessions)
            {
                options.WithPersistentSessions();
            }

            if (_settings.UseOriginalReseiverClientId)
            {
                options.WithApplicationMessageInterceptor(_messageInterceptor);
            }

            return(options.Build());
        }
示例#2
0
        public void changeServerConfig(ServerConfigData MQTTConfigData)
        {
            _theAuthenticator    = MQTTConfigData.Authenticator;
            _ServerConfiguration = new MqttServerOptionsBuilder();
            _ServerConfiguration.WithConnectionValidator(this);
            _ServerConfiguration.WithApplicationMessageInterceptor(this);
            _ServerConfiguration.WithSubscriptionInterceptor(this);
            _ServerConfiguration.WithClientId("Server");
            _ServerConfiguration.WithDefaultEndpointBoundIPAddress(MQTTConfigData.listenAdress);
            _ServerConfiguration.WithDefaultEndpointPort(MQTTConfigData.Port);
            _ServerConfiguration.WithMaxPendingMessagesPerClient(FedNetConstante.MAX_MESSAGE_PENDING);

            _logSystem.Info("reinitialisation finnised !!");
        }
示例#3
0
        public FedNetServer(ServerConfigData MQTTConfigData, IFedNetLogger newlogSystem = null)
        {
            _logSystem = newlogSystem;
            if (_logSystem == null)
            {
                _logSystem = new DefaultLogger();
            }

            _theAuthenticator    = MQTTConfigData.Authenticator;
            _ServerConfiguration = new MqttServerOptionsBuilder();
            _ServerConfiguration.WithConnectionValidator(this);
            _ServerConfiguration.WithApplicationMessageInterceptor(this);
            _ServerConfiguration.WithSubscriptionInterceptor(this);
            _ServerConfiguration.WithClientId("Server");
            _ServerConfiguration.WithDefaultEndpointBoundIPAddress(MQTTConfigData.listenAdress);
            _ServerConfiguration.WithDefaultEndpointPort(MQTTConfigData.Port);
            _ServerConfiguration.WithMaxPendingMessagesPerClient(FedNetConstante.MAX_MESSAGE_PENDING);

            _theClientList = new List <ClientData>();
            _theGameServer = new MqttFactory().CreateMqttServer();
            _theGameServer.UseApplicationMessageReceivedHandler(e => {
                if (e.ClientId == "" || e.ClientId == " " || e.ClientId == null)
                {
                    return;
                }
                if (MessageReceived != null)
                {
                    MessageReceived.Invoke(this, Message.getMessage(e.ApplicationMessage));
                }
            });
            _theGameServer.UseClientConnectedHandler(e => {
                _logSystem.Info("new client connected : " + e.ClientId);
                //_theGameServer.SubscribeAsync("Server", FedNetConstante.CLIENT_TO_SERVER + FedNetConstante.DEFAULT_TOPIC_SEPARATOR + FedNetConstante.DEFAULT_TOPIC_JOKER, (MqttQualityOfServiceLevel)FedNetConstante.PRIORITY_SERVER_TO_CLIENT);
            });
            _theGameServer.StartedHandler            = this;
            _theGameServer.StoppedHandler            = this;
            _theGameServer.ClientDisconnectedHandler = this;

            _logSystem.Info("initialisation finished !!");
        }
示例#4
0
        static Igloo15MqttServer InitializeServer(Options config, ILoggerFactory factory)
        {
            Igloo15MqttServer server = null;

            try
            {
                MqttServerOptionsBuilder serverBuilder = new MqttServerOptionsBuilder();

                if (config.Encrypted && File.Exists(config.CertificateLocation))
                {
                    if (config.IPAddress == "Any")
                    {
                        serverBuilder.WithEncryptedEndpoint();
                    }
                    else if (IPAddress.TryParse(config.IPAddress, out IPAddress address))
                    {
                        serverBuilder.WithEncryptedEndpointBoundIPAddress(address);
                    }
                    else
                    {
                        _logger.LogWarning($"Failed to parse provided IP Address : {config.IPAddress} using default");
                        serverBuilder.WithEncryptedEndpoint();
                    }

                    serverBuilder.WithEncryptedEndpointPort(config.Port);

                    var certificate = new X509Certificate2(config.CertificateLocation, config.CertificatePassword);
                    var certBytes   = certificate.Export(X509ContentType.Cert);

                    serverBuilder.WithEncryptionCertificate(certBytes);
                }
                else
                {
                    if (config.IPAddress == "Any")
                    {
                        serverBuilder.WithDefaultEndpoint();
                    }
                    else if (IPAddress.TryParse(config.IPAddress, out IPAddress address))
                    {
                        serverBuilder.WithDefaultEndpointBoundIPAddress(address);
                    }
                    else
                    {
                        _logger.LogWarning("Failed to parse provided IP Address : {IPAddress} using default", config.IPAddress);
                        serverBuilder.WithDefaultEndpoint();
                    }

                    serverBuilder.WithDefaultEndpointPort(config.Port);
                }

                if (config.Persistent)
                {
                    serverBuilder.WithPersistentSessions();
                }

                if (!String.IsNullOrEmpty(config.MessageStorageLocation))
                {
                    serverBuilder.WithStorage(new RetainedMessageHandler(config.MessageStorageLocation));
                }

                if (config.ShowSubscriptions)
                {
                    serverBuilder.WithSubscriptionInterceptor(_interceptors.SubscriptionInterceptor);
                }

                if (config.ShowMessages)
                {
                    serverBuilder.WithApplicationMessageInterceptor(_interceptors.MessageInterceptor);
                }

                if (config.ShowClientConnections)
                {
                    serverBuilder.WithConnectionValidator(_interceptors.ConnectionInterceptor);
                }

                serverBuilder
                .WithConnectionBacklog(config.ConnectionBacklog)
                .WithMaxPendingMessagesPerClient(config.MaxPendingMessagesPerClient)
                .WithDefaultCommunicationTimeout(TimeSpan.FromSeconds(config.CommunicationTimeout));


                server = new Igloo15MqttServer(serverBuilder.Build(), factory);
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Exception occured during Initialization of Server");
                return(null);
            }

            return(server);
        }
示例#5
0
        private async void btnStart_Click(object sender, EventArgs e)
        {
            if (this.mqttServer != null)
            {
                return;
            }

            this.mqttServer = new MqttFactory().CreateMqttServer();
            this.mqttServer.StartedHandler                    = this;
            this.mqttServer.StoppedHandler                    = this;
            this.mqttServer.ClientConnectedHandler            = this;
            this.mqttServer.ClientDisconnectedHandler         = this;
            this.mqttServer.ClientSubscribedTopicHandler      = this;
            this.mqttServer.ClientUnsubscribedTopicHandler    = this;
            this.mqttServer.ApplicationMessageReceivedHandler = this;

            var optionsBuilder = new MqttServerOptionsBuilder().WithConnectionBacklog(1000).WithDefaultEndpointPort(Convert.ToInt32(tbxPort.Text));

            if (!string.IsNullOrEmpty(tbxServer.Text))
            {
                optionsBuilder.WithDefaultEndpointBoundIPAddress(IPAddress.Parse(tbxServer.Text));
            }

            optionsBuilder.WithConnectionValidator(context =>
            {
                if (string.IsNullOrEmpty(context.ClientId))
                {
                    context.ReasonCode = MqttConnectReasonCode.ClientIdentifierNotValid;
                    return;
                }

                if (context.Username != Settings.Default.Username || context.Password != Settings.Default.Password)
                {
                    context.ReasonCode = MqttConnectReasonCode.BadUserNameOrPassword;
                    return;
                }

                context.ReasonCode = MqttConnectReasonCode.Success;
            });

            optionsBuilder.WithApplicationMessageInterceptor(context =>
            {
                // 控管 client 發佈
                if (string.IsNullOrEmpty(context.ClientId) || !context.ClientId.StartsWith(Settings.Default.ClientId))
                {
                    context.AcceptPublish = false;
                    return;
                }
            });

            optionsBuilder.WithSubscriptionInterceptor(context =>
            {
                // 控管 client 訂閱
                if (!context.ClientId.StartsWith(Settings.Default.ClientId))
                {
                    context.AcceptSubscription = false;
                    return;
                }
            });

            await this.mqttServer.StartAsync(optionsBuilder.Build());

            tbxServer.Enabled = false;
            tbxPort.Enabled   = false;
            btnStart.Enabled  = false;
            btnStop.Enabled   = true;
        }