From ab842462cfd8e3d29881c117662a5f84372b746e Mon Sep 17 00:00:00 2001 From: Joscha Date: Wed, 7 Feb 2018 21:34:40 +0000 Subject: [PATCH] Close Connection on exception while connecting --- src/EuphApi/Connection.hs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/EuphApi/Connection.hs b/src/EuphApi/Connection.hs index d5fc570..364309c 100644 --- a/src/EuphApi/Connection.hs +++ b/src/EuphApi/Connection.hs @@ -51,7 +51,7 @@ type RecvQueue = TBQueue Recv type EventQueue = TBQueue (Maybe Event) -- 'Nothing' ends the event stream type LockedFlag = TVar Bool --- @ A connection to a room on euphoria. +-- | A connection to a room on euphoria. -- -- __TODO__: Add more information on usage etc. data Connection = Connection LockedFlag SendQueue EventQueue @@ -71,9 +71,15 @@ startEuphConnection host room = do eventQueue <- atomically $ newTBQueue 10 locked <- atomically $ newTVar False let euphCon = Connection locked sendQueue eventQueue - -- TODO: catch failed connection and send note to qEvent - void $ async $ WSS.runSecureClient host 443 ("/room/" ++ room ++ "/ws") (recvClient euphCon recvQueue) + void + $ forkIO + $ handle (handleException eventQueue) + $ WSS.runSecureClient host 443 ("/room/" ++ room ++ "/ws") + $ recvClient euphCon recvQueue return euphCon + where + handleException :: EventQueue -> WS.HandshakeException -> IO () + handleException qEvent _ = atomically $ writeTBQueue qEvent Nothing {- - Fetch thread