From 8fe574a8f1f2f71b68c89aeacccd05d084a4003d Mon Sep 17 00:00:00 2001 From: James Crayne Date: Mon, 28 May 2018 19:54:59 +0000 Subject: ncLastNMsgs is now CyclicBuffer type --- src/Network/Tox/Crypto/Handlers.hs | 23 +++++------------------ 1 file changed, 5 insertions(+), 18 deletions(-) (limited to 'src/Network/Tox/Crypto') diff --git a/src/Network/Tox/Crypto/Handlers.hs b/src/Network/Tox/Crypto/Handlers.hs index ee8af399..92cb19b8 100644 --- a/src/Network/Tox/Crypto/Handlers.hs +++ b/src/Network/Tox/Crypto/Handlers.hs @@ -192,17 +192,12 @@ data NetCryptoSession = NCrypto CryptoMessage (CryptoPacket Encrypted) CryptoData - , ncLastNMsgs :: PacketQueue (Bool{-Handled?-},(ViewSnapshot,InOrOut CryptoMessage)) + , ncLastNMsgs :: CyclicBuffer (Bool{-Handled?-},(ViewSnapshot,InOrOut CryptoMessage)) -- ^ cyclic buffer, holds the last N non-handshake crypto messages -- even if there is no attached user interface. , ncListeners :: TVar (IntMap.IntMap (ListenerType,TMChan CryptoMessage)) -- ^ user interfaces may "listen" by inserting themselves into this map -- with a unique id and a new TChan, and then reading from the TChan - , ncMsgNumVar :: TVar Word32 - -- ^ The number of non-handshake crypto messages written to ncLastNMsgs total - , ncDropCntVar :: TVar Word32 - -- ^ The number of crypto messages that were overwritten in the ncLastNMsgs - -- before anybody got to see them. } data NetCryptoSessions = NCSessions @@ -450,7 +445,7 @@ freshCryptoSession sessions writeTVar ncMyPacketNonce0 n24plus1 return (return (f n24, n24, ncOutgoingIdMap0)) pktoq <- atomically $ PQ.newOutGoing pktq ncToWire toWireIO 0 (outboundQueueCapacity sessions) 0 - msgQ <- atomically (PQ.newOverwrite 10 0 :: STM (PacketQueue (Bool,(ViewSnapshot,InOrOut CryptoMessage)))) + lastNQ <- atomically (CB.new 10 0 :: STM (CyclicBuffer (Bool,(ViewSnapshot,InOrOut CryptoMessage)))) listeners <- atomically $ newTVar IntMap.empty msgNum <- atomically $ newTVar 0 dropNum <- atomically $ newTVar 0 @@ -478,10 +473,8 @@ freshCryptoSession sessions , ncDequeueThread = Nothing -- error "you want the NetCrypto-Dequeue thread id, but is it started?" , ncPingMachine = Nothing -- error "you want the NetCrypto-PingMachine, but is it started?" , ncOutgoingQueue = pktoq - , ncLastNMsgs = msgQ + , ncLastNMsgs = lastNQ , ncListeners = listeners - , ncMsgNumVar = msgNum - , ncDropCntVar = dropNum } -- launch dequeue thread threadid <- forkIO $ do @@ -886,17 +879,11 @@ hookHelper handledFlg typ session cm = do addMsgToLastN :: Bool -> MessageType -> NetCryptoSession -> InOrOut CryptoMessage -> IO () addMsgToLastN handledFlg typ session cm = do let lastNQ = ncLastNMsgs session - msgNumVar = ncMsgNumVar session - dropCntVar = ncDropCntVar session atomically $ do - num <- readTVar msgNumVar view <- readTVar (ncView session) snapshot <- viewSnapshot view - (wraps,offset) <- PQ.enqueue lastNQ num (handledFlg,(snapshot,cm)) - capacity <- PQ.getCapacity lastNQ - let dropped = wraps * capacity + offset - modifyTVar' msgNumVar (+1) - writeTVar dropCntVar dropped + num <- CB.getNextSequenceNum lastNQ + CB.enqueue lastNQ num (handledFlg,(snapshot,cm)) -- | use to add a single hook to a specific session. -- cgit v1.2.3