2{-# LANGUAGE FlexibleContexts #-}
3{-# LANGUAGE RecordWildCards #-}
8-- Module : Streamly.Internal.Network.Socket
9-- Copyright : (c) 2018 Composewell Technologies
12-- Maintainer : streamly@composewell.com
13-- Stability : experimental
16module Streamly.Internal.Network.Socket
23 -- * Accept connections
27 -- * Read from connection
40 , readChunksWithBufferOf
43 , toChunksWithBufferOf
47 -- * Write to connection
55 , fromBytesWithBufferOf
61 , writeChunksWithBufferOf
64 -- reading/writing datagrams
68import Control.Concurrent (threadWaitWrite, rtsSupportsBoundThreads)
69import Control.Monad.Catch (MonadCatch, finally, MonadMask)
70import Control.Monad.IO.Class (MonadIO(..))
71import Control.Monad (when)
72import Data.Word (Word8)
73import Foreign.ForeignPtr (withForeignPtr)
74import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
75import Foreign.Ptr (minusPtr, plusPtr, Ptr, castPtr)
76import Foreign.Storable (Storable(..))
77import GHC.ForeignPtr (mallocPlainForeignPtrBytes)
79 (Socket, SocketOption(..), Family(..), SockAddr(..),
80 ProtocolNumber, withSocketsDo, SocketType(..), socket, bind,
81 setSocketOption, sendBuf, recvBuf)
82#if MIN_VERSION_network(3,1,0)
83import Network.Socket (withFdSocket)
85import Network.Socket (fdSocket)
87import Prelude hiding (read)
89import qualified Network.Socket as Net
91import Streamly (MonadAsync)
92import Streamly.Internal.Data.Unfold.Types (Unfold(..))
93import Streamly.Internal.Memory.Array.Types (Array(..), lpackArraysChunksOf)
94import Streamly.Internal.Data.Stream.Serial (SerialT)
95import Streamly.Internal.Data.Stream.StreamK.Type (IsStream, mkStream)
96import Streamly.Data.Fold (Fold)
97-- import Streamly.String (encodeUtf8, decodeUtf8, foldLines)
99import qualified Streamly.Data.Fold as FL
100import qualified Streamly.Internal.Data.Fold.Types as FL
101import qualified Streamly.Internal.Data.Unfold as UF
102import qualified Streamly.Internal.Memory.Array as IA
103import qualified Streamly.Memory.Array as A
104import qualified Streamly.Internal.Memory.ArrayStream as AS
105import qualified Streamly.Internal.Memory.Array.Types as A
106import qualified Streamly.Prelude as S
107import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
109-- | @'handleWithM' socket act@ runs the monadic computation @act@ passing the
110-- socket handle to it. The handle will be closed on exit from 'handleWithM',
111-- whether by normal termination or by raising an exception. If closing the
112-- handle raises an exception, then this exception will be raised by
113-- 'handleWithM' rather than any exception raised by 'act'.
116{-# INLINE handleWithM #-}
117handleWithM :: (MonadMask m, MonadIO m) => (Socket -> m ()) -> Socket -> m ()
118handleWithM f sk = finally (f sk) (liftIO (Net.close sk))
120-- | Like 'handleWithM' but runs a streaming computation instead of a monadic
124{-# INLINE handleWith #-}
125handleWith :: (IsStream t, MonadCatch m, MonadIO m)
126 => Socket -> (Socket -> t m a) -> t m a
127handleWith sk f = S.finally (liftIO $ Net.close sk) (f sk)
129-------------------------------------------------------------------------------
131-------------------------------------------------------------------------------
133-- XXX Protocol specific socket options should be separated from socket level
136-- | Specify the socket protocol details.
137data SockSpec = SockSpec
139 sockFamily :: !Family
140 , sockType :: !SocketType
141 , sockProto :: !ProtocolNumber
142 , sockOpts :: ![(SocketOption, Int)]
145initListener :: Int -> SockSpec -> SockAddr -> IO Socket
146initListener listenQLen SockSpec{..} addr =
148 sock <- socket sockFamily sockType sockProto
149 mapM_ (\(opt, val) -> setSocketOption sock opt val) sockOpts
151 Net.listen sock listenQLen
154{-# INLINE listenTuples #-}
155listenTuples :: MonadIO m
156 => Unfold m (Int, SockSpec, SockAddr) (Socket, SockAddr)
157listenTuples = Unfold step inject
159 inject (listenQLen, spec, addr) = liftIO $ initListener listenQLen spec addr
162 r <- liftIO $ Net.accept listener
163 -- XXX error handling
164 return $ D.Yield r listener
166-- | Unfold a three tuple @(listenQLen, spec, addr)@ into a stream of connected
167-- protocol sockets corresponding to incoming connections. @listenQLen@ is the
168-- maximum number of pending connections in the backlog. @spec@ is the socket
169-- protocol and options specification and @addr@ is the protocol address where
170-- the server listens for incoming connections.
174accept :: MonadIO m => Unfold m (Int, SockSpec, SockAddr) Socket
175accept = UF.map fst listenTuples
177-------------------------------------------------------------------------------
179-------------------------------------------------------------------------------
181{-# INLINE recvConnectionTuplesWith #-}
182recvConnectionTuplesWith :: MonadAsync m
183 => Int -> SockSpec -> SockAddr -> SerialT m (Socket, SockAddr)
184recvConnectionTuplesWith tcpListenQ spec addr = S.unfoldrM step Nothing
187 listener <- liftIO $ initListener tcpListenQ spec addr
188 r <- liftIO $ Net.accept listener
189 -- XXX error handling
190 return $ Just (r, Just listener)
192 step (Just listener) = do
193 r <- liftIO $ Net.accept listener
194 -- XXX error handling
195 return $ Just (r, Just listener)
197-- | Start a TCP stream server that listens for connections on the supplied
198-- server address specification (address family, local interface IP address and
199-- port). The server generates a stream of connected sockets. The first
200-- argument is the maximum number of pending connections in the backlog.
203{-# INLINE connections #-}
204connections :: MonadAsync m => Int -> SockSpec -> SockAddr -> SerialT m Socket
205connections tcpListenQ spec addr = fst <$> 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
247#elif MIN_VERSION_network(3,0,0)
248 fdSocket s >>= threadWaitWrite . fromIntegral
250 let fd = fdSocket s in threadWaitWrite $ fromIntegral fd
252waitWhen0 _ _ = return ()
254sendAll :: Socket -> Ptr Word8 -> Int -> IO ()
255sendAll _ _ len | len <= 0 = return ()
257 sent <- sendBuf s p len
259 -- assert (sent <= len)
260 when (sent >= 0) $ sendAll s (p `plusPtr` sent) (len - sent)
262{-# INLINABLE writeArrayWith #-}
263writeArrayWith :: Storable a
264 => (h -> Ptr Word8 -> Int -> IO ())
268writeArrayWith _ _ arr | A.length arr == 0 = return ()
269writeArrayWith f h Array{..} = withForeignPtr aStart $ \p ->
273 let p = unsafeForeignPtrToPtr aStart
276-- | Write an Array to a file handle.
279{-# INLINABLE writeChunk #-}
280writeChunk :: Storable a => Socket -> Array a -> IO ()
281writeChunk = writeArrayWith sendAll
283-------------------------------------------------------------------------------
284-- Stream of Arrays IO
285-------------------------------------------------------------------------------
287{-# INLINABLE _readChunksUptoWith #-}
288_readChunksUptoWith :: (IsStream t, MonadIO m)
289 => (Int -> h -> IO (Array Word8))
290 -> Int -> h -> t m (Array Word8)
291_readChunksUptoWith f size h = go
293 -- XXX use cons/nil instead
294 go = mkStream $ \_ yld _ stp -> do
295 arr <- liftIO $ f size h
300-- | @toChunksWithBufferOf size h@ reads a stream of arrays from file handle @h@.
301-- The maximum size of a single array is limited to @size@.
302-- 'fromHandleArraysUpto' ignores the prevailing 'TextEncoding' and 'NewlineMode'
304{-# INLINE_NORMAL toChunksWithBufferOf #-}
305toChunksWithBufferOf :: (IsStream t, MonadIO m)
306 => Int -> Socket -> t m (Array Word8)
307-- toChunksWithBufferOf = _readChunksUptoWith readArrayOf
308toChunksWithBufferOf size h = D.fromStreamD (D.Stream step ())
310 {-# INLINE_LATE step #-}
312 arr <- liftIO $ readArrayOf size h
318-- XXX read 'Array a' instead of Word8
320-- | @toChunks h@ reads a stream of arrays from socket handle @h@.
321-- The maximum size of a single array is limited to @defaultChunkSize@.
324{-# INLINE toChunks #-}
325toChunks :: (IsStream t, MonadIO m) => Socket -> t m (Array Word8)
326toChunks = toChunksWithBufferOf A.defaultChunkSize
328-- | Unfold the tuple @(bufsize, socket)@ into a stream of 'Word8' arrays.
329-- Read requests to the socket are performed using a buffer of size @bufsize@.
330-- The size of an array in the resulting stream is always less than or equal to
334{-# INLINE_NORMAL readChunksWithBufferOf #-}
335readChunksWithBufferOf :: MonadIO m => Unfold m (Int, Socket) (Array Word8)
336readChunksWithBufferOf = Unfold step return
338 {-# INLINE_LATE step #-}
340 arr <- liftIO $ readArrayOf size h
344 _ -> D.Yield arr (size, h)
346-- | Unfolds a socket into a stream of 'Word8' arrays. Requests to the socket
347-- are performed using a buffer of size
348-- 'Streamly.Internal.Memory.Array.Types.defaultChunkSize'. The
349-- size of arrays in the resulting stream are therefore less than or equal to
350-- 'Streamly.Internal.Memory.Array.Types.defaultChunkSize'.
353{-# INLINE readChunks #-}
354readChunks :: MonadIO m => Unfold m Socket (Array Word8)
355readChunks = UF.supplyFirst readChunksWithBufferOf A.defaultChunkSize
357-------------------------------------------------------------------------------
358-- Read File to Stream
359-------------------------------------------------------------------------------
361-- TODO for concurrent streams implement readahead IO. We can send multiple
362-- read requests at the same time. For serial case we can use async IO. We can
363-- also control the read throughput in mbps or IOPS.
366-- | @readWithBufferOf bufsize handle@ reads a byte stream from a file
367-- handle, reads are performed in chunks of up to @bufsize@. The stream ends
368-- as soon as EOF is encountered.
370{-# INLINE readWithBufferOf #-}
371readWithBufferOf :: (IsStream t, MonadIO m) => Int -> Handle -> t m Word8
372readWithBufferOf chunkSize h = A.flattenArrays $ readChunksUpto chunkSize h
376-- read :: (IsStream t, MonadIO m, Storable a) => Handle -> t m a
378-- > read = 'readByChunks' A.defaultChunkSize
379-- | Generate a stream of elements of the given type from a socket. The
380-- stream ends when EOF is encountered.
383{-# INLINE toBytes #-}
384toBytes :: (IsStream t, MonadIO m) => Socket -> t m Word8
385toBytes = AS.concat . toChunks
387-- | Unfolds the tuple @(bufsize, socket)@ into a byte stream, read requests
388-- to the socket are performed using buffers of @bufsize@.
391{-# INLINE readWithBufferOf #-}
392readWithBufferOf :: MonadIO m => Unfold m (Int, Socket) Word8
393readWithBufferOf = UF.concat readChunksWithBufferOf A.read
395-- | Unfolds a 'Socket' into a byte stream. IO requests to the socket are
396-- performed in sizes of
397-- 'Streamly.Internal.Memory.Array.Types.defaultChunkSize'.
401read :: MonadIO m => Unfold m Socket Word8
402read = UF.supplyFirst readWithBufferOf A.defaultChunkSize
404-------------------------------------------------------------------------------
406-------------------------------------------------------------------------------
408-- | Write a stream of arrays to a handle.
411{-# INLINE fromChunks #-}
412fromChunks :: (MonadIO m, Storable a)
413 => Socket -> SerialT m (Array a) -> m ()
414fromChunks h = S.mapM_ (liftIO . writeChunk h)
416-- | Write a stream of arrays to a socket. Each array in the stream is written
417-- to the socket as a separate IO request.
420{-# INLINE writeChunks #-}
421writeChunks :: (MonadIO m, Storable a) => Socket -> Fold m (Array a) ()
422writeChunks h = FL.drainBy (liftIO . writeChunk h)
424-- | @writeChunksWithBufferOf bufsize socket@ writes a stream of arrays
425-- to @socket@ after coalescing the adjacent arrays in chunks of @bufsize@.
426-- We never split an array, if a single array is bigger than the specified size
427-- it emitted as it is. Multiple arrays are coalesed as long as the total size
428-- remains below the specified size.
431{-# INLINE writeChunksWithBufferOf #-}
432writeChunksWithBufferOf :: (MonadIO m, Storable a)
433 => Int -> Socket -> Fold m (Array a) ()
434writeChunksWithBufferOf n h = lpackArraysChunksOf n (writeChunks h)
436-- | Write a stream of strings to a socket in Latin1 encoding. Output is
437-- flushed to the socket for each string.
441{-# INLINE writeStrings #-}
442writeStrings :: MonadIO m
443 => (SerialT m Char -> SerialT m Word8) -> Socket -> Fold m String ()
444writeStrings encode h =
445 FL.lmapM (IA.fromStream . encode . S.fromList) (writeChunks h)
447-- GHC buffer size dEFAULT_FD_BUFFER_SIZE=8192 bytes.
450-- Note that if you use a chunk size less than 8K (GHC's default buffer
451-- size) then you are advised to use 'NOBuffering' mode on the 'Handle' in case you
452-- do not want buffering to occur at GHC level as well. Same thing applies to
455-- | Like 'write' but provides control over the write buffer. Output will
456-- be written to the IO device as soon as we collect the specified number of
460{-# INLINE fromBytesWithBufferOf #-}
461fromBytesWithBufferOf :: MonadIO m => Int -> Socket -> SerialT m Word8 -> m ()
462fromBytesWithBufferOf n h m = fromChunks h $ AS.arraysOf n m
464-- | Write a byte stream to a socket. Accumulates the input in chunks of
465-- specified number of bytes before writing.
468{-# INLINE writeWithBufferOf #-}
469writeWithBufferOf :: MonadIO m => Int -> Socket -> Fold m Word8 ()
470writeWithBufferOf n h = FL.lchunksOf n (A.writeNUnsafe n) (writeChunks h)
472-- > write = 'writeWithBufferOf' A.defaultChunkSize
474-- | Write a byte stream to a file handle. Combines the bytes in chunks of size
475-- up to 'A.defaultChunkSize' before writing. Note that the write behavior
476-- depends on the 'IOMode' and the current seek position of the handle.
479{-# INLINE fromBytes #-}
480fromBytes :: MonadIO m => Socket -> SerialT m Word8 -> m ()
481fromBytes = fromBytesWithBufferOf A.defaultChunkSize
483-- | Write a byte stream to a socket. Accumulates the input in chunks of
484-- up to 'A.defaultChunkSize' bytes before writing.
487-- write = 'writeWithBufferOf' 'A.defaultChunkSize'
492write :: MonadIO m => Socket -> Fold m Word8 ()
493write = writeWithBufferOf A.defaultChunkSize
497write :: (MonadIO m, Storable a) => Handle -> SerialT m a -> m ()
498write = toHandleWith A.defaultChunkSize
501-------------------------------------------------------------------------------
502-- IO with encoding/decoding Unicode characters
503-------------------------------------------------------------------------------
507-- > readUtf8 = decodeUtf8 . read
509-- Read a UTF8 encoded stream of unicode characters from a file handle.
512{-# INLINE readUtf8 #-}
513readUtf8 :: (IsStream t, MonadIO m) => Handle -> t m Char
514readUtf8 = decodeUtf8 . read
517-- > writeUtf8 h s = write h $ encodeUtf8 s
519-- Encode a stream of unicode characters to UTF8 and write it to the given file
520-- handle. Default block buffering applies to the writes.
523{-# INLINE writeUtf8 #-}
524writeUtf8 :: MonadIO m => Handle -> SerialT m Char -> m ()
525writeUtf8 h s = write h $ encodeUtf8 s
527-- | Write a stream of unicode characters after encoding to UTF-8 in chunks
528-- separated by a linefeed character @'\n'@. If the size of the buffer exceeds
529-- @defaultChunkSize@ and a linefeed is not yet found, the buffer is written
530-- anyway. This is similar to writing to a 'Handle' with the 'LineBuffering'
534{-# INLINE writeUtf8ByLines #-}
535writeUtf8ByLines :: (IsStream t, MonadIO m) => Handle -> t m Char -> m ()
536writeUtf8ByLines = undefined
538-- | Read UTF-8 lines from a file handle and apply the specified fold to each
539-- line. This is similar to reading a 'Handle' with the 'LineBuffering' option.
542{-# INLINE readLines #-}
543readLines :: (IsStream t, MonadIO m) => Handle -> Fold m Char b -> t m b
544readLines h f = foldLines (readUtf8 h) f
546-------------------------------------------------------------------------------
547-- Framing on a sequence
548-------------------------------------------------------------------------------
550-- | Read a stream from a file handle and split it into frames delimited by
551-- the specified sequence of elements. The supplied fold is applied on each
555{-# INLINE readFrames #-}
556readFrames :: (IsStream t, MonadIO m, Storable a)
557 => Array a -> Handle -> Fold m a b -> t m b
558readFrames = undefined -- foldFrames . read
560-- | Write a stream to the given file handle buffering up to frames separated
561-- by the given sequence or up to a maximum of @defaultChunkSize@.
564{-# INLINE writeByFrames #-}
565writeByFrames :: (IsStream t, MonadIO m, Storable a)
566 => Array a -> Handle -> t m a -> m ()
567writeByFrames = 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