Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore behavior of _Server_ and _Port_ in client options #2009

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .github/workflows/ReleaseNotes.md
Original file line number Diff line number Diff line change
@@ -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).
56 changes: 53 additions & 3 deletions Source/MQTTnet.Tests/Clients/MqttClient/MqttClient_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
Expand Down Expand Up @@ -86,7 +87,7 @@ public async Task Connect_Disconnect_Connect()
await client.ConnectAsync(clientOptions);
}
}

[TestMethod]
[ExpectedException(typeof(InvalidOperationException))]
public async Task Connect_Multiple_Times_Should_Fail()
Expand Down Expand Up @@ -748,8 +749,8 @@ public async Task Send_Reply_In_Message_Handler()
{
if (e.ApplicationMessage.Topic == "request")
{
// Use AtMostOnce here because with QoS 1 or even QoS 2 the process waits for
// the ACK etc. The problem is that the SpinUntil below only waits until the
// Use AtMostOnce here because with QoS 1 or even QoS 2 the process waits for
// the ACK etc. The problem is that the SpinUntil below only waits until the
// flag is set. It does not wait until the client has sent the ACK
await client2.PublishStringAsync("reply");
}
Expand Down Expand Up @@ -925,5 +926,54 @@ public async Task Subscribe_With_QoS2()
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);
}
}
}
49 changes: 47 additions & 2 deletions Source/MQTTnet/Client/Options/MqttClientTcpOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -49,13 +51,56 @@
/// </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;

Check warning on line 63 in Source/MQTTnet/Client/Options/MqttClientTcpOptions.cs

View workflow job for this annotation

GitHub Actions / build

'MqttClientTcpOptions.Server' is obsolete: 'Use RemoteEndpoint or MqttClientOptionsBuilder instead.'

Check warning on line 63 in Source/MQTTnet/Client/Options/MqttClientTcpOptions.cs

View workflow job for this annotation

GitHub Actions / build

'MqttClientTcpOptions.Server' is obsolete: 'Use RemoteEndpoint or MqttClientOptionsBuilder instead.'
Port = dnsEndPoint.Port;

Check warning on line 64 in Source/MQTTnet/Client/Options/MqttClientTcpOptions.cs

View workflow job for this annotation

GitHub Actions / build

'MqttClientTcpOptions.Port' is obsolete: 'Use RemoteEndpoint or MqttClientOptionsBuilder instead.'

Check warning on line 64 in Source/MQTTnet/Client/Options/MqttClientTcpOptions.cs

View workflow job for this annotation

GitHub Actions / build

'MqttClientTcpOptions.Port' is obsolete: 'Use RemoteEndpoint or MqttClientOptionsBuilder instead.'
}
else if (_remoteEndpoint is IPEndPoint ipEndPoint)
{
Server = ipEndPoint.Address.ToString();

Check warning on line 68 in Source/MQTTnet/Client/Options/MqttClientTcpOptions.cs

View workflow job for this annotation

GitHub Actions / build

'MqttClientTcpOptions.Server' is obsolete: 'Use RemoteEndpoint or MqttClientOptionsBuilder instead.'

Check warning on line 68 in Source/MQTTnet/Client/Options/MqttClientTcpOptions.cs

View workflow job for this annotation

GitHub Actions / build

'MqttClientTcpOptions.Server' is obsolete: 'Use RemoteEndpoint or MqttClientOptionsBuilder instead.'
Port = ipEndPoint.Port;

Check warning on line 69 in Source/MQTTnet/Client/Options/MqttClientTcpOptions.cs

View workflow job for this annotation

GitHub Actions / build

'MqttClientTcpOptions.Port' is obsolete: 'Use RemoteEndpoint or MqttClientOptionsBuilder instead.'

Check warning on line 69 in Source/MQTTnet/Client/Options/MqttClientTcpOptions.cs

View workflow job for this annotation

GitHub Actions / build

'MqttClientTcpOptions.Port' is obsolete: 'Use RemoteEndpoint or MqttClientOptionsBuilder instead.'
}
}
}

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))

Check warning on line 83 in Source/MQTTnet/Client/Options/MqttClientTcpOptions.cs

View workflow job for this annotation

GitHub Actions / build

'MqttClientTcpOptions.Server' is obsolete: 'Use RemoteEndpoint or MqttClientOptionsBuilder instead.'

Check warning on line 83 in Source/MQTTnet/Client/Options/MqttClientTcpOptions.cs

View workflow job for this annotation

GitHub Actions / build

'MqttClientTcpOptions.Server' is obsolete: 'Use RemoteEndpoint or MqttClientOptionsBuilder instead.'
{
return $"{Server}:{GetPort()}";

Check warning on line 85 in Source/MQTTnet/Client/Options/MqttClientTcpOptions.cs

View workflow job for this annotation

GitHub Actions / build

'MqttClientTcpOptions.Server' is obsolete: 'Use RemoteEndpoint or MqttClientOptionsBuilder instead.'

Check warning on line 85 in Source/MQTTnet/Client/Options/MqttClientTcpOptions.cs

View workflow job for this annotation

GitHub Actions / build

'MqttClientTcpOptions.Server' is obsolete: 'Use RemoteEndpoint or MqttClientOptionsBuilder instead.'
}

return string.Empty;
}

int GetPort()
{
if (Port.HasValue)

Check warning on line 93 in Source/MQTTnet/Client/Options/MqttClientTcpOptions.cs

View workflow job for this annotation

GitHub Actions / build

'MqttClientTcpOptions.Port' is obsolete: 'Use RemoteEndpoint or MqttClientOptionsBuilder instead.'

Check warning on line 93 in Source/MQTTnet/Client/Options/MqttClientTcpOptions.cs

View workflow job for this annotation

GitHub Actions / build

'MqttClientTcpOptions.Port' is obsolete: 'Use RemoteEndpoint or MqttClientOptionsBuilder instead.'
{
return Port.Value;

Check warning on line 95 in Source/MQTTnet/Client/Options/MqttClientTcpOptions.cs

View workflow job for this annotation

GitHub Actions / build

'MqttClientTcpOptions.Port' is obsolete: 'Use RemoteEndpoint or MqttClientOptionsBuilder instead.'

Check warning on line 95 in Source/MQTTnet/Client/Options/MqttClientTcpOptions.cs

View workflow job for this annotation

GitHub Actions / build

'MqttClientTcpOptions.Port' is obsolete: 'Use RemoteEndpoint or MqttClientOptionsBuilder instead.'
}

if (TlsOptions?.UseTls == true)
{
return MqttPorts.Secure;
}

return MqttPorts.Default;
}
}
}
Loading