@@ -62,6 +62,7 @@ module Streamly.Internal.Network.Socket
62 -- -- * Array Write
62 -- -- * Array Write
63 , writeChunk
63 , writeChunk
64 , writeChunks
64 , writeChunks
65+ , writeChunksWithBufferOf
65 , writeStrings
66 , writeStrings
67 -- reading/writing datagrams
68 -- reading/writing datagrams
@@ -94,7 +95,7 @@ import qualified Network.Socket as Net
95import Streamly (MonadAsync)
96import Streamly (MonadAsync)
96import Streamly.Internal.Data.Unfold.Types (Unfold(..))
97import Streamly.Internal.Data.Unfold.Types (Unfold(..))
97−import Streamly.Internal.Memory.Array.Types (Array(..))
98+import Streamly.Internal.Memory.Array.Types (Array(..), lpackArraysChunksOf)
98import Streamly.Internal.Data.Stream.Serial (SerialT)
99import Streamly.Internal.Data.Stream.Serial (SerialT)
99import Streamly.Internal.Data.Stream.StreamK.Type (IsStream, mkStream)
100import Streamly.Internal.Data.Stream.StreamK.Type (IsStream, mkStream)
100import Streamly.Data.Fold (Fold)
101import Streamly.Data.Fold (Fold)
@@ -426,6 +427,18 @@ fromChunks h m = S.mapM_ (liftIO . writeChunk h) m
426writeChunks :: (MonadIO m, Storable a) => Socket -> Fold m (Array a) ()
427writeChunks :: (MonadIO m, Storable a) => Socket -> Fold m (Array a) ()
427writeChunks h = FL.drainBy (liftIO . writeChunk h)
428writeChunks h = FL.drainBy (liftIO . writeChunk h)
430+-- | @writeChunksWithBufferOf bufsize socket@ writes a stream of arrays
431+-- to @socket@ after coalescing the adjacent arrays in chunks of @bufsize@.
432+-- We never split an array, if a single array is bigger than the specified size
433+-- it emitted as it is. Multiple arrays are coalesed as long as the total size
434+-- remains below the specified size.
437+{-# INLINE writeChunksWithBufferOf #-}
438+writeChunksWithBufferOf :: (MonadIO m, Storable a)
439+ => Int -> Socket -> Fold m (Array a) ()
440+writeChunksWithBufferOf n h = lpackArraysChunksOf n (writeChunks h)
429-- | Write a stream of strings to a socket in Latin1 encoding. Output is
442-- | Write a stream of strings to a socket in Latin1 encoding. Output is
430-- flushed to the socket for each string.
443-- flushed to the socket for each string.