From d966c772fd0047528f6cc7110955ff4d8be89fbd Mon Sep 17 00:00:00 2001 From: Ramakrishnan Muthukrishnan Date: Sun, 17 May 2015 13:22:12 +0530 Subject: [PATCH] rewrite bitfieldToList --- src/FuncTorrent/Peer.hs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/FuncTorrent/Peer.hs b/src/FuncTorrent/Peer.hs index 93e4a15..481f5da 100644 --- a/src/FuncTorrent/Peer.hs +++ b/src/FuncTorrent/Peer.hs @@ -145,19 +145,20 @@ getMsg h = do msg <- hGet h l return $ decode $ fromStrict $ concat [lBS, msg] + bsToInt :: ByteString -> Int bsToInt x = fromIntegral (runGet getWord32be (fromChunks (return x))) -bitfieldToList :: [Word8] -> Integer -> [Integer] -bitfieldToList [] pos = [] -bitfieldToList (b:bs) pos = - let setBits = [pos*8 + (toInteger i) | i <- [0..8], testBit b i] - in - setBits ++ (bitfieldToList bs (pos + 1)) +bitfieldToList :: [Word8] -> [Integer] +bitfieldToList bs = go bs 0 + where go [] _ = [] + go (b:bs') pos = + let setBits = [pos*8 + (toInteger i) | i <- [0..8], testBit b i] + in + setBits ++ (go bs' (pos + 1)) -- loop1 :: shake hands with all peers, find out the pieces they have, form PieceData. -- recvMsg :: Peer -> Handle -> Msg - msgLoop :: Handle -> ByteString -> IO () msgLoop h pieceHash = let numPieces = (toInteger . (`quot` 20) . BC.length) pieceHash -- 2.37.2