diff --git a/Build/MQTTnet.AspNetCore.nuspec b/Build/MQTTnet.AspNetCore.nuspec index b29ad5c..b98eec0 100644 --- a/Build/MQTTnet.AspNetCore.nuspec +++ b/Build/MQTTnet.AspNetCore.nuspec @@ -2,7 +2,7 @@ MQTTnet.AspNetCore - 2.5.2 + 2.5.3 Christian Kratky Christian Kratky https://github.com/chkr1011/MQTTnet/blob/master/LICENSE @@ -10,14 +10,13 @@ https://raw.githubusercontent.com/chkr1011/MQTTnet/master/Images/Logo_128x128.png false This is a support library to integrate MQTTnet into AspNetCore. - * Updated to MQTTnet 2.5.2. -* Fixed WebSocket protocol negotiation. + * Updated to MQTTnet 2.5.3. Copyright Christian Kratky 2016-2017 MQTT Message Queue Telemetry Transport MQTTClient MQTTServer Server MQTTBroker Broker NETStandard IoT InternetOfThings Messaging Hardware Arduino Sensor Actuator M2M ESP Smart Home Cities Automation Xamarin - + diff --git a/Frameworks/MQTTnet.NetStandard/Implementations/MqttWebSocketChannel.cs b/Frameworks/MQTTnet.NetStandard/Implementations/MqttWebSocketChannel.cs index 9046bb9..d8e24a4 100644 --- a/Frameworks/MQTTnet.NetStandard/Implementations/MqttWebSocketChannel.cs +++ b/Frameworks/MQTTnet.NetStandard/Implementations/MqttWebSocketChannel.cs @@ -31,7 +31,7 @@ namespace MQTTnet.Implementations var uri = _options.Uri; if (!uri.StartsWith("ws://", StringComparison.OrdinalIgnoreCase) && !uri.StartsWith("wss://", StringComparison.OrdinalIgnoreCase)) { - if (!_options.TlsOptions.UseTls) + if (_options.TlsOptions?.UseTls == false) { uri = "ws://" + uri; } diff --git a/MQTTnet.Core/Server/MqttClientSubscriptionsManager.cs b/MQTTnet.Core/Server/MqttClientSubscriptionsManager.cs index 8801c01..8d88ddc 100644 --- a/MQTTnet.Core/Server/MqttClientSubscriptionsManager.cs +++ b/MQTTnet.Core/Server/MqttClientSubscriptionsManager.cs @@ -27,7 +27,7 @@ namespace MQTTnet.Core.Server foreach (var topicFilter in subscribePacket.TopicFilters) { var interceptorContext = new MqttSubscriptionInterceptorContext(clientId, topicFilter); - _options.SubscriptionsInterceptor?.Invoke(interceptorContext); + _options.SubscriptionInterceptor?.Invoke(interceptorContext); responsePacket.SubscribeReturnCodes.Add(interceptorContext.AcceptSubscription ? MqttSubscribeReturnCode.SuccessMaximumQoS1 : MqttSubscribeReturnCode.Failure); if (interceptorContext.CloseConnection) diff --git a/MQTTnet.Core/Server/MqttConnectionValidatorContext.cs b/MQTTnet.Core/Server/MqttConnectionValidatorContext.cs new file mode 100644 index 0000000..83a1eab --- /dev/null +++ b/MQTTnet.Core/Server/MqttConnectionValidatorContext.cs @@ -0,0 +1,25 @@ +using MQTTnet.Core.Protocol; + +namespace MQTTnet.Core.Server +{ + public class MqttConnectionValidatorContext + { + public MqttConnectionValidatorContext(string clientId, string username, string password, MqttApplicationMessage willMessage) + { + ClientId = clientId; + Username = username; + Password = password; + WillMessage = willMessage; + } + + public string ClientId { get; } + + public string Username { get; } + + public string Password { get; } + + public MqttApplicationMessage WillMessage { get; } + + public MqttConnectReturnCode ReturnCode { get; set; } = MqttConnectReturnCode.ConnectionAccepted; + } +} diff --git a/MQTTnet.Core/Server/MqttServerOptions.cs b/MQTTnet.Core/Server/MqttServerOptions.cs index 4b09676..8988b11 100644 --- a/MQTTnet.Core/Server/MqttServerOptions.cs +++ b/MQTTnet.Core/Server/MqttServerOptions.cs @@ -11,14 +11,14 @@ namespace MQTTnet.Core.Server public MqttServerTlsEndpointOptions TlsEndpointOptions { get; } = new MqttServerTlsEndpointOptions(); public int ConnectionBacklog { get; set; } = 10; - + public TimeSpan DefaultCommunicationTimeout { get; set; } = TimeSpan.FromSeconds(15); public Func ConnectionValidator { get; set; } public Action ApplicationMessageInterceptor { get; set; } - public Action SubscriptionsInterceptor { get; set; } + public Action SubscriptionInterceptor { get; set; } public IMqttServerStorage Storage { get; set; } } diff --git a/MQTTnet.Core/Server/MqttServerOptionsBuilder.cs b/MQTTnet.Core/Server/MqttServerOptionsBuilder.cs new file mode 100644 index 0000000..692f7c1 --- /dev/null +++ b/MQTTnet.Core/Server/MqttServerOptionsBuilder.cs @@ -0,0 +1,88 @@ +using System; +using MQTTnet.Core.Packets; +using MQTTnet.Core.Protocol; + +namespace MQTTnet.Core.Server +{ + public class MqttServerOptionsBuilder + { + private readonly MqttServerOptions _options = new MqttServerOptions(); + + public MqttServerOptionsBuilder WithConnectionBacklog(int value) + { + _options.ConnectionBacklog = value; + return this; + } + + public MqttServerOptionsBuilder WithDefaultCommunicationTimeout(TimeSpan value) + { + _options.DefaultCommunicationTimeout = value; + return this; + } + + public MqttServerOptionsBuilder WithDefaultEndpointPort(int value) + { + _options.DefaultEndpointOptions.Port = value; + return this; + } + + public MqttServerOptionsBuilder WithDefaultEndpoint() + { + _options.DefaultEndpointOptions.IsEnabled = true; + return this; + } + + public MqttServerOptionsBuilder WithoutDefaultEndpoint() + { + _options.DefaultEndpointOptions.IsEnabled = false; + return this; + } + + public MqttServerOptionsBuilder WithEncryptedEndpoint() + { + _options.TlsEndpointOptions.IsEnabled = true; + return this; + } + + public MqttServerOptionsBuilder WithoutEncryptedEndpoint() + { + _options.TlsEndpointOptions.IsEnabled = false; + return this; + } + + public MqttServerOptionsBuilder WithEncryptionCertificate(byte[] value) + { + _options.TlsEndpointOptions.Certificate = value; + return this; + } + + public MqttServerOptionsBuilder WithStorage(IMqttServerStorage value) + { + _options.Storage = value; + return this; + } + + public MqttServerOptionsBuilder WithConnectionValidator(Func value) + { + _options.ConnectionValidator = value; + return this; + } + + public MqttServerOptionsBuilder WithApplicationMessageInterceptor(Action value) + { + _options.ApplicationMessageInterceptor = value; + return this; + } + + public MqttServerOptionsBuilder WithSubscriptionInterceptor(Action value) + { + _options.SubscriptionInterceptor = value; + return this; + } + + public MqttServerOptions Build() + { + return _options; + } + } +} diff --git a/Tests/MQTTnet.TestApp.AspNetCore2/MQTTnet.TestApp.AspNetCore2.csproj b/Tests/MQTTnet.TestApp.AspNetCore2/MQTTnet.TestApp.AspNetCore2.csproj index 7e4e0e3..553b341 100644 --- a/Tests/MQTTnet.TestApp.AspNetCore2/MQTTnet.TestApp.AspNetCore2.csproj +++ b/Tests/MQTTnet.TestApp.AspNetCore2/MQTTnet.TestApp.AspNetCore2.csproj @@ -10,7 +10,7 @@ - + diff --git a/Tests/MQTTnet.TestApp.NetCore/ServerTest.cs b/Tests/MQTTnet.TestApp.NetCore/ServerTest.cs index a0b1626..2a8180b 100644 --- a/Tests/MQTTnet.TestApp.NetCore/ServerTest.cs +++ b/Tests/MQTTnet.TestApp.NetCore/ServerTest.cs @@ -40,7 +40,7 @@ namespace MQTTnet.TestApp.NetCore context.ApplicationMessage.Payload = Encoding.UTF8.GetBytes(DateTime.Now.ToString("O")); } }, - SubscriptionsInterceptor = context => + SubscriptionInterceptor = context => { if (context.TopicFilter.Topic.StartsWith("admin/foo/bar") && context.ClientId != "theAdmin") {