From bbc451970f54f675bd5f8b7ed2fe9bbed97a1d23 Mon Sep 17 00:00:00 2001 From: James Crayne Date: Mon, 18 Jun 2018 03:13:35 +0000 Subject: netcrypto ping thread wip --- src/Network/Tox/Crypto/Handlers.hs | 20 +++++++++++++++++++- src/Network/Tox/Crypto/Transport.hs | 2 +- 2 files changed, 20 insertions(+), 2 deletions(-) (limited to 'src/Network/Tox') diff --git a/src/Network/Tox/Crypto/Handlers.hs b/src/Network/Tox/Crypto/Handlers.hs index 3336052b..5192e180 100644 --- a/src/Network/Tox/Crypto/Handlers.hs +++ b/src/Network/Tox/Crypto/Handlers.hs @@ -768,9 +768,21 @@ runUponHandshake netCryptoSession0 addr pktoq = do sendSessionPacket sessions addr pkt loop dput XNetCrypto $ "runUponHandshake: " ++ show threadidOutgoing ++ " = NetCryptoDequeueOutgoing." ++ show (key2id remotePublicKey) - -- launch ping thread + -- launch ping Machine thread fuzz <- randomRIO (0,2000) pingMachine <- forkPingMachine ("NetCrypto." ++ show (key2id remotePublicKey)) (15000 + fuzz) 2000 + pingThreadId <- forkIO $ do + tid <- myThreadId + event <- atomically $ pingWait pingMachine + labelThread tid ("NetCryptoPingSender." ++ show (key2id remotePublicKey) ++ "." ++ show (ncSessionId netCryptoSession0)) + case event of + PingIdle -> do + dput XNetCrypto $ "pingThread (session: " ++ show (ncSessionId netCryptoSession0) ++ ") PingIdle" + lr <- sendPing crypto netCryptoSession0 + case lr of + Left s -> dput XNetCrypto $ "(pingThread session: " ++ show (ncSessionId netCryptoSession0) ++ ") " ++ s + Right _ -> return () + PingTimeOut -> dput XNetCrypto $ "(pingThread session: " ++ show (ncSessionId netCryptoSession0) ++ ") PingTimeOut TODO" -- update session with thread ids let netCryptoSession = netCryptoSession0 {ncDequeueThread=Just threadid, ncPingMachine=Just pingMachine} -- add this session to the lookup maps @@ -1173,6 +1185,12 @@ sendCrypto crypto session updateLocal cm = do PQ.OGFull -> return (Left "Outgoing packet buffer is full") PQ.OGEncodeFail -> return (Left "Failed to encode outgoing packet") +sendPing :: TransportCrypto -> NetCryptoSession -> IO (Either String ()) +sendPing crypto session = do + let cm=OneByte PING + addMsgToLastN False (cm ^. messageType) session (Out cm) + sendCrypto crypto session (return ()) (OneByte PING) + sendOnline :: TransportCrypto -> NetCryptoSession -> IO (Either String ()) sendOnline crypto session = do let cm=OneByte ONLINE diff --git a/src/Network/Tox/Crypto/Transport.hs b/src/Network/Tox/Crypto/Transport.hs index 73143e35..ce3bafee 100644 --- a/src/Network/Tox/Crypto/Transport.hs +++ b/src/Network/Tox/Crypto/Transport.hs @@ -734,7 +734,7 @@ data MessageID -- First byte indicates data | UnspecifiedPacket013 | UnspecifiedPacket014 | UnspecifiedPacket015 - | MessengerLossless016 -- ^ 16+ reserved for Messenger usage (lossless packets) + | PING -- ^ 16+ reserved for Messenger usage (lossless packets) | MessengerLossless017 | MessengerLossless018 | MessengerLossless019 -- cgit v1.2.3