mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-28 17:22:37 +08:00
Dispose socket when TCP connect failed.
This commit is contained in:
@@ -17,6 +17,7 @@
|
|||||||
* [Client] Added method to trigger PING/PONG manually (connection check etc.).
|
* [Client] Added method to trigger PING/PONG manually (connection check etc.).
|
||||||
* [Client] Added support for certificate validation callback when using Web Sockets (requires netstandard2.1+).
|
* [Client] Added support for certificate validation callback when using Web Sockets (requires netstandard2.1+).
|
||||||
* [Client] Fixed a memory leak when web socket based connections trying to reconnect with an offline server.
|
* [Client] Fixed a memory leak when web socket based connections trying to reconnect with an offline server.
|
||||||
|
* [Client] Fixed a memory leak when TCP based connections trying to reconnect with an offline server.
|
||||||
* [Client] Fixed an issue when connecting to an invalid host (format).
|
* [Client] Fixed an issue when connecting to an invalid host (format).
|
||||||
* [ManagedClient] Added method to trigger PING/PONG manually (connection check etc.).
|
* [ManagedClient] Added method to trigger PING/PONG manually (connection check etc.).
|
||||||
* [MQTTnet.AspNetCore] improved compatibility with AspNetCore 3.1.
|
* [MQTTnet.AspNetCore] improved compatibility with AspNetCore 3.1.
|
||||||
|
|||||||
@@ -143,7 +143,7 @@ namespace MQTTnet.Implementations
|
|||||||
{
|
{
|
||||||
_networkStream?.Dispose();
|
_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()))
|
using (cancellationToken.Register(() => _socket.Dispose()))
|
||||||
{
|
{
|
||||||
cancellationToken.ThrowIfCancellationRequested();
|
cancellationToken.ThrowIfCancellationRequested();
|
||||||
|
|||||||
@@ -46,56 +46,63 @@ namespace MQTTnet.Implementations
|
|||||||
|
|
||||||
public async Task ConnectAsync(CancellationToken cancellationToken)
|
public async Task ConnectAsync(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
CrossPlatformSocket socket;
|
CrossPlatformSocket socket = null;
|
||||||
|
try
|
||||||
if (_options.AddressFamily == AddressFamily.Unspecified)
|
|
||||||
{
|
{
|
||||||
socket = new CrossPlatformSocket();
|
if (_options.AddressFamily == AddressFamily.Unspecified)
|
||||||
}
|
|
||||||
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
|
|
||||||
{
|
{
|
||||||
await sslStream.AuthenticateAsClientAsync(_options.Server, LoadCertificates(), _options.TlsOptions.SslProtocol, !_options.TlsOptions.IgnoreCertificateRevocationErrors).ConfigureAwait(false);
|
socket = new CrossPlatformSocket();
|
||||||
}
|
}
|
||||||
catch
|
else
|
||||||
{
|
{
|
||||||
sslStream.Dispose();
|
socket = new CrossPlatformSocket(_options.AddressFamily);
|
||||||
throw;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_stream = sslStream;
|
socket.ReceiveBufferSize = _options.BufferSize;
|
||||||
}
|
socket.SendBufferSize = _options.BufferSize;
|
||||||
else
|
socket.NoDelay = _options.NoDelay;
|
||||||
{
|
|
||||||
_stream = networkStream;
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
public Task DisconnectAsync(CancellationToken cancellationToken)
|
||||||
|
|||||||
Reference in New Issue
Block a user