From: Ramakrishnan Muthukrishnan Date: Fri, 18 Sep 2015 06:32:42 +0000 (+0530) Subject: refactor doHandshake to use pattern matching X-Git-Url: https://git.rkrishnan.org/specifications/components/statistics?a=commitdiff_plain;h=36438aebae3f2785118b6b89d7c76791d487bd4d;p=functorrent.git refactor doHandshake to use pattern matching --- diff --git a/src/FuncTorrent/Peer.hs b/src/FuncTorrent/Peer.hs index 2caf377..ed88e83 100644 --- a/src/FuncTorrent/Peer.hs +++ b/src/FuncTorrent/Peer.hs @@ -84,30 +84,28 @@ connectToPeer (Peer _ ip port) = do return h doHandshake :: Bool -> Handle -> Peer -> ByteString -> String -> IO () -doHandshake isClient h peer infoHash peerid = +doHandshake True h peer infoHash peerid = do let hs = genHandshakeMsg infoHash peerid - in - if isClient - then do - hPut h hs - putStrLn $ "--> handhake to peer: " ++ show peer - _ <- hGet h (length (unpack hs)) - putStrLn $ "<-- handshake from peer: " ++ show peer - return () - else do - putStrLn $ "waiting for a handshake" - hsMsg <- hGet h (length (unpack hs)) - putStrLn $ "<-- handshake from peer: " ++ show peer - let rxInfoHash = take 20 $ drop 28 hsMsg - if rxInfoHash /= infoHash - then do - putStrLn $ "infoHashes does not match" - hClose h - return () - else do - _ <- hPut h hs - putStrLn $ "--> handhake to peer: " ++ show peer - return () + hPut h hs + putStrLn $ "--> handhake to peer: " ++ show peer + _ <- hGet h (length (unpack hs)) + putStrLn $ "<-- handshake from peer: " ++ show peer + return () +doHandshake False h peer infoHash peerid = do + let hs = genHandshakeMsg infoHash peerid + putStrLn $ "waiting for a handshake" + hsMsg <- hGet h (length (unpack hs)) + putStrLn $ "<-- handshake from peer: " ++ show peer + let rxInfoHash = take 20 $ drop 28 hsMsg + if rxInfoHash /= infoHash + then do + putStrLn $ "infoHashes does not match" + hClose h + return () + else do + _ <- hPut h hs + putStrLn $ "--> handhake to peer: " ++ show peer + return () bitfieldToList :: [Word8] -> [Integer] bitfieldToList bs = go bs 0