X-Git-Url: https://git.rkrishnan.org/?a=blobdiff_plain;f=src%2FFuncTorrent%2FPeer.hs;h=7b263a52f46da173bd7e722873177b7f46d020cb;hb=dee4c3617bf46858525ba7675190d532c8594059;hp=36607ea35f935eda4ad0b87c75c6159cc16b64e3;hpb=8a7af16c553a3242a8585d7047c361ba3b8775fc;p=functorrent.git diff --git a/src/FuncTorrent/Peer.hs b/src/FuncTorrent/Peer.hs index 36607ea..7b263a5 100644 --- a/src/FuncTorrent/Peer.hs +++ b/src/FuncTorrent/Peer.hs @@ -133,20 +133,20 @@ instance Binary PeerMsg where 8 -> liftA3 CancelMsg getInteger getInteger getInteger where getInteger = fromIntegral <$> getWord32be 9 -> liftM (PortMsg . fromIntegral) getWord16be - _ -> error "unknown message ID" + _ -> error ("unknown message ID: " ++ show msgid) getMsg :: Handle -> IO PeerMsg getMsg h = do lBS <- hGet h 4 - let lenBS = length (unpack lBS) - putStrLn $ "bytes read: " ++ (show lenBS) let l = bsToInt lBS if l == 0 then return KeepAliveMsg else do - putStrLn $ "len: " ++ (show lBS) - msg <- hGet h l - return $ decode $ fromStrict $ append lBS msg + putStrLn $ "len: " ++ show l + msgType <- hGet h 1 + putStrLn $ "msg Type: " ++ show msgType + msg <- hGet h (l - 1) + return $ decode $ fromStrict $ concat [lBS, msgType, msg] bsToInt :: ByteString -> Int bsToInt x = fromIntegral (runGet getWord32be (fromChunks (return x))) @@ -157,4 +157,4 @@ bsToInt x = fromIntegral (runGet getWord32be (fromChunks (return x))) msgLoop :: Handle -> IO () msgLoop h = forever $ do msg <- getMsg h - putStrLn $ "got a " ++ (show msg) + putStrLn $ "got a " ++ show msg