From 36438aebae3f2785118b6b89d7c76791d487bd4d Mon Sep 17 00:00:00 2001 From: Ramakrishnan Muthukrishnan <ram@rkrishnan.org> Date: Fri, 18 Sep 2015 12:02:42 +0530 Subject: [PATCH] refactor doHandshake to use pattern matching --- src/FuncTorrent/Peer.hs | 44 ++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 23 deletions(-) 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 -- 2.45.2