Improving dispose in DeviceClient and ProvisioningDeviceClient (#304)

This commit is contained in:
Laurent Ellerbach 2023-10-23 11:03:54 +02:00 коммит произвёл GitHub
Родитель 41d021b722
Коммит 087d7b564e
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 39 добавлений и 6 удалений

Просмотреть файл

@ -69,15 +69,12 @@
</Reference> </Reference>
<Reference Include="nanoFramework.Json, Version=2.2.101.0, Culture=neutral, PublicKeyToken=c07d481e9758c731"> <Reference Include="nanoFramework.Json, Version=2.2.101.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<HintPath>..\packages\nanoFramework.Json.2.2.101\lib\nanoFramework.Json.dll</HintPath> <HintPath>..\packages\nanoFramework.Json.2.2.101\lib\nanoFramework.Json.dll</HintPath>
<Private>True</Private>
</Reference> </Reference>
<Reference Include="nanoFramework.M2Mqtt, Version=5.1.96.0, Culture=neutral, PublicKeyToken=c07d481e9758c731"> <Reference Include="nanoFramework.M2Mqtt, Version=5.1.96.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<HintPath>..\packages\nanoFramework.M2Mqtt.5.1.96\lib\nanoFramework.M2Mqtt.dll</HintPath> <HintPath>..\packages\nanoFramework.M2Mqtt.5.1.96\lib\nanoFramework.M2Mqtt.dll</HintPath>
<Private>True</Private>
</Reference> </Reference>
<Reference Include="nanoFramework.M2Mqtt.Core, Version=0.0.0.0, Culture=neutral, PublicKeyToken=c07d481e9758c731"> <Reference Include="nanoFramework.M2Mqtt.Core, Version=0.0.0.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<HintPath>..\packages\nanoFramework.M2Mqtt.5.1.96\lib\nanoFramework.M2Mqtt.Core.dll</HintPath> <HintPath>..\packages\nanoFramework.M2Mqtt.5.1.96\lib\nanoFramework.M2Mqtt.Core.dll</HintPath>
<Private>True</Private>
</Reference> </Reference>
<Reference Include="nanoFramework.Runtime.Events"> <Reference Include="nanoFramework.Runtime.Events">
<HintPath>..\packages\nanoFramework.Runtime.Events.1.11.6\lib\nanoFramework.Runtime.Events.dll</HintPath> <HintPath>..\packages\nanoFramework.Runtime.Events.1.11.6\lib\nanoFramework.Runtime.Events.dll</HintPath>

Просмотреть файл

@ -50,6 +50,8 @@ namespace nanoFramework.Azure.Devices.Client
private readonly object _lock = new object(); private readonly object _lock = new object();
private Timer _timerTokenRenew; private Timer _timerTokenRenew;
private bool _hasClientCertificate; private bool _hasClientCertificate;
private bool _isDisposed = false;
private bool _isClosed = true;
/// <summary> /// <summary>
/// Device twin updated event. /// Device twin updated event.
@ -359,6 +361,7 @@ namespace nanoFramework.Azure.Devices.Client
_timerTokenRenew = new Timer(TimerCallbackReconnect, null, new TimeSpan(23, 50, 0), TimeSpan.MaxValue); _timerTokenRenew = new Timer(TimerCallbackReconnect, null, new TimeSpan(23, 50, 0), TimeSpan.MaxValue);
} }
_isClosed = !_mqttc.IsConnected;
return _mqttc.IsConnected; return _mqttc.IsConnected;
} }
@ -382,6 +385,11 @@ namespace nanoFramework.Azure.Devices.Client
/// </summary> /// </summary>
public void Close() public void Close()
{ {
if (_isClosed)
{
return;
}
if (_mqttc != null) if (_mqttc != null)
{ {
if (_mqttc.IsConnected) if (_mqttc.IsConnected)
@ -394,13 +402,21 @@ namespace nanoFramework.Azure.Devices.Client
} }
_mqttc.Disconnect(); _mqttc.Disconnect();
try
{
_mqttc.Close(); _mqttc.Close();
}
catch
{
// Nothing on purpose, we want to make sure we can continue
}
// Make sure all get disconnected, cleared // Make sure all get disconnected, cleared
Thread.Sleep(1000); Thread.Sleep(1000);
} }
_timerTokenRenew?.Dispose(); _timerTokenRenew?.Dispose();
_isClosed = true;
} }
/// <summary> /// <summary>
@ -804,6 +820,12 @@ namespace nanoFramework.Azure.Devices.Client
/// <inheritdoc/> /// <inheritdoc/>
public void Dispose() public void Dispose()
{ {
if (_isDisposed)
{
return;
}
_isDisposed = true;
if (_mqttc != null) if (_mqttc != null)
{ {
// Making sure we unregister the registered events // Making sure we unregister the registered events
@ -815,7 +837,6 @@ namespace nanoFramework.Azure.Devices.Client
while (_mqttc.IsConnected) while (_mqttc.IsConnected)
{ {
Thread.Sleep(100); Thread.Sleep(100);
} }
// Cleaning // Cleaning

Просмотреть файл

@ -34,6 +34,7 @@ namespace nanoFramework.Azure.Devices.Provisioning.Client
private ProvisioningRegistrationStatusType _status = ProvisioningRegistrationStatusType.Unassigned; private ProvisioningRegistrationStatusType _status = ProvisioningRegistrationStatusType.Unassigned;
private DeviceRegistrationResult _result = null; private DeviceRegistrationResult _result = null;
private bool _isMessageProcessed = false; private bool _isMessageProcessed = false;
private bool _isDisposed = false;
/// <summary> /// <summary>
/// Creates an instance of the Device Provisioning Client. /// Creates an instance of the Device Provisioning Client.
@ -228,9 +229,17 @@ namespace nanoFramework.Azure.Devices.Provisioning.Client
{ {
// We need to clean everything now // We need to clean everything now
_mqttc.MqttMsgPublishReceived -= ClientMqttMsgReceived; _mqttc.MqttMsgPublishReceived -= ClientMqttMsgReceived;
try
{
_mqttc.Unsubscribe(new[] { _mqttc.Unsubscribe(new[] {
DpsSubscription DpsSubscription
}); });
}
catch
{
// Nothing on purpose, just cleaning
}
_mqttc.Disconnect(); _mqttc.Disconnect();
while (_mqttc.IsConnected) while (_mqttc.IsConnected)
{ {
@ -337,6 +346,12 @@ namespace nanoFramework.Azure.Devices.Provisioning.Client
/// <inheritdoc/> /// <inheritdoc/>
public void Dispose() public void Dispose()
{ {
if(_isDisposed)
{
return;
}
_isDisposed = true;
if (_mqttc != null) if (_mqttc != null)
{ {
CleanAll(); CleanAll();