@@ -29,10 +29,12 @@ import Control.Concurrent (forkIOWithUnmask, killThread, ThreadId, threadDelay
|
29 | 29 | , mkWeakThreadId)
|
30 | 30 | import Control.Concurrent.STM.TQueue (TQueue, writeTQueue, newTQueueIO)
|
31 | 31 | import Control.Concurrent.STM (atomically)
|
32 |
| -import Control.Exception (SomeException, bracketOnError, catch, mask_) |
| 32 | +import Control.Exception (SomeException, bracketOnError, catch, mask_, |
| 33 | +catch, throwIO) |
33 | 34 | import GHC.Conc (labelThread)
|
34 | 35 | import Crypto.Hash (hash, Digest, MD5)
|
35 | 36 | import System.Mem.Weak (Weak, deRefWeak)
|
| 37 | +import System.Socket (eBadFileDescriptor) |
36 | 38 | import qualified Data.HashMap.Strict as HM
|
37 | 39 | import qualified Data.ByteString as B
|
38 | 40 | import qualified Data.ByteString.Char8 as BS(pack, unpack)
|
@@ -131,7 +133,6 @@ sendMessage rawConn msg = void $
|
131 | 133 | sendEncode :: AbsConnection c -> Encode -> IO ()
|
132 | 134 | sendEncode conn = void . rSend (connRawConnection conn) . runEncode
|
133 | 135 |
|
134 |
| - |
135 | 136 | connectWith
|
136 | 137 | :: ConnectionSettings
|
137 | 138 | -> (RawConnection -> ConnectionParameters -> IO (AbsConnection c))
|
@@ -251,18 +252,19 @@ parseParameters action str = Right <$> do
|
251 | 252 | handshakeTls :: RawConnection -> IO ()
|
252 | 253 | handshakeTls _ = pure ()
|
253 | 254 |
|
254 |
| --- | Public |
255 |
| --- TODO add termination |
| 255 | +-- | Closes connection. Does not throw exceptions when socket is closed. |
256 | 256 | close :: AbsConnection c -> IO ()
|
257 | 257 | close conn = do
|
258 | 258 | maybe (pure ()) killThread =<< deRefWeak (connReceiverThread conn)
|
| 259 | +sendMessage (connRawConnection conn) Terminate `catch` handlerEx |
259 | 260 | rClose $ connRawConnection conn
|
| 261 | +where |
| 262 | +handlerEx e | e == eBadFileDescriptor = pure () |
| 263 | +| otherwise = throwIO e |
260 | 264 |
|
261 | 265 | -- | Any exception prevents thread from future work.
|
262 | 266 | receiverThread :: RawConnection -> InDataChan -> IO ()
|
263 | 267 | receiverThread rawConn dataChan = loopExtractDataRows
|
264 |
| --- TODO |
265 |
| --- dont append strings. Allocate buffer manually and use unsafeReceive |
266 | 268 | (\bs -> rReceive rawConn bs 4096)
|
267 | 269 | (writeChan dataChan . Right)
|
268 | 270 |
|
@@ -279,8 +281,6 @@ receiverThreadCommon rawConn chan msgFilter ntfHandler = go ""
|
279 | 281 | (rest, msg) <- decodeNextServerMessage bs readMoreAction
|
280 | 282 | handler msg >> go rest
|
281 | 283 |
|
282 |
| --- TODO |
283 |
| --- dont append strings. Allocate buffer manually and use unsafeReceive |
284 | 284 | readMoreAction = (\bs -> rReceive rawConn bs 4096)
|
285 | 285 | handler msg = do
|
286 | 286 | disIfNotification msg ntfHandler
|
|
0 commit comments