binary-communicator 1.0.1 → 1.0.2.1
raw patch · 2 files changed
+32/−16 lines, 2 filesdep ~mtlnew-uploaderPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
Dependency ranges changed: mtl
API changes (from Hackage documentation)
- Data.BinaryCom: flush :: MonadIO m => BinaryCom -> m ()
- Data.BinaryCom: sendFlush :: (Binary a, MonadIO m) => BinaryCom -> a -> m ()
+ Data.BinaryCom: (+|) :: a -> a -> Bool -> a
+ Data.BinaryCom: flushAfter :: MonadIO m => BinaryCom -> (BinaryCom -> m ()) -> m ()
Files
- Data/BinaryCom.hs +24/−12
- binary-communicator.cabal +8/−4
Data/BinaryCom.hs view
@@ -10,12 +10,14 @@ module Data.BinaryCom (BinaryCom, binaryCom, binaryCom2H, binaryComBS,- send, flush, sendFlush, receive,- sendPut, receiveGet)+ send, flushAfter, receive,+ sendPut, receiveGet,+ (+|)) where import System.IO import Data.IORef+import Control.Monad (when) import Control.Monad.Trans import qualified Data.ByteString.Lazy as L import qualified Data.Binary as B@@ -26,6 +28,7 @@ -- | 'BinaryCom' type data BinaryCom = BinaryCom (IORef L.ByteString) -- For reading Handle -- For writing+ Bool -- Auto-flush when 'send' called -- | Creates a 'BinaryCom' from a 'Handle' opened for both reading and writing. -- Be careful not to use the handle afterwards@@ -48,19 +51,24 @@ -> m BinaryCom -- ^ New 'BinaryCom' binaryComBS inp hW = liftIO $ do ref <- newIORef inp- return $ BinaryCom ref hW+ return $ BinaryCom ref hW True -- | Sends a serializable value through a 'BinaryCom' send :: (B.Binary a, MonadIO m) => BinaryCom -> a -> m ()-send b = sendPut b . B.put+send b@(BinaryCom _ _ doFlush) val = do+ sendPut b . B.put $ val+ when doFlush $ flush b --- | Flushes a 'BinaryCom'. Do not forget to do this after sending!-flush :: (MonadIO m) => BinaryCom -> m ()-flush (BinaryCom _ hW) = liftIO $ hFlush hW+-- | Runs a continuation, passing it a binary com with auto-flush deactivated.+-- Flushes when the continuation is finished.+-- It permits not to flush at each call to 'send'.+flushAfter :: (MonadIO m) => BinaryCom -> (BinaryCom -> m ()) -> m ()+flushAfter b@(BinaryCom ref hW _) cont = do+ cont $ BinaryCom ref hW False+ flush b --- | Shortcut for sending a value and flushing the 'BinaryCom'-sendFlush :: (B.Binary a, MonadIO m) => BinaryCom -> a -> m ()-sendFlush b v = send b v >> flush b+flush :: (MonadIO m) => BinaryCom -> m ()+flush (BinaryCom _ hW _) = liftIO $ hFlush hW -- | Receives a serializable value through a 'BinaryCom' receive :: (B.Binary a, MonadIO m) => BinaryCom -> m a@@ -68,14 +76,18 @@ -- | Runs a 'B.Put' monad and sends its result sendPut :: (MonadIO m) => BinaryCom -> B.Put -> m ()-sendPut (BinaryCom _ hW) putAction = liftIO $+sendPut (BinaryCom _ hW _) putAction = liftIO $ L.hPut hW $ B.runPut putAction -- | Receives a value. Runs a 'B.Get' monad to parse it receiveGet :: (MonadIO m) => BinaryCom -> B.Get a -> m a-receiveGet (BinaryCom ref _) getAction = liftIO $ do+receiveGet (BinaryCom ref _ _) getAction = liftIO $ do inp <- readIORef ref let (val, inpRest, _) = B.runGetState getAction inp 0 writeIORef ref inpRest return val++-- | A if-then-else, but with the condition as last argument+(+|) :: a -> a -> Bool -> a+(+|) t e c = if c then t else e
binary-communicator.cabal view
@@ -1,20 +1,24 @@ Name: binary-communicator Description: Simple datatype that makes easier to send and receive values in any MonadIO. Inspired by Gregory Crosswhite's 'binary-protocol' package.-Version: 1.0.1+Version: 1.0.2.1 Category: Data-Cabal-Version: >= 1.2+Cabal-Version: >= 1.6 License: BSD3 License-File: LICENSE Author: Yves Parès-Maintainer: Yves Parès <limestrael@gmail.com>+Maintainer: Sönke Hahn <shahn@joyridelabs.de> Synopsis: Flexible way to ease transmission of binary data. Build-Type: Simple +source-repository head+ type: git+ location: git://github.com/YwenSubrosian/BinaryCommunicator.git+ Library Build-Depends: base >= 4 && < 5, binary >= 0.5 && < 0.6, bytestring >= 0.9.1 && < 1,- mtl >= 1.1 && < 2+ mtl >= 1.1 && < 3 Hs-Source-Dirs: . Exposed-Modules: Data.BinaryCom