packages feed

binary-communicator (empty) → 1.0

raw patch · 4 files changed

+108/−0 lines, 4 filesdep +basedep +binarydep +bytestringsetup-changed

Dependencies added: base, binary, bytestring, mtl

Files

+ Data/BinaryCom.hs view
@@ -0,0 +1,57 @@+module Data.BinaryCom+  (BinaryCom, binaryCom, binaryCom', binaryCom'',+   send, flush, sendFlush, receive)+where++import System.IO+import Data.IORef+import Control.Monad (liftM)+import Control.Monad.Trans (MonadIO(..))+import qualified Data.ByteString.Lazy as L+import qualified Data.Binary as B+import qualified Data.Binary.Get as B+++-- | @BinaryCom@ type+newtype BinaryCom = BinaryCom (IORef (L.ByteString, Handle))++-- | 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++-- | Creates a @BinaryCom@ from two @Handle@s: one for reading, one for writing+binaryCom' :: (MonadIO m) => Handle -> Handle -> m BinaryCom+binaryCom' hR hW = do+  inp <- liftIO $ L.hGetContents hR+  binaryCom'' 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)++-- | 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)++-- | 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++-- | 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@+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)+  return val+
+ LICENSE view
@@ -0,0 +1,26 @@+Copyright (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.
+ Setup.hs view
@@ -0,0 +1,4 @@+import Distribution.Simple++main = defaultMain+
+ binary-communicator.cabal view
@@ -0,0 +1,21 @@+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+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.+Build-Type:     Simple++Library+  Build-Depends:    base >= 4 && < 5,+                    binary >= 0.5 && < 0.6,+                    bytestring >= 0.9.1 && < 1,+                    mtl >= 1.1 && < 2+  Hs-Source-Dirs:   .+  Exposed-Modules:  Data.BinaryCom+  GHC-Options:      -Wall+