import Prelude hiding (lookup, concat, replicate, splitAt)
import System.IO
-import Data.ByteString (ByteString, pack, unpack, concat, hGet, hPut, singleton)
-import qualified Data.ByteString.Char8 as BC (replicate, pack)
+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 Network (connectTo, PortID(..))
-import Data.Binary (Binary(..))
+import Data.Binary (Binary(..), decode)
import Data.Binary.Put (putWord32be, putWord16be, putWord8)
import Data.Binary.Get (getWord32be, getWord16be, getWord8)
import Control.Monad (replicateM, liftM)
, am_choking :: Bool
, am_interested :: Bool
, peer_choking :: Bool
- , peer_interested :: Bool }
+ , peer_interested :: Bool}
-- Maintain info on every piece and the current state of it.
-- should probably be a TVar.
9 -> liftM (PortMsg . fromIntegral) getWord16be
_ -> error "unknown message ID"
+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
+
+
-- loop1 :: shake hands with all peers, find out the pieces they have, form PieceData.
-- recvMsg :: Peer -> Handle -> Msg