From: Ramakrishnan Muthukrishnan Date: Sun, 17 May 2015 07:52:12 +0000 (+0530) Subject: rewrite bitfieldToList X-Git-Url: https://git.rkrishnan.org/%5B/frontends/%22news.html/cyclelanguage?a=commitdiff_plain;h=d966c772fd0047528f6cc7110955ff4d8be89fbd;p=functorrent.git rewrite bitfieldToList --- 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