From 8916df8d7b070caf67ec0a6a5e598c978f14132e Mon Sep 17 00:00:00 2001 From: Christian Kratky Date: Thu, 27 Jun 2019 15:20:27 +0200 Subject: [PATCH] Move ReuseAddress to server options. --- Build/MQTTnet.nuspec | 3 +-- .../Implementations/MqttTcpServerListener.cs | 13 +++++++++++-- .../Server/MqttServerTcpEndpointBaseOptions.cs | 5 +++++ 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/Build/MQTTnet.nuspec b/Build/MQTTnet.nuspec index 21127d6..c9d4016 100644 --- a/Build/MQTTnet.nuspec +++ b/Build/MQTTnet.nuspec @@ -11,8 +11,7 @@ false MQTTnet is a high performance .NET library for MQTT based communication. It provides a MQTT client and a MQTT server (broker) and supports v3.1.0, v3.1.1 and v5.0.0 of the MQTT protocol. -* [Core] Fixed wrong versions in nuget packages. -* [Server] The TCP address is now reused when starting which should prevent "port in used" error when restarting. +* [Server] Moved new socket options to TCP options to avoid incompatibility with Linux hosts. Copyright Christian Kratky 2016-2019 MQTT Message Queue Telemetry Transport MQTTClient MQTTServer Server MQTTBroker Broker NETStandard IoT InternetOfThings Messaging Hardware Arduino Sensor Actuator M2M ESP Smart Home Cities Automation Xamarin diff --git a/Source/MQTTnet/Implementations/MqttTcpServerListener.cs b/Source/MQTTnet/Implementations/MqttTcpServerListener.cs index d48f599..62eea00 100644 --- a/Source/MQTTnet/Implementations/MqttTcpServerListener.cs +++ b/Source/MQTTnet/Implementations/MqttTcpServerListener.cs @@ -60,8 +60,17 @@ namespace MQTTnet.Implementations _logger.Info($"Starting TCP listener for {_localEndPoint} TLS={_tlsCertificate != null}."); _socket = new Socket(_addressFamily, SocketType.Stream, ProtocolType.Tcp); - _socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); - _socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.NoDelay, true); + + if (_options.ReuseAddress) + { + _socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); + } + + if (_options.NoDelay) + { + _socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.NoDelay, true); + } + _socket.Bind(_localEndPoint); _socket.Listen(_options.ConnectionBacklog); diff --git a/Source/MQTTnet/Server/MqttServerTcpEndpointBaseOptions.cs b/Source/MQTTnet/Server/MqttServerTcpEndpointBaseOptions.cs index 01b8c70..7f305fe 100644 --- a/Source/MQTTnet/Server/MqttServerTcpEndpointBaseOptions.cs +++ b/Source/MQTTnet/Server/MqttServerTcpEndpointBaseOptions.cs @@ -19,5 +19,10 @@ namespace MQTTnet.Server public IPAddress BoundInterNetworkAddress { get; set; } = IPAddress.Any; public IPAddress BoundInterNetworkV6Address { get; set; } = IPAddress.IPv6Any; + + /// + /// This requires admin permissions on Linux. + /// + public bool ReuseAddress { get; set; } } } \ No newline at end of file