diff options
author | jim@bo <jim@bo> | 2018-06-22 15:06:51 -0400 |
---|---|---|
committer | jim@bo <jim@bo> | 2018-06-22 15:06:51 -0400 |
commit | d96aa110fcc32d9a8afb14564f45f296dd1624e4 (patch) | |
tree | 8043a7ae3623501c012198fb4dd2f1eaf9c79b61 /src/Data | |
parent | 2f0e7ebc7bc3abf5cb03ccff2de77d3557ba2702 (diff) |
packet request off by 1 serializatoin fix & getMissing fix
Diffstat (limited to 'src/Data')
-rw-r--r-- | src/Data/PacketQueue.hs | 59 |
1 files changed, 30 insertions, 29 deletions
diff --git a/src/Data/PacketQueue.hs b/src/Data/PacketQueue.hs index b5d8a756..59b41d91 100644 --- a/src/Data/PacketQueue.hs +++ b/src/Data/PacketQueue.hs | |||
@@ -19,6 +19,7 @@ module Data.PacketQueue | |||
19 | , packetQueueViewList | 19 | , packetQueueViewList |
20 | , newOutGoing | 20 | , newOutGoing |
21 | , readyOutGoing | 21 | , readyOutGoing |
22 | , toPNums | ||
22 | , getRequested | 23 | , getRequested |
23 | , peekPacket | 24 | , peekPacket |
24 | , tryAppendQueueOutgoing | 25 | , tryAppendQueueOutgoing |
@@ -86,13 +87,18 @@ observeOutOfBand PacketQueue { seqno, qsize, buffend } numberOfNextLosslessPacke | |||
86 | -- | If seqno < buffend then return expected packet numbers for all | 87 | -- | If seqno < buffend then return expected packet numbers for all |
87 | -- the Nothings in the array between them. | 88 | -- the Nothings in the array between them. |
88 | -- Otherwise, return empty list. | 89 | -- Otherwise, return empty list. |
89 | getMissing :: PacketQueue a -> STM [Word32] | 90 | getMissing :: Show a => PacketQueue a -> STM [Word32] |
90 | getMissing PacketQueue { pktq, seqno, qsize, buffend } = do | 91 | getMissing PacketQueue { pktq, seqno, qsize, buffend } = do |
91 | seqno0 <- readTVar seqno | 92 | seqno0 <- readTVar seqno |
92 | buffend0 <- readTVar buffend | 93 | buffend0 <- readTVar buffend |
94 | -- tput XNetCrypto $ "getMissing: seqno = " ++ show seqno0 | ||
95 | -- tput XNetCrypto $ "getMissing: buffend0 = " ++ show buffend0 | ||
93 | -- note relying on fact that [ b .. a ] is null when a < b | 96 | -- note relying on fact that [ b .. a ] is null when a < b |
94 | maybes <- mapM (readArray pktq) (take (fromIntegral qsize) $ map (`mod` qsize) [ seqno0 .. buffend0 ]) | 97 | let indices = take (fromIntegral qsize) $ map (`mod` qsize) [ seqno0 .. buffend0 - 1] |
95 | let nums = map fst . filter (isNothing . snd) $ zip [buffend0 ..] maybes | 98 | -- tput XNetCrypto $ "indices: " ++ show indices |
99 | maybes <- mapM (\i -> do {x <- readArray pktq i; return (i,x)}) indices | ||
100 | -- tput XNetCrypto $ "getMissing: maybers = " ++ show maybes | ||
101 | let nums = map fst . filter (isNothing . snd) $ maybes | ||
96 | return nums | 102 | return nums |
97 | 103 | ||
98 | -- | If seqno < buffend then return expected packet numbers for all | 104 | -- | If seqno < buffend then return expected packet numbers for all |
@@ -209,23 +215,28 @@ data OutGoingResult = OGSuccess | OGFull | OGEncodeFail | |||
209 | readyOutGoing :: PacketOutQueue extra msg wire fromwire -> IO (STM extra) | 215 | readyOutGoing :: PacketOutQueue extra msg wire fromwire -> IO (STM extra) |
210 | readyOutGoing (PacketOutQueue {pktoToWireIO }) = pktoToWireIO | 216 | readyOutGoing (PacketOutQueue {pktoToWireIO }) = pktoToWireIO |
211 | 217 | ||
212 | getRequested :: STM extra -> PacketOutQueue extra msg wire fromwire -> Word32 -> [Word8] -> STM [Maybe (Word32,wire)] | 218 | getRequested :: Show wire => STM extra -> PacketOutQueue extra msg wire fromwire -> Word32 -> [Word8] -> STM [Maybe (Word32,wire)] |
213 | getRequested _ _ _ [] = return [] | 219 | getRequested _ _ _ [] = return [] |
214 | getRequested getExtra pktoq snum ns = do | 220 | getRequested getExtra pktoq snum ns = do |
215 | let pnums = toPNums snum ns | 221 | let pnums = toPNums snum ns |
216 | indices = map toIndex pnums | 222 | indices = map toIndex pnums |
223 | tput XNetCrypto $ "getRequested: snum = " ++ show snum | ||
224 | tput XNetCrypto $ "getRequested: pnums = " ++ show pnums ++ " indices = " ++ show indices | ||
225 | xs <- packetQueueViewList (pktoOutPQ pktoq) | ||
226 | tput XNetCrypto $ "getRequested viewList -> " | ||
227 | mapM_ (tput XNetCrypto . show) xs | ||
217 | forM indices $ \i -> readArray (pktq $ pktoOutPQ pktoq) i | 228 | forM indices $ \i -> readArray (pktq $ pktoOutPQ pktoq) i |
218 | where | 229 | where |
219 | toIndex :: Word32 -> Word32 | 230 | toIndex :: Word32 -> Word32 |
220 | toIndex = (`mod` qsize (pktoOutPQ pktoq)) | 231 | toIndex = (`mod` qsize (pktoOutPQ pktoq)) |
221 | 232 | ||
222 | toPNums :: Word32 -> [Word8] -> [Word32] | 233 | toPNums :: Word32 -> [Word8] -> [Word32] |
223 | toPNums snum ns = reverse . snd $ foldl doOne (snum,[]) ns | 234 | toPNums snum ns = reverse . snd $ foldl doOne (snum-1,[]) ns |
224 | where | 235 | where |
225 | doOne :: (Word32,[Word32]) -> Word8 -> (Word32,[Word32]) | 236 | doOne :: (Word32,[Word32]) -> Word8 -> (Word32,[Word32]) |
226 | doOne (addend,as) 0 = (addend+255,as) | 237 | doOne (addend,as) 0 = (addend+255,as) |
227 | doOne (addend,as) x = let y = fromIntegral x + addend | 238 | doOne (addend,as) x = let y = fromIntegral x + addend |
228 | in (y,y:as) | 239 | in (y,y:as) |
229 | 240 | ||
230 | peekPacket :: STM extra -> PacketOutQueue extra msg wire fromwire -> msg -> STM (Maybe (wire,Word32)) | 241 | peekPacket :: STM extra -> PacketOutQueue extra msg wire fromwire -> msg -> STM (Maybe (wire,Word32)) |
231 | peekPacket getExtra q@(PacketOutQueue { pktoInPQ, pktoOutPQ, pktoPacketNo, pktoToWireIO, pktoToWire }) msg | 242 | peekPacket getExtra q@(PacketOutQueue { pktoInPQ, pktoOutPQ, pktoPacketNo, pktoToWireIO, pktoToWire }) msg |
@@ -256,40 +267,30 @@ peekPacket getExtra q@(PacketOutQueue { pktoInPQ, pktoOutPQ, pktoPacketNo, pktoT | |||
256 | -- index in this implementation.) | 267 | -- index in this implementation.) |
257 | tryAppendQueueOutgoing :: STM extra -> PacketOutQueue extra msg wire fromwire -> msg -> STM OutGoingResult | 268 | tryAppendQueueOutgoing :: STM extra -> PacketOutQueue extra msg wire fromwire -> msg -> STM OutGoingResult |
258 | tryAppendQueueOutgoing getExtra q@(PacketOutQueue { pktoInPQ, pktoOutPQ, pktoPacketNo, pktoToWireIO, pktoToWire }) msg | 269 | tryAppendQueueOutgoing getExtra q@(PacketOutQueue { pktoInPQ, pktoOutPQ, pktoPacketNo, pktoToWireIO, pktoToWire }) msg |
259 | = dtrace XNetCrypto "(tryAppendQueueOutgoing)" $ do | 270 | = do |
260 | be <- readTVar (buffend pktoOutPQ) | 271 | be <- readTVar (buffend pktoOutPQ) |
261 | let i = be `mod` (qsize pktoOutPQ) | 272 | let i = be `mod` (qsize pktoOutPQ) |
262 | let arrayEmpty :: MArray a e m => a Word32 e -> m Bool | 273 | let arrayEmpty :: MArray a e m => a Word32 e -> m Bool |
263 | arrayEmpty ar = do (lowB,highB) <- getBounds ar | 274 | arrayEmpty ar = do (lowB,highB) <- getBounds ar |
264 | let result= lowB > highB | 275 | return $ lowB > highB |
265 | tput XNetCrypto | ||
266 | ("arrayEmpty result=" ++ show result | ||
267 | ++ " lowB=" ++ show lowB | ||
268 | ++ " highB = " ++ show highB | ||
269 | ++ " i = " ++ show i) | ||
270 | return result | ||
271 | mbPkt <- do emp <- arrayEmpty (pktq pktoOutPQ) | 276 | mbPkt <- do emp <- arrayEmpty (pktq pktoOutPQ) |
272 | if emp then tput XNetCrypto "(tryAppendQueueOutgoing empty)" >> return Nothing | 277 | if emp then return Nothing |
273 | else do tput XNetCrypto "(tryAppendQueueOutgoing nonempty)" | 278 | else do readArray (pktq pktoOutPQ) i |
274 | result <- readArray (pktq pktoOutPQ) i | ||
275 | tput XNetCrypto ("readArray (isJust result)==" ++ show (isJust result)) | ||
276 | return result | ||
277 | pktno <- readTVar pktoPacketNo | 279 | pktno <- readTVar pktoPacketNo |
278 | nextno <- readTVar (seqno pktoInPQ) | 280 | nextno <- readTVar (seqno pktoInPQ) |
279 | mbWire <- pktoToWire getExtra nextno be pktno msg | 281 | mbWire <- pktoToWire getExtra nextno be pktno msg |
280 | -- TODO all the above lines ^^ can be replaced with call to peekPacket | 282 | -- TODO all the above lines ^^ can be replaced with call to peekPacket |
281 | case dtrace XNetCrypto "(tryAppendQueueOutgoing mbWire)" mbWire of | 283 | case mbWire of |
282 | Just (pkt,pktno') | 284 | Just (pkt,pktno') |
283 | -> dtrace XNetCrypto "(tryAppendQueueOutgoing A)" | 285 | -> case mbPkt of |
284 | $ case mbPkt of | ||
285 | -- slot is free, insert element | 286 | -- slot is free, insert element |
286 | Nothing -> dtrace XNetCrypto "(tryAppendQueueOutgoing Nothing case)" $ do | 287 | Nothing -> do |
287 | modifyTVar' (buffend pktoOutPQ) (+1) | 288 | modifyTVar' (buffend pktoOutPQ) (+1) |
288 | writeTVar pktoPacketNo $! pktno' | 289 | writeTVar pktoPacketNo $! pktno' |
289 | writeArray (pktq pktoOutPQ) i (Just (pktno,pkt)) | 290 | writeArray (pktq pktoOutPQ) i (Just (pktno,pkt)) |
290 | return OGSuccess | 291 | return OGSuccess |
291 | -- queue is full | 292 | -- queue is full |
292 | Just (n,_) -> dtrace XNetCrypto "tryAppendQueueOutgoing Just case)" $ do | 293 | Just (n,_) -> do |
293 | nn <- getHighestHandledPacketPlus1 q | 294 | nn <- getHighestHandledPacketPlus1 q |
294 | if (n < nn) | 295 | if (n < nn) |
295 | -- but we can overwrite an old packet | 296 | -- but we can overwrite an old packet |