Skip to content

Commit

Permalink
Improving dispose in DeviceClient and ProvisioningDeviceClient (#304)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ellerbach authored Oct 23, 2023
1 parent 41d021b commit 087d7b5
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,12 @@
</Reference>
<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>
<Private>True</Private>
</Reference>
<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>
<Private>True</Private>
</Reference>
<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>
<Private>True</Private>
</Reference>
<Reference Include="nanoFramework.Runtime.Events">
<HintPath>..\packages\nanoFramework.Runtime.Events.1.11.6\lib\nanoFramework.Runtime.Events.dll</HintPath>
Expand Down
25 changes: 23 additions & 2 deletions nanoFramework.Azure.Devices.Client/DeviceClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public class DeviceClient : IDisposable
private readonly object _lock = new object();
private Timer _timerTokenRenew;
private bool _hasClientCertificate;
private bool _isDisposed = false;
private bool _isClosed = true;

/// <summary>
/// Device twin updated event.
Expand Down Expand Up @@ -359,6 +361,7 @@ public bool Open()
_timerTokenRenew = new Timer(TimerCallbackReconnect, null, new TimeSpan(23, 50, 0), TimeSpan.MaxValue);
}

_isClosed = !_mqttc.IsConnected;
return _mqttc.IsConnected;
}

Expand All @@ -382,6 +385,11 @@ private void TimerCallbackReconnect(object state)
/// </summary>
public void Close()
{
if (_isClosed)
{
return;
}

if (_mqttc != null)
{
if (_mqttc.IsConnected)
Expand All @@ -394,13 +402,21 @@ public void Close()
}

_mqttc.Disconnect();
_mqttc.Close();
try
{
_mqttc.Close();
}
catch
{
// Nothing on purpose, we want to make sure we can continue
}

// Make sure all get disconnected, cleared
Thread.Sleep(1000);
}

_timerTokenRenew?.Dispose();
_isClosed = true;
}

/// <summary>
Expand Down Expand Up @@ -804,6 +820,12 @@ private void ClientConnectionClosed(object sender, EventArgs e)
/// <inheritdoc/>
public void Dispose()
{
if (_isDisposed)
{
return;
}

_isDisposed = true;
if (_mqttc != null)
{
// Making sure we unregister the registered events
Expand All @@ -815,7 +837,6 @@ public void Dispose()
while (_mqttc.IsConnected)
{
Thread.Sleep(100);

}

// Cleaning
Expand Down
17 changes: 16 additions & 1 deletion nanoFramework.Azure.Devices.Client/ProvisioningDeviceClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class ProvisioningDeviceClient : IDisposable
private ProvisioningRegistrationStatusType _status = ProvisioningRegistrationStatusType.Unassigned;
private DeviceRegistrationResult _result = null;
private bool _isMessageProcessed = false;
private bool _isDisposed = false;

/// <summary>
/// Creates an instance of the Device Provisioning Client.
Expand Down Expand Up @@ -228,9 +229,17 @@ private void CleanAll()
{
// We need to clean everything now
_mqttc.MqttMsgPublishReceived -= ClientMqttMsgReceived;
_mqttc.Unsubscribe(new[] {
try
{
_mqttc.Unsubscribe(new[] {
DpsSubscription
});
}
catch
{
// Nothing on purpose, just cleaning
}

_mqttc.Disconnect();
while (_mqttc.IsConnected)
{
Expand Down Expand Up @@ -337,6 +346,12 @@ private ProvisioningRegistrationStatusType AssignStatus(string status)
/// <inheritdoc/>
public void Dispose()
{
if(_isDisposed)
{
return;
}

_isDisposed = true;
if (_mqttc != null)
{
CleanAll();
Expand Down

0 comments on commit 087d7b5

Please sign in to comment.