binary-communicator-1.0.2.1/0000755000000000000000000000000011632161136014021 5ustar0000000000000000binary-communicator-1.0.2.1/Setup.hs0000644000000000000000000000006011632161136015451 0ustar0000000000000000import Distribution.Simple main = defaultMain binary-communicator-1.0.2.1/LICENSE0000644000000000000000000000265711632161136015040 0ustar0000000000000000Copyright (c) 2010 Yves Parès All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the author nor the names of his contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. binary-communicator-1.0.2.1/binary-communicator.cabal0000644000000000000000000000145311632161136020772 0ustar0000000000000000Name: 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.2.1 Category: Data Cabal-Version: >= 1.6 License: BSD3 License-File: LICENSE Author: Yves Parès Maintainer: Sönke Hahn 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 && < 3 Hs-Source-Dirs: . Exposed-Modules: Data.BinaryCom binary-communicator-1.0.2.1/Data/0000755000000000000000000000000011632161136014672 5ustar0000000000000000binary-communicator-1.0.2.1/Data/BinaryCom.hs0000644000000000000000000000620011632161136017107 0ustar0000000000000000-- | -- 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, binaryCom2H, binaryComBS, 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 import qualified Data.Binary.Get as B import qualified Data.Binary.Put as B -- | '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 binaryCom :: (MonadIO m) => Handle -> m BinaryCom binaryCom h = binaryCom2H h h -- | 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 binaryComBS 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 True -- | Sends a serializable value through a 'BinaryCom' send :: (B.Binary a, MonadIO m) => BinaryCom -> a -> m () send b@(BinaryCom _ _ doFlush) val = do sendPut b . B.put $ val when doFlush $ flush b -- | 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 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 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 -- | A if-then-else, but with the condition as last argument (+|) :: a -> a -> Bool -> a (+|) t e c = if c then t else e