Refactor latest changes

This commit is contained in:
Christian Kratky
2017-09-15 15:26:26 +02:00
parent eed56cdf55
commit 76105de4c7
18 changed files with 280 additions and 274 deletions
@@ -6,7 +6,6 @@ using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
using MQTTnet.Core.Channel;
using MQTTnet.Core.Client;
using MQTTnet.Core.Exceptions;
using System.IO;
namespace MQTTnet.Implementations
@@ -16,10 +15,6 @@ namespace MQTTnet.Implementations
private Socket _socket;
private SslStream _sslStream;
public Stream ReceiveStream { get; private set; }
public Stream RawStream => ReceiveStream;
public Stream SendStream => ReceiveStream;
/// <summary>
/// called on client sockets are created in connect
/// </summary>
@@ -38,55 +33,45 @@ namespace MQTTnet.Implementations
ReceiveStream = (Stream)sslStream ?? new NetworkStream(socket);
}
public Stream ReceiveStream { get; private set; }
public Stream RawStream => ReceiveStream;
public Stream SendStream => ReceiveStream;
public async Task ConnectAsync(MqttClientOptions options)
{
if (options == null) throw new ArgumentNullException(nameof(options));
try
if (_socket == null)
{
if (_socket == null)
{
_socket = new Socket(SocketType.Stream, ProtocolType.Tcp);
}
await _socket.ConnectAsync(options.Server, options.GetPort()).ConfigureAwait(false);
if (options.TlsOptions.UseTls)
{
_sslStream = new SslStream(new NetworkStream(_socket, true));
ReceiveStream = _sslStream;
await _sslStream.AuthenticateAsClientAsync(options.Server, LoadCertificates(options), SslProtocols.Tls12, options.TlsOptions.CheckCertificateRevocation).ConfigureAwait(false);
}
else
{
ReceiveStream = new NetworkStream(_socket);
}
_socket = new Socket(SocketType.Stream, ProtocolType.Tcp);
}
catch (SocketException exception)
await _socket.ConnectAsync(options.Server, options.GetPort()).ConfigureAwait(false);
if (options.TlsOptions.UseTls)
{
throw new MqttCommunicationException(exception);
_sslStream = new SslStream(new NetworkStream(_socket, true));
ReceiveStream = _sslStream;
await _sslStream.AuthenticateAsClientAsync(options.Server, LoadCertificates(options), SslProtocols.Tls12, options.TlsOptions.CheckCertificateRevocation).ConfigureAwait(false);
}
else
{
ReceiveStream = new NetworkStream(_socket);
}
}
public Task DisconnectAsync()
{
try
{
Dispose();
return Task.FromResult(0);
}
catch (SocketException exception)
{
throw new MqttCommunicationException(exception);
}
Dispose();
return Task.FromResult(0);
}
public void Dispose()
{
_socket?.Dispose();
_sslStream?.Dispose();
_socket = null;
_sslStream?.Dispose();
_sslStream = null;
}