@@ -7,10 +7,8 @@ using System.Security.Cryptography.X509Certificates; | |||||
using System.Threading; | using System.Threading; | ||||
using System.Threading.Tasks; | using System.Threading.Tasks; | ||||
using MQTTnet.Core.Adapter; | using MQTTnet.Core.Adapter; | ||||
using MQTTnet.Core.Serializer; | |||||
using MQTTnet.Core.Server; | using MQTTnet.Core.Server; | ||||
using Microsoft.Extensions.Logging; | using Microsoft.Extensions.Logging; | ||||
using MQTTnet.Core.Client; | |||||
namespace MQTTnet.Implementations | namespace MQTTnet.Implementations | ||||
{ | { | ||||
@@ -18,6 +16,7 @@ namespace MQTTnet.Implementations | |||||
{ | { | ||||
private readonly ILogger<MqttServerAdapter> _logger; | private readonly ILogger<MqttServerAdapter> _logger; | ||||
private readonly IMqttCommunicationAdapterFactory _mqttCommunicationAdapterFactory; | private readonly IMqttCommunicationAdapterFactory _mqttCommunicationAdapterFactory; | ||||
private CancellationTokenSource _cancellationTokenSource; | private CancellationTokenSource _cancellationTokenSource; | ||||
private Socket _defaultEndpointSocket; | private Socket _defaultEndpointSocket; | ||||
private Socket _tlsEndpointSocket; | private Socket _tlsEndpointSocket; | ||||
@@ -89,7 +89,7 @@ namespace MQTTnet.Implementations | |||||
RawReceiveStream = ReceiveStream; | RawReceiveStream = ReceiveStream; | ||||
} | } | ||||
private static Certificate LoadCertificate(BaseMqttClientOptions options) | |||||
private static Certificate LoadCertificate(MqttClientOptions options) | |||||
{ | { | ||||
if (options.TlsOptions.Certificates == null || !options.TlsOptions.Certificates.Any()) | if (options.TlsOptions.Certificates == null || !options.TlsOptions.Certificates.Any()) | ||||
{ | { | ||||
@@ -79,7 +79,7 @@ namespace MQTTnet | |||||
public MqttPacketSerializer CreateSerializer(MqttProtocolVersion protocolVersion) | public MqttPacketSerializer CreateSerializer(MqttProtocolVersion protocolVersion) | ||||
{ | { | ||||
return new MqttPacketSerializer() | |||||
return new MqttPacketSerializer | |||||
{ | { | ||||
ProtocolVersion = protocolVersion | ProtocolVersion = protocolVersion | ||||
}; | }; | ||||
@@ -3,20 +3,18 @@ using MQTTnet.Core.Serializer; | |||||
namespace MQTTnet.Core.Client | namespace MQTTnet.Core.Client | ||||
{ | { | ||||
public abstract class BaseMqttClientOptions : IMqttClientOptions | |||||
public class MqttClientOptions : IMqttClientOptions | |||||
{ | { | ||||
public MqttClientTlsOptions TlsOptions { get; set; } = new MqttClientTlsOptions(); | public MqttClientTlsOptions TlsOptions { get; set; } = new MqttClientTlsOptions(); | ||||
public MqttApplicationMessage WillMessage { get; set; } | public MqttApplicationMessage WillMessage { get; set; } | ||||
public string UserName { get; set; } | |||||
public string Password { get; set; } | |||||
public string ClientId { get; set; } = Guid.NewGuid().ToString("N"); | public string ClientId { get; set; } = Guid.NewGuid().ToString("N"); | ||||
public bool CleanSession { get; set; } = true; | public bool CleanSession { get; set; } = true; | ||||
public IMqttClientCredentials Credentials { get; set; } = new MqttClientCredentials(); | |||||
public TimeSpan KeepAlivePeriod { get; set; } = TimeSpan.FromSeconds(5); | public TimeSpan KeepAlivePeriod { get; set; } = TimeSpan.FromSeconds(5); | ||||
public TimeSpan DefaultCommunicationTimeout { get; set; } = TimeSpan.FromSeconds(10); | public TimeSpan DefaultCommunicationTimeout { get; set; } = TimeSpan.FromSeconds(10); | ||||
@@ -0,0 +1,8 @@ | |||||
namespace MQTTnet.Core.Client | |||||
{ | |||||
public interface IMqttClientCredentials | |||||
{ | |||||
string Password { get; } | |||||
string Username { get; } | |||||
} | |||||
} |
@@ -5,14 +5,14 @@ namespace MQTTnet.Core.Client | |||||
{ | { | ||||
public interface IMqttClientOptions | public interface IMqttClientOptions | ||||
{ | { | ||||
bool CleanSession { get; } | |||||
string ClientId { get; } | string ClientId { get; } | ||||
bool CleanSession { get; } | |||||
MqttApplicationMessage WillMessage { get; } | |||||
IMqttClientCredentials Credentials { get; } | |||||
TimeSpan DefaultCommunicationTimeout { get; } | TimeSpan DefaultCommunicationTimeout { get; } | ||||
TimeSpan KeepAlivePeriod { get; } | TimeSpan KeepAlivePeriod { get; } | ||||
string Password { get; } | |||||
MqttProtocolVersion ProtocolVersion { get; } | MqttProtocolVersion ProtocolVersion { get; } | ||||
MqttClientTlsOptions TlsOptions { get; } | MqttClientTlsOptions TlsOptions { get; } | ||||
string UserName { get; } | |||||
MqttApplicationMessage WillMessage { get; } | |||||
} | } | ||||
} | } |
@@ -186,8 +186,8 @@ namespace MQTTnet.Core.Client | |||||
var connectPacket = new MqttConnectPacket | var connectPacket = new MqttConnectPacket | ||||
{ | { | ||||
ClientId = _options.ClientId, | ClientId = _options.ClientId, | ||||
Username = _options.UserName, | |||||
Password = _options.Password, | |||||
Username = _options.Credentials?.Username, | |||||
Password = _options.Credentials?.Password, | |||||
CleanSession = _options.CleanSession, | CleanSession = _options.CleanSession, | ||||
KeepAlivePeriod = (ushort)_options.KeepAlivePeriod.TotalSeconds, | KeepAlivePeriod = (ushort)_options.KeepAlivePeriod.TotalSeconds, | ||||
WillMessage = willApplicationMessage | WillMessage = willApplicationMessage | ||||
@@ -0,0 +1,9 @@ | |||||
namespace MQTTnet.Core.Client | |||||
{ | |||||
public class MqttClientCredentials : IMqttClientCredentials | |||||
{ | |||||
public string Username { get; set; } | |||||
public string Password { get; set; } | |||||
} | |||||
} |
@@ -1,6 +1,6 @@ | |||||
namespace MQTTnet.Core.Client | namespace MQTTnet.Core.Client | ||||
{ | { | ||||
public class MqttClientTcpOptions : BaseMqttClientOptions | |||||
public class MqttClientTcpOptions : MqttClientOptions | |||||
{ | { | ||||
public string Server { get; set; } | public string Server { get; set; } | ||||
@@ -3,7 +3,7 @@ using System.Net; | |||||
namespace MQTTnet.Core.Client | namespace MQTTnet.Core.Client | ||||
{ | { | ||||
public class MqttClientWebSocketOptions : BaseMqttClientOptions | |||||
public class MqttClientWebSocketOptions : MqttClientOptions | |||||
{ | { | ||||
public string Uri { get; set; } | public string Uri { get; set; } | ||||
@@ -10,7 +10,5 @@ namespace MQTTnet.Core.ManagedClient | |||||
TimeSpan AutoReconnectDelay { get; } | TimeSpan AutoReconnectDelay { get; } | ||||
IManagedMqttClientStorage Storage { get; } | IManagedMqttClientStorage Storage { get; } | ||||
Func<IManagedMqttClientOptions, string> PasswordProvider { get; } | |||||
} | } | ||||
} | } |
@@ -6,7 +6,6 @@ using System.Threading; | |||||
using System.Threading.Tasks; | using System.Threading.Tasks; | ||||
using MQTTnet.Core.Client; | using MQTTnet.Core.Client; | ||||
using MQTTnet.Core.Exceptions; | using MQTTnet.Core.Exceptions; | ||||
using MQTTnet.Core.Packets; | |||||
using MQTTnet.Core.Protocol; | using MQTTnet.Core.Protocol; | ||||
using Microsoft.Extensions.Logging; | using Microsoft.Extensions.Logging; | ||||
@@ -277,7 +276,6 @@ namespace MQTTnet.Core.ManagedClient | |||||
try | try | ||||
{ | { | ||||
_options.PasswordProvider?.Invoke(_options); | |||||
await _mqttClient.ConnectAsync(_options.ClientOptions).ConfigureAwait(false); | await _mqttClient.ConnectAsync(_options.ClientOptions).ConfigureAwait(false); | ||||
return ReconnectionResult.Reconnected; | return ReconnectionResult.Reconnected; | ||||
} | } | ||||
@@ -10,8 +10,5 @@ namespace MQTTnet.Core.ManagedClient | |||||
public TimeSpan AutoReconnectDelay { get; set; } = TimeSpan.FromSeconds(5); | public TimeSpan AutoReconnectDelay { get; set; } = TimeSpan.FromSeconds(5); | ||||
public IManagedMqttClientStorage Storage { get; set; } | public IManagedMqttClientStorage Storage { get; set; } | ||||
public Func<IManagedMqttClientOptions, string> PasswordProvider { get; set; } | |||||
} | } | ||||
} | } |
@@ -3,7 +3,6 @@ using System.Threading.Tasks; | |||||
using MQTTnet.Core; | using MQTTnet.Core; | ||||
using MQTTnet.Core.Client; | using MQTTnet.Core.Client; | ||||
using MQTTnet.Core.ManagedClient; | using MQTTnet.Core.ManagedClient; | ||||
using MQTTnet.Core.Packets; | |||||
using MQTTnet.Core.Protocol; | using MQTTnet.Core.Protocol; | ||||
using Microsoft.Extensions.DependencyInjection; | using Microsoft.Extensions.DependencyInjection; | ||||
using Microsoft.Extensions.Logging; | using Microsoft.Extensions.Logging; | ||||
@@ -25,32 +24,21 @@ namespace MQTTnet.TestApp.NetCore | |||||
services.GetService<ILoggerFactory>() | services.GetService<ILoggerFactory>() | ||||
.AddConsole(); | .AddConsole(); | ||||
ClientRetainedMessageHandler ms = new ClientRetainedMessageHandler(); | |||||
Func<ManagedMqttClientOptions, string> func = delegate (ManagedMqttClientOptions managedMqttClientOptions) | |||||
{ | |||||
return "password"; | |||||
}; | |||||
var ms = new ClientRetainedMessageHandler(); | |||||
var options = new ManagedMqttClientOptions | var options = new ManagedMqttClientOptions | ||||
{ | { | ||||
ClientOptions = new MqttClientTcpOptions | ClientOptions = new MqttClientTcpOptions | ||||
{ | { | ||||
Server = "broker.hivemq.com", | Server = "broker.hivemq.com", | ||||
ClientId = "MQTTnetManagedClientTest", | ClientId = "MQTTnetManagedClientTest", | ||||
Password = "pippo", | |||||
Credentials = new RandomPassword() | |||||
}, | }, | ||||
AutoReconnectDelay = TimeSpan.FromSeconds(1), | AutoReconnectDelay = TimeSpan.FromSeconds(1), | ||||
Storage = ms, | |||||
PasswordProvider = o => | |||||
{ | |||||
//o.ClientOptions.Password = GetPassword(); | |||||
return o.ClientOptions.Password; | |||||
} | |||||
Storage = ms | |||||
}; | }; | ||||
try | try | ||||
{ | { | ||||
var managedClient = services.GetRequiredService<ManagedMqttClient>(); | var managedClient = services.GetRequiredService<ManagedMqttClient>(); | ||||
@@ -78,11 +66,18 @@ namespace MQTTnet.TestApp.NetCore | |||||
} | } | ||||
public static string GetPassword() | |||||
public class RandomPassword : IMqttClientCredentials | |||||
{ | { | ||||
return "password"; | |||||
} | |||||
public string Password | |||||
{ | |||||
get | |||||
{ | |||||
return Guid.NewGuid().ToString(); // The random password. | |||||
} | |||||
} | |||||
public string Username => "the_static_user"; | |||||
} | |||||
public class ClientRetainedMessageHandler : IManagedMqttClientStorage | public class ClientRetainedMessageHandler : IManagedMqttClientStorage | ||||
{ | { | ||||
@@ -42,7 +42,7 @@ namespace MQTTnet.TestApp.UniversalWindows | |||||
private async void Connect(object sender, RoutedEventArgs e) | private async void Connect(object sender, RoutedEventArgs e) | ||||
{ | { | ||||
BaseMqttClientOptions options = null; | |||||
MqttClientOptions options = null; | |||||
if (UseTcp.IsChecked == true) | if (UseTcp.IsChecked == true) | ||||
{ | { | ||||
options = new MqttClientTcpOptions | options = new MqttClientTcpOptions | ||||
@@ -64,8 +64,12 @@ namespace MQTTnet.TestApp.UniversalWindows | |||||
throw new InvalidOperationException(); | throw new InvalidOperationException(); | ||||
} | } | ||||
options.UserName = User.Text; | |||||
options.Password = Password.Text; | |||||
options.Credentials = new MqttClientCredentials | |||||
{ | |||||
Username = User.Text, | |||||
Password = Password.Text | |||||
}; | |||||
options.ClientId = ClientId.Text; | options.ClientId = ClientId.Text; | ||||
options.TlsOptions.UseTls = UseTls.IsChecked == true; | options.TlsOptions.UseTls = UseTls.IsChecked == true; | ||||
options.TlsOptions.IgnoreCertificateChainErrors = true; | options.TlsOptions.IgnoreCertificateChainErrors = true; | ||||