X-Git-Url: https://git.rkrishnan.org/?a=blobdiff_plain;f=src%2FFuncTorrent%2FPeer.hs;h=99906b074293bd260ec748cb5b027fdbf15c2aa9;hb=144c253adea364bdd52027f484020eeafafa20f6;hp=65372d9500dfc939c7959c65345860ad6a71c5c5;hpb=755a5f299f8ff9768d98fd780dd8a51420ae3192;p=functorrent.git diff --git a/src/FuncTorrent/Peer.hs b/src/FuncTorrent/Peer.hs index 65372d9..99906b0 100644 --- a/src/FuncTorrent/Peer.hs +++ b/src/FuncTorrent/Peer.hs @@ -84,7 +84,13 @@ bitfieldToList bs = go bs 0 setBits ++ go bs' (pos + 1) -- helper functions to manipulate PeerState -toPeerState :: Handle -> Peer -> Bool -> Bool -> Bool -> Bool -> PState +toPeerState :: Handle + -> Peer + -> Bool -- ^ meChoking + -> Bool -- ^ meInterested + -> Bool -- ^ heChoking + -> Bool -- ^ heInterested + -> PState toPeerState h p meCh meIn heCh heIn = PState { handle = h , peer = p @@ -173,16 +179,15 @@ handlePeerMsgs p m peerId = do return () msgLoop :: PieceMap -> FilePath -> StateT PState IO () -msgLoop pieceStatus file = - StateT(\pState -> do - let h = handle pState - msg <- getMsg h - liftIO $ putStrLn $ "<-- " ++ show msg ++ "from peer: " ++ show (peer pState) - case msg of - KeepAliveMsg -> do - sendMsg h KeepAliveMsg - liftIO $ putStrLn $ "--> " ++ "KeepAliveMsg to peer: " ++ show (peer pState) - runStateT (msgLoop pieceStatus file) pState) +msgLoop pieceStatus file = do + h <- gets handle + msg <- liftIO $ getMsg h + gets peer >>= (\p -> liftIO $ putStrLn $ "<-- " ++ show msg ++ "from peer: " ++ show p) + case msg of + KeepAliveMsg -> do + liftIO $ sendMsg h KeepAliveMsg + gets peer >>= (\p -> liftIO $ putStrLn $ "--> " ++ "KeepAliveMsg to peer: " ++ show p) + msgLoop pieceStatus file downloadPiece :: Handle -> Integer -> Integer -> IO ByteString downloadPiece h index pieceLength = do