mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-21 12:16:02 +08:00
Refactor TCP channel.
This commit is contained in:
@@ -92,50 +92,9 @@ namespace MQTTnet.Implementations
|
|||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
try
|
TryDispose(_readStream, () => _readStream = null);
|
||||||
{
|
TryDispose(_writeStream, () => _writeStream = null);
|
||||||
_readStream?.Dispose();
|
TryDispose(_socket, () => _socket = null);
|
||||||
}
|
|
||||||
catch (ObjectDisposedException)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
catch (NullReferenceException)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
_readStream = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
_writeStream?.Dispose();
|
|
||||||
}
|
|
||||||
catch (ObjectDisposedException)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
catch (NullReferenceException)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
_writeStream = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
_socket?.Dispose();
|
|
||||||
}
|
|
||||||
catch (ObjectDisposedException)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
catch (NullReferenceException)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
_socket = null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Certificate LoadCertificate(MqttClientTcpOptions options)
|
private static Certificate LoadCertificate(MqttClientTcpOptions options)
|
||||||
@@ -187,6 +146,24 @@ namespace MQTTnet.Implementations
|
|||||||
_readStream = _socket.InputStream.AsStreamForRead(_bufferSize);
|
_readStream = _socket.InputStream.AsStreamForRead(_bufferSize);
|
||||||
_writeStream = _socket.OutputStream.AsStreamForWrite(_bufferSize);
|
_writeStream = _socket.OutputStream.AsStreamForWrite(_bufferSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void TryDispose(IDisposable disposable, Action afterDispose)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
disposable?.Dispose();
|
||||||
|
}
|
||||||
|
catch (ObjectDisposedException)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
catch (NullReferenceException)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
afterDispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@@ -84,35 +84,8 @@ namespace MQTTnet.Implementations
|
|||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
try
|
TryDispose(_stream, () => _stream = null);
|
||||||
{
|
TryDispose(_socket, () => _socket = null);
|
||||||
_stream?.Dispose();
|
|
||||||
}
|
|
||||||
catch (ObjectDisposedException)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
catch (NullReferenceException)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
_stream = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
_socket?.Dispose();
|
|
||||||
}
|
|
||||||
catch (ObjectDisposedException)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
catch (NullReferenceException)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
_socket = null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool InternalUserCertificateValidationCallback(object sender, X509Certificate x509Certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
|
private bool InternalUserCertificateValidationCallback(object sender, X509Certificate x509Certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
|
||||||
@@ -173,6 +146,24 @@ namespace MQTTnet.Implementations
|
|||||||
_stream = new NetworkStream(_socket, true);
|
_stream = new NetworkStream(_socket, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void TryDispose(IDisposable disposable, Action afterDispose)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
disposable?.Dispose();
|
||||||
|
}
|
||||||
|
catch (ObjectDisposedException)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
catch (NullReferenceException)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
afterDispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user