@@ -32,9 +32,11 @@
33module Streamly.Network.Socket
33module Streamly.Network.Socket
35 withSocket
36 withSocket
36 -- * Read from connection
39 -- * Read from connection
39 -- , readUtf8
41 -- , readUtf8
40 -- , readLines
42 -- , readLines
@@ -94,16 +96,35 @@ import qualified Streamly.Mem.Array as A
94import qualified Streamly.Mem.Array.Types as A hiding (flattenArrays)
96import qualified Streamly.Mem.Array.Types as A hiding (flattenArrays)
95import qualified Streamly.Prelude as S
97import qualified Streamly.Prelude as S
97−-- | @'withSocket' socket act@ runs the computation @act@ passing the socket
99+-- | @'withSocket' socket act@ runs the monadic computation @act@ passing the
98−-- handle to it. The handle will be closed on exit from 'withSocket', whether
100+-- socket handle to it. The handle will be closed on exit from 'withSocket',
99−-- by normal termination or by raising an exception. If closing the handle
101+-- whether by normal termination or by raising an exception. If closing the
100−-- raises an exception, then this exception will be raised by 'withSocket'
102+-- handle raises an exception, then this exception will be raised by
101−-- rather than any exception raised by 'act'.
103+-- 'withSocket' rather than any exception raised by 'act'.
106+{-# INLINE withSocket #-}
102withSocket :: (MonadCatch m, MonadIO m) => Socket -> (Socket -> m ()) -> m ()
107withSocket :: (MonadCatch m, MonadIO m) => Socket -> (Socket -> m ()) -> m ()
103withSocket sk f = do
108withSocket sk f = do
104 f sk `onException` liftIO (Net.close sk)
109 f sk `onException` liftIO (Net.close sk)
105 liftIO (Net.close sk)
110 liftIO (Net.close sk)
112+-- XXX bracket performs 2x better than 'finally'. That's perhaps because of
113+-- better fusion of "A.flattenArrays . readArrays"?
115+-- withSocketS :: (IsStream t, MonadCatch m, MonadIO m) => Socket -> t m Word8
116+-- withSocketS sk = A.flattenArrays $
117+-- S.finally (liftIO (Net.close sk)) (readArrays sk)
119+-- | Like 'withSocket' but runs a streaming computation instead of a monadic
123+{-# INLINE withSocketS #-}
124+withSocketS :: (IsStream t, MonadCatch m, MonadIO m)
125+ => Socket -> (Socket -> t m a) -> t m a
126+withSocketS sk = S.bracket (return sk) (liftIO . Net.close)
107-------------------------------------------------------------------------------
128-------------------------------------------------------------------------------
108-- Array IO (Input)
129-- Array IO (Input)
109-------------------------------------------------------------------------------
130-------------------------------------------------------------------------------
@@ -246,12 +267,6 @@ readByChunksUpto chunkSize h = A.flattenArrays $ readArraysUpto chunkSize h
246read :: (IsStream t, MonadIO m) => Socket -> t m Word8
267read :: (IsStream t, MonadIO m) => Socket -> t m Word8
247read = A.flattenArrays . readArrays
268read = A.flattenArrays . readArrays
249−-- | Read a stream of Word8 from a socket, closing the socket when done.
250−{-# INLINE fromSocket #-}
251−fromSocket :: (MonadCatch m, MonadIO m) => Socket -> SerialT m Word8
252−fromSocket sk = A.flattenArrays $
253− S.finally (liftIO (Net.close sk)) (readArrays sk)
255-------------------------------------------------------------------------------
270-------------------------------------------------------------------------------
256-- Writing
271-- Writing
257-------------------------------------------------------------------------------
272-------------------------------------------------------------------------------