Browse Source

Added the bound IP address to the server options.

release/3.x.x
Christian Kratky 7 years ago
parent
commit
e0284886b5
5 changed files with 46 additions and 15 deletions
  1. +2
    -2
      Frameworks/MQTTnet.NetStandard/Implementations/MqttServerAdapter.cs
  2. +8
    -4
      Frameworks/MQTTnet.NetStandard/Server/IMqttServerOptions.cs
  3. +5
    -1
      Frameworks/MQTTnet.NetStandard/Server/MqttServerDefaultEndpointOptions.cs
  4. +26
    -7
      Frameworks/MQTTnet.NetStandard/Server/MqttServerOptionsBuilder.cs
  5. +5
    -1
      Frameworks/MQTTnet.NetStandard/Server/MqttServerTlsEndpointOptions.cs

+ 2
- 2
Frameworks/MQTTnet.NetStandard/Implementations/MqttServerAdapter.cs View File

@@ -39,7 +39,7 @@ namespace MQTTnet.Implementations
if (options.DefaultEndpointOptions.IsEnabled) if (options.DefaultEndpointOptions.IsEnabled)
{ {
_defaultEndpointSocket = new Socket(SocketType.Stream, ProtocolType.Tcp); _defaultEndpointSocket = new Socket(SocketType.Stream, ProtocolType.Tcp);
_defaultEndpointSocket.Bind(new IPEndPoint(IPAddress.Any, options.GetDefaultEndpointPort()));
_defaultEndpointSocket.Bind(new IPEndPoint(options.DefaultEndpointOptions.BoundIPAddress, options.GetDefaultEndpointPort()));
_defaultEndpointSocket.Listen(options.ConnectionBacklog); _defaultEndpointSocket.Listen(options.ConnectionBacklog);


Task.Run(async () => await AcceptDefaultEndpointConnectionsAsync(_cancellationTokenSource.Token).ConfigureAwait(false), _cancellationTokenSource.Token).ConfigureAwait(false); Task.Run(async () => await AcceptDefaultEndpointConnectionsAsync(_cancellationTokenSource.Token).ConfigureAwait(false), _cancellationTokenSource.Token).ConfigureAwait(false);
@@ -59,7 +59,7 @@ namespace MQTTnet.Implementations
} }


_tlsEndpointSocket = new Socket(SocketType.Stream, ProtocolType.Tcp); _tlsEndpointSocket = new Socket(SocketType.Stream, ProtocolType.Tcp);
_tlsEndpointSocket.Bind(new IPEndPoint(IPAddress.Any, options.GetTlsEndpointPort()));
_tlsEndpointSocket.Bind(new IPEndPoint(options.TlsEndpointOptions.BoundIPAddress, options.GetTlsEndpointPort()));
_tlsEndpointSocket.Listen(options.ConnectionBacklog); _tlsEndpointSocket.Listen(options.ConnectionBacklog);


Task.Run(async () => await AcceptTlsEndpointConnectionsAsync(_cancellationTokenSource.Token).ConfigureAwait(false), _cancellationTokenSource.Token).ConfigureAwait(false); Task.Run(async () => await AcceptTlsEndpointConnectionsAsync(_cancellationTokenSource.Token).ConfigureAwait(false), _cancellationTokenSource.Token).ConfigureAwait(false);


+ 8
- 4
Frameworks/MQTTnet.NetStandard/Server/IMqttServerOptions.cs View File

@@ -1,16 +1,20 @@
using System; using System;
using System.Net;


namespace MQTTnet.Server namespace MQTTnet.Server
{ {
public interface IMqttServerOptions public interface IMqttServerOptions
{ {
Action<MqttApplicationMessageInterceptorContext> ApplicationMessageInterceptor { get; }
int ConnectionBacklog { get; } int ConnectionBacklog { get; }
Action<MqttConnectionValidatorContext> ConnectionValidator { get; }
TimeSpan DefaultCommunicationTimeout { get; } TimeSpan DefaultCommunicationTimeout { get; }
MqttServerDefaultEndpointOptions DefaultEndpointOptions { get; }
IMqttServerStorage Storage { get; }
Action<MqttConnectionValidatorContext> ConnectionValidator { get; }
Action<MqttSubscriptionInterceptorContext> SubscriptionInterceptor { get; } Action<MqttSubscriptionInterceptorContext> SubscriptionInterceptor { get; }
Action<MqttApplicationMessageInterceptorContext> ApplicationMessageInterceptor { get; }

MqttServerDefaultEndpointOptions DefaultEndpointOptions { get; }
MqttServerTlsEndpointOptions TlsEndpointOptions { get; } MqttServerTlsEndpointOptions TlsEndpointOptions { get; }

IMqttServerStorage Storage { get; }
} }
} }

+ 5
- 1
Frameworks/MQTTnet.NetStandard/Server/MqttServerDefaultEndpointOptions.cs View File

@@ -1,9 +1,13 @@
namespace MQTTnet.Server
using System.Net;

namespace MQTTnet.Server
{ {
public sealed class MqttServerDefaultEndpointOptions public sealed class MqttServerDefaultEndpointOptions
{ {
public bool IsEnabled { get; set; } = true; public bool IsEnabled { get; set; } = true;


public int? Port { get; set; } public int? Port { get; set; }

public IPAddress BoundIPAddress { get; set; } = IPAddress.Any;
} }
} }

+ 26
- 7
Frameworks/MQTTnet.NetStandard/Server/MqttServerOptionsBuilder.cs View File

@@ -1,4 +1,5 @@
using System; using System;
using System.Net;


namespace MQTTnet.Server namespace MQTTnet.Server
{ {
@@ -18,33 +19,45 @@ namespace MQTTnet.Server
return this; return this;
} }


public MqttServerOptionsBuilder WithDefaultEndpointPort(int value)
public MqttServerOptionsBuilder WithDefaultEndpoint()
{ {
_options.DefaultEndpointOptions.Port = value;
_options.DefaultEndpointOptions.IsEnabled = true;
return this; return this;
} }


public MqttServerOptionsBuilder WithDefaultEndpoint()
public MqttServerOptionsBuilder WithDefaultEndpointPort(int? value)
{ {
_options.DefaultEndpointOptions.IsEnabled = true;
_options.DefaultEndpointOptions.Port = value;
return this; return this;
} }


public MqttServerOptionsBuilder WithDefaultEndpointBoundIPAddress(IPAddress value)
{
_options.DefaultEndpointOptions.BoundIPAddress = value ?? IPAddress.Any;
return this;
}
public MqttServerOptionsBuilder WithoutDefaultEndpoint() public MqttServerOptionsBuilder WithoutDefaultEndpoint()
{ {
_options.DefaultEndpointOptions.IsEnabled = false; _options.DefaultEndpointOptions.IsEnabled = false;
return this; return this;
} }

public MqttServerOptionsBuilder WithEncryptedEndpoint() public MqttServerOptionsBuilder WithEncryptedEndpoint()
{ {
_options.TlsEndpointOptions.IsEnabled = true; _options.TlsEndpointOptions.IsEnabled = true;
return this; return this;
} }


public MqttServerOptionsBuilder WithoutEncryptedEndpoint()
public MqttServerOptionsBuilder WithEncryptedEndpointPort(int? value)
{ {
_options.TlsEndpointOptions.IsEnabled = false;
_options.TlsEndpointOptions.Port = value;
return this;
}

public MqttServerOptionsBuilder WithEncryptedEndpointBoundIPAddress(IPAddress value)
{
_options.TlsEndpointOptions.BoundIPAddress = value;
return this; return this;
} }


@@ -54,6 +67,12 @@ namespace MQTTnet.Server
return this; return this;
} }


public MqttServerOptionsBuilder WithoutEncryptedEndpoint()
{
_options.TlsEndpointOptions.IsEnabled = false;
return this;
}
public MqttServerOptionsBuilder WithStorage(IMqttServerStorage value) public MqttServerOptionsBuilder WithStorage(IMqttServerStorage value)
{ {
_options.Storage = value; _options.Storage = value;


+ 5
- 1
Frameworks/MQTTnet.NetStandard/Server/MqttServerTlsEndpointOptions.cs View File

@@ -1,4 +1,6 @@
namespace MQTTnet.Server
using System.Net;

namespace MQTTnet.Server
{ {
public sealed class MqttServerTlsEndpointOptions public sealed class MqttServerTlsEndpointOptions
{ {
@@ -7,5 +9,7 @@
public int? Port { get; set; } public int? Port { get; set; }


public byte[] Certificate { get; set; } public byte[] Certificate { get; set; }

public IPAddress BoundIPAddress { get; set; } = IPAddress.Any;
} }
} }

Loading…
Cancel
Save