on 770887d help sign in
648 lines 22 kB history raw
1#include "inline.hs"
3-- |
4-- Module : Streamly.Internal.Network.Socket
5-- Copyright : (c) 2018 Composewell Technologies
6--
7-- License : BSD3
8-- Maintainer : streamly@composewell.com
9-- Stability : experimental
10-- Portability : GHC
11--
12module Streamly.Internal.Network.Socket
13 (
14 SockSpec (..)
15 -- * Use a socket
16 , forSocketM
17 , withSocket
19 -- * Accept connections
20 , accept
21 , connections
22 , connect
23 , connectFrom
25 -- * Read from connection
26 , read
27 , readWith
28 -- , readUtf8
29 -- , readLines
30 -- , readFrames
31 -- , readByChunks
33 -- -- * Array Read
34 -- , readArrayUpto
35 -- , readChunksUpto
36 , readChunk
37 , readChunks
38 , readChunksWith
40 , toChunksWith
41 , toChunks
42 , toBytes
44 -- * Write to connection
45 , write
46 -- , writeUtf8
47 -- , writeUtf8ByLines
48 -- , writeByFrames
49 , writeWith
50 , writeMaybesWith
52 , putChunks
53 , putBytesWith
54 , putBytes
56 -- -- * Array Write
57 , writeChunk
58 , writeChunks
59 , writeChunksWith
61 -- reading/writing datagrams
63 -- * Deprecated
64 , readWithBufferOf
65 , readChunksWithBufferOf
66 , writeWithBufferOf
67 , writeChunksWithBufferOf
68 )
69where
71import Control.Concurrent (threadWaitWrite, rtsSupportsBoundThreads)
72import Control.Exception (onException)
73import Control.Monad.Catch (MonadCatch, finally, MonadMask)
74import Control.Monad.IO.Class (MonadIO(..))
75import Control.Monad (forM_, when)
76import Data.Maybe (isNothing, fromJust)
77import Data.Word (Word8)
78import Foreign.Ptr (plusPtr, Ptr, castPtr)
79import Streamly.Internal.Data.Unboxed (Unboxed)
80import Network.Socket
81 (Socket, SocketOption(..), Family(..), SockAddr(..),
82 ProtocolNumber, withSocketsDo, SocketType(..), socket, bind,
83 setSocketOption, sendBuf, recvBuf)
84#if MIN_VERSION_network(3,1,0)
85import Network.Socket (withFdSocket)
86#else
87import Network.Socket (fdSocket)
88#endif
89import Prelude hiding (read)
91import qualified Network.Socket as Net
93import Streamly.Internal.Control.Concurrent (MonadAsync)
94import Streamly.Internal.Data.Array.Unboxed.Type (Array(..))
95import Streamly.Internal.Data.Array.Unboxed.Stream (lpackArraysChunksOf)
96import Streamly.Internal.Data.Fold (Fold)
97import Streamly.Internal.Data.Stream.Type (Stream)
98import Streamly.Internal.Data.Unfold.Type (Unfold(..))
99-- import Streamly.String (encodeUtf8, decodeUtf8, foldLines)
100import Streamly.Internal.System.IO (defaultChunkSize)
102import qualified Streamly.Data.Array.Unboxed as A (read, length, writeN)
103import qualified Streamly.Data.Fold as FL
104import qualified Streamly.Internal.Data.Array.Unboxed.Type as A
105 (unsafeFreeze, asPtrUnsafe, byteLength, writeNUnsafe)
106import qualified Streamly.Internal.Data.Array.Unboxed.Mut as MArray
107 (Array(..), newPinnedBytes, asPtrUnsafe)
108import qualified Streamly.Internal.Data.Array.Unboxed.Stream as AS
109import qualified Streamly.Internal.Data.Stream as S
110import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
111 (Stream(..), Step(..))
112import qualified Streamly.Data.Unfold as UF
113import qualified Streamly.Internal.Data.Unfold as UF (first, map)
114import qualified Streamly.Internal.Data.Stream.StreamK.Type as K (mkStream)
116-- | @'forSocketM' action socket@ runs the monadic computation @action@ passing
117-- the socket handle to it. The handle will be closed on exit from
118-- 'forSocketM', whether by normal termination or by raising an exception. If
119-- closing the handle raises an exception, then this exception will be raised
120-- by 'forSocketM' rather than any exception raised by 'action'.
121--
122-- @since 0.8.0
123{-# INLINE forSocketM #-}
124forSocketM :: (MonadMask m, MonadIO m) => (Socket -> m ()) -> Socket -> m ()
125forSocketM f sk = finally (f sk) (liftIO (Net.close sk))
127-- | Like 'forSocketM' but runs a streaming computation instead of a monadic
128-- computation.
129--
130-- /Inhibits stream fusion/
131--
132-- /Internal/
133{-# INLINE withSocket #-}
134withSocket :: (MonadAsync m, MonadCatch m)
135 => Socket -> (Socket -> Stream m a) -> Stream m a
136withSocket sk f = S.finally (liftIO $ Net.close sk) (f sk)
138-------------------------------------------------------------------------------
139-- Accept (Unfolds)
140-------------------------------------------------------------------------------
142-- XXX Protocol specific socket options should be separated from socket level
143-- options.
144--
145-- | Specify the socket protocol details.
146data SockSpec = SockSpec
147 {
148 sockFamily :: !Family
149 , sockType :: !SocketType
150 , sockProto :: !ProtocolNumber
151 , sockOpts :: ![(SocketOption, Int)]
152 }
154initListener :: Int -> SockSpec -> SockAddr -> IO Socket
155initListener listenQLen SockSpec{..} addr =
156 withSocketsDo $ do
157 sock <- socket sockFamily sockType sockProto
158 use sock `onException` Net.close sock
159 return sock
161 where
163 use sock = do
164 mapM_ (\(opt, val) -> setSocketOption sock opt val) sockOpts
165 bind sock addr
166 Net.listen sock listenQLen
168{-# INLINE listenTuples #-}
169listenTuples :: MonadIO m
170 => Unfold m (Int, SockSpec, SockAddr) (Socket, SockAddr)
171listenTuples = Unfold step inject
172 where
173 inject (listenQLen, spec, addr) =
174 liftIO $ initListener listenQLen spec addr
176 step listener = do
177 r <- liftIO (Net.accept listener `onException` Net.close listener)
178 return $ D.Yield r listener
180-- | Unfold a three tuple @(listenQLen, spec, addr)@ into a stream of connected
181-- protocol sockets corresponding to incoming connections. @listenQLen@ is the
182-- maximum number of pending connections in the backlog. @spec@ is the socket
183-- protocol and options specification and @addr@ is the protocol address where
184-- the server listens for incoming connections.
185--
186-- @since 0.7.0
187{-# INLINE accept #-}
188accept :: MonadIO m => Unfold m (Int, SockSpec, SockAddr) Socket
189accept = UF.map fst listenTuples
191{-# INLINE connectCommon #-}
192connectCommon :: SockSpec -> Maybe SockAddr -> SockAddr -> IO Socket
193connectCommon SockSpec{..} local remote = withSocketsDo $ do
194 sock <- socket sockFamily sockType sockProto
195 use sock `onException` Net.close sock
196 return sock
198 where
200 use sock = do
201 mapM_ (\(opt, val) -> setSocketOption sock opt val) sockOpts
202 forM_ local (bind sock)
203 Net.connect sock remote
205-- | Connect to a remote host using the given socket specification and remote
206-- address. Returns a connected socket or throws an exception.
207--
208-- /Pre-release/
209--
210{-# INLINE connect #-}
211connect :: SockSpec -> SockAddr -> IO Socket
212connect spec = connectCommon spec Nothing
214-- | Connect to a remote host using the given socket specification, a local
215-- address to bind to and a remote address to connect to. Returns a connected
216-- socket or throws an exception.
217--
218-- /Pre-release/
219--
220{-# INLINE connectFrom #-}
221connectFrom :: SockSpec -> SockAddr -> SockAddr -> IO Socket
222connectFrom spec local = connectCommon spec (Just local)
224-------------------------------------------------------------------------------
225-- Listen (Streams)
226-------------------------------------------------------------------------------
228{-# INLINE recvConnectionTuplesWith #-}
229recvConnectionTuplesWith :: MonadAsync m
230 => Int -> SockSpec -> SockAddr -> Stream m (Socket, SockAddr)
231recvConnectionTuplesWith tcpListenQ spec addr = S.unfoldrM step Nothing
232 where
233 step Nothing = do
234 listener <- liftIO $ initListener tcpListenQ spec addr
235 r <- liftIO (Net.accept listener `onException` Net.close listener)
236 return $ Just (r, Just listener)
238 step (Just listener) = do
239 r <- liftIO (Net.accept listener `onException` Net.close listener)
240 return $ Just (r, Just listener)
242-- | Start a TCP stream server that listens for connections on the supplied
243-- server address specification (address family, local interface IP address and
244-- port). The server generates a stream of connected sockets. The first
245-- argument is the maximum number of pending connections in the backlog.
246--
247-- /Pre-release/
248{-# INLINE connections #-}
249connections :: MonadAsync m => Int -> SockSpec -> SockAddr -> Stream m Socket
250connections tcpListenQ spec addr =
251 fst <$> recvConnectionTuplesWith tcpListenQ spec addr
253-------------------------------------------------------------------------------
254-- Array IO (Input)
255-------------------------------------------------------------------------------
257{-# INLINABLE readArrayUptoWith #-}
258readArrayUptoWith
259 :: (h -> Ptr Word8 -> Int -> IO Int)
260 -> Int
261 -> h
262 -> IO (Array Word8)
263readArrayUptoWith f size h = do
264 arr <- MArray.newPinnedBytes size
265 -- ptr <- mallocPlainForeignPtrAlignedBytes size (alignment (undefined :: Word8))
266 MArray.asPtrUnsafe arr $ \p -> do
267 n <- f h p size
268 let v = A.unsafeFreeze
269 $ arr { MArray.arrEnd = n, MArray.arrBound = size }
271 -- XXX shrink only if the diff is significant
272 -- A.shrinkToFit v
273 return v
275-- | Read a byte array from a file handle up to a maximum of the requested
276-- size. If no data is available on the handle it blocks until some data
277-- becomes available. If data is available then it immediately returns that
278-- data without blocking.
279--
280-- @since 0.8.0
281{-# INLINABLE readChunk #-}
282readChunk :: Int -> Socket -> IO (Array Word8)
283readChunk = readArrayUptoWith recvBuf
285-------------------------------------------------------------------------------
286-- Array IO (output)
287-------------------------------------------------------------------------------
289waitWhen0 :: Int -> Socket -> IO ()
290waitWhen0 0 s = when rtsSupportsBoundThreads $
291#if MIN_VERSION_network(3,1,0)
292 withFdSocket s $ \fd -> threadWaitWrite $ fromIntegral fd
293#elif MIN_VERSION_network(3,0,0)
294 fdSocket s >>= threadWaitWrite . fromIntegral
295#else
296 let fd = fdSocket s in threadWaitWrite $ fromIntegral fd
297#endif
298waitWhen0 _ _ = return ()
300sendAll :: Socket -> Ptr Word8 -> Int -> IO ()
301sendAll _ _ len | len <= 0 = return ()
302sendAll s p len = do
303 sent <- sendBuf s p len
304 waitWhen0 sent s
305 -- assert (sent <= len)
306 when (sent >= 0) $ sendAll s (p `plusPtr` sent) (len - sent)
308{-# INLINABLE writeArrayWith #-}
309writeArrayWith :: Unboxed a
310 => (h -> Ptr Word8 -> Int -> IO ())
311 -> h
312 -> Array a
313 -> IO ()
314writeArrayWith _ _ arr | A.length arr == 0 = return ()
315writeArrayWith f h arr = A.asPtrUnsafe arr $ \ptr -> f h (castPtr ptr) aLen
317 where
319 aLen = A.byteLength arr
321-- | Write an Array to a file handle.
322--
323-- @since 0.8.0
324{-# INLINABLE writeChunk #-}
325writeChunk :: Unboxed a => Socket -> Array a -> IO ()
326writeChunk = writeArrayWith sendAll
328-------------------------------------------------------------------------------
329-- Stream of Arrays IO
330-------------------------------------------------------------------------------
332{-# INLINE _readChunksUptoWith #-}
333_readChunksUptoWith :: (MonadIO m)
334 => (Int -> h -> IO (Array Word8))
335 -> Int -> h -> Stream m (Array Word8)
336_readChunksUptoWith f size h = S.fromStreamK go
337 where
338 -- XXX use cons/nil instead
339 go = K.mkStream $ \_ yld _ stp -> do
340 arr <- liftIO $ f size h
341 if A.length arr == 0
342 then stp
343 else yld arr go
345-- | @toChunksWith size h@ reads a stream of arrays from file handle @h@.
346-- The maximum size of a single array is limited to @size@.
347-- 'fromHandleArraysUpto' ignores the prevailing 'TextEncoding' and 'NewlineMode'
348-- on the 'Handle'.
349{-# INLINE_NORMAL toChunksWith #-}
350toChunksWith :: (MonadIO m)
351 => Int -> Socket -> Stream m (Array Word8)
352-- toChunksWith = _readChunksUptoWith readChunk
353toChunksWith size h = S.fromStreamD (D.Stream step ())
354 where
355 {-# INLINE_LATE step #-}
356 step _ _ = do
357 arr <- liftIO $ readChunk size h
358 return $
359 case A.length arr of
360 0 -> D.Stop
361 _ -> D.Yield arr ()
363-- XXX read 'Array a' instead of Word8
364--
365-- | @toChunks h@ reads a stream of arrays from socket handle @h@.
366-- The maximum size of a single array is limited to @defaultChunkSize@.
367--
368-- @since 0.7.0
369{-# INLINE toChunks #-}
370toChunks :: (MonadIO m) => Socket -> Stream m (Array Word8)
371toChunks = toChunksWith defaultChunkSize
373-- | Unfold the tuple @(bufsize, socket)@ into a stream of 'Word8' arrays.
374-- Read requests to the socket are performed using a buffer of size @bufsize@.
375-- The size of an array in the resulting stream is always less than or equal to
376-- @bufsize@.
377--
378-- @since 0.9.0
379{-# INLINE_NORMAL readChunksWith #-}
380readChunksWith :: MonadIO m => Unfold m (Int, Socket) (Array Word8)
381readChunksWith = Unfold step return
382 where
383 {-# INLINE_LATE step #-}
384 step (size, h) = do
385 arr <- liftIO $ readChunk size h
386 return $
387 case A.length arr of
388 0 -> D.Stop
389 _ -> D.Yield arr (size, h)
391-- | Same as 'readChunksWith'
392--
393-- @since 0.7.0
394{-# DEPRECATED readChunksWithBufferOf "Please use 'readChunksWith' instead" #-}
395{-# INLINE_NORMAL readChunksWithBufferOf #-}
396readChunksWithBufferOf :: MonadIO m => Unfold m (Int, Socket) (Array Word8)
397readChunksWithBufferOf = readChunksWith
399-- | Unfolds a socket into a stream of 'Word8' arrays. Requests to the socket
400-- are performed using a buffer of size
401-- 'Streamly.Internal.Data.Array.Unboxed.Type.defaultChunkSize'. The
402-- size of arrays in the resulting stream are therefore less than or equal to
403-- 'Streamly.Internal.Data.Array.Unboxed.Type.defaultChunkSize'.
404--
405-- @since 0.7.0
406{-# INLINE readChunks #-}
407readChunks :: MonadIO m => Unfold m Socket (Array Word8)
408readChunks = UF.first defaultChunkSize readChunksWith
410-------------------------------------------------------------------------------
411-- Read File to Stream
412-------------------------------------------------------------------------------
414-- TODO for concurrent streams implement readahead IO. We can send multiple
415-- read requests at the same time. For serial case we can use async IO. We can
416-- also control the read throughput in mbps or IOPS.
418{-
419-- | @readWith bufsize handle@ reads a byte stream from a file
420-- handle, reads are performed in chunks of up to @bufsize@. The stream ends
421-- as soon as EOF is encountered.
422--
423{-# INLINE readWith #-}
424readWith :: (MonadIO m) => Int -> Handle -> Stream m Word8
425readWith chunkSize h = A.flattenArrays $ readChunksUpto chunkSize h
426-}
428-- TODO
429-- read :: (MonadIO m, Unboxed a) => Handle -> Stream m a
430--
431-- > read = 'readByChunks' defaultChunkSize
432-- | Generate a stream of elements of the given type from a socket. The
433-- stream ends when EOF is encountered.
434--
435-- @since 0.7.0
436{-# INLINE toBytes #-}
437toBytes :: MonadIO m => Socket -> Stream m Word8
438toBytes = AS.concat . toChunks
440-- | Unfolds the tuple @(bufsize, socket)@ into a byte stream, read requests
441-- to the socket are performed using buffers of @bufsize@.
442--
443-- @since 0.9.0
444{-# INLINE readWith #-}
445readWith :: MonadIO m => Unfold m (Int, Socket) Word8
446readWith = UF.many A.read readChunksWith
448-- | Same as 'readWith'
449--
450-- @since 0.7.0
451{-# DEPRECATED readWithBufferOf "Please use 'readWith' instead" #-}
452{-# INLINE readWithBufferOf #-}
453readWithBufferOf :: MonadIO m => Unfold m (Int, Socket) Word8
454readWithBufferOf = readWith
456-- | Unfolds a 'Socket' into a byte stream. IO requests to the socket are
457-- performed in sizes of
458-- 'Streamly.Internal.Data.Array.Unboxed.Type.defaultChunkSize'.
459--
460-- @since 0.7.0
461{-# INLINE read #-}
462read :: MonadIO m => Unfold m Socket Word8
463read = UF.first defaultChunkSize readWith
465-------------------------------------------------------------------------------
466-- Writing
467-------------------------------------------------------------------------------
469-- | Write a stream of arrays to a handle.
470--
471-- @since 0.7.0
472{-# INLINE putChunks #-}
473putChunks :: (MonadIO m, Unboxed a)
474 => Socket -> Stream m (Array a) -> m ()
475putChunks h = S.fold (FL.drainBy (liftIO . writeChunk h))
477-- | Write a stream of arrays to a socket. Each array in the stream is written
478-- to the socket as a separate IO request.
479--
480-- @since 0.7.0
481{-# INLINE writeChunks #-}
482writeChunks :: (MonadIO m, Unboxed a) => Socket -> Fold m (Array a) ()
483writeChunks h = FL.drainBy (liftIO . writeChunk h)
485-- | @writeChunksWith bufsize socket@ writes a stream of arrays to
486-- @socket@ after coalescing the adjacent arrays in chunks of @bufsize@.
487-- Multiple arrays are coalesed as long as the total size remains below the
488-- specified size. It never splits an array, if a single array is bigger than
489-- the specified size it emitted as it is.
490--
491-- @since 0.9.0
492{-# INLINE writeChunksWith #-}
493writeChunksWith :: (MonadIO m, Unboxed a)
494 => Int -> Socket -> Fold m (Array a) ()
495writeChunksWith n h = lpackArraysChunksOf n (writeChunks h)
497-- | Same as 'writeChunksWith'
498--
499-- @since 0.7.0
500{-# DEPRECATED writeChunksWithBufferOf "Please use 'writeChunksWith' instead" #-}
501{-# INLINE writeChunksWithBufferOf #-}
502writeChunksWithBufferOf :: (MonadIO m, Unboxed a)
503 => Int -> Socket -> Fold m (Array a) ()
504writeChunksWithBufferOf = writeChunksWith
506-- GHC buffer size dEFAULT_FD_BUFFER_SIZE=8192 bytes.
507--
508-- XXX test this
509-- Note that if you use a chunk size less than 8K (GHC's default buffer
510-- size) then you are advised to use 'NOBuffering' mode on the 'Handle' in case you
511-- do not want buffering to occur at GHC level as well. Same thing applies to
512-- writes as well.
514-- | Like 'write' but provides control over the write buffer. Output will
515-- be written to the IO device as soon as we collect the specified number of
516-- input elements.
517--
518-- @since 0.9.0
519{-# INLINE putBytesWith #-}
520putBytesWith :: MonadIO m => Int -> Socket -> Stream m Word8 -> m ()
521putBytesWith n h m = putChunks h $ AS.arraysOf n m
523-- | Write a byte stream to a socket. Accumulates the input in chunks of
524-- specified number of bytes before writing.
525--
526-- @since 0.9.0
527{-# INLINE writeWith #-}
528writeWith :: MonadIO m => Int -> Socket -> Fold m Word8 ()
529writeWith n h = FL.chunksOf n (A.writeNUnsafe n) (writeChunks h)
531-- | Same as 'writeWith'
532--
533-- @since 0.7.0
534{-# DEPRECATED writeWithBufferOf "Please use 'writeWith' instead" #-}
535{-# INLINE writeWithBufferOf #-}
536writeWithBufferOf :: MonadIO m => Int -> Socket -> Fold m Word8 ()
537writeWithBufferOf = writeWith
539-- | Write a stream of 'Maybe' values. Keep buffering the 'Just' values in an
540-- array. Write the array to the 'Handle' as soon as a 'Nothing' is encountered
541-- or the buffer size exceeds the specified limit.
542--
543-- /Pre-release/
544{-# INLINE writeMaybesWith #-}
545writeMaybesWith :: (MonadIO m )
546 => Int -> Socket -> Fold m (Maybe Word8) ()
547writeMaybesWith n h =
548 let writeNJusts = FL.lmap fromJust $ A.writeN n
549 writeOnNothing = FL.takeEndBy_ isNothing writeNJusts
550 in FL.many writeOnNothing (writeChunks h)
552-- > write = 'writeWith' defaultChunkSize
553--
554-- | Write a byte stream to a file handle. Combines the bytes in chunks of size
555-- up to 'defaultChunkSize' before writing. Note that the write behavior
556-- depends on the 'IOMode' and the current seek position of the handle.
557--
558-- @since 0.7.0
559{-# INLINE putBytes #-}
560putBytes :: MonadIO m => Socket -> Stream m Word8 -> m ()
561putBytes = putBytesWith defaultChunkSize
563-- | Write a byte stream to a socket. Accumulates the input in chunks of
564-- up to 'defaultChunkSize' bytes before writing.
565--
566-- @
567-- write = 'writeWith' 'defaultChunkSize'
568-- @
569--
570-- @since 0.7.0
571{-# INLINE write #-}
572write :: MonadIO m => Socket -> Fold m Word8 ()
573write = writeWith defaultChunkSize
575{-
576{-# INLINE write #-}
577write :: (MonadIO m, Unboxed a) => Handle -> Stream m a -> m ()
578write = toHandleWith defaultChunkSize
579-}
581-------------------------------------------------------------------------------
582-- IO with encoding/decoding Unicode characters
583-------------------------------------------------------------------------------
585{-
586-- |
587-- > readUtf8 = decodeUtf8 . read
588--
589-- Read a UTF8 encoded stream of unicode characters from a file handle.
590--
591-- @since 0.7.0
592{-# INLINE readUtf8 #-}
593readUtf8 :: MonadIO m => Handle -> Stream m Char
594readUtf8 = decodeUtf8 . read
596-- |
597-- > writeUtf8 h s = write h $ encodeUtf8 s
598--
599-- Encode a stream of unicode characters to UTF8 and write it to the given file
600-- handle. Default block buffering applies to the writes.
601--
602-- @since 0.7.0
603{-# INLINE writeUtf8 #-}
604writeUtf8 :: MonadIO m => Handle -> Stream m Char -> m ()
605writeUtf8 h s = write h $ encodeUtf8 s
607-- | Write a stream of unicode characters after encoding to UTF-8 in chunks
608-- separated by a linefeed character @'\n'@. If the size of the buffer exceeds
609-- @defaultChunkSize@ and a linefeed is not yet found, the buffer is written
610-- anyway. This is similar to writing to a 'Handle' with the 'LineBuffering'
611-- option.
612--
613-- @since 0.7.0
614{-# INLINE writeUtf8ByLines #-}
615writeUtf8ByLines :: MonadIO m => Handle -> Stream m Char -> m ()
616writeUtf8ByLines = undefined
618-- | Read UTF-8 lines from a file handle and apply the specified fold to each
619-- line. This is similar to reading a 'Handle' with the 'LineBuffering' option.
620--
621-- @since 0.7.0
622{-# INLINE readLines #-}
623readLines :: MonadIO m => Handle -> Fold m Char b -> Stream m b
624readLines h f = foldLines (readUtf8 h) f
626-------------------------------------------------------------------------------
627-- Framing on a sequence
628-------------------------------------------------------------------------------
630-- | Read a stream from a file handle and split it into frames delimited by
631-- the specified sequence of elements. The supplied fold is applied on each
632-- frame.
633--
634-- @since 0.7.0
635{-# INLINE readFrames #-}
636readFrames :: (MonadIO m, Unboxed a)
637 => Array a -> Handle -> Fold m a b -> Stream m b
638readFrames = undefined -- foldFrames . read
640-- | Write a stream to the given file handle buffering up to frames separated
641-- by the given sequence or up to a maximum of @defaultChunkSize@.
642--
643-- @since 0.7.0
644{-# INLINE writeByFrames #-}
645writeByFrames :: (MonadIO m, Unboxed a)
646 => Array a -> Handle -> Stream m a -> m ()
647writeByFrames = undefined
648-}