|
|
@@ -1,5 +1,4 @@ |
|
|
|
#if NET451 || NETSTANDARD1_3 |
|
|
|
|
|
|
|
using System; |
|
|
|
using System.Net; |
|
|
|
using System.Net.Security; |
|
|
@@ -17,17 +16,17 @@ namespace MQTTnet.Implementations |
|
|
|
public class MqttServerAdapter : IMqttServerAdapter, IDisposable |
|
|
|
{ |
|
|
|
private readonly ILogger<MqttServerAdapter> _logger; |
|
|
|
private readonly IMqttCommunicationAdapterFactory _mqttCommunicationAdapterFactory; |
|
|
|
private readonly IMqttCommunicationAdapterFactory _communicationAdapterFactory; |
|
|
|
|
|
|
|
private CancellationTokenSource _cancellationTokenSource; |
|
|
|
private Socket _defaultEndpointSocket; |
|
|
|
private Socket _tlsEndpointSocket; |
|
|
|
private X509Certificate2 _tlsCertificate; |
|
|
|
|
|
|
|
public MqttServerAdapter(ILogger<MqttServerAdapter> logger, IMqttCommunicationAdapterFactory mqttCommunicationAdapterFactory) |
|
|
|
public MqttServerAdapter(ILogger<MqttServerAdapter> logger, IMqttCommunicationAdapterFactory communicationAdapterFactory) |
|
|
|
{ |
|
|
|
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); |
|
|
|
_mqttCommunicationAdapterFactory = mqttCommunicationAdapterFactory ?? throw new ArgumentNullException(nameof(mqttCommunicationAdapterFactory)); |
|
|
|
_communicationAdapterFactory = communicationAdapterFactory ?? throw new ArgumentNullException(nameof(communicationAdapterFactory)); |
|
|
|
} |
|
|
|
|
|
|
|
public event EventHandler<MqttServerAdapterClientAcceptedEventArgs> ClientAccepted; |
|
|
@@ -104,7 +103,7 @@ namespace MQTTnet.Implementations |
|
|
|
#else |
|
|
|
var clientSocket = await _defaultEndpointSocket.AcceptAsync().ConfigureAwait(false); |
|
|
|
#endif |
|
|
|
var clientAdapter = _mqttCommunicationAdapterFactory.CreateServerMqttCommunicationAdapter(new MqttTcpChannel(clientSocket, null)); |
|
|
|
var clientAdapter = _communicationAdapterFactory.CreateServerCommunicationAdapter(new MqttTcpChannel(clientSocket, null)); |
|
|
|
ClientAccepted?.Invoke(this, new MqttServerAdapterClientAcceptedEventArgs(clientAdapter)); |
|
|
|
} |
|
|
|
catch (Exception exception) |
|
|
@@ -132,7 +131,7 @@ namespace MQTTnet.Implementations |
|
|
|
var sslStream = new SslStream(new NetworkStream(clientSocket)); |
|
|
|
await sslStream.AuthenticateAsServerAsync(_tlsCertificate, false, SslProtocols.Tls12, false).ConfigureAwait(false); |
|
|
|
|
|
|
|
var clientAdapter = _mqttCommunicationAdapterFactory.CreateServerMqttCommunicationAdapter(new MqttTcpChannel(clientSocket, sslStream)); |
|
|
|
var clientAdapter = _communicationAdapterFactory.CreateServerCommunicationAdapter(new MqttTcpChannel(clientSocket, sslStream)); |
|
|
|
ClientAccepted?.Invoke(this, new MqttServerAdapterClientAcceptedEventArgs(clientAdapter)); |
|
|
|
} |
|
|
|
catch (Exception exception) |