mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-15 00:15:15 +08:00
Restore behavior of _Server_ and _Port_ in client options (#2009)
* Restore old server and port behavior * Fix unit test
This commit is contained in:
@@ -1,2 +1 @@
|
||||
* [Client] Fix _None of the discovered or specified addresses match the socket address family._ (#1997).
|
||||
* [Client] Remove the obsolete attribute from the _WithConnectionUri_ methods (#1979).
|
||||
* [Client] Restored _Server_ and _Port_ behavior of client options (#2005).
|
||||
|
||||
@@ -6,6 +6,7 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
@@ -925,5 +926,54 @@ namespace MQTTnet.Tests.Clients.MqttClient
|
||||
Assert.IsFalse(disconnectedFired);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Backward_compatible_TCP_options()
|
||||
{
|
||||
var options = new MqttClientOptionsBuilder().WithTcpServer("host", 3).Build();
|
||||
|
||||
Assert.AreEqual("host", ((MqttClientTcpOptions)options.ChannelOptions).Server);
|
||||
Assert.AreEqual(3, ((MqttClientTcpOptions)options.ChannelOptions).Port);
|
||||
|
||||
options = new MqttClientOptions
|
||||
{
|
||||
ChannelOptions = new MqttClientTcpOptions
|
||||
{
|
||||
Server = "host",
|
||||
Port = 3
|
||||
}
|
||||
};
|
||||
|
||||
Assert.AreEqual("host:3", options.ChannelOptions.ToString());
|
||||
Assert.AreEqual("host", ((MqttClientTcpOptions)options.ChannelOptions).Server);
|
||||
Assert.AreEqual(3, ((MqttClientTcpOptions)options.ChannelOptions).Port);
|
||||
|
||||
options = new MqttClientOptionsBuilder().WithEndPoint(new DnsEndPoint("host", 3)).Build();
|
||||
Assert.AreEqual("Unspecified/host:3", options.ChannelOptions.ToString());
|
||||
Assert.AreEqual("host", ((MqttClientTcpOptions)options.ChannelOptions).Server);
|
||||
Assert.AreEqual(3, ((MqttClientTcpOptions)options.ChannelOptions).Port);
|
||||
|
||||
options = new MqttClientOptionsBuilder().WithTcpServer("host").Build();
|
||||
|
||||
Assert.AreEqual("host", ((MqttClientTcpOptions)options.ChannelOptions).Server);
|
||||
Assert.AreEqual(1883, ((MqttClientTcpOptions)options.ChannelOptions).Port);
|
||||
Assert.AreEqual("Unspecified/host:1883", options.ChannelOptions.ToString());
|
||||
|
||||
options = new MqttClientOptionsBuilder().WithTlsOptions(o => o.UseTls()).WithTcpServer("host").Build();
|
||||
|
||||
Assert.AreEqual("host", ((MqttClientTcpOptions)options.ChannelOptions).Server);
|
||||
Assert.AreEqual(8883, ((MqttClientTcpOptions)options.ChannelOptions).Port);
|
||||
|
||||
options = new MqttClientOptions
|
||||
{
|
||||
ChannelOptions = new MqttClientTcpOptions
|
||||
{
|
||||
Server = "host"
|
||||
}
|
||||
};
|
||||
|
||||
Assert.AreEqual("host", ((MqttClientTcpOptions)options.ChannelOptions).Server);
|
||||
Assert.AreEqual(null, ((MqttClientTcpOptions)options.ChannelOptions).Port);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,11 +5,13 @@
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using MQTTnet.Protocol;
|
||||
|
||||
namespace MQTTnet.Client
|
||||
{
|
||||
public sealed class MqttClientTcpOptions : IMqttClientChannelOptions
|
||||
{
|
||||
EndPoint _remoteEndpoint;
|
||||
public AddressFamily AddressFamily { get; set; } = AddressFamily.Unspecified;
|
||||
|
||||
public int BufferSize { get; set; } = 8192;
|
||||
@@ -49,13 +51,56 @@ namespace MQTTnet.Client
|
||||
/// </summary>
|
||||
public ProtocolType ProtocolType { get; set; } = ProtocolType.Tcp;
|
||||
|
||||
public EndPoint RemoteEndpoint { get; set; }
|
||||
public EndPoint RemoteEndpoint
|
||||
{
|
||||
get => _remoteEndpoint;
|
||||
set
|
||||
{
|
||||
_remoteEndpoint = value;
|
||||
|
||||
if (_remoteEndpoint is DnsEndPoint dnsEndPoint)
|
||||
{
|
||||
Server = dnsEndPoint.Host;
|
||||
Port = dnsEndPoint.Port;
|
||||
}
|
||||
else if (_remoteEndpoint is IPEndPoint ipEndPoint)
|
||||
{
|
||||
Server = ipEndPoint.Address.ToString();
|
||||
Port = ipEndPoint.Port;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public MqttClientTlsOptions TlsOptions { get; set; } = new MqttClientTlsOptions();
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return RemoteEndpoint?.ToString() ?? string.Empty;
|
||||
if (RemoteEndpoint != null)
|
||||
{
|
||||
return RemoteEndpoint.ToString();
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(Server))
|
||||
{
|
||||
return $"{Server}:{GetPort()}";
|
||||
}
|
||||
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
int GetPort()
|
||||
{
|
||||
if (Port.HasValue)
|
||||
{
|
||||
return Port.Value;
|
||||
}
|
||||
|
||||
if (TlsOptions?.UseTls == true)
|
||||
{
|
||||
return MqttPorts.Secure;
|
||||
}
|
||||
|
||||
return MqttPorts.Default;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user