mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-30 18:56:30 +08:00
adapted uwp platform projects
This commit is contained in:
-30
@@ -1,30 +0,0 @@
|
|||||||
using System;
|
|
||||||
using MQTTnet.Core.Adapter;
|
|
||||||
using MQTTnet.Core.Client;
|
|
||||||
using MQTTnet.Core.Diagnostics;
|
|
||||||
using MQTTnet.Core.Serializer;
|
|
||||||
|
|
||||||
namespace MQTTnet.Implementations
|
|
||||||
{
|
|
||||||
public class MqttCommunicationAdapterFactory : IMqttCommunicationAdapterFactory
|
|
||||||
{
|
|
||||||
public IMqttCommunicationAdapter CreateMqttCommunicationAdapter(IMqttClientOptions options)
|
|
||||||
{
|
|
||||||
if (options == null) throw new ArgumentNullException(nameof(options));
|
|
||||||
|
|
||||||
if (options is MqttClientTcpOptions tcpOptions)
|
|
||||||
{
|
|
||||||
var trace = new MqttNetTrace();
|
|
||||||
return new MqttChannelCommunicationAdapter(new MqttTcpChannel(tcpOptions), new MqttPacketSerializer { ProtocolVersion = options.ProtocolVersion }, trace);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (options is MqttClientWebSocketOptions webSocketOptions)
|
|
||||||
{
|
|
||||||
var trace = new MqttNetTrace();
|
|
||||||
return new MqttChannelCommunicationAdapter(new MqttWebSocketChannel(webSocketOptions), new MqttPacketSerializer { ProtocolVersion = options.ProtocolVersion }, trace);
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new NotSupportedException();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,21 +1,22 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using MQTTnet.Core.Adapter;
|
using MQTTnet.Core.Adapter;
|
||||||
using MQTTnet.Core.Diagnostics;
|
|
||||||
using MQTTnet.Core.Serializer;
|
|
||||||
using MQTTnet.Core.Server;
|
using MQTTnet.Core.Server;
|
||||||
using Windows.Networking.Sockets;
|
using Windows.Networking.Sockets;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
namespace MQTTnet.Implementations
|
namespace MQTTnet.Implementations
|
||||||
{
|
{
|
||||||
public class MqttServerAdapter : IMqttServerAdapter, IDisposable
|
public class MqttServerAdapter : IMqttServerAdapter, IDisposable
|
||||||
{
|
{
|
||||||
private readonly MqttNetTrace _trace;
|
private readonly ILogger<MqttServerAdapter> _logger;
|
||||||
|
private readonly IMqttCommunicationAdapterFactory _mqttCommunicationAdapterFactory;
|
||||||
private StreamSocketListener _defaultEndpointSocket;
|
private StreamSocketListener _defaultEndpointSocket;
|
||||||
|
|
||||||
public MqttServerAdapter(MqttNetTrace trace)
|
public MqttServerAdapter(ILogger<MqttServerAdapter> logger, IMqttCommunicationAdapterFactory mqttCommunicationAdapterFactory)
|
||||||
{
|
{
|
||||||
_trace = trace ?? throw new ArgumentNullException(nameof(trace));
|
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||||
|
_mqttCommunicationAdapterFactory = mqttCommunicationAdapterFactory ?? throw new ArgumentNullException(nameof(mqttCommunicationAdapterFactory));
|
||||||
}
|
}
|
||||||
|
|
||||||
public event EventHandler<MqttServerAdapterClientAcceptedEventArgs> ClientAccepted;
|
public event EventHandler<MqttServerAdapterClientAcceptedEventArgs> ClientAccepted;
|
||||||
@@ -56,12 +57,12 @@ namespace MQTTnet.Implementations
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var clientAdapter = new MqttChannelCommunicationAdapter(new MqttTcpChannel(args.Socket), new MqttPacketSerializer(), _trace);
|
var clientAdapter = _mqttCommunicationAdapterFactory.CreateServerMqttCommunicationAdapter(new MqttTcpChannel(args.Socket));
|
||||||
ClientAccepted?.Invoke(this, new MqttServerAdapterClientAcceptedEventArgs(clientAdapter));
|
ClientAccepted?.Invoke(this, new MqttServerAdapterClientAcceptedEventArgs(clientAdapter));
|
||||||
}
|
}
|
||||||
catch (Exception exception)
|
catch (Exception exception)
|
||||||
{
|
{
|
||||||
_trace.Error(nameof(MqttServerAdapter), exception, "Error while accepting connection at default endpoint.");
|
_logger.LogError(new EventId(), exception, "Error while accepting connection at default endpoint.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -110,14 +110,13 @@
|
|||||||
<DocumentationFile>bin\x64\Release\MQTTnet.XML</DocumentationFile>
|
<DocumentationFile>bin\x64\Release\MQTTnet.XML</DocumentationFile>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="Implementations\MqttCommunicationAdapterFactory.cs" />
|
|
||||||
<Compile Include="Implementations\MqttWebSocketChannel.cs" />
|
<Compile Include="Implementations\MqttWebSocketChannel.cs" />
|
||||||
<Compile Include="MqttClientFactory.cs" />
|
<Compile Include="MqttFactory.cs" />
|
||||||
<Compile Include="Implementations\MqttServerAdapter.cs" />
|
<Compile Include="Implementations\MqttServerAdapter.cs" />
|
||||||
<Compile Include="Implementations\MqttTcpChannel.cs" />
|
<Compile Include="Implementations\MqttTcpChannel.cs" />
|
||||||
<Compile Include="MqttServerFactory.cs" />
|
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
<Compile Include="Implementations\WebSocketStream.cs" />
|
<Compile Include="Implementations\WebSocketStream.cs" />
|
||||||
|
<Compile Include="ServiceCollectionExtensions.cs" />
|
||||||
<EmbeddedResource Include="Properties\MQTTnet.Universal.rd.xml" />
|
<EmbeddedResource Include="Properties\MQTTnet.Universal.rd.xml" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
@@ -127,6 +126,12 @@
|
|||||||
</ProjectReference>
|
</ProjectReference>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.Extensions.DependencyInjection">
|
||||||
|
<Version>1.1.1</Version>
|
||||||
|
</PackageReference>
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Logging">
|
||||||
|
<Version>1.1.2</Version>
|
||||||
|
</PackageReference>
|
||||||
<PackageReference Include="Microsoft.NETCore.UniversalWindowsPlatform">
|
<PackageReference Include="Microsoft.NETCore.UniversalWindowsPlatform">
|
||||||
<Version>5.4.0</Version>
|
<Version>5.4.0</Version>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
|
|||||||
@@ -1,20 +0,0 @@
|
|||||||
using MQTTnet.Core.Client;
|
|
||||||
using MQTTnet.Core.Diagnostics;
|
|
||||||
using MQTTnet.Core.ManagedClient;
|
|
||||||
using MQTTnet.Implementations;
|
|
||||||
|
|
||||||
namespace MQTTnet
|
|
||||||
{
|
|
||||||
public class MqttClientFactory : IMqttClientFactory
|
|
||||||
{
|
|
||||||
public IMqttClient CreateMqttClient(IMqttNetTraceHandler traceHandler = null)
|
|
||||||
{
|
|
||||||
return new MqttClient(new MqttCommunicationAdapterFactory(), new MqttNetTrace(traceHandler));
|
|
||||||
}
|
|
||||||
|
|
||||||
public ManagedMqttClient CreateManagedMqttClient(IMqttNetTraceHandler traceHandler = null)
|
|
||||||
{
|
|
||||||
return new ManagedMqttClient(new MqttCommunicationAdapterFactory(), new MqttNetTrace(traceHandler));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,108 @@
|
|||||||
|
using System;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using MQTTnet.Core.Adapter;
|
||||||
|
using MQTTnet.Core.Channel;
|
||||||
|
using MQTTnet.Core.Client;
|
||||||
|
using MQTTnet.Core.ManagedClient;
|
||||||
|
using MQTTnet.Core.Serializer;
|
||||||
|
using MQTTnet.Core.Server;
|
||||||
|
using MQTTnet.Implementations;
|
||||||
|
|
||||||
|
namespace MQTTnet
|
||||||
|
{
|
||||||
|
public class MqttFactory : IMqttCommunicationAdapterFactory, IMqttClientSesssionFactory, IMqttClientFactory, IMqttServerFactory
|
||||||
|
{
|
||||||
|
private readonly IServiceProvider _serviceProvider;
|
||||||
|
|
||||||
|
private static IServiceProvider BuildServiceProvider()
|
||||||
|
{
|
||||||
|
var serviceProvider = new ServiceCollection()
|
||||||
|
.AddMqttClient()
|
||||||
|
.AddMqttServer()
|
||||||
|
.AddLogging()
|
||||||
|
.BuildServiceProvider();
|
||||||
|
|
||||||
|
serviceProvider.GetRequiredService<ILoggerFactory>()
|
||||||
|
.AddMqttTrace();
|
||||||
|
|
||||||
|
return serviceProvider;
|
||||||
|
}
|
||||||
|
|
||||||
|
public MqttFactory()
|
||||||
|
: this(BuildServiceProvider())
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public MqttFactory(IServiceProvider serviceProvider)
|
||||||
|
{
|
||||||
|
_serviceProvider = serviceProvider;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IMqttCommunicationAdapter CreateClientMqttCommunicationAdapter(IMqttClientOptions options)
|
||||||
|
{
|
||||||
|
var logger = _serviceProvider.GetRequiredService<ILogger<MqttChannelCommunicationAdapter>>();
|
||||||
|
return new MqttChannelCommunicationAdapter(CreateMqttCommunicationChannel(options), CreateSerializer(options.ProtocolVersion), logger);
|
||||||
|
}
|
||||||
|
|
||||||
|
public IMqttCommunicationAdapter CreateServerMqttCommunicationAdapter(IMqttCommunicationChannel channel)
|
||||||
|
{
|
||||||
|
var serializer = _serviceProvider.GetRequiredService<IMqttPacketSerializer>();
|
||||||
|
var logger = _serviceProvider.GetRequiredService<ILogger<MqttChannelCommunicationAdapter>>();
|
||||||
|
return new MqttChannelCommunicationAdapter(channel, serializer, logger);
|
||||||
|
}
|
||||||
|
|
||||||
|
public IMqttCommunicationChannel CreateMqttCommunicationChannel(IMqttClientOptions options)
|
||||||
|
{
|
||||||
|
if (options == null) throw new ArgumentNullException(nameof(options));
|
||||||
|
|
||||||
|
switch (options)
|
||||||
|
{
|
||||||
|
case MqttClientTcpOptions tcpOptions:
|
||||||
|
return CreateTcpChannel(tcpOptions);
|
||||||
|
case MqttClientWebSocketOptions webSocketOptions:
|
||||||
|
return CreateWebSocketChannel(webSocketOptions);
|
||||||
|
default:
|
||||||
|
throw new NotSupportedException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public MqttTcpChannel CreateTcpChannel(MqttClientTcpOptions tcpOptions)
|
||||||
|
{
|
||||||
|
return new MqttTcpChannel(tcpOptions);
|
||||||
|
}
|
||||||
|
|
||||||
|
public MqttWebSocketChannel CreateWebSocketChannel(MqttClientWebSocketOptions webSocketOptions)
|
||||||
|
{
|
||||||
|
return new MqttWebSocketChannel(webSocketOptions);
|
||||||
|
}
|
||||||
|
|
||||||
|
public MqttPacketSerializer CreateSerializer(MqttProtocolVersion protocolVersion)
|
||||||
|
{
|
||||||
|
return new MqttPacketSerializer()
|
||||||
|
{
|
||||||
|
ProtocolVersion = protocolVersion
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public MqttClientSession CreateClientSession(string sessionId, MqttClientSessionsManager mqttClientSessionsManager)
|
||||||
|
{
|
||||||
|
return new MqttClientSession(sessionId, mqttClientSessionsManager, _serviceProvider.GetRequiredService<ILogger<MqttClientSession>>(), _serviceProvider.GetRequiredService<ILogger<MqttClientPendingMessagesQueue>>());
|
||||||
|
}
|
||||||
|
|
||||||
|
public IMqttClient CreateMqttClient()
|
||||||
|
{
|
||||||
|
return _serviceProvider.GetRequiredService<IMqttClient>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public ManagedMqttClient CreateManagedMqttClient()
|
||||||
|
{
|
||||||
|
return _serviceProvider.GetRequiredService<ManagedMqttClient>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public IMqttServer CreateMqttServer()
|
||||||
|
{
|
||||||
|
return _serviceProvider.GetRequiredService<IMqttServer>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using MQTTnet.Core.Adapter;
|
|
||||||
using MQTTnet.Core.Diagnostics;
|
|
||||||
using MQTTnet.Core.Server;
|
|
||||||
using MQTTnet.Implementations;
|
|
||||||
|
|
||||||
namespace MQTTnet
|
|
||||||
{
|
|
||||||
public class MqttServerFactory : IMqttServerFactory
|
|
||||||
{
|
|
||||||
public IMqttServer CreateMqttServer(MqttServerOptions options, IMqttNetTraceHandler traceHandler = null)
|
|
||||||
{
|
|
||||||
if (options == null) throw new ArgumentNullException(nameof(options));
|
|
||||||
|
|
||||||
var trace = new MqttNetTrace(traceHandler);
|
|
||||||
return new MqttServer(options, new List<IMqttServerAdapter> { new MqttServerAdapter(trace) }, trace);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using MQTTnet.Core.Adapter;
|
||||||
|
using MQTTnet.Core.Client;
|
||||||
|
using MQTTnet.Core.ManagedClient;
|
||||||
|
using MQTTnet.Core.Serializer;
|
||||||
|
using MQTTnet.Core.Server;
|
||||||
|
using MQTTnet.Implementations;
|
||||||
|
using System;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using MQTTnet.Core.Diagnostics;
|
||||||
|
|
||||||
|
namespace MQTTnet
|
||||||
|
{
|
||||||
|
public static class ServiceCollectionExtensions
|
||||||
|
{
|
||||||
|
public static IServiceCollection AddMqttServer(this IServiceCollection services)
|
||||||
|
{
|
||||||
|
services.AddOptions();
|
||||||
|
services.AddSingleton<MqttFactory>();
|
||||||
|
services.AddSingleton<IMqttCommunicationAdapterFactory, MqttFactory>();
|
||||||
|
services.AddSingleton<IMqttClientSesssionFactory, MqttFactory>();
|
||||||
|
|
||||||
|
|
||||||
|
services.AddSingleton<IMqttServer,MqttServer>();
|
||||||
|
services.AddSingleton<MqttServer>();
|
||||||
|
|
||||||
|
services.AddTransient<IMqttServerAdapter, MqttServerAdapter>();
|
||||||
|
services.AddTransient<IMqttPacketSerializer, MqttPacketSerializer>();
|
||||||
|
|
||||||
|
services.AddTransient<MqttClientSessionsManager>();
|
||||||
|
services.AddTransient<MqttClientRetainedMessagesManager>();
|
||||||
|
return services;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IServiceCollection AddMqttServer(this IServiceCollection services, Action<MqttServerOptions> configureOptions)
|
||||||
|
{
|
||||||
|
return services
|
||||||
|
.AddMqttServer()
|
||||||
|
.Configure(configureOptions);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IServiceCollection AddMqttClient(this IServiceCollection services)
|
||||||
|
{
|
||||||
|
services.AddSingleton<MqttFactory>();
|
||||||
|
services.AddSingleton<IMqttCommunicationAdapterFactory, MqttFactory>();
|
||||||
|
|
||||||
|
services.AddTransient<IMqttClient, MqttClient>();
|
||||||
|
services.AddTransient<MqttClient>();
|
||||||
|
services.AddTransient<IMqttPacketSerializer, MqttPacketSerializer>();
|
||||||
|
services.AddTransient<ManagedMqttClient>();
|
||||||
|
services.AddTransient<MqttPacketDispatcher>();
|
||||||
|
|
||||||
|
return services;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ILoggerFactory AddMqttTrace(this ILoggerFactory factory)
|
||||||
|
{
|
||||||
|
factory.AddProvider(new MqttNetTrace());
|
||||||
|
return factory;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,6 +4,7 @@ using System.Threading.Tasks;
|
|||||||
using Windows.Security.Cryptography.Certificates;
|
using Windows.Security.Cryptography.Certificates;
|
||||||
using Windows.UI.Core;
|
using Windows.UI.Core;
|
||||||
using Windows.UI.Xaml;
|
using Windows.UI.Xaml;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using MQTTnet.Core;
|
using MQTTnet.Core;
|
||||||
using MQTTnet.Core.Client;
|
using MQTTnet.Core.Client;
|
||||||
using MQTTnet.Core.Diagnostics;
|
using MQTTnet.Core.Diagnostics;
|
||||||
@@ -78,7 +79,7 @@ namespace MQTTnet.TestApp.UniversalWindows
|
|||||||
await _mqttClient.DisconnectAsync();
|
await _mqttClient.DisconnectAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
var factory = new MqttClientFactory();
|
var factory = new MqttFactory();
|
||||||
_mqttClient = factory.CreateMqttClient();
|
_mqttClient = factory.CreateMqttClient();
|
||||||
await _mqttClient.ConnectAsync(options);
|
await _mqttClient.ConnectAsync(options);
|
||||||
}
|
}
|
||||||
@@ -200,7 +201,35 @@ namespace MQTTnet.TestApp.UniversalWindows
|
|||||||
// ReSharper disable once UnusedMember.Local
|
// ReSharper disable once UnusedMember.Local
|
||||||
private async Task WikiCode()
|
private async Task WikiCode()
|
||||||
{
|
{
|
||||||
var mqttClient = new MqttClientFactory().CreateMqttClient();
|
var services = new ServiceCollection()
|
||||||
|
.AddMqttClient()
|
||||||
|
.AddMqttServer(options =>
|
||||||
|
{
|
||||||
|
options.ConnectionValidator = c =>
|
||||||
|
{
|
||||||
|
if (c.ClientId.Length < 10)
|
||||||
|
{
|
||||||
|
return MqttConnectReturnCode.ConnectionRefusedIdentifierRejected;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (c.Username != "mySecretUser")
|
||||||
|
{
|
||||||
|
return MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (c.Password != "mySecretPassword")
|
||||||
|
{
|
||||||
|
return MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword;
|
||||||
|
}
|
||||||
|
|
||||||
|
return MqttConnectReturnCode.ConnectionAccepted;
|
||||||
|
};
|
||||||
|
})
|
||||||
|
.BuildServiceProvider();
|
||||||
|
|
||||||
|
var factory = new MqttFactory(services);
|
||||||
|
|
||||||
|
var mqttClient = factory.CreateMqttClient();
|
||||||
|
|
||||||
// ----------------------------------
|
// ----------------------------------
|
||||||
|
|
||||||
@@ -239,9 +268,7 @@ namespace MQTTnet.TestApp.UniversalWindows
|
|||||||
|
|
||||||
// ----------------------------------
|
// ----------------------------------
|
||||||
{
|
{
|
||||||
var options = new MqttServerOptions();
|
var mqttServer = factory.CreateMqttServer();
|
||||||
|
|
||||||
var mqttServer = new MqttServerFactory().CreateMqttServer(options);
|
|
||||||
await mqttServer.StartAsync();
|
await mqttServer.StartAsync();
|
||||||
|
|
||||||
Console.WriteLine("Press any key to exit.");
|
Console.WriteLine("Press any key to exit.");
|
||||||
@@ -252,33 +279,6 @@ namespace MQTTnet.TestApp.UniversalWindows
|
|||||||
|
|
||||||
// ----------------------------------
|
// ----------------------------------
|
||||||
|
|
||||||
{
|
|
||||||
var options = new MqttServerOptions
|
|
||||||
{
|
|
||||||
ConnectionValidator = c =>
|
|
||||||
{
|
|
||||||
if (c.ClientId.Length < 10)
|
|
||||||
{
|
|
||||||
return MqttConnectReturnCode.ConnectionRefusedIdentifierRejected;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (c.Username != "mySecretUser")
|
|
||||||
{
|
|
||||||
return MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (c.Password != "mySecretPassword")
|
|
||||||
{
|
|
||||||
return MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword;
|
|
||||||
}
|
|
||||||
|
|
||||||
return MqttConnectReturnCode.ConnectionAccepted;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// ----------------------------------
|
|
||||||
|
|
||||||
// For UWP apps:
|
// For UWP apps:
|
||||||
MqttTcpChannel.CustomIgnorableServerCertificateErrorsResolver = o =>
|
MqttTcpChannel.CustomIgnorableServerCertificateErrorsResolver = o =>
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user