Fix missing endpoint in client status.

This commit is contained in:
Christian Kratky
2019-04-23 14:48:38 +02:00
parent d4c6fb44c9
commit c7409c2ff4
3 changed files with 23 additions and 18 deletions
@@ -4,6 +4,7 @@ using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime; using System.Runtime.InteropServices.WindowsRuntime;
using System.Security.Authentication;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Windows.Networking; using Windows.Networking;
@@ -38,22 +39,13 @@ namespace MQTTnet.Implementations
CreateStreams(); CreateStreams();
IsSecureConnection = socket.Information.ProtectionLevel >= SocketProtectionLevel.Tls12; IsSecureConnection = socket.Information.ProtectionLevel >= SocketProtectionLevel.Tls12;
Endpoint = _socket.Information.RemoteAddress + ":" + _socket.Information.RemotePort;
} }
public static Func<MqttClientTcpOptions, IEnumerable<ChainValidationResult>> CustomIgnorableServerCertificateErrorsResolver { get; set; } public static Func<MqttClientTcpOptions, IEnumerable<ChainValidationResult>> CustomIgnorableServerCertificateErrorsResolver { get; set; }
public string Endpoint public string Endpoint { get; private set; }
{
get
{
if (_socket?.Information != null)
{
return _socket.Information.RemoteAddress + ":" + _socket.Information.RemotePort;
}
return null;
}
}
public bool IsSecureConnection { get; } public bool IsSecureConnection { get; }
@@ -66,7 +58,7 @@ namespace MQTTnet.Implementations
_socket.Control.KeepAlive = true; _socket.Control.KeepAlive = true;
} }
if (!_options.TlsOptions.UseTls) if (_options.TlsOptions?.UseTls != true)
{ {
await _socket.ConnectAsync(new HostName(_options.Server), _options.GetPort().ToString()); await _socket.ConnectAsync(new HostName(_options.Server), _options.GetPort().ToString());
} }
@@ -79,9 +71,21 @@ namespace MQTTnet.Implementations
_socket.Control.IgnorableServerCertificateErrors.Add(ignorableChainValidationResult); _socket.Control.IgnorableServerCertificateErrors.Add(ignorableChainValidationResult);
} }
await _socket.ConnectAsync(new HostName(_options.Server), _options.GetPort().ToString(), SocketProtectionLevel.Tls12); var socketProtectionLevel = SocketProtectionLevel.Tls12;
if (_options.TlsOptions.SslProtocol == SslProtocols.Tls11)
{
socketProtectionLevel = SocketProtectionLevel.Tls11;
}
else if (_options.TlsOptions.SslProtocol == SslProtocols.Tls)
{
socketProtectionLevel = SocketProtectionLevel.Tls10;
}
await _socket.ConnectAsync(new HostName(_options.Server), _options.GetPort().ToString(), socketProtectionLevel);
} }
Endpoint = _socket.Information.RemoteAddress + ":" + _socket.Information.RemotePort;
CreateStreams(); CreateStreams();
} }
@@ -28,11 +28,12 @@ namespace MQTTnet.Implementations
IsSecureConnection = clientOptions.ChannelOptions?.TlsOptions?.UseTls == true; IsSecureConnection = clientOptions.ChannelOptions?.TlsOptions?.UseTls == true;
} }
public MqttTcpChannel(Stream stream) public MqttTcpChannel(Stream stream, string endpoint)
{ {
_stream = stream ?? throw new ArgumentNullException(nameof(stream)); _stream = stream ?? throw new ArgumentNullException(nameof(stream));
IsSecureConnection = stream is SslStream; IsSecureConnection = stream is SslStream;
Endpoint = endpoint;
} }
public string Endpoint { get; private set; } public string Endpoint { get; private set; }
@@ -97,11 +97,11 @@ namespace MQTTnet.Implementations
private async Task TryHandleClientConnectionAsync(Socket clientSocket) private async Task TryHandleClientConnectionAsync(Socket clientSocket)
{ {
Stream stream = null; Stream stream = null;
EndPoint remoteEndPoint = null; string remoteEndPoint = null;
try try
{ {
remoteEndPoint = clientSocket.RemoteEndPoint; remoteEndPoint = clientSocket.RemoteEndPoint.ToString();
_logger.Verbose("Client '{0}' accepted by TCP listener '{1}, {2}'.", _logger.Verbose("Client '{0}' accepted by TCP listener '{1}, {2}'.",
remoteEndPoint, remoteEndPoint,
@@ -122,7 +122,7 @@ namespace MQTTnet.Implementations
var clientHandler = ClientHandler; var clientHandler = ClientHandler;
if (clientHandler != null) if (clientHandler != null)
{ {
using (var clientAdapter = new MqttChannelAdapter(new MqttTcpChannel(stream), new MqttPacketFormatterAdapter(), _logger)) using (var clientAdapter = new MqttChannelAdapter(new MqttTcpChannel(stream, remoteEndPoint), new MqttPacketFormatterAdapter(), _logger))
{ {
await clientHandler(clientAdapter).ConfigureAwait(false); await clientHandler(clientAdapter).ConfigureAwait(false);
} }