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
26 -- * Accept connections
30 -- * Read from connection
44 , readChunksRequestsOf
50 -- * Write to connection
61 -- reading/writing datagrams
65import Control.Concurrent (threadWaitWrite, rtsSupportsBoundThreads)
66import Control.Monad.Catch (MonadCatch, finally, MonadMask)
67import Control.Monad.IO.Class (MonadIO(..))
68import Control.Monad (when)
69import Data.Word (Word8)
70import Foreign.ForeignPtr (withForeignPtr)
71import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
72import Foreign.Ptr (minusPtr, plusPtr, Ptr, castPtr)
73import Foreign.Storable (Storable(..))
74import GHC.ForeignPtr (mallocPlainForeignPtrBytes)
75import Network.Socket (sendBuf, recvBuf)
77 (Socket, SocketOption(..), Family(..), SockAddr(..),
78 ProtocolNumber, withSocketsDo, SocketType(..), socket, bind,
80#if MIN_VERSION_network(3,1,0)
81import Network.Socket (withFdSocket)
83import Network.Socket (fdSocket)
85import Prelude hiding (read)
87import qualified Network.Socket as Net
89import Streamly (MonadAsync)
90import Streamly.Internal.Data.Unfold.Types (Unfold(..))
91import Streamly.Internal.Memory.Array.Types (Array(..))
92import Streamly.Streams.Serial (SerialT)
93import Streamly.Streams.StreamK.Type (IsStream, mkStream)
94import Streamly.Data.Fold (Fold)
95-- import Streamly.String (encodeUtf8, decodeUtf8, foldLines)
97import qualified Streamly.Data.Fold as FL
98import qualified Streamly.Internal.Data.Fold.Types as FL
99import qualified Streamly.Internal.Data.Unfold as UF
100import qualified Streamly.Memory.Array as A
101import qualified Streamly.Internal.Memory.ArrayStream as AS
102import qualified Streamly.Internal.Memory.Array.Types as A
103import qualified Streamly.Prelude as S
104import qualified Streamly.Streams.StreamD.Type as D
106-- | @'useSocketM' socket act@ runs the monadic computation @act@ passing the
107-- socket handle to it. The handle will be closed on exit from 'useSocketM',
108-- whether by normal termination or by raising an exception. If closing the
109-- handle raises an exception, then this exception will be raised by
110-- 'useSocketM' rather than any exception raised by 'act'.
113{-# INLINE useSocketM #-}
114useSocketM :: (MonadMask m, MonadIO m) => Socket -> (Socket -> m ()) -> m ()
115useSocketM sk f = finally (liftIO (Net.close sk)) (f sk)
117-- | Like 'useSocketM' but runs a streaming computation instead of a monadic
121{-# INLINE useSocket #-}
122useSocket :: (IsStream t, MonadCatch m, MonadIO m)
123 => Socket -> (Socket -> t m a) -> t m a
124useSocket sk f = S.finally (liftIO $ Net.close sk) (f sk)
126-------------------------------------------------------------------------------
128-------------------------------------------------------------------------------
130-- XXX Protocol specific socket options should be separated from socket level
133-- | Specify the socket protocol details.
134data SockSpec = SockSpec
136 sockFamily :: !Family
137 , sockType :: !SocketType
138 , sockProto :: !ProtocolNumber
139 , sockOpts :: ![(SocketOption, Int)]
142initListener :: Int -> SockSpec -> SockAddr -> IO Socket
143initListener listenQLen SockSpec{..} addr =
145 sock <- socket sockFamily sockType sockProto
146 mapM_ (\(opt, val) -> setSocketOption sock opt val) sockOpts
148 Net.listen sock listenQLen
151{-# INLINE listenTuples #-}
152listenTuples :: MonadIO m
153 => Unfold m (Int, SockSpec, SockAddr) (Socket, SockAddr)
154listenTuples = Unfold step inject
156 inject (listenQLen, spec, addr) = do
157 listener <- liftIO $ initListener listenQLen spec addr
161 r <- liftIO $ Net.accept listener
162 -- XXX error handling
163 return $ D.Yield r listener
165-- | Unfold a three tuple @(listenQLen, spec, addr)@ into a stream of connected
166-- protocol sockets corresponding to incoming connections. @listenQLen@ is the
167-- maximum number of pending connections in the backlog. @spec@ is the socket
168-- protocol and options specification and @addr@ is the protocol address where
169-- the server listens for incoming connections.
173accept :: MonadIO m => Unfold m (Int, SockSpec, SockAddr) Socket
174accept = UF.map fst listenTuples
176-------------------------------------------------------------------------------
178-------------------------------------------------------------------------------
180{-# INLINE recvConnectionTuplesWith #-}
181recvConnectionTuplesWith :: MonadAsync m
182 => Int -> SockSpec -> SockAddr -> SerialT m (Socket, SockAddr)
183recvConnectionTuplesWith tcpListenQ spec addr = S.unfoldrM step Nothing
186 listener <- liftIO $ initListener tcpListenQ spec addr
187 r <- liftIO $ Net.accept listener
188 -- XXX error handling
189 return $ Just (r, Just listener)
191 step (Just listener) = do
192 r <- liftIO $ Net.accept listener
193 -- XXX error handling
194 return $ Just (r, Just listener)
196-- | Start a TCP stream server that listens for connections on the supplied
197-- server address specification (address family, local interface IP address and
198-- port). The server generates a stream of connected sockets. The first
199-- argument is the maximum number of pending connections in the backlog.
202{-# INLINE connections #-}
203connections :: MonadAsync m => Int -> SockSpec -> SockAddr -> SerialT m Socket
204connections tcpListenQ spec addr = fmap fst $
205 recvConnectionTuplesWith tcpListenQ spec addr
207-------------------------------------------------------------------------------
209-------------------------------------------------------------------------------
211{-# INLINABLE readArrayUptoWith #-}
213 :: (h -> Ptr Word8 -> Int -> IO Int)
217readArrayUptoWith f size h = do
218 ptr <- mallocPlainForeignPtrBytes size
219 -- ptr <- mallocPlainForeignPtrAlignedBytes size (alignment (undefined :: Word8))
220 withForeignPtr ptr $ \p -> do
224 , aEnd = p `plusPtr` n
225 , aBound = p `plusPtr` size
227 -- XXX shrink only if the diff is significant
231-- | Read a 'ByteArray' from a file handle. If no data is available on the
232-- handle it blocks until some data becomes available. If data is available
233-- then it immediately returns that data without blocking. It reads a maximum
234-- of up to the size requested.
235{-# INLINABLE readArrayOf #-}
236readArrayOf :: Int -> Socket -> IO (Array Word8)
237readArrayOf = readArrayUptoWith recvBuf
239-------------------------------------------------------------------------------
241-------------------------------------------------------------------------------
243waitWhen0 :: Int -> Socket -> IO ()
244waitWhen0 0 s = when rtsSupportsBoundThreads $
245#if MIN_VERSION_network(3,1,0)
246 withFdSocket s $ \fd -> threadWaitWrite $ fromIntegral fd
248 let fd = fdSocket s in threadWaitWrite $ fromIntegral fd
250waitWhen0 _ _ = return ()
252sendAll :: Socket -> Ptr Word8 -> Int -> IO ()
253sendAll _ _ len | len <= 0 = return ()
255 sent <- sendBuf s p len
257 -- assert (sent <= len)
258 when (sent >= 0) $ sendAll s (p `plusPtr` sent) (len - sent)
260{-# INLINABLE writeArrayWith #-}
261writeArrayWith :: Storable a
262 => (h -> Ptr Word8 -> Int -> IO ())
266writeArrayWith _ _ arr | A.length arr == 0 = return ()
267writeArrayWith f h Array{..} = withForeignPtr aStart $ \p ->
271 let p = unsafeForeignPtrToPtr aStart
274-- | Write an Array to a file handle.
277{-# INLINABLE writeArray #-}
278writeArray :: Storable a => Socket -> Array a -> IO ()
279writeArray = writeArrayWith sendAll
281-------------------------------------------------------------------------------
282-- Stream of Arrays IO
283-------------------------------------------------------------------------------
285{-# INLINABLE readChunksUptoWith #-}
286readChunksUptoWith :: (IsStream t, MonadIO m)
287 => (Int -> h -> IO (Array Word8))
288 -> Int -> h -> t m (Array Word8)
289readChunksUptoWith f size h = go
291 -- XXX use cons/nil instead
292 go = mkStream $ \_ yld _ stp -> do
293 arr <- liftIO $ f size h
298-- | @toChunksRequestsOf size h@ reads a stream of arrays from file handle @h@.
299-- The maximum size of a single array is limited to @size@.
300-- 'fromHandleArraysUpto' ignores the prevailing 'TextEncoding' and 'NewlineMode'
302{-# INLINABLE toChunksRequestsOf #-}
303toChunksRequestsOf :: (IsStream t, MonadIO m)
304 => Int -> Socket -> t m (Array Word8)
305toChunksRequestsOf = readChunksUptoWith readArrayOf
307-- XXX read 'Array a' instead of Word8
309-- | @toChunks h@ reads a stream of arrays from socket handle @h@.
310-- The maximum size of a single array is limited to @defaultChunkSize@.
313{-# INLINE toChunks #-}
314toChunks :: (IsStream t, MonadIO m) => Socket -> t m (Array Word8)
315toChunks = toChunksRequestsOf A.defaultChunkSize
317-- | Unfold the tuple @(size, socket)@ into a stream of 'Word8' arrays. The
318-- stream consists of arrays representing chunks of data read from the socket.
319-- The size of an array in the resulting stream is less than or equal to
323{-# INLINE_NORMAL readChunksRequestsOf #-}
324readChunksRequestsOf :: MonadIO m => Unfold m (Int, Socket) (Array Word8)
325readChunksRequestsOf = Unfold step return
327 {-# INLINE_LATE step #-}
329 arr <- liftIO $ readArrayOf size h
333 _ -> D.Yield arr (size, h)
335-- | Unfolds a socket into a stream of 'Word8' arrays. Requests to the socket
336-- are performed using a buffer of size
337-- 'Streamly.Internal.Memory.Array.Types.defaultChunkSize'. The
338-- size of arrays in the resulting stream are therefore less than or equal to
339-- 'Streamly.Internal.Memory.Array.Types.defaultChunkSize'.
342{-# INLINE readChunks #-}
343readChunks :: MonadIO m => Unfold m Socket (Array Word8)
344readChunks = UF.supplyFirst readChunksRequestsOf A.defaultChunkSize
346-------------------------------------------------------------------------------
347-- Read File to Stream
348-------------------------------------------------------------------------------
350-- TODO for concurrent streams implement readahead IO. We can send multiple
351-- read requests at the same time. For serial case we can use async IO. We can
352-- also control the read throughput in mbps or IOPS.
355-- | @readRequestsOf chunkSize handle@ reads a byte stream from a file
356-- handle, reads are performed in chunks of up to @chunkSize@. The stream ends
357-- as soon as EOF is encountered.
359{-# INLINE readRequestsOf #-}
360readRequestsOf :: (IsStream t, MonadIO m) => Int -> Handle -> t m Word8
361readRequestsOf chunkSize h = A.flattenArrays $ readChunksUpto chunkSize h
365-- read :: (IsStream t, MonadIO m, Storable a) => Handle -> t m a
367-- > read = 'readByChunks' A.defaultChunkSize
368-- | Generate a stream of elements of the given type from a socket. The
369-- stream ends when EOF is encountered.
372{-# INLINE toStream #-}
373toStream :: (IsStream t, MonadIO m) => Socket -> t m Word8
374toStream = AS.concat . toChunks
376-- | Unfolds the tuple @(chunksize, socket)@ into a byte stream, read requests
377-- to the socket are performed using buffers of @chunkSize@.
380{-# INLINE readRequestsOf #-}
381readRequestsOf :: MonadIO m => Unfold m (Int, Socket) Word8
382readRequestsOf = UF.concat readChunksRequestsOf A.read
384-- | Unfolds a 'Socket' into a byte stream. IO requests to the socket are
385-- performed in sizes of
386-- 'Streamly.Internal.Memory.Array.Types.defaultChunkSize'.
390read :: MonadIO m => Unfold m Socket Word8
391read = UF.supplyFirst readRequestsOf A.defaultChunkSize
393-------------------------------------------------------------------------------
395-------------------------------------------------------------------------------
397-- | Write a stream of arrays to a handle.
400{-# INLINE writeChunksS #-}
401writeChunksS :: (MonadIO m, Storable a)
402 => Socket -> SerialT m (Array a) -> m ()
403writeChunksS h m = S.mapM_ (liftIO . writeArray h) m
405-- | Write a stream of arrays to a socket. Each array in the stream is written
406-- to the socket as a separate IO request.
409{-# INLINE writeChunks #-}
410writeChunks :: (MonadIO m, Storable a) => Socket -> Fold m (Array a) ()
411writeChunks h = FL.drainBy (liftIO . writeArray h)
413-- GHC buffer size dEFAULT_FD_BUFFER_SIZE=8192 bytes.
416-- Note that if you use a chunk size less than 8K (GHC's default buffer
417-- size) then you are advised to use 'NOBuffering' mode on the 'Handle' in case you
418-- do not want buffering to occur at GHC level as well. Same thing applies to
421-- | Like 'write' but provides control over the write buffer. Output will
422-- be written to the IO device as soon as we collect the specified number of
426{-# INLINE writeRequestsOfS #-}
427writeRequestsOfS :: MonadIO m => Int -> Socket -> SerialT m Word8 -> m ()
428writeRequestsOfS n h m = writeChunksS h $ AS.arraysOf n m
430-- | Write a byte stream to a socket. Accumulates the input in chunks of
431-- specified number of bytes before writing.
434{-# INLINE writeRequestsOf #-}
435writeRequestsOf :: MonadIO m => Int -> Socket -> Fold m Word8 ()
436writeRequestsOf n h = FL.lchunksOf n (A.writeNUnsafe n) (writeChunks h)
438-- > write = 'writeRequestsOf' A.defaultChunkSize
440-- | Write a byte stream to a file handle. Combines the bytes in chunks of size
441-- up to 'A.defaultChunkSize' before writing. Note that the write behavior
442-- depends on the 'IOMode' and the current seek position of the handle.
445{-# INLINE _writeS #-}
446_writeS :: MonadIO m => Socket -> SerialT m Word8 -> m ()
447_writeS = writeRequestsOfS A.defaultChunkSize
449-- | Write a byte stream to a socket. Accumulates the input in chunks of
450-- up to 'A.defaultChunkSize' bytes before writing.
453-- write = 'writeRequestsOf' 'A.defaultChunkSize'
458write :: MonadIO m => Socket -> Fold m Word8 ()
459write = writeRequestsOf A.defaultChunkSize
463write :: (MonadIO m, Storable a) => Handle -> SerialT m a -> m ()
464write = toHandleWith A.defaultChunkSize
467-------------------------------------------------------------------------------
468-- IO with encoding/decoding Unicode characters
469-------------------------------------------------------------------------------
473-- > readUtf8 = decodeUtf8 . read
475-- Read a UTF8 encoded stream of unicode characters from a file handle.
478{-# INLINE readUtf8 #-}
479readUtf8 :: (IsStream t, MonadIO m) => Handle -> t m Char
480readUtf8 = decodeUtf8 . read
483-- > writeUtf8 h s = write h $ encodeUtf8 s
485-- Encode a stream of unicode characters to UTF8 and write it to the given file
486-- handle. Default block buffering applies to the writes.
489{-# INLINE writeUtf8 #-}
490writeUtf8 :: MonadIO m => Handle -> SerialT m Char -> m ()
491writeUtf8 h s = write h $ encodeUtf8 s
493-- | Write a stream of unicode characters after encoding to UTF-8 in chunks
494-- separated by a linefeed character @'\n'@. If the size of the buffer exceeds
495-- @defaultChunkSize@ and a linefeed is not yet found, the buffer is written
496-- anyway. This is similar to writing to a 'Handle' with the 'LineBuffering'
500{-# INLINE writeUtf8ByLines #-}
501writeUtf8ByLines :: (IsStream t, MonadIO m) => Handle -> t m Char -> m ()
502writeUtf8ByLines = undefined
504-- | Read UTF-8 lines from a file handle and apply the specified fold to each
505-- line. This is similar to reading a 'Handle' with the 'LineBuffering' option.
508{-# INLINE readLines #-}
509readLines :: (IsStream t, MonadIO m) => Handle -> Fold m Char b -> t m b
510readLines h f = foldLines (readUtf8 h) f
512-------------------------------------------------------------------------------
513-- Framing on a sequence
514-------------------------------------------------------------------------------
516-- | Read a stream from a file handle and split it into frames delimited by
517-- the specified sequence of elements. The supplied fold is applied on each
521{-# INLINE readFrames #-}
522readFrames :: (IsStream t, MonadIO m, Storable a)
523 => Array a -> Handle -> Fold m a b -> t m b
524readFrames = undefined -- foldFrames . read
526-- | Write a stream to the given file handle buffering up to frames separated
527-- by the given sequence or up to a maximum of @defaultChunkSize@.
530{-# INLINE writeByFrames #-}
531writeByFrames :: (IsStream t, MonadIO m, Storable a)
532 => Array a -> Handle -> t m a -> m ()
533writeByFrames = 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