2{-# LANGUAGE BangPatterns #-}
3{-# LANGUAGE FlexibleContexts #-}
4{-# LANGUAGE MagicHash #-}
5{-# LANGUAGE RecordWildCards #-}
6{-# LANGUAGE UnboxedTuples #-}
11-- Module : Streamly.Internal.Network.Socket
12-- Copyright : (c) 2018 Composewell Technologies
15-- Maintainer : streamly@composewell.com
16-- Stability : experimental
19module Streamly.Internal.Network.Socket
25 -- * Read from connection
44 -- * Write to connection
55 -- reading/writing datagrams
59import Control.Concurrent (threadWaitWrite, rtsSupportsBoundThreads)
60import Control.Monad.Catch (MonadCatch, finally, MonadMask)
61import Control.Monad.IO.Class (MonadIO(..))
62import Control.Monad (when)
63import Data.Word (Word8)
64import Foreign.ForeignPtr (withForeignPtr)
65import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
66import Foreign.Ptr (minusPtr, plusPtr, Ptr, castPtr)
67import Foreign.Storable (Storable(..))
68import GHC.ForeignPtr (mallocPlainForeignPtrBytes)
69import Network.Socket (Socket, sendBuf, recvBuf)
70#if MIN_VERSION_network(3,1,0)
71import Network.Socket (withFdSocket)
73import Network.Socket (fdSocket)
75import Prelude hiding (read)
77import qualified Network.Socket as Net
79import Streamly.Internal.Data.Unfold.Types (Unfold(..))
80import Streamly.Internal.Memory.Array.Types (Array(..))
81import Streamly.Streams.Serial (SerialT)
82import Streamly.Streams.StreamK.Type (IsStream, mkStream)
83import Streamly.Data.Fold (Fold)
84-- import Streamly.String (encodeUtf8, decodeUtf8, foldLines)
86import qualified Streamly.Data.Fold as FL
87import qualified Streamly.Internal.Data.Fold.Types as FL
88import qualified Streamly.Internal.Data.Unfold as UF
89import qualified Streamly.Memory.Array as A
90import qualified Streamly.Internal.Memory.ArrayStream as AS
91import qualified Streamly.Internal.Memory.Array.Types as A
92import qualified Streamly.Prelude as S
93import qualified Streamly.Streams.StreamD.Type as D
95-- | @'withSocketM' socket act@ runs the monadic computation @act@ passing the
96-- socket handle to it. The handle will be closed on exit from 'withSocketM',
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
99-- 'withSocketM' rather than any exception raised by 'act'.
102{-# INLINE withSocketM #-}
103withSocketM :: (MonadMask m, MonadIO m) => Socket -> (Socket -> m ()) -> m ()
105-- f sk `onException` liftIO (Net.close sk)
106-- liftIO (Net.close sk)
107 finally (liftIO (Net.close sk)) (f sk)
109-- XXX bracket performs 2x better than 'finally'. That's perhaps because of
110-- better fusion of "A.flattenArrays . readArrays"?
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)
116-- | Like 'withSocketM' but runs a streaming computation instead of a monadic
120{-# INLINE withSocket #-}
121withSocket :: (IsStream t, MonadCatch m, MonadIO m)
122 => Socket -> (Socket -> t m a) -> t m a
123withSocket sk = S.bracket (return sk) (liftIO . Net.close)
125-------------------------------------------------------------------------------
127-------------------------------------------------------------------------------
129{-# INLINABLE readArrayUptoWith #-}
131 :: (h -> Ptr Word8 -> Int -> IO Int)
135readArrayUptoWith f size h = do
136 ptr <- mallocPlainForeignPtrBytes size
137 -- ptr <- mallocPlainForeignPtrAlignedBytes size (alignment (undefined :: Word8))
138 withForeignPtr ptr $ \p -> do
142 , aEnd = p `plusPtr` n
143 , aBound = p `plusPtr` size
145 -- XXX shrink only if the diff is significant
149-- | Read a 'ByteArray' from a file handle. If no data is available on the
150-- handle it blocks until some data becomes available. If data is available
151-- then it immediately returns that data without blocking. It reads a maximum
152-- of up to the size requested.
153{-# INLINABLE readArrayOf #-}
154readArrayOf :: Int -> Socket -> IO (Array Word8)
155readArrayOf = readArrayUptoWith recvBuf
157-------------------------------------------------------------------------------
159-------------------------------------------------------------------------------
161waitWhen0 :: Int -> Socket -> IO ()
162waitWhen0 0 s = when rtsSupportsBoundThreads $
163#if MIN_VERSION_network(3,1,0)
164 withFdSocket s $ \fd -> threadWaitWrite $ fromIntegral fd
166 let fd = fdSocket s in threadWaitWrite $ fromIntegral fd
168waitWhen0 _ _ = return ()
170sendAll :: Socket -> Ptr Word8 -> Int -> IO ()
171sendAll _ _ len | len <= 0 = return ()
173 sent <- sendBuf s p len
175 -- assert (sent <= len)
176 when (sent >= 0) $ sendAll s (p `plusPtr` sent) (len - sent)
178{-# INLINABLE writeArrayWith #-}
179writeArrayWith :: Storable a
180 => (h -> Ptr Word8 -> Int -> IO ())
184writeArrayWith _ _ arr | A.length arr == 0 = return ()
185writeArrayWith f h Array{..} = withForeignPtr aStart $ \p ->
189 let p = unsafeForeignPtrToPtr aStart
192-- | Write an Array to a file handle.
195{-# INLINABLE writeArray #-}
196writeArray :: Storable a => Socket -> Array a -> IO ()
197writeArray = writeArrayWith sendAll
199-------------------------------------------------------------------------------
200-- Stream of Arrays IO
201-------------------------------------------------------------------------------
203{-# INLINABLE readArraysUptoWith #-}
204readArraysUptoWith :: (IsStream t, MonadIO m)
205 => (Int -> h -> IO (Array Word8))
206 -> Int -> h -> t m (Array Word8)
207readArraysUptoWith f size h = go
209 -- XXX use cons/nil instead
210 go = mkStream $ \_ yld _ stp -> do
211 arr <- liftIO $ f size h
216-- | @toStreamArraysOf size h@ reads a stream of arrays from file handle @h@.
217-- The maximum size of a single array is limited to @size@.
218-- 'fromHandleArraysUpto' ignores the prevailing 'TextEncoding' and 'NewlineMode'
220{-# INLINABLE toStreamArraysOf #-}
221toStreamArraysOf :: (IsStream t, MonadIO m)
222 => Int -> Socket -> t m (Array Word8)
223toStreamArraysOf = readArraysUptoWith readArrayOf
225-- XXX read 'Array a' instead of Word8
227-- | @toStreamArrays h@ reads a stream of arrays from socket handle @h@.
228-- The maximum size of a single array is limited to @defaultChunkSize@.
231{-# INLINE toStreamArrays #-}
232toStreamArrays :: (IsStream t, MonadIO m) => Socket -> t m (Array Word8)
233toStreamArrays = toStreamArraysOf A.defaultChunkSize
235-- | Unfold the tuple @(size, socket)@ into a stream of 'Word8' arrays. The
236-- stream consists of arrays representing chunks of data read from the socket.
237-- The maximum size of a single array is limited to @size@.
240{-# INLINE_NORMAL readArraysOf #-}
241readArraysOf :: MonadIO m => Unfold m (Int, Socket) (Array Word8)
242readArraysOf = Unfold step return
244 {-# INLINE_LATE step #-}
246 arr <- liftIO $ readArrayOf size h
250 _ -> D.Yield arr (size, h)
252-- | Unfolds a socket into a stream of 'Word8' arrays. The maximum size of a
253-- single array is limited to @defaultChunkSize@.
256{-# INLINE readArrays #-}
257readArrays :: MonadIO m => Unfold m Socket (Array Word8)
258readArrays = UF.first readArraysOf A.defaultChunkSize
260-------------------------------------------------------------------------------
261-- Read File to Stream
262-------------------------------------------------------------------------------
264-- TODO for concurrent streams implement readahead IO. We can send multiple
265-- read requests at the same time. For serial case we can use async IO. We can
266-- also control the read throughput in mbps or IOPS.
269-- | @readInChunksOf chunkSize handle@ reads a byte stream from a file
270-- handle, reads are performed in chunks of up to @chunkSize@. The stream ends
271-- as soon as EOF is encountered.
273{-# INLINE readInChunksOf #-}
274readInChunksOf :: (IsStream t, MonadIO m) => Int -> Handle -> t m Word8
275readInChunksOf chunkSize h = A.flattenArrays $ readArraysUpto chunkSize h
279-- read :: (IsStream t, MonadIO m, Storable a) => Handle -> t m a
281-- > read = 'readByChunks' A.defaultChunkSize
282-- | Generate a stream of elements of the given type from a socket. The
283-- stream ends when EOF is encountered.
286{-# INLINE toStream #-}
287toStream :: (IsStream t, MonadIO m) => Socket -> t m Word8
288toStream = AS.concat . toStreamArrays
290-- | @readInChunksOf@ unfolds @(bufsize, socket)@ into a byte stream, reads
291-- are performed in buffers of up to @bufsize@.
294{-# INLINE readInChunksOf #-}
295readInChunksOf :: MonadIO m => Unfold m (Int, Socket) Word8
296readInChunksOf = UF.concat readArraysOf A.read
298-- | Unfolds a 'Socket' into a byte stream.
302read :: MonadIO m => Unfold m Socket Word8
303read = UF.first readInChunksOf A.defaultChunkSize
305-------------------------------------------------------------------------------
307-------------------------------------------------------------------------------
309-- | Write a stream of arrays to a handle.
312{-# INLINE writeArraysS #-}
313writeArraysS :: (MonadIO m, Storable a)
314 => Socket -> SerialT m (Array a) -> m ()
315writeArraysS h m = S.mapM_ (liftIO . writeArray h) m
317-- | Write a stream of arrays to a socket.
320{-# INLINE writeArrays #-}
321writeArrays :: (MonadIO m, Storable a) => Socket -> Fold m (Array a) ()
322writeArrays h = FL.drainBy (liftIO . writeArray h)
324-- GHC buffer size dEFAULT_FD_BUFFER_SIZE=8192 bytes.
327-- Note that if you use a chunk size less than 8K (GHC's default buffer
328-- size) then you are advised to use 'NOBuffering' mode on the 'Handle' in case you
329-- do not want buffering to occur at GHC level as well. Same thing applies to
332-- | Like 'write' but provides control over the write buffer. Output will
333-- be written to the IO device as soon as we collect the specified number of
337{-# INLINE writeInChunksOfS #-}
338writeInChunksOfS :: MonadIO m => Int -> Socket -> SerialT m Word8 -> m ()
339writeInChunksOfS n h m = writeArraysS h $ AS.arraysOf n m
341-- | Write a byte stream to a socket. Accumulates the input in chunks of
342-- specified number of bytes before writing.
345{-# INLINE writeInChunksOf #-}
346writeInChunksOf :: MonadIO m => Int -> Socket -> Fold m Word8 ()
347writeInChunksOf n h = FL.lchunksOf n (A.writeNUnsafe n) (writeArrays h)
349-- > write = 'writeInChunksOf' A.defaultChunkSize
351-- | Write a byte stream to a file handle. Combines the bytes in chunks of size
352-- up to 'A.defaultChunkSize' before writing. Note that the write behavior
353-- depends on the 'IOMode' and the current seek position of the handle.
356{-# INLINE _writeS #-}
357_writeS :: MonadIO m => Socket -> SerialT m Word8 -> m ()
358_writeS = writeInChunksOfS A.defaultChunkSize
360-- | Write a byte stream to a socket. Accumulates the input in chunks of
361-- up to 'A.defaultChunkSize' bytes before writing.
364-- write = 'writeInChunksOf' 'A.defaultChunkSize'
369write :: MonadIO m => Socket -> Fold m Word8 ()
370write = writeInChunksOf A.defaultChunkSize
374write :: (MonadIO m, Storable a) => Handle -> SerialT m a -> m ()
375write = toHandleWith A.defaultChunkSize
378-------------------------------------------------------------------------------
379-- IO with encoding/decoding Unicode characters
380-------------------------------------------------------------------------------
384-- > readUtf8 = decodeUtf8 . read
386-- Read a UTF8 encoded stream of unicode characters from a file handle.
389{-# INLINE readUtf8 #-}
390readUtf8 :: (IsStream t, MonadIO m) => Handle -> t m Char
391readUtf8 = decodeUtf8 . read
394-- > writeUtf8 h s = write h $ encodeUtf8 s
396-- Encode a stream of unicode characters to UTF8 and write it to the given file
397-- handle. Default block buffering applies to the writes.
400{-# INLINE writeUtf8 #-}
401writeUtf8 :: MonadIO m => Handle -> SerialT m Char -> m ()
402writeUtf8 h s = write h $ encodeUtf8 s
404-- | Write a stream of unicode characters after encoding to UTF-8 in chunks
405-- separated by a linefeed character @'\n'@. If the size of the buffer exceeds
406-- @defaultChunkSize@ and a linefeed is not yet found, the buffer is written
407-- anyway. This is similar to writing to a 'Handle' with the 'LineBuffering'
411{-# INLINE writeUtf8ByLines #-}
412writeUtf8ByLines :: (IsStream t, MonadIO m) => Handle -> t m Char -> m ()
413writeUtf8ByLines = undefined
415-- | Read UTF-8 lines from a file handle and apply the specified fold to each
416-- line. This is similar to reading a 'Handle' with the 'LineBuffering' option.
419{-# INLINE readLines #-}
420readLines :: (IsStream t, MonadIO m) => Handle -> Fold m Char b -> t m b
421readLines h f = foldLines (readUtf8 h) f
423-------------------------------------------------------------------------------
424-- Framing on a sequence
425-------------------------------------------------------------------------------
427-- | Read a stream from a file handle and split it into frames delimited by
428-- the specified sequence of elements. The supplied fold is applied on each
432{-# INLINE readFrames #-}
433readFrames :: (IsStream t, MonadIO m, Storable a)
434 => Array a -> Handle -> Fold m a b -> t m b
435readFrames = undefined -- foldFrames . read
437-- | Write a stream to the given file handle buffering up to frames separated
438-- by the given sequence or up to a maximum of @defaultChunkSize@.
441{-# INLINE writeByFrames #-}
442writeByFrames :: (IsStream t, MonadIO m, Storable a)
443 => Array a -> Handle -> t m a -> m ()
444writeByFrames = undefined
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list