diff options
author | James Crayne <jim.crayne@gmail.com> | 2017-11-22 21:25:25 +0000 |
---|---|---|
committer | James Crayne <jim.crayne@gmail.com> | 2017-11-22 21:25:25 +0000 |
commit | 27cb2cbe0338c19fd4f8a22b4453086288dae5c4 (patch) | |
tree | f2e605f0df5543517331973819a0fda70959dddb /src/Network/Tox/Crypto/Handlers.hs | |
parent | 941a079c06f80991af521f433364fc8a72a93d8a (diff) |
dequeue thread & handle session close
Diffstat (limited to 'src/Network/Tox/Crypto/Handlers.hs')
-rw-r--r-- | src/Network/Tox/Crypto/Handlers.hs | 24 |
1 files changed, 12 insertions, 12 deletions
diff --git a/src/Network/Tox/Crypto/Handlers.hs b/src/Network/Tox/Crypto/Handlers.hs index 4f53888b..50dd8c67 100644 --- a/src/Network/Tox/Crypto/Handlers.hs +++ b/src/Network/Tox/Crypto/Handlers.hs | |||
@@ -28,7 +28,6 @@ import qualified Data.Set as Set | |||
28 | import qualified Data.Array.Unboxed as A | 28 | import qualified Data.Array.Unboxed as A |
29 | import SensibleDir | 29 | import SensibleDir |
30 | import System.FilePath | 30 | import System.FilePath |
31 | import System.IO | ||
32 | import System.IO.Temp | 31 | import System.IO.Temp |
33 | import System.Environment | 32 | import System.Environment |
34 | import System.Directory | 33 | import System.Directory |
@@ -108,6 +107,7 @@ data NetCryptoSession = NCrypto | |||
108 | , ncView :: TVar SessionView | 107 | , ncView :: TVar SessionView |
109 | , ncPacketQueue :: PacketQueue CryptoData | 108 | , ncPacketQueue :: PacketQueue CryptoData |
110 | , ncBufferStart :: TVar Word32 | 109 | , ncBufferStart :: TVar Word32 |
110 | , ncDequeueThread :: Maybe ThreadId | ||
111 | , ncPingMachine :: Maybe PingMachine | 111 | , ncPingMachine :: Maybe PingMachine |
112 | , ncOutgoingQueue :: PQ.PacketOutQueue (State,Nonce24,TVar MsgOutMap) | 112 | , ncOutgoingQueue :: PQ.PacketOutQueue (State,Nonce24,TVar MsgOutMap) |
113 | CryptoMessage | 113 | CryptoMessage |
@@ -364,24 +364,23 @@ freshCryptoSession sessions | |||
364 | , ncView = ncView0 | 364 | , ncView = ncView0 |
365 | , ncPacketQueue = pktq | 365 | , ncPacketQueue = pktq |
366 | , ncBufferStart = bufstart | 366 | , ncBufferStart = bufstart |
367 | , ncDequeueThread = Nothing -- error "you want the NetCrypto-Dequeue thread id, but is it started?" | ||
367 | , ncPingMachine = Nothing -- error "you want the NetCrypto-PingMachine, but is it started?" | 368 | , ncPingMachine = Nothing -- error "you want the NetCrypto-PingMachine, but is it started?" |
368 | , ncOutgoingQueue = pktoq | 369 | , ncOutgoingQueue = pktoq |
369 | } | 370 | } |
370 | 371 | -- launch dequeue thread | |
371 | hooks <- atomically $ readTVar (announceNewSessionHooks sessions) | 372 | threadid <- forkIO $ do |
372 | 373 | tid <- myThreadId | |
373 | -- Dequeue thread: | 374 | labelThread tid ("NetCryptoDequeue." ++ show (key2id remotePublicKey)) |
374 | -- | 375 | fix $ \loop -> do |
375 | -- Hopefully, somebody will launch a thread to repeatedly call | 376 | cd <- atomically $ PQ.dequeue pktq |
376 | -- 'receiveCrypto' in order to dequeue messages from ncPacketQueue. | 377 | _ <- runCryptoHook (netCryptoSession0 {ncDequeueThread=Just tid}) cd |
377 | when (null hooks) $ do | 378 | loop |
378 | hPutStrLn stderr "Warning: Missing new-session handler. Lost session!" | ||
379 | |||
380 | -- launch ping thread | 379 | -- launch ping thread |
381 | fuzz <- randomRIO (0,2000) | 380 | fuzz <- randomRIO (0,2000) |
382 | pingMachine <- forkPingMachine ("NetCrypto." ++ show (key2id remotePublicKey)) (15000 + fuzz) 2000 | 381 | pingMachine <- forkPingMachine ("NetCrypto." ++ show (key2id remotePublicKey)) (15000 + fuzz) 2000 |
383 | -- update session with thread ids | 382 | -- update session with thread ids |
384 | let netCryptoSession = netCryptoSession0 {ncPingMachine=Just pingMachine} | 383 | let netCryptoSession = netCryptoSession0 {ncDequeueThread=Just threadid, ncPingMachine=Just pingMachine} |
385 | -- add this session to the lookup maps | 384 | -- add this session to the lookup maps |
386 | atomically $ do | 385 | atomically $ do |
387 | modifyTVar allsessions (Map.insert addr netCryptoSession) | 386 | modifyTVar allsessions (Map.insert addr netCryptoSession) |
@@ -390,6 +389,7 @@ freshCryptoSession sessions | |||
390 | Nothing -> modifyTVar allsessionsByKey (Map.insert remotePublicKey [netCryptoSession]) | 389 | Nothing -> modifyTVar allsessionsByKey (Map.insert remotePublicKey [netCryptoSession]) |
391 | Just xs -> modifyTVar allsessionsByKey (Map.insert remotePublicKey (netCryptoSession:xs)) | 390 | Just xs -> modifyTVar allsessionsByKey (Map.insert remotePublicKey (netCryptoSession:xs)) |
392 | -- run announceNewSessionHooks | 391 | -- run announceNewSessionHooks |
392 | hooks <- atomically $ readTVar (announceNewSessionHooks sessions) | ||
393 | flip fix (hooks,netCryptoSession) $ \loop (hooks,session) -> | 393 | flip fix (hooks,netCryptoSession) $ \loop (hooks,session) -> |
394 | case hooks of | 394 | case hooks of |
395 | [] -> return () | 395 | [] -> return () |