From: Ramakrishnan Muthukrishnan Date: Tue, 21 Jul 2015 06:21:33 +0000 (+0530) Subject: sendMsg: equiv of getMsg but on the sending side X-Git-Url: https://git.rkrishnan.org/simplejson/htmlfontify-example.html?a=commitdiff_plain;h=b7faeddf7a6bf2d8ce48f2739d26df528781488f;p=functorrent.git sendMsg: equiv of getMsg but on the sending side --- diff --git a/src/FuncTorrent/Peer.hs b/src/FuncTorrent/Peer.hs index f596033..70e89d8 100644 --- a/src/FuncTorrent/Peer.hs +++ b/src/FuncTorrent/Peer.hs @@ -8,10 +8,10 @@ import Prelude hiding (lookup, concat, replicate, splitAt, empty, writeFile) 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) @@ -166,6 +166,11 @@ getMsg h = do 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)))