From d9159028e812f2855558ba183d3c11040d98e408 Mon Sep 17 00:00:00 2001 From: Joe Crayne Date: Fri, 7 Sep 2018 23:27:49 -0400 Subject: Added some minor helper utiltities to PSQ and Network.Tox.Session. --- src/Data/Wrapper/PSQ.hs | 13 +++++++++++-- src/Data/Wrapper/PSQInt.hs | 6 ++---- src/Network/Tox/Session.hs | 18 ++++++++++++++++-- 3 files changed, 29 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/Data/Wrapper/PSQ.hs b/src/Data/Wrapper/PSQ.hs index 5d32e16c..745e556b 100644 --- a/src/Data/Wrapper/PSQ.hs +++ b/src/Data/Wrapper/PSQ.hs @@ -22,9 +22,11 @@ fold' f a q = PSQueue.foldr f' a q -- import Data.OrdPSQ as OrdPSQ hiding (insert, map, singleton, minView) -- import qualified Data.OrdPSQ as OrdPSQ -import Data.HashPSQ as HashPSQ hiding (insert, map, singleton, minView) -import qualified Data.HashPSQ as Q import Data.Hashable +import qualified Data.HashPSQ as Q + ;import Data.HashPSQ as HashPSQ hiding (insert, map, minView, + singleton) +import Data.Time.Clock.POSIX (POSIXTime) type PSQ' k p v = HashPSQ k p v type PSQ k p = PSQ' k p () @@ -78,4 +80,11 @@ minView :: (PSQKey k, Ord p) => PSQ' k p v -> Maybe (Binding' k p v, PSQ' k p v) minView q = fmap (\(k,p,v,q') -> (Binding k v p, q')) $ Q.minView q {-# INLINE minView #-} + +-- | Utility to convert a 'POSIXTime' delta into microseconds suitable for +-- passing to 'threadDelay'. +toMicroseconds :: POSIXTime -> Int +toMicroseconds = round . (* 1000) . (* 1000) + + #endif diff --git a/src/Data/Wrapper/PSQInt.hs b/src/Data/Wrapper/PSQInt.hs index c61b7ab6..5badb8b2 100644 --- a/src/Data/Wrapper/PSQInt.hs +++ b/src/Data/Wrapper/PSQInt.hs @@ -19,12 +19,10 @@ fold' f a q = PSQueue.foldr f' a q #else ( module Data.Wrapper.PSQInt , module IntPSQ - , pattern (:->) - , key - , prio + , module Data.Wrapper.PSQ ) where -import Data.Wrapper.PSQ (Binding, pattern (:->), key, prio) +import Data.Wrapper.PSQ (Binding, pattern (:->), key, prio, toMicroseconds) import Data.IntPSQ as IntPSQ hiding (insert, map, singleton, minView) import qualified Data.IntPSQ as Q diff --git a/src/Network/Tox/Session.hs b/src/Network/Tox/Session.hs index e8be4d01..525338b2 100644 --- a/src/Network/Tox/Session.hs +++ b/src/Network/Tox/Session.hs @@ -4,6 +4,8 @@ module Network.Tox.Session ( SessionParams(..) , SessionKey , Session(..) + , sTheirUserKey + , sClose , handshakeH ) where @@ -11,7 +13,7 @@ import Control.Concurrent.STM import Control.Monad import Data.Functor.Identity import Data.Word -import Network.Socket +import Network.Socket (SockAddr) import Crypto.Tox import Data.PacketBuffer (PacketInboundEvent (..)) @@ -21,7 +23,7 @@ import Network.Lossless import Network.QueryResponse import Network.SessionTransports import Network.Tox.Crypto.Transport -import Network.Tox.DHT.Transport (Cookie,key2id) +import Network.Tox.DHT.Transport (Cookie (..), key2id, longTermKey) import Network.Tox.Handshake -- | Alias for 'SecretKey' to document that it is used as the temporary Tox @@ -79,6 +81,18 @@ data Session = Session , sSessionID :: Int } +-- | Helper to obtain the remote ToxID key from the locally-issued cookie +-- associated with the session. +sTheirUserKey :: Session -> PublicKey +sTheirUserKey s = longTermKey $ runIdentity cookie + where + Cookie _ cookie = handshakeCookie (sReceivedHandshake s) + +-- | Helper to close the 'Transport' associated with a session. +sClose :: Session -> IO () +sClose s = closeTransport (sTransport s) + + -- | Call this whenever a new handshake arrives so that a session is -- negotiated. It always returns Nothing which makes it convenient to use with -- 'Network.QueryResponse.addHandler'. -- cgit v1.2.3