From d0cad3e46bc798dfc9e5ef2f4483d9e637bf8a67 Mon Sep 17 00:00:00 2001 From: "jim@bo" Date: Thu, 21 Jun 2018 04:26:17 -0400 Subject: update PacketQueue in preparation for request thread --- src/Data/PacketQueue.hs | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/Data/PacketQueue.hs b/src/Data/PacketQueue.hs index 1e79b851..57845ae5 100644 --- a/src/Data/PacketQueue.hs +++ b/src/Data/PacketQueue.hs @@ -10,6 +10,7 @@ module Data.PacketQueue , new , newOverwrite , dequeue + , dequeueOrGetMissing , markButNotDequeue , enqueue , observeOutOfBand @@ -36,7 +37,7 @@ import Debug.Trace data PacketQueue a = PacketQueue { pktq :: TArray Word32 (Maybe a) - , seqno :: TVar Word32 + , seqno :: TVar Word32 -- (buffer_start) , qsize :: Word32 , buffend :: TVar Word32 -- on incoming, next packet they'll send + 1 , qOverWriteMode :: Bool -- TODO: Remove me, use Data.CyclicBuffer instead. @@ -85,28 +86,31 @@ newOverwrite capacity seqstart = do , qOverWriteMode = True } +-- | Update the packet queue given: +-- * packet queue +-- * the number of next lossless packet they intend to send you observeOutOfBand :: PacketQueue a -> Word32-> STM () -observeOutOfBand PacketQueue { seqno, qsize, buffend } no = do +observeOutOfBand PacketQueue { seqno, qsize, buffend } numberOfNextLosslessPacketThatTheyWillSend = do low <- readTVar seqno - let proj = no - low + let proj = numberOfNextLosslessPacketThatTheyWillSend - low -- Ignore packet if out of range. when ( proj < qsize) $ do - modifyTVar' buffend (\be -> if be - low <= proj then no + 1 else be) + modifyTVar' buffend (\be -> if be - low <= proj then numberOfNextLosslessPacketThatTheyWillSend + 1 else be) --- | If buffend < seqno then return expected packet numbers for all +-- | If seqno < buffend then return expected packet numbers for all -- the Nothings in the array between them. -- Otherwise, behave as 'dequeue' would. dequeueOrGetMissing :: PacketQueue a -> STM (Either [Word32] a) dequeueOrGetMissing PacketQueue { pktq, seqno, qsize, buffend } = do - i0 <- readTVar seqno - be <- readTVar buffend - if i0 < be + seqno0 <- readTVar seqno + buffend0 <- readTVar buffend + if seqno0 < buffend0 then do - maybes <- mapM (readArray pktq) (take (fromIntegral qsize) $ map (`mod` qsize) [ be .. i0 ]) - let nums = map fst . filter (isNothing . snd) $ zip [be ..] maybes + maybes <- mapM (readArray pktq) (take (fromIntegral qsize) $ map (`mod` qsize) [ buffend0 .. seqno0 ]) + let nums = map fst . filter (isNothing . snd) $ zip [buffend0 ..] maybes return (Left nums) else do - let i = i0 `mod` qsize + let i = seqno0 `mod` qsize x <- maybe retry return =<< readArray pktq i writeArray pktq i Nothing modifyTVar' seqno succ -- cgit v1.2.3