Skip to content

Commit

Permalink
Extended AfterDisconnect Support (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
pglombardo authored Sep 28, 2023
1 parent fe7d12f commit 394c89a
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 8 deletions.
3 changes: 2 additions & 1 deletion Examples/Reconnect/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,6 @@
}
}

Console.WriteLine("Disconnecting gracefully...");
Console.WriteLine("Disconnecting gracefully and waiting 5 seconds...");
await client.DisconnectAsync().ConfigureAwait(false);
await Task.Delay(5000).ConfigureAwait(false);
2 changes: 1 addition & 1 deletion Examples/Reconnect/Reconnect.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

<!-- Update the version to match -->
<ItemGroup>
<PackageReference Include="HiveMQtt" Version="0.4.0" />
<PackageReference Include="HiveMQtt" Version="0.4.1" />
</ItemGroup>

</Project>
4 changes: 2 additions & 2 deletions Source/HiveMQtt/Client/HiveMQClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,11 @@ public async Task<bool> DisconnectAsync(DisconnectOptions? options = null)
this.OnDisconnectSent -= eventHandler;
}

this.connectState = ConnectState.Disconnected;

// Close the socket
this.CloseSocket();

this.connectState = ConnectState.Disconnected;

// Clear the send queue
this.sendQueue.Clear();
return true;
Expand Down
4 changes: 2 additions & 2 deletions Source/HiveMQtt/Client/HiveMQClientSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,6 @@ internal async Task<bool> ConnectSocketAsync()

internal bool CloseSocket(bool? shutdownPipeline = true)
{
this.cancellationSource.Cancel();

if (shutdownPipeline == true)
{
// Shutdown the pipeline
Expand All @@ -150,6 +148,8 @@ internal bool CloseSocket(bool? shutdownPipeline = true)
this.socket?.Shutdown(SocketShutdown.Both);
this.socket?.Close();

this.cancellationSource.Cancel();

return true;
}
}
3 changes: 1 addition & 2 deletions Source/HiveMQtt/Client/HiveMQClientTrafficProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ private Task<bool> TrafficInflowProcessorAsync(CancellationToken cancellationTok
switch (this.connectState)
{
case ConnectState.Disconnecting:
case ConnectState.Disconnected:
// This disconnect was either user or broker initiated.
// Launch the AfterDisconnect event with a clean disconnect set to true.
this.AfterDisconnectEventLauncher(true);
Expand All @@ -364,8 +365,6 @@ private Task<bool> TrafficInflowProcessorAsync(CancellationToken cancellationTok
break;
case ConnectState.Connecting:
break;
case ConnectState.Disconnected:
break;
default:
break;
}
Expand Down

0 comments on commit 394c89a

Please sign in to comment.