on 3de89fe help sign in
445 lines 15 kB history raw
1{-# LANGUAGE CPP #-}
2{-# LANGUAGE BangPatterns #-}
3{-# LANGUAGE FlexibleContexts #-}
4{-# LANGUAGE MagicHash #-}
5{-# LANGUAGE RecordWildCards #-}
6{-# LANGUAGE UnboxedTuples #-}
8#include "inline.hs"
10-- |
11-- Module : Streamly.Internal.Network.Socket
12-- Copyright : (c) 2018 Composewell Technologies
13--
14-- License : BSD3
15-- Maintainer : streamly@composewell.com
16-- Stability : experimental
17-- Portability : GHC
18--
19module Streamly.Internal.Network.Socket
20 (
21 -- * Use a socket
22 withSocketM
23 , withSocket
25 -- * Read from connection
26 , read
27 -- , readUtf8
28 -- , readLines
29 -- , readFrames
30 -- , readByChunks
31 , toStream
33 -- -- * Array Read
34 -- , readArrayUpto
35 -- , readArrayOf
37 -- , readArraysUpto
38 , readArraysOf
39 , readArrays
41 , toStreamArraysOf
42 , toStreamArrays
44 -- * Write to connection
45 , write
46 -- , writeUtf8
47 -- , writeUtf8ByLines
48 -- , writeByFrames
49 , writeInChunksOf
51 -- -- * Array Write
52 , writeArray
53 , writeArrays
55 -- reading/writing datagrams
56 )
57where
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)
72#else
73import Network.Socket (fdSocket)
74#endif
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'.
100--
101-- @since 0.7.0
102{-# INLINE withSocketM #-}
103withSocketM :: (MonadMask m, MonadIO m) => Socket -> (Socket -> m ()) -> m ()
104withSocketM sk f = do
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"?
111--
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
117-- computation.
118--
119-- @since 0.7.0
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-------------------------------------------------------------------------------
126-- Array IO (Input)
127-------------------------------------------------------------------------------
129{-# INLINABLE readArrayUptoWith #-}
130readArrayUptoWith
131 :: (h -> Ptr Word8 -> Int -> IO Int)
132 -> Int
133 -> h
134 -> IO (Array Word8)
135readArrayUptoWith f size h = do
136 ptr <- mallocPlainForeignPtrBytes size
137 -- ptr <- mallocPlainForeignPtrAlignedBytes size (alignment (undefined :: Word8))
138 withForeignPtr ptr $ \p -> do
139 n <- f h p size
140 let v = Array
141 { aStart = ptr
142 , aEnd = p `plusPtr` n
143 , aBound = p `plusPtr` size
144 }
145 -- XXX shrink only if the diff is significant
146 -- A.shrinkToFit v
147 return v
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-------------------------------------------------------------------------------
158-- Array IO (output)
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
165#else
166 let fd = fdSocket s in threadWaitWrite $ fromIntegral fd
167#endif
168waitWhen0 _ _ = return ()
170sendAll :: Socket -> Ptr Word8 -> Int -> IO ()
171sendAll _ _ len | len <= 0 = return ()
172sendAll s p len = do
173 sent <- sendBuf s p len
174 waitWhen0 sent s
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 ())
181 -> h
182 -> Array a
183 -> IO ()
184writeArrayWith _ _ arr | A.length arr == 0 = return ()
185writeArrayWith f h Array{..} = withForeignPtr aStart $ \p ->
186 f h (castPtr p) aLen
187 where
188 aLen =
189 let p = unsafeForeignPtrToPtr aStart
190 in aEnd `minusPtr` p
192-- | Write an Array to a file handle.
193--
194-- @since 0.7.0
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
208 where
209 -- XXX use cons/nil instead
210 go = mkStream $ \_ yld _ stp -> do
211 arr <- liftIO $ f size h
212 if A.length arr == 0
213 then stp
214 else yld arr go
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'
219-- on the 'Handle'.
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
226--
227-- | @toStreamArrays h@ reads a stream of arrays from socket handle @h@.
228-- The maximum size of a single array is limited to @defaultChunkSize@.
229--
230-- @since 0.7.0
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@.
238--
239-- @since 0.7.0
240{-# INLINE_NORMAL readArraysOf #-}
241readArraysOf :: MonadIO m => Unfold m (Int, Socket) (Array Word8)
242readArraysOf = Unfold step return
243 where
244 {-# INLINE_LATE step #-}
245 step (size, h) = do
246 arr <- liftIO $ readArrayOf size h
247 return $
248 case A.length arr of
249 0 -> D.Stop
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@.
254--
255-- @since 0.7.0
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.
268{-
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.
272--
273{-# INLINE readInChunksOf #-}
274readInChunksOf :: (IsStream t, MonadIO m) => Int -> Handle -> t m Word8
275readInChunksOf chunkSize h = A.flattenArrays $ readArraysUpto chunkSize h
276-}
278-- TODO
279-- read :: (IsStream t, MonadIO m, Storable a) => Handle -> t m a
280--
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.
284--
285-- @since 0.7.0
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@.
292--
293-- @since 0.7.0
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.
299--
300-- @since 0.7.0
301{-# INLINE read #-}
302read :: MonadIO m => Unfold m Socket Word8
303read = UF.first readInChunksOf A.defaultChunkSize
305-------------------------------------------------------------------------------
306-- Writing
307-------------------------------------------------------------------------------
309-- | Write a stream of arrays to a handle.
310--
311-- @since 0.7.0
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.
318--
319-- @since 0.7.0
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.
325--
326-- XXX test this
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
330-- writes as well.
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
334-- input elements.
335--
336-- @since 0.7.0
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.
343--
344-- @since 0.7.0
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
350--
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.
354--
355-- @since 0.7.0
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.
362--
363-- @
364-- write = 'writeInChunksOf' 'A.defaultChunkSize'
365-- @
366--
367-- @since 0.7.0
368{-# INLINE write #-}
369write :: MonadIO m => Socket -> Fold m Word8 ()
370write = writeInChunksOf A.defaultChunkSize
372{-
373{-# INLINE write #-}
374write :: (MonadIO m, Storable a) => Handle -> SerialT m a -> m ()
375write = toHandleWith A.defaultChunkSize
376-}
378-------------------------------------------------------------------------------
379-- IO with encoding/decoding Unicode characters
380-------------------------------------------------------------------------------
382{-
383-- |
384-- > readUtf8 = decodeUtf8 . read
385--
386-- Read a UTF8 encoded stream of unicode characters from a file handle.
387--
388-- @since 0.7.0
389{-# INLINE readUtf8 #-}
390readUtf8 :: (IsStream t, MonadIO m) => Handle -> t m Char
391readUtf8 = decodeUtf8 . read
393-- |
394-- > writeUtf8 h s = write h $ encodeUtf8 s
395--
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.
398--
399-- @since 0.7.0
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'
408-- option.
409--
410-- @since 0.7.0
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.
417--
418-- @since 0.7.0
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
429-- frame.
430--
431-- @since 0.7.0
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@.
439--
440-- @since 0.7.0
441{-# INLINE writeByFrames #-}
442writeByFrames :: (IsStream t, MonadIO m, Storable a)
443 => Array a -> Handle -> t m a -> m ()
444writeByFrames = undefined
445-}