Просмотр исходного кода

Add interface for server options. Add context class for connection validator (hide internal packets).

release/3.x.x
Christian Kratky 7 лет назад
Родитель
Сommit
ee6ec14bf8
20 измененных файлов: 75 добавлений и 51 удалений
  1. +3
    -3
      Build/MQTTnet.AspNetCore.nuspec
  2. +1
    -1
      Frameworks/MQTTnet.AspnetCore/MqttWebSocketServerAdapter.cs
  3. +1
    -1
      Frameworks/MQTTnet.AspnetCore/ServiceCollectionExtensions.cs
  4. +1
    -1
      Frameworks/MQTTnet.NetStandard/Adapter/IMqttServerAdapter.cs
  5. +1
    -1
      Frameworks/MQTTnet.NetStandard/Implementations/MqttServerAdapter.Uwp.cs
  6. +1
    -1
      Frameworks/MQTTnet.NetStandard/Implementations/MqttServerAdapter.cs
  7. +1
    -1
      Frameworks/MQTTnet.NetStandard/Server/IMqttServer.cs
  8. +16
    -0
      Frameworks/MQTTnet.NetStandard/Server/IMqttServerOptions.cs
  9. +2
    -2
      Frameworks/MQTTnet.NetStandard/Server/MqttClientPendingMessagesQueue.cs
  10. +2
    -2
      Frameworks/MQTTnet.NetStandard/Server/MqttClientSession.cs
  11. +13
    -6
      Frameworks/MQTTnet.NetStandard/Server/MqttClientSessionsManager.cs
  12. +2
    -2
      Frameworks/MQTTnet.NetStandard/Server/MqttClientSubscriptionsManager.cs
  13. +2
    -2
      Frameworks/MQTTnet.NetStandard/Server/MqttRetainedMessagesManager.cs
  14. +2
    -2
      Frameworks/MQTTnet.NetStandard/Server/MqttServer.cs
  15. +2
    -4
      Frameworks/MQTTnet.NetStandard/Server/MqttServerOptions.cs
  16. +2
    -4
      Frameworks/MQTTnet.NetStandard/Server/MqttServerOptionsBuilder.cs
  17. +2
    -2
      Frameworks/MQTTnet.NetStandard/Server/MqttServerOptionsExtensions.cs
  18. +1
    -1
      Tests/MQTTnet.Core.Tests/TestMqttServerAdapter.cs
  19. +1
    -3
      Tests/MQTTnet.TestApp.NetCore/ServerTest.cs
  20. +19
    -12
      Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml.cs

+ 3
- 3
Build/MQTTnet.AspNetCore.nuspec Просмотреть файл

@@ -2,7 +2,7 @@
<package >
<metadata>
<id>MQTTnet.AspNetCore</id>
<version>2.5.3</version>
<version>2.6.0</version>
<authors>Christian Kratky</authors>
<owners>Christian Kratky</owners>
<licenseUrl>https://github.com/chkr1011/MQTTnet/blob/master/LICENSE</licenseUrl>
@@ -10,13 +10,13 @@
<iconUrl>https://raw.githubusercontent.com/chkr1011/MQTTnet/master/Images/Logo_128x128.png</iconUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>This is a support library to integrate MQTTnet into AspNetCore.</description>
<releaseNotes>* Updated to MQTTnet 2.5.3.
<releaseNotes>* Updated to MQTTnet 2.6.0.
</releaseNotes>
<copyright>Copyright Christian Kratky 2016-2017</copyright>
<tags>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</tags>
<dependencies>
<group targetFramework="netstandard2.0">
<dependency id="MQTTnet" version="2.5.3" />
<dependency id="MQTTnet" version="2.6.0" />
</group>
</dependencies>
</metadata>


+ 1
- 1
Frameworks/MQTTnet.AspnetCore/MqttWebSocketServerAdapter.cs Просмотреть файл

@@ -12,7 +12,7 @@ namespace MQTTnet.AspNetCore
{
public event EventHandler<MqttServerAdapterClientAcceptedEventArgs> ClientAccepted;

public Task StartAsync(MqttServerOptions options)
public Task StartAsync(IMqttServerOptions options)
{
return Task.CompletedTask;
}


+ 1
- 1
Frameworks/MQTTnet.AspnetCore/ServiceCollectionExtensions.cs Просмотреть файл

@@ -9,7 +9,7 @@ namespace MQTTnet.AspNetCore
{
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddHostedMqttServer(this IServiceCollection services, MqttServerOptions options)
public static IServiceCollection AddHostedMqttServer(this IServiceCollection services, IMqttServerOptions options)
{
if (options == null) throw new ArgumentNullException(nameof(options));



+ 1
- 1
Frameworks/MQTTnet.NetStandard/Adapter/IMqttServerAdapter.cs Просмотреть файл

@@ -8,7 +8,7 @@ namespace MQTTnet.Adapter
{
event EventHandler<MqttServerAdapterClientAcceptedEventArgs> ClientAccepted;

Task StartAsync(MqttServerOptions options);
Task StartAsync(IMqttServerOptions options);
Task StopAsync();
}
}

+ 1
- 1
Frameworks/MQTTnet.NetStandard/Implementations/MqttServerAdapter.Uwp.cs Просмотреть файл

@@ -21,7 +21,7 @@ namespace MQTTnet.Implementations

public event EventHandler<MqttServerAdapterClientAcceptedEventArgs> ClientAccepted;

public async Task StartAsync(MqttServerOptions options)
public async Task StartAsync(IMqttServerOptions options)
{
if (options == null) throw new ArgumentNullException(nameof(options));



+ 1
- 1
Frameworks/MQTTnet.NetStandard/Implementations/MqttServerAdapter.cs Просмотреть файл

@@ -30,7 +30,7 @@ namespace MQTTnet.Implementations

public event EventHandler<MqttServerAdapterClientAcceptedEventArgs> ClientAccepted;

public Task StartAsync(MqttServerOptions options)
public Task StartAsync(IMqttServerOptions options)
{
if (_cancellationTokenSource != null) throw new InvalidOperationException("Server is already started.");



+ 1
- 1
Frameworks/MQTTnet.NetStandard/Server/IMqttServer.cs Просмотреть файл

@@ -12,7 +12,7 @@ namespace MQTTnet.Server

Task<IList<ConnectedMqttClient>> GetConnectedClientsAsync();

Task StartAsync(MqttServerOptions options);
Task StartAsync(IMqttServerOptions options);
Task StopAsync();
}
}

+ 16
- 0
Frameworks/MQTTnet.NetStandard/Server/IMqttServerOptions.cs Просмотреть файл

@@ -0,0 +1,16 @@
using System;

namespace MQTTnet.Server
{
public interface IMqttServerOptions
{
Action<MqttApplicationMessageInterceptorContext> ApplicationMessageInterceptor { get; }
int ConnectionBacklog { get; }
Action<MqttConnectionValidatorContext> ConnectionValidator { get; }
TimeSpan DefaultCommunicationTimeout { get; }
MqttServerDefaultEndpointOptions DefaultEndpointOptions { get; }
IMqttServerStorage Storage { get; }
Action<MqttSubscriptionInterceptorContext> SubscriptionInterceptor { get; }
MqttServerTlsEndpointOptions TlsEndpointOptions { get; }
}
}

+ 2
- 2
Frameworks/MQTTnet.NetStandard/Server/MqttClientPendingMessagesQueue.cs Просмотреть файл

@@ -13,11 +13,11 @@ namespace MQTTnet.Server
public sealed class MqttClientPendingMessagesQueue
{
private readonly BlockingCollection<MqttPublishPacket> _pendingPublishPackets = new BlockingCollection<MqttPublishPacket>();
private readonly MqttServerOptions _options;
private readonly IMqttServerOptions _options;
private readonly MqttClientSession _session;
private readonly IMqttNetLogger _logger;

public MqttClientPendingMessagesQueue(MqttServerOptions options, MqttClientSession session, IMqttNetLogger logger)
public MqttClientPendingMessagesQueue(IMqttServerOptions options, MqttClientSession session, IMqttNetLogger logger)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_session = session ?? throw new ArgumentNullException(nameof(session));


+ 2
- 2
Frameworks/MQTTnet.NetStandard/Server/MqttClientSession.cs Просмотреть файл

@@ -19,7 +19,7 @@ namespace MQTTnet.Server
private readonly MqttClientSubscriptionsManager _subscriptionsManager;
private readonly MqttClientSessionsManager _sessionsManager;
private readonly MqttClientPendingMessagesQueue _pendingMessagesQueue;
private readonly MqttServerOptions _options;
private readonly IMqttServerOptions _options;
private readonly IMqttNetLogger _logger;

private IMqttChannelAdapter _adapter;
@@ -28,7 +28,7 @@ namespace MQTTnet.Server

public MqttClientSession(
string clientId,
MqttServerOptions options,
IMqttServerOptions options,
MqttClientSessionsManager sessionsManager,
IMqttNetLogger logger)
{


+ 13
- 6
Frameworks/MQTTnet.NetStandard/Server/MqttClientSessionsManager.cs Просмотреть файл

@@ -18,11 +18,11 @@ namespace MQTTnet.Server
private readonly Dictionary<string, MqttClientSession> _sessions = new Dictionary<string, MqttClientSession>();
private readonly SemaphoreSlim _sessionsSemaphore = new SemaphoreSlim(1, 1);

private readonly MqttServerOptions _options;
private readonly IMqttServerOptions _options;
private readonly MqttRetainedMessagesManager _retainedMessagesManager;
private readonly IMqttNetLogger _logger;

public MqttClientSessionsManager(MqttServerOptions options, MqttRetainedMessagesManager retainedMessagesManager, IMqttNetLogger logger)
public MqttClientSessionsManager(IMqttServerOptions options, MqttRetainedMessagesManager retainedMessagesManager, IMqttNetLogger logger)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_options = options ?? throw new ArgumentNullException(nameof(options));
@@ -174,12 +174,19 @@ namespace MQTTnet.Server

private MqttConnectReturnCode ValidateConnection(MqttConnectPacket connectPacket)
{
if (_options.ConnectionValidator != null)
if (_options.ConnectionValidator == null)
{
return _options.ConnectionValidator(connectPacket);
return MqttConnectReturnCode.ConnectionAccepted;
}

return MqttConnectReturnCode.ConnectionAccepted;
var context = new MqttConnectionValidatorContext(
connectPacket.ClientId,
connectPacket.Username,
connectPacket.Password,
connectPacket.WillMessage);

_options.ConnectionValidator(context);
return context.ReturnCode;
}

private async Task<GetOrCreateClientSessionResult> GetOrCreateClientSessionAsync(MqttConnectPacket connectPacket)


+ 2
- 2
Frameworks/MQTTnet.NetStandard/Server/MqttClientSubscriptionsManager.cs Просмотреть файл

@@ -8,9 +8,9 @@ namespace MQTTnet.Server
public sealed class MqttClientSubscriptionsManager
{
private readonly Dictionary<string, MqttQualityOfServiceLevel> _subscriptions = new Dictionary<string, MqttQualityOfServiceLevel>();
private readonly MqttServerOptions _options;
private readonly IMqttServerOptions _options;

public MqttClientSubscriptionsManager(MqttServerOptions options)
public MqttClientSubscriptionsManager(IMqttServerOptions options)
{
_options = options ?? throw new ArgumentNullException(nameof(options));
}


+ 2
- 2
Frameworks/MQTTnet.NetStandard/Server/MqttRetainedMessagesManager.cs Просмотреть файл

@@ -13,9 +13,9 @@ namespace MQTTnet.Server
private readonly Dictionary<string, MqttApplicationMessage> _retainedMessages = new Dictionary<string, MqttApplicationMessage>();
private readonly SemaphoreSlim _gate = new SemaphoreSlim(1, 1);
private readonly IMqttNetLogger _logger;
private readonly MqttServerOptions _options;
private readonly IMqttServerOptions _options;

public MqttRetainedMessagesManager(MqttServerOptions options, IMqttNetLogger logger)
public MqttRetainedMessagesManager(IMqttServerOptions options, IMqttNetLogger logger)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_options = options ?? throw new ArgumentNullException(nameof(options));


+ 2
- 2
Frameworks/MQTTnet.NetStandard/Server/MqttServer.cs Просмотреть файл

@@ -16,7 +16,7 @@ namespace MQTTnet.Server
private MqttClientSessionsManager _clientSessionsManager;
private MqttRetainedMessagesManager _retainedMessagesManager;
private CancellationTokenSource _cancellationTokenSource;
private MqttServerOptions _options;
private IMqttServerOptions _options;

public MqttServer(IEnumerable<IMqttServerAdapter> adapters, IMqttNetLogger logger)
{
@@ -52,7 +52,7 @@ namespace MQTTnet.Server
}
}

public async Task StartAsync(MqttServerOptions options)
public async Task StartAsync(IMqttServerOptions options)
{
_options = options ?? throw new ArgumentNullException(nameof(options));



+ 2
- 4
Frameworks/MQTTnet.NetStandard/Server/MqttServerOptions.cs Просмотреть файл

@@ -1,10 +1,8 @@
using System;
using MQTTnet.Packets;
using MQTTnet.Protocol;

namespace MQTTnet.Server
{
public class MqttServerOptions
public class MqttServerOptions : IMqttServerOptions
{
public MqttServerDefaultEndpointOptions DefaultEndpointOptions { get; } = new MqttServerDefaultEndpointOptions();

@@ -14,7 +12,7 @@ namespace MQTTnet.Server
public TimeSpan DefaultCommunicationTimeout { get; set; } = TimeSpan.FromSeconds(15);

public Func<MqttConnectPacket, MqttConnectReturnCode> ConnectionValidator { get; set; }
public Action<MqttConnectionValidatorContext> ConnectionValidator { get; set; }

public Action<MqttApplicationMessageInterceptorContext> ApplicationMessageInterceptor { get; set; }



+ 2
- 4
Frameworks/MQTTnet.NetStandard/Server/MqttServerOptionsBuilder.cs Просмотреть файл

@@ -1,6 +1,4 @@
using System;
using MQTTnet.Packets;
using MQTTnet.Protocol;

namespace MQTTnet.Server
{
@@ -62,7 +60,7 @@ namespace MQTTnet.Server
return this;
}

public MqttServerOptionsBuilder WithConnectionValidator(Func<MqttConnectPacket, MqttConnectReturnCode> value)
public MqttServerOptionsBuilder WithConnectionValidator(Action<MqttConnectionValidatorContext> value)
{
_options.ConnectionValidator = value;
return this;
@@ -80,7 +78,7 @@ namespace MQTTnet.Server
return this;
}

public MqttServerOptions Build()
public IMqttServerOptions Build()
{
return _options;
}


+ 2
- 2
Frameworks/MQTTnet.NetStandard/Server/MqttServerOptionsExtensions.cs Просмотреть файл

@@ -4,7 +4,7 @@ namespace MQTTnet.Server
{
public static class MqttServerOptionsExtensions
{
public static int GetTlsEndpointPort(this MqttServerOptions options)
public static int GetTlsEndpointPort(this IMqttServerOptions options)
{
if (options == null) throw new ArgumentNullException(nameof(options));

@@ -16,7 +16,7 @@ namespace MQTTnet.Server
return options.TlsEndpointOptions.Port.Value;
}

public static int GetDefaultEndpointPort(this MqttServerOptions options)
public static int GetDefaultEndpointPort(this IMqttServerOptions options)
{
if (options == null) throw new ArgumentNullException(nameof(options));



+ 1
- 1
Tests/MQTTnet.Core.Tests/TestMqttServerAdapter.cs Просмотреть файл

@@ -62,7 +62,7 @@ namespace MQTTnet.Core.Tests
ClientAccepted?.Invoke(this, new MqttServerAdapterClientAcceptedEventArgs(adapter));
}

public Task StartAsync(MqttServerOptions options)
public Task StartAsync(IMqttServerOptions options)
{
return Task.FromResult(0);
}


+ 1
- 3
Tests/MQTTnet.TestApp.NetCore/ServerTest.cs Просмотреть файл

@@ -23,11 +23,9 @@ namespace MQTTnet.TestApp.NetCore
{
if (p.Username != "USER" || p.Password != "PASS")
{
return MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword;
p.ReturnCode = MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword;
}
}

return MqttConnectReturnCode.ConnectionAccepted;
},

Storage = new RetainedMessageHandler(),


+ 19
- 12
Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml.cs Просмотреть файл

@@ -354,20 +354,23 @@ namespace MQTTnet.TestApp.UniversalWindows
{
if (c.ClientId.Length < 10)
{
return MqttConnectReturnCode.ConnectionRefusedIdentifierRejected;
c.ReturnCode = MqttConnectReturnCode.ConnectionRefusedIdentifierRejected;
return;
}

if (c.Username != "mySecretUser")
{
return MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword;
c.ReturnCode = MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword;
return;
}

if (c.Password != "mySecretPassword")
{
return MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword;
c.ReturnCode = MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword;
return;
}

return MqttConnectReturnCode.ConnectionAccepted;
c.ReturnCode = MqttConnectReturnCode.ConnectionAccepted;
};

var factory = new MqttFactory();
@@ -410,14 +413,15 @@ namespace MQTTnet.TestApp.UniversalWindows
};

options.DefaultEndpointOptions.Port = 1884;
options.ConnectionValidator = packet =>
options.ConnectionValidator = c =>
{
if (packet.ClientId != "Highlander")
if (c.ClientId != "Highlander")
{
return MqttConnectReturnCode.ConnectionRefusedIdentifierRejected;
c.ReturnCode = MqttConnectReturnCode.ConnectionRefusedIdentifierRejected;
return;
}

return MqttConnectReturnCode.ConnectionAccepted;
c.ReturnCode = MqttConnectReturnCode.ConnectionAccepted;
};

var mqttServer = new MqttFactory().CreateMqttServer();
@@ -432,20 +436,23 @@ namespace MQTTnet.TestApp.UniversalWindows
{
if (c.ClientId.Length < 10)
{
return MqttConnectReturnCode.ConnectionRefusedIdentifierRejected;
c.ReturnCode = MqttConnectReturnCode.ConnectionRefusedIdentifierRejected;
return;
}

if (c.Username != "mySecretUser")
{
return MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword;
c.ReturnCode = MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword;
return;
}

if (c.Password != "mySecretPassword")
{
return MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword;
c.ReturnCode = MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword;
return;
}

return MqttConnectReturnCode.ConnectionAccepted;
c.ReturnCode = MqttConnectReturnCode.ConnectionAccepted;
}
};
}


Загрузка…
Отмена
Сохранить