Skip to content
Snippets Groups Projects
Commit 7d1e4826 authored by Jens Nolte's avatar Jens Nolte
Browse files

Improve connection error handling

parent 97a1e454
No related branches found
No related tags found
No related merge requests found
...@@ -30,6 +30,10 @@ instance IsResourceManager (WaylandConnection s) where ...@@ -30,6 +30,10 @@ instance IsResourceManager (WaylandConnection s) where
instance IsDisposable (WaylandConnection s) where instance IsDisposable (WaylandConnection s) where
toDisposable connection = toDisposable connection.resourceManager toDisposable connection = toDisposable connection.resourceManager
data SocketClosed = SocketClosed
deriving stock Show
deriving anyclass Exception
newWaylandConnection :: forall s m. MonadResourceManager m => Callback s STM I_wl_display -> Socket -> m (WaylandConnection s) newWaylandConnection :: forall s m. MonadResourceManager m => Callback s STM I_wl_display -> Socket -> m (WaylandConnection s)
newWaylandConnection wlDisplayCallback socket = do newWaylandConnection wlDisplayCallback socket = do
protocolStateVar <- liftIO $ newTVarIO $ initialProtocolState wlDisplayCallback protocolStateVar <- liftIO $ newTVarIO $ initialProtocolState wlDisplayCallback
...@@ -48,8 +52,8 @@ newWaylandConnection wlDisplayCallback socket = do ...@@ -48,8 +52,8 @@ newWaylandConnection wlDisplayCallback socket = do
registerDisposeAction $ closeConnection connection registerDisposeAction $ closeConnection connection
runUnlimitedAsync do runUnlimitedAsync do
async $ liftIO $ waylandConnectionSendThread connection `catchAll` \ex -> traceIO (displayException ex) >> void (dispose resourceManager) connectionThread connection $ sendThread connection
async $ liftIO $ waylandConnectionReceiveThread connection `catchAll` \ex -> traceIO (displayException ex) >> void (dispose resourceManager) connectionThread connection $ receiveThread connection
-- HACK to send first message (queued internally) -- HACK to send first message (queued internally)
stepProtocol connection $ feedInput "" stepProtocol connection $ feedInput ""
...@@ -68,21 +72,26 @@ stepProtocol connection step = liftIO do ...@@ -68,21 +72,26 @@ stepProtocol connection step = liftIO do
Left ex -> throwM (ex :: SomeException) Left ex -> throwM (ex :: SomeException)
Right result -> pure result Right result -> pure result
connectionThread :: MonadAsync m => WaylandConnection s -> IO () -> m ()
connectionThread connection work = async_ $ liftIO $ work `catches` [ignoreCancelTask, handleAll]
where
ignoreCancelTask = Handler (throwM :: CancelTask -> IO a)
handleAll = Handler (\(ex :: SomeException) -> traceIO (displayException ex) >> void (dispose connection))
waylandConnectionSendThread :: WaylandConnection s -> IO () sendThread :: WaylandConnection s -> IO ()
waylandConnectionSendThread connection = forever do sendThread connection = forever do
bytes <- atomically $ takeTMVar connection.outboxVar bytes <- atomically $ takeTMVar connection.outboxVar
traceIO $ "Sending data: " <> show (BSL.length bytes) <> " bytes" traceIO $ "Sending " <> show (BSL.length bytes) <> " bytes"
SocketL.sendAll connection.socket bytes SocketL.sendAll connection.socket bytes
waylandConnectionReceiveThread :: WaylandConnection s -> IO () receiveThread :: WaylandConnection s -> IO ()
waylandConnectionReceiveThread connection = forever do receiveThread connection = forever do
bytes <- Socket.recv connection.socket 4096 bytes <- Socket.recv connection.socket 4096
when (BS.length bytes == 0) do when (BS.length bytes == 0) do
fail "Socket is closed" throwM SocketClosed
traceIO $ "Received " <> show (BS.length bytes) <> " bytes" traceIO $ "Received " <> show (BS.length bytes) <> " bytes"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment