packages feed

binary-communicator 1.0 → 1.0.1

raw patch · 2 files changed

+57/−34 lines, 2 filesPVP: major bump suggested

API removals or changes: PVP suggests a major version bump

API changes (from Hackage documentation)

- Data.BinaryCom: binaryCom' :: (MonadIO m) => Handle -> Handle -> m BinaryCom
- Data.BinaryCom: binaryCom'' :: (MonadIO m) => ByteString -> Handle -> m BinaryCom
+ Data.BinaryCom: binaryCom2H :: MonadIO m => Handle -> Handle -> m BinaryCom
+ Data.BinaryCom: binaryComBS :: MonadIO m => ByteString -> Handle -> m BinaryCom
+ Data.BinaryCom: receiveGet :: MonadIO m => BinaryCom -> Get a -> m a
+ Data.BinaryCom: sendPut :: MonadIO m => BinaryCom -> Put -> m ()
- Data.BinaryCom: binaryCom :: (MonadIO m) => Handle -> m BinaryCom
+ Data.BinaryCom: binaryCom :: MonadIO m => Handle -> m BinaryCom
- Data.BinaryCom: flush :: (MonadIO m) => BinaryCom -> m ()
+ Data.BinaryCom: flush :: MonadIO m => BinaryCom -> m ()

Files

Data/BinaryCom.hs view
@@ -1,57 +1,81 @@+-- |+-- Binary Communicator+-- +-- This module provides the datatype BinaryCom, which enables you+-- to easily send and receive data to and from a binary source.+-- The transmitted data can be an instance of the 'Binary' class,+-- or you can provide your own Put and Get actions to serialize+-- and parse the binary stream.+ module Data.BinaryCom-  (BinaryCom, binaryCom, binaryCom', binaryCom'',-   send, flush, sendFlush, receive)+  (BinaryCom,+   binaryCom, binaryCom2H, binaryComBS,+   send, flush, sendFlush, receive,+   sendPut, receiveGet) where  import System.IO import Data.IORef-import Control.Monad (liftM)-import Control.Monad.Trans (MonadIO(..))+import Control.Monad.Trans import qualified Data.ByteString.Lazy as L import qualified Data.Binary as B import qualified Data.Binary.Get as B+import qualified Data.Binary.Put as B  --- | @BinaryCom@ type-newtype BinaryCom = BinaryCom (IORef (L.ByteString, Handle))+-- | 'BinaryCom' type+data BinaryCom = BinaryCom (IORef L.ByteString) -- For reading+                           Handle               -- For writing --- | Creates a @BinaryCom@ from a @Handle@ opened for both reading and writing.+-- | Creates a 'BinaryCom' from a 'Handle' opened for both reading and writing. -- Be careful not to use the handle afterwards binaryCom :: (MonadIO m) => Handle -> m BinaryCom-binaryCom h = binaryCom' h h+binaryCom h = binaryCom2H h h --- | Creates a @BinaryCom@ from two @Handle@s: one for reading, one for writing-binaryCom' :: (MonadIO m) => Handle -> Handle -> m BinaryCom-binaryCom' hR hW = do+-- | Creates a 'BinaryCom' from two 'Handle's: one for reading, one for writing+binaryCom2H :: (MonadIO m) =>+               Handle      -- ^ For reading+            -> Handle      -- ^ For writing+            -> m BinaryCom -- ^ New 'BinaryCom'+binaryCom2H hR hW = do   inp <- liftIO $ L.hGetContents hR-  binaryCom'' inp hW+  binaryComBS inp hW --- | Creates a @BinaryCom@ from a lazy @ByteString@ (for reading) and a @Handle@ (for writing)-binaryCom'' :: (MonadIO m) => L.ByteString -> Handle -> m BinaryCom-binaryCom'' inp hW = liftIO $-  liftM BinaryCom $ newIORef (inp, hW)+-- | Creates a 'BinaryCom' from a lazy 'L.ByteString' (for reading) and a 'Handle' (for writing)+binaryComBS :: (MonadIO m) =>+               L.ByteString -- ^ For reading+            -> Handle       -- ^ For writing+            -> m BinaryCom  -- ^ New 'BinaryCom'+binaryComBS inp hW = liftIO $ do+  ref <- newIORef inp+  return $ BinaryCom ref hW --- | Sends a serializable value through a @BinaryCom@+-- | Sends a serializable value through a 'BinaryCom' send :: (B.Binary a, MonadIO m) => BinaryCom -> a -> m ()-send (BinaryCom ref) val = liftIO $ do-  (_, h) <- readIORef ref-  L.hPut h (B.encode val)+send b = sendPut b . B.put --- | Flushes a @BinaryCom@. Do not forget to do this after sending!+-- | Flushes a 'BinaryCom'. Do not forget to do this after sending! flush :: (MonadIO m) => BinaryCom -> m ()-flush (BinaryCom ref) = liftIO $ do-  (_, h) <- readIORef ref-  hFlush h+flush (BinaryCom _ hW) = liftIO $ hFlush hW --- | Shortcut for sending a value and flushing the @BinaryCom@+-- | 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 --- | Receives a serializable value through a @BinaryCom@+-- | Receives a serializable value through a 'BinaryCom' receive :: (B.Binary a, MonadIO m) => BinaryCom -> m a-receive (BinaryCom ref) = liftIO $ do-  (inp, h) <- readIORef ref-  let (val, inpRest, _) = B.runGetState B.get inp 0-  writeIORef ref (inpRest, h)+receive b = receiveGet b B.get++-- | Runs a 'B.Put' monad and sends its result+sendPut :: (MonadIO m) => BinaryCom -> B.Put -> m ()+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+  inp <- readIORef ref+  let (val, inpRest, _) = B.runGetState getAction inp 0+  writeIORef ref inpRest   return val 
binary-communicator.cabal view
@@ -1,13 +1,13 @@ Name:           binary-communicator-Description:    Simple datatype that makes easier to send and receive serializable values in any MonadIO. Inspired by Gregory Crosswhite's 'binary-protocol' package.-Version:        1.0+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 Category:       Data Cabal-Version:  >= 1.2 License:        BSD3 License-File:   LICENSE Author:         Yves Parès Maintainer:     Yves Parès <limestrael@gmail.com>-Synopsis:       Send and receive binary data.+Synopsis:       Flexible way to ease transmission of binary data. Build-Type:     Simple  Library@@ -17,5 +17,4 @@                     mtl >= 1.1 && < 2   Hs-Source-Dirs:   .   Exposed-Modules:  Data.BinaryCom-  GHC-Options:      -Wall