on master help sign in
Export forSocketM, rename transformBytesWith
commitae1ffbf520e4a11b6cba24fa2734943bc954a209 authorHarendra Kumar <harendra@composewell.com> dateThu 22 Apr 2021, 00:04 (6 years ago) parent22a42d0
6 files changed+39−32
src/Streamly/Internal/Network/Inet/TCP.hs+4−5 src/Streamly/Internal/Network/Socket.hs+18−16 src/Streamly/Network/Inet/TCP.hs+4 src/Streamly/Network/Socket.hs+9−7 test/Streamly/Test/Network/Inet/TCP.hs+2−2 test/Streamly/Test/Network/Socket.hs+2−2
src/Streamly/Internal/Network/Inet/TCP.hs+4−5
@@ -68,7 +68,7 @@ module Streamly.Internal.Network.Inet.TCP
6868 , fromChunks
6969
7070 -- ** Transformation
71− , transformBytesWith
71+ , processBytes
7272 {-
7373 -- ** Sink Servers
7474
@@ -446,12 +446,11 @@ withInputConnect addr port input f = S.bracket pre post handler
446446--
447447-- /Pre-release/
448448--
449−{-# INLINABLE transformBytesWith #-}
450−transformBytesWith
449+{-# INLINABLE processBytes #-}
450+processBytes
451451 :: (IsStream t, MonadAsync m, MonadCatch m)
452452 => (Word8, Word8, Word8, Word8)
453453 -> PortNumber
454454 -> SerialT m Word8
455455 -> t m Word8
456−transformBytesWith addr port input =
457− withInputConnect addr port input ISK.toBytes
456+processBytes addr port input = withInputConnect addr port input ISK.toBytes
src/Streamly/Internal/Network/Socket.hs+18−16
@@ -13,8 +13,8 @@ module Streamly.Internal.Network.Socket
1313 (
1414 SockSpec (..)
1515 -- * Use a socket
16− , handleWithM
17− , handleWith
16+ , forSocketM
17+ , withSocket
1818
1919 -- * Accept connections
2020 , accept
@@ -105,25 +105,27 @@ import qualified Streamly.Internal.Data.Array.Foreign.Type as A
105105import qualified Streamly.Internal.Data.Stream.IsStream as S
106106import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
107107
108−-- | @'handleWithM' socket act@ runs the monadic computation @act@ passing the
109−-- socket handle to it. The handle will be closed on exit from 'handleWithM',
110−-- whether by normal termination or by raising an exception. If closing the
111−-- handle raises an exception, then this exception will be raised by
112−-- 'handleWithM' rather than any exception raised by 'act'.
108+-- | @'forSocketM' action socket@ runs the monadic computation @action@ passing
109+-- the socket handle to it. The handle will be closed on exit from
110+-- 'forSocketM', whether by normal termination or by raising an exception. If
111+-- closing the handle raises an exception, then this exception will be raised
112+-- by 'forSocketM' rather than any exception raised by 'action'.
113113--
114−-- @since 0.7.0
115−{-# INLINE handleWithM #-}
116−handleWithM :: (MonadMask m, MonadIO m) => (Socket -> m ()) -> Socket -> m ()
117−handleWithM f sk = finally (f sk) (liftIO (Net.close sk))
114+-- @since 0.8.0
115+{-# INLINE forSocketM #-}
116+forSocketM :: (MonadMask m, MonadIO m) => (Socket -> m ()) -> Socket -> m ()
117+forSocketM f sk = finally (f sk) (liftIO (Net.close sk))
118118
119−-- | Like 'handleWithM' but runs a streaming computation instead of a monadic
119+-- | Like 'forSocketM' but runs a streaming computation instead of a monadic
120120-- computation.
121121--
122−-- @since 0.7.0
123−{-# INLINE handleWith #-}
124−handleWith :: (IsStream t, MonadAsync m, MonadCatch m)
122+-- /Inhibits stream fusion/
123+--
124+-- /Internal/
125+{-# INLINE withSocket #-}
126+withSocket :: (IsStream t, MonadAsync m, MonadCatch m)
125127 => Socket -> (Socket -> t m a) -> t m a
126−handleWith sk f = S.finally (liftIO $ Net.close sk) (f sk)
128+withSocket sk f = S.finally (liftIO $ Net.close sk) (f sk)
127129
128130-------------------------------------------------------------------------------
129131-- Accept (Unfolds)
src/Streamly/Network/Inet/TCP.hs+4
@@ -22,6 +22,10 @@ module Streamly.Network.Inet.TCP
2222 -- * Connect to Servers
2323 , connect
2424
25+ -- XXX Expose this as a pipe when we have pipes.
26+ -- * Transformation
27+ -- , processBytes
28+
2529 {-
2630 -- ** Sink Servers
2731
src/Streamly/Network/Socket.hs+9−7
@@ -2,7 +2,7 @@
22-- Module : Streamly.Network.Socket
33-- Copyright : (c) 2018 Composewell Technologies
44--
5−-- License : BSD3
5+-- License : BSD-3-Clause
66-- Maintainer : streamly@composewell.com
77-- Stability : experimental
88-- Portability : GHC
@@ -32,7 +32,6 @@
3232--
3333-- import Data.Function ((&))
3434-- import Network.Socket
35−-- import Streamly.Internal.Network.Socket (handleWithM)
3635-- import Streamly.Network.Socket (SockSpec(..))
3736--
3837-- import qualified Streamly.Prelude as Stream
@@ -52,8 +51,8 @@
5251--
5352-- server spec addr =
5453-- Stream.unfold Socket.accept (maxListenQueue, spec, addr) -- ParallelT IO Socket
55−-- & Stream.mapM (handleWithM echo) -- ParallelT IO ()
56−-- & fromParallel -- SerialT IO ()
54+-- & Stream.mapM (Socket.forSocketM echo) -- ParallelT IO ()
55+-- & Stream.fromParallel -- SerialT IO ()
5756-- & Stream.drain -- IO ()
5857--
5958-- echo sk =
@@ -64,12 +63,12 @@
6463-- = Programmer Notes
6564--
6665-- Read IO requests to connected stream sockets are performed in chunks of
67−-- 'Streamly.Internal.Data.Array.Foreign.Type.defaultChunkSize'. Unless specified
68−-- otherwise in the API, writes are collected into chunks of
66+-- 'Streamly.Internal.Data.Array.Foreign.Type.defaultChunkSize'. Unless
67+-- specified otherwise in the API, writes are collected into chunks of
6968-- 'Streamly.Internal.Data.Array.Foreign.Type.defaultChunkSize' before they are
7069-- written to the socket. APIs are provided to control the chunking behavior.
7170--
72−-- > import qualified Streamly.Network.Socket as SK
71+-- > import qualified Streamly.Network.Socket as Socket
7372--
7473-- = See Also
7574--
@@ -113,6 +112,9 @@ module Streamly.Network.Socket
113112 , writeChunks
114113 , writeChunksWithBufferOf
115114 , writeChunk
115+
116+ -- * Exceptions
117+ , forSocketM
116118 )
117119where
118120
test/Streamly/Test/Network/Inet/TCP.hs+2−2
@@ -72,7 +72,7 @@ server
7272server listener port sem handler = do
7373 putMVar sem ()
7474 Stream.fromSerial (Stream.unfold listener port)
75− & (Stream.fromAsync . Stream.mapM (Socket.handleWithM handler))
75+ & (Stream.fromAsync . Stream.mapM (Socket.forSocketM handler))
7676 & Stream.drain
7777
7878remoteAddr :: (Word8,Word8,Word8,Word8)
@@ -85,7 +85,7 @@ sender port sem = do
8585 Stream.replicate 1000 testData -- SerialT IO String
8686 & Stream.concatMap Stream.fromList -- SerialT IO Char
8787 & Unicode.encodeLatin1 -- SerialT IO Word8
88− & TCP.transformBytesWith remoteAddr port -- SerialT IO Word8
88+ & TCP.processBytes remoteAddr port -- SerialT IO Word8
8989 & Unicode.decodeLatin1 -- SerialT IO Char
9090
9191execute
test/Streamly/Test/Network/Socket.hs+2−2
@@ -83,7 +83,7 @@ server :: PortNumber -> MVar () -> (Socket -> IO ()) -> IO ()
8383server port sem handler = do
8484 putMVar sem ()
8585 Stream.fromSerial (Stream.unfold TCP.acceptOnPort port)
86− & Stream.fromAsync . Stream.mapM (Socket.handleWithM handler)
86+ & Stream.fromAsync . Stream.mapM (Socket.forSocketM handler)
8787 & Stream.drain
8888
8989remoteAddr :: (Word8,Word8,Word8,Word8)
@@ -96,7 +96,7 @@ sender port sem = do
9696 Stream.replicate 1000 testData -- SerialT IO String
9797 & Stream.concatMap Stream.fromList -- SerialT IO Char
9898 & Unicode.encodeLatin1 -- SerialT IO Word8
99− & TCP.transformBytesWith remoteAddr port -- SerialT IO Word8
99+ & TCP.processBytes remoteAddr port -- SerialT IO Word8
100100 & Unicode.decodeLatin1 -- SerialT IO Char
101101
102102execute :: PortNumber -> Int -> (Socket -> IO ()) -> IO (SerialT IO Char)