Skip to content

Commit 25c1da6

Browse files
authored
Restore behavior of _Server_ and _Port_ in client options (#2009)
* Restore old server and port behavior * Fix unit test
1 parent 4e3ede4 commit 25c1da6

File tree

3 files changed

+101
-7
lines changed

3 files changed

+101
-7
lines changed

.github/workflows/ReleaseNotes.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
* [Client] Fix _None of the discovered or specified addresses match the socket address family._ (#1997).
2-
* [Client] Remove the obsolete attribute from the _WithConnectionUri_ methods (#1979).
1+
* [Client] Restored _Server_ and _Port_ behavior of client options (#2005).

Source/MQTTnet.Tests/Clients/MqttClient/MqttClient_Tests.cs

+53-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Collections.Generic;
77
using System.Diagnostics;
88
using System.Linq;
9+
using System.Net;
910
using System.Net.Sockets;
1011
using System.Text;
1112
using System.Threading;
@@ -86,7 +87,7 @@ public async Task Connect_Disconnect_Connect()
8687
await client.ConnectAsync(clientOptions);
8788
}
8889
}
89-
90+
9091
[TestMethod]
9192
[ExpectedException(typeof(InvalidOperationException))]
9293
public async Task Connect_Multiple_Times_Should_Fail()
@@ -748,8 +749,8 @@ public async Task Send_Reply_In_Message_Handler()
748749
{
749750
if (e.ApplicationMessage.Topic == "request")
750751
{
751-
// Use AtMostOnce here because with QoS 1 or even QoS 2 the process waits for
752-
// the ACK etc. The problem is that the SpinUntil below only waits until the
752+
// Use AtMostOnce here because with QoS 1 or even QoS 2 the process waits for
753+
// the ACK etc. The problem is that the SpinUntil below only waits until the
753754
// flag is set. It does not wait until the client has sent the ACK
754755
await client2.PublishStringAsync("reply");
755756
}
@@ -925,5 +926,54 @@ public async Task Subscribe_With_QoS2()
925926
Assert.IsFalse(disconnectedFired);
926927
}
927928
}
929+
930+
[TestMethod]
931+
public void Backward_compatible_TCP_options()
932+
{
933+
var options = new MqttClientOptionsBuilder().WithTcpServer("host", 3).Build();
934+
935+
Assert.AreEqual("host", ((MqttClientTcpOptions)options.ChannelOptions).Server);
936+
Assert.AreEqual(3, ((MqttClientTcpOptions)options.ChannelOptions).Port);
937+
938+
options = new MqttClientOptions
939+
{
940+
ChannelOptions = new MqttClientTcpOptions
941+
{
942+
Server = "host",
943+
Port = 3
944+
}
945+
};
946+
947+
Assert.AreEqual("host:3", options.ChannelOptions.ToString());
948+
Assert.AreEqual("host", ((MqttClientTcpOptions)options.ChannelOptions).Server);
949+
Assert.AreEqual(3, ((MqttClientTcpOptions)options.ChannelOptions).Port);
950+
951+
options = new MqttClientOptionsBuilder().WithEndPoint(new DnsEndPoint("host", 3)).Build();
952+
Assert.AreEqual("Unspecified/host:3", options.ChannelOptions.ToString());
953+
Assert.AreEqual("host", ((MqttClientTcpOptions)options.ChannelOptions).Server);
954+
Assert.AreEqual(3, ((MqttClientTcpOptions)options.ChannelOptions).Port);
955+
956+
options = new MqttClientOptionsBuilder().WithTcpServer("host").Build();
957+
958+
Assert.AreEqual("host", ((MqttClientTcpOptions)options.ChannelOptions).Server);
959+
Assert.AreEqual(1883, ((MqttClientTcpOptions)options.ChannelOptions).Port);
960+
Assert.AreEqual("Unspecified/host:1883", options.ChannelOptions.ToString());
961+
962+
options = new MqttClientOptionsBuilder().WithTlsOptions(o => o.UseTls()).WithTcpServer("host").Build();
963+
964+
Assert.AreEqual("host", ((MqttClientTcpOptions)options.ChannelOptions).Server);
965+
Assert.AreEqual(8883, ((MqttClientTcpOptions)options.ChannelOptions).Port);
966+
967+
options = new MqttClientOptions
968+
{
969+
ChannelOptions = new MqttClientTcpOptions
970+
{
971+
Server = "host"
972+
}
973+
};
974+
975+
Assert.AreEqual("host", ((MqttClientTcpOptions)options.ChannelOptions).Server);
976+
Assert.AreEqual(null, ((MqttClientTcpOptions)options.ChannelOptions).Port);
977+
}
928978
}
929979
}

Source/MQTTnet/Client/Options/MqttClientTcpOptions.cs

+47-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
using System;
66
using System.Net;
77
using System.Net.Sockets;
8+
using MQTTnet.Protocol;
89

910
namespace MQTTnet.Client
1011
{
1112
public sealed class MqttClientTcpOptions : IMqttClientChannelOptions
1213
{
14+
EndPoint _remoteEndpoint;
1315
public AddressFamily AddressFamily { get; set; } = AddressFamily.Unspecified;
1416

1517
public int BufferSize { get; set; } = 8192;
@@ -49,13 +51,56 @@ public sealed class MqttClientTcpOptions : IMqttClientChannelOptions
4951
/// </summary>
5052
public ProtocolType ProtocolType { get; set; } = ProtocolType.Tcp;
5153

52-
public EndPoint RemoteEndpoint { get; set; }
54+
public EndPoint RemoteEndpoint
55+
{
56+
get => _remoteEndpoint;
57+
set
58+
{
59+
_remoteEndpoint = value;
60+
61+
if (_remoteEndpoint is DnsEndPoint dnsEndPoint)
62+
{
63+
Server = dnsEndPoint.Host;
64+
Port = dnsEndPoint.Port;
65+
}
66+
else if (_remoteEndpoint is IPEndPoint ipEndPoint)
67+
{
68+
Server = ipEndPoint.Address.ToString();
69+
Port = ipEndPoint.Port;
70+
}
71+
}
72+
}
5373

5474
public MqttClientTlsOptions TlsOptions { get; set; } = new MqttClientTlsOptions();
5575

5676
public override string ToString()
5777
{
58-
return RemoteEndpoint?.ToString() ?? string.Empty;
78+
if (RemoteEndpoint != null)
79+
{
80+
return RemoteEndpoint.ToString();
81+
}
82+
83+
if (!string.IsNullOrEmpty(Server))
84+
{
85+
return $"{Server}:{GetPort()}";
86+
}
87+
88+
return string.Empty;
89+
}
90+
91+
int GetPort()
92+
{
93+
if (Port.HasValue)
94+
{
95+
return Port.Value;
96+
}
97+
98+
if (TlsOptions?.UseTls == true)
99+
{
100+
return MqttPorts.Secure;
101+
}
102+
103+
return MqttPorts.Default;
59104
}
60105
}
61106
}

0 commit comments

Comments
 (0)