From a33fd34516e405d29654dff85df235b3c26ab565 Mon Sep 17 00:00:00 2001 From: "jim@bo" Date: Thu, 21 Jun 2018 04:26:54 -0400 Subject: netCrypto packet-request thread wip --- src/Network/Tox/Crypto/Handlers.hs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'src') diff --git a/src/Network/Tox/Crypto/Handlers.hs b/src/Network/Tox/Crypto/Handlers.hs index ae8ce873..ea5c0f98 100644 --- a/src/Network/Tox/Crypto/Handlers.hs +++ b/src/Network/Tox/Crypto/Handlers.hs @@ -342,6 +342,10 @@ data NetCryptoSession = NCrypto -- ^ a buffer in which incoming packets may be stored out of order -- but from which they may be extracted in sequence, -- helps ensure lossless packets are processed in order + , ncRequestInterval :: TVar Int + -- ^ How long (in microseconds) to wait between packet requests + , ncRequestThread :: TVar (Maybe ThreadId) + -- ^ thread which sends packet requests , ncDequeueThread :: TVar (Maybe ThreadId) -- ^ when the thread which dequeues from ncPacketQueue -- is started, its ThreadId is stored here @@ -605,6 +609,8 @@ freshCryptoSession sessions dmsg $ "freshCryptoSession: Session ncTheirBaseNonce=" ++ show theirbasenonce dmsg $ "freshCryptoSession: My Session Public =" ++ show (key2id $ toPublic newsession) ncTheirSessionPublic0 <- newTVar (frmMaybe mbtheirSessionKey) + ncRequestInterval0 <- newTVar 250000 -- quarter of a second + ncRequestThread0 <- newTVar Nothing ncDequeueThread0 <- newTVar Nothing ncDequeueOutGoingThread0 <- newTVar Nothing ncPingMachine0 <- newTVar Nothing @@ -631,6 +637,8 @@ freshCryptoSession sessions , ncOutgoingIdMapEscapedLossless = losslessEscapeIdMap , ncView = ncView0 , ncPacketQueue = pktq + , ncRequestInterval = ncRequestInterval0 + , ncRequestThread = ncRequestThread0 , ncDequeueThread = ncDequeueThread0 , ncDequeueOutGoingThread = ncDequeueOutGoingThread0 , ncPingMachine = ncPingMachine0 @@ -737,6 +745,25 @@ runUponHandshake netCryptoSession0 addr pktoq = do _ <- runCryptoHook netCryptoSession0 (bufferData cd) loop dput XNetCrypto $ "runUponHandshake: " ++ show threadid ++ " = NetCryptoDequeue." ++ show (key2id remotePublicKey) ++ sidStr + -- launch request thread + -- (In terms of data dependency, this thread could be launched prior to handshake) + threadid <- forkIO $ do + tid <- myThreadId + atomically $ writeTVar (ncDequeueThread netCryptoSession0) (Just tid) + labelThread tid ("NetCryptoRequest." ++ show (key2id remotePublicKey) ++ sidStr) + fix $ \loop -> do + atomically (readTVar (ncRequestInterval netCryptoSession0)) >>= threadDelay + result <- atomically $ PQ.dequeueOrGetMissing pktq + case result of + Right cd -> do + dput XNetCrypto $ "Dequeued(Request Thread)::" ++ show (bufferData cd) ++ " now running hook..." + _ <- runCryptoHook netCryptoSession0 (bufferData cd) + return () + Left nums -> do + dput XNetCrypto $ "(Request Thread) Missing Packets detected:" ++ show nums + dput XNetCrypto $ "TODO: compose PacketRequest message and send it." + loop + dput XNetCrypto $ "runUponHandshake: " ++ show threadid ++ " = NetCryptoRequest." ++ show (key2id remotePublicKey) ++ sidStr -- launch dequeueOutgoing thread threadidOutgoing <- forkIO $ do tid <- myThreadId -- cgit v1.2.3