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