From: Ramakrishnan Muthukrishnan Date: Wed, 6 May 2015 17:33:53 +0000 (+0530) Subject: WIP: receive msgs from a connected peer and print them X-Git-Url: https://git.rkrishnan.org/vdrive/%22file:/frontends/%22doc.html/%22news.html/status?a=commitdiff_plain;h=8a7af16c553a3242a8585d7047c361ba3b8775fc;p=functorrent.git WIP: receive msgs from a connected peer and print them Something wierd going on. Getting an incorrect length prefix. --- diff --git a/src/FuncTorrent/Peer.hs b/src/FuncTorrent/Peer.hs index aeba52f..36607ea 100644 --- a/src/FuncTorrent/Peer.hs +++ b/src/FuncTorrent/Peer.hs @@ -9,14 +9,15 @@ import Prelude hiding (lookup, concat, replicate, splitAt) import System.IO import Data.ByteString (ByteString, pack, unpack, concat, hGet, hPut, singleton, append) -import Data.ByteString.Lazy (fromStrict) -import qualified Data.ByteString.Char8 as BC (replicate, pack, readInt) +import Data.ByteString.Lazy (fromStrict, fromChunks) +import qualified Data.ByteString.Char8 as BC (replicate, pack, readInt, putStrLn) import Network (connectTo, PortID(..)) import Data.Binary (Binary(..), decode) import Data.Binary.Put (putWord32be, putWord16be, putWord8) -import Data.Binary.Get (getWord32be, getWord16be, getWord8) +import Data.Binary.Get (getWord32be, getWord16be, getWord8, runGet) import Control.Monad (replicateM, liftM, forever) import Control.Applicative ((<$>), liftA3) +import Control.Concurrent (threadDelay) type ID = String type IP = String @@ -137,10 +138,18 @@ instance Binary PeerMsg where getMsg :: Handle -> IO PeerMsg getMsg h = do lBS <- hGet h 4 - let (Just (l, _)) = BC.readInt lBS - msg <- hGet h l - return $ decode $ fromStrict $ append lBS msg - + 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 + +bsToInt :: ByteString -> Int +bsToInt x = fromIntegral (runGet getWord32be (fromChunks (return x))) -- loop1 :: shake hands with all peers, find out the pieces they have, form PieceData. -- recvMsg :: Peer -> Handle -> Msg