Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add stopClient to close connection without sending disconnect message #39

Merged
merged 1 commit into from
Jun 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/Network/MQTT/Client.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module Network.MQTT.Client (
-- * Running and waiting for the client.
waitForClient,
connectURI, isConnected,
disconnect, normalDisconnect,
disconnect, normalDisconnect, stopClient,
-- * General client interactions.
subscribe, unsubscribe, publish, publishq, pubAliased,
svrProps, connACK, MQTTException(..),
Expand Down Expand Up @@ -373,6 +373,15 @@ waitForClient c@MQTTClient{..} = flip E.finally (stopCallbackThread c) $ do
void . traverse wait =<< readTVarIO _ct
maybe (pure ()) E.throwIO =<< atomically (stateX c Stopped)

-- | Stops the client and closes the connection without sending a DISCONNECT
-- message to the broker. This will cause the last-will message to be delivered
-- by the broker if it has been defined.
stopClient :: MQTTClient -> IO ()
stopClient MQTTClient{..} = do
void . traverse cancel =<< readTVarIO _ct
void . traverse cancel =<< readTVarIO _cbHandle
atomically (writeTVar _st Stopped)

stateX :: MQTTClient -> ConnState -> STM (Maybe E.SomeException)
stateX MQTTClient{..} want = f <$> readTVar _st

Expand Down