@@ -66,10 +66,11 @@ import Prelude hiding (read)
66import qualified Network.Socket as Net
66import qualified Network.Socket as Net
68import Streamly (MonadAsync)
68import Streamly (MonadAsync)
69−import Streamly.Mem.Array.Types (Array(..))
69+import Streamly.Mem.Array.Types (Array(..), defaultChunkSize)
70import Streamly.Streams.Serial (SerialT)
70import Streamly.Streams.Serial (SerialT)
71import Streamly.Streams.StreamK.Type (IsStream)
71import Streamly.Streams.StreamK.Type (IsStream)
73+import qualified Streamly.Mem.Array as A
73import qualified Streamly.Prelude as S
74import qualified Streamly.Prelude as S
74import qualified Streamly.Network.Socket as SK
75import qualified Streamly.Network.Socket as SK
@@ -91,6 +92,7 @@ openConnection addr port = do
91-- 'withConnection' rather than any exception raised by 'act'.
92-- 'withConnection' rather than any exception raised by 'act'.
93-- @since 0.7.0
94-- @since 0.7.0
95+{-# INLINABLE withConnection #-}
94withConnection :: (IsStream t, MonadCatch m, MonadIO m)
96withConnection :: (IsStream t, MonadCatch m, MonadIO m)
95 => (Word8, Word8, Word8, Word8) -> PortNumber -> (Socket -> t m a) -> t m a
97 => (Word8, Word8, Word8, Word8) -> PortNumber -> (Socket -> t m a) -> t m a
96withConnection addr port =
98withConnection addr port =
@@ -103,26 +105,20 @@ withConnection addr port =
103-- | Read a stream from the supplied IPv4 host address and port number.
105-- | Read a stream from the supplied IPv4 host address and port number.
105-- @since 0.7.0
107-- @since 0.7.0
106read :: (IsStream t, MonadCatch m, MonadIO m)
109read :: (IsStream t, MonadCatch m, MonadIO m)
107 => (Word8, Word8, Word8, Word8) -> PortNumber -> t m Word8
110 => (Word8, Word8, Word8, Word8) -> PortNumber -> t m Word8
108−read addr port = withConnection addr port SK.read
111+read addr port = A.flattenArrays $ withConnection addr port SK.readArrays
110-------------------------------------------------------------------------------
113-------------------------------------------------------------------------------
111-- Writing
114-- Writing
112-------------------------------------------------------------------------------
115-------------------------------------------------------------------------------
114−-- | Write a stream to the supplied IPv4 host address and port number.
117−write :: (MonadCatch m, MonadIO m)
118− => (Word8, Word8, Word8, Word8) -> PortNumber -> SerialT m Word8 -> m ()
120− S.drain $ withConnection addr port (\sk -> S.yieldM $ SK.write sk xs)
122-- | Write a stream of arrays to the supplied IPv4 host address and port
117-- | Write a stream of arrays to the supplied IPv4 host address and port
123-- number.
118-- number.
125-- @since 0.7.0
120-- @since 0.7.0
121+{-# INLINE writeArrays #-}
126writeArrays
122writeArrays
127 :: (MonadCatch m, MonadAsync m)
123 :: (MonadCatch m, MonadAsync m)
128 => (Word8, Word8, Word8, Word8)
124 => (Word8, Word8, Word8, Word8)
@@ -131,3 +127,26 @@ writeArrays
132writeArrays addr port xs =
128writeArrays addr port xs =
133 S.drain $ withConnection addr port (\sk -> S.yieldM $ SK.writeArrays sk xs)
129 S.drain $ withConnection addr port (\sk -> S.yieldM $ SK.writeArrays sk xs)
131+-- | Like 'write' but provides control over the write buffer. Output will
132+-- be written to the IO device as soon as we collect the specified number of
136+{-# INLINE writeByChunks #-}
138+ :: (MonadCatch m, MonadAsync m)
140+ -> (Word8, Word8, Word8, Word8)
144+writeByChunks n addr port m = writeArrays addr port $ A.arraysOf n m
146+-- | Write a stream to the supplied IPv4 host address and port number.
150+write :: (MonadCatch m, MonadAsync m)
151+ => (Word8, Word8, Word8, Word8) -> PortNumber -> SerialT m Word8 -> m ()
152+write = writeByChunks defaultChunkSize