on master help sign in
rename withSocket to useSocket
df8de8dHarendra Kumar
examples/EchoServer.hs+2−2
@@ -1,13 +1,13 @@
1-- A concurrent TCP server that echoes everything that it receives.
1-- A concurrent TCP server that echoes everything that it receives.
2
2
3import Streamly
3import Streamly
4−import Streamly.Internal.Network.Socket (readArrays)
4+import Streamly.Internal.Network.Socket (useSocketM, readArrays)
5import Streamly.Network.Socket
5import Streamly.Network.Socket
6import Streamly.Network.Server
6import Streamly.Network.Server
7import qualified Streamly.Prelude as S
7import qualified Streamly.Prelude as S
8
8
9main :: IO ()
9main :: IO ()
10main = S.drain
10main = S.drain
11− $ parallely $ S.mapM (flip withSocketM echo)
11+ $ parallely $ S.mapM (flip useSocketM echo)
12 $ serially $ S.unfold listenOnPort 8090
12 $ serially $ S.unfold listenOnPort 8090
13 where echo sk = S.fold (writeArrays sk) $ S.unfold readArrays sk
13 where echo sk = S.fold (writeArrays sk) $ S.unfold readArrays sk
examples/FileSinkServer.hs+2−1
@@ -9,6 +9,7 @@ import System.Environment (getArgs)
9
9
10import Streamly
10import Streamly
11import Streamly.Data.String
11import Streamly.Data.String
12+import Streamly.Internal.Network.Socket (useSocket)
12import qualified Streamly.FileSystem.Handle as FH
13import qualified Streamly.FileSystem.Handle as FH
13import qualified Streamly.Memory.Array as A
14import qualified Streamly.Memory.Array as A
14import qualified Streamly.Network.Socket as NS
15import qualified Streamly.Network.Socket as NS
@@ -24,7 +25,7 @@ main = do
24 (\src -> S.fold (FH.write src)
25 (\src -> S.fold (FH.write src)
25 $ encodeChar8Unchecked
26 $ encodeChar8Unchecked
26 $ S.concatUnfold A.read
27 $ S.concatUnfold A.read
27− $ S.concatMapWith parallel (flip NS.withSocket recv)
28+ $ S.concatMapWith parallel (flip useSocket recv)
28 $ S.unfold NS.listenOnPort 8090)
29 $ S.unfold NS.listenOnPort 8090)
29
30
30 where
31 where
src/Streamly/Internal/Network/Socket.hs+12−22
@@ -19,8 +19,8 @@
19module Streamly.Internal.Network.Socket
19module Streamly.Internal.Network.Socket
20 (
20 (
21 -- * Use a socket
21 -- * Use a socket
22− withSocketM
22+ useSocketM
23− , withSocket
23+ , useSocket
24
24
25 -- * Read from connection
25 -- * Read from connection
26 , read
26 , read
@@ -92,35 +92,25 @@ import qualified Streamly.Internal.Memory.Array.Types as A
92import qualified Streamly.Prelude as S
92import qualified Streamly.Prelude as S
93import qualified Streamly.Streams.StreamD.Type as D
93import qualified Streamly.Streams.StreamD.Type as D
94
94
95−-- | @'withSocketM' socket act@ runs the monadic computation @act@ passing the
95+-- | @'useSocketM' socket act@ runs the monadic computation @act@ passing the
96−-- socket handle to it. The handle will be closed on exit from 'withSocketM',
96+-- socket handle to it. The handle will be closed on exit from 'useSocketM',
97-- whether by normal termination or by raising an exception. If closing the
97-- whether by normal termination or by raising an exception. If closing the
98-- handle raises an exception, then this exception will be raised by
98-- handle raises an exception, then this exception will be raised by
99−-- 'withSocketM' rather than any exception raised by 'act'.
99+-- 'useSocketM' rather than any exception raised by 'act'.
100--
100--
101-- @since 0.7.0
101-- @since 0.7.0
102−{-# INLINE withSocketM #-}
102+{-# INLINE useSocketM #-}
103−withSocketM :: (MonadMask m, MonadIO m) => Socket -> (Socket -> m ()) -> m ()
103+useSocketM :: (MonadMask m, MonadIO m) => Socket -> (Socket -> m ()) -> m ()
104−withSocketM sk f = do
105−-- f sk `onException` liftIO (Net.close sk)
106−-- liftIO (Net.close sk)
107− finally (liftIO (Net.close sk)) (f sk)
104+useSocketM sk f = finally (liftIO (Net.close sk)) (f sk)
108−
109−-- XXX bracket performs 2x better than 'finally'. That's perhaps because of
110−-- better fusion of "A.flattenArrays . readArrays"?
111−--
112−-- withSocketS :: (IsStream t, MonadCatch m, MonadIO m) => Socket -> t m Word8
113−-- withSocketS sk = A.flattenArrays $
114−-- S.finally (liftIO (Net.close sk)) (readArrays sk)
115
105
116−-- | Like 'withSocketM' but runs a streaming computation instead of a monadic
106+-- | Like 'useSocketM' but runs a streaming computation instead of a monadic
117-- computation.
107-- computation.
118--
108--
119-- @since 0.7.0
109-- @since 0.7.0
120−{-# INLINE withSocket #-}
110+{-# INLINE useSocket #-}
121−withSocket :: (IsStream t, MonadCatch m, MonadIO m)
111+useSocket :: (IsStream t, MonadCatch m, MonadIO m)
122 => Socket -> (Socket -> t m a) -> t m a
112 => Socket -> (Socket -> t m a) -> t m a
123−withSocket sk = S.bracket (return sk) (liftIO . Net.close)
113+useSocket sk = S.bracket (return sk) (liftIO . Net.close)
124
114
125-------------------------------------------------------------------------------
115-------------------------------------------------------------------------------
126-- Array IO (Input)
116-- Array IO (Input)
src/Streamly/Network/Socket.hs+1−5
@@ -23,12 +23,8 @@
23
23
24module Streamly.Network.Socket
24module Streamly.Network.Socket
25 (
25 (
26− -- * Use a socket
27− withSocketM
28− , withSocket
29−
30 -- * Read from connection
26 -- * Read from connection
31− , read
27+ read
32 -- , readUtf8
28 -- , readUtf8
33 -- , readLines
29 -- , readLines
34 -- , readFrames
30 -- , readFrames