import System.IO (Handle, BufferMode(..), hSetBuffering)
import Data.ByteString (ByteString, pack, unpack, concat, hGet, hPut, singleton, writeFile)
-import Data.ByteString.Lazy (fromStrict, fromChunks)
+import Data.ByteString.Lazy (fromStrict, fromChunks, toStrict)
import qualified Data.ByteString.Char8 as BC (replicate, pack, length)
import Network (connectTo, PortID(..))
-import Data.Binary (Binary(..), decode)
+import Data.Binary (Binary(..), decode, encode)
import Data.Binary.Put (putWord32be, putWord16be, putWord8)
import Data.Binary.Get (getWord32be, getWord16be, getWord8, runGet)
import Control.Monad (replicateM, liftM, forever)
msg <- hGet h l
return $ decode $ fromStrict $ concat [lBS, msg]
+sendMsg :: Handle -> PeerMsg -> IO ()
+sendMsg h msg =
+ let bsMsg = toStrict $ encode msg
+ in
+ hPut h bsMsg
bsToInt :: ByteString -> Int
bsToInt x = fromIntegral (runGet getWord32be (fromChunks (return x)))