Dispose socket when TCP connect failed.

This commit is contained in:
Christian Kratky
2020-04-30 23:02:05 +02:00
parent 856861e1b1
commit f85bc9b956
3 changed files with 52 additions and 44 deletions
@@ -143,7 +143,7 @@ namespace MQTTnet.Implementations
{
_networkStream?.Dispose();
// Workaround for: workaround for https://github.com/dotnet/corefx/issues/24430
// Workaround for: https://github.com/dotnet/corefx/issues/24430
using (cancellationToken.Register(() => _socket.Dispose()))
{
cancellationToken.ThrowIfCancellationRequested();
@@ -46,56 +46,63 @@ namespace MQTTnet.Implementations
public async Task ConnectAsync(CancellationToken cancellationToken)
{
CrossPlatformSocket socket;
if (_options.AddressFamily == AddressFamily.Unspecified)
CrossPlatformSocket socket = null;
try
{
socket = new CrossPlatformSocket();
}
else
{
socket = new CrossPlatformSocket(_options.AddressFamily);
}
socket.ReceiveBufferSize = _options.BufferSize;
socket.SendBufferSize = _options.BufferSize;
socket.NoDelay = _options.NoDelay;
if (_options.DualMode.HasValue)
{
// It is important to avoid setting the flag if no specific value is set by the user
// because on IPv4 only networks the setter will always throw an exception. Regardless
// of the actual value.
socket.DualMode = _options.DualMode.Value;
}
await socket.ConnectAsync(_options.Server, _options.GetPort(), cancellationToken).ConfigureAwait(false);
cancellationToken.ThrowIfCancellationRequested();
var networkStream = socket.GetStream();
if (_options.TlsOptions?.UseTls == true)
{
var sslStream = new SslStream(networkStream, false, InternalUserCertificateValidationCallback);
try
if (_options.AddressFamily == AddressFamily.Unspecified)
{
await sslStream.AuthenticateAsClientAsync(_options.Server, LoadCertificates(), _options.TlsOptions.SslProtocol, !_options.TlsOptions.IgnoreCertificateRevocationErrors).ConfigureAwait(false);
socket = new CrossPlatformSocket();
}
catch
else
{
sslStream.Dispose();
throw;
socket = new CrossPlatformSocket(_options.AddressFamily);
}
_stream = sslStream;
}
else
{
_stream = networkStream;
}
socket.ReceiveBufferSize = _options.BufferSize;
socket.SendBufferSize = _options.BufferSize;
socket.NoDelay = _options.NoDelay;
Endpoint = socket.RemoteEndPoint?.ToString();
if (_options.DualMode.HasValue)
{
// It is important to avoid setting the flag if no specific value is set by the user
// because on IPv4 only networks the setter will always throw an exception. Regardless
// of the actual value.
socket.DualMode = _options.DualMode.Value;
}
await socket.ConnectAsync(_options.Server, _options.GetPort(), cancellationToken).ConfigureAwait(false);
cancellationToken.ThrowIfCancellationRequested();
var networkStream = socket.GetStream();
if (_options.TlsOptions?.UseTls == true)
{
var sslStream = new SslStream(networkStream, false, InternalUserCertificateValidationCallback);
try
{
await sslStream.AuthenticateAsClientAsync(_options.Server, LoadCertificates(), _options.TlsOptions.SslProtocol, !_options.TlsOptions.IgnoreCertificateRevocationErrors).ConfigureAwait(false);
}
catch
{
sslStream.Dispose();
throw;
}
_stream = sslStream;
}
else
{
_stream = networkStream;
}
Endpoint = socket.RemoteEndPoint?.ToString();
}
catch (Exception)
{
socket?.Dispose();
throw;
}
}
public Task DisconnectAsync(CancellationToken cancellationToken)