4-- Module : Streamly.Internal.Network.Socket
5-- Copyright : (c) 2018 Composewell Technologies
8-- Maintainer : streamly@composewell.com
9-- Stability : experimental
12module Streamly.Internal.Network.Socket
19 -- * Accept connections
27 -- * Read from connection
42 -- * Write to connection
57 -- reading/writing datagrams
61 , readChunksWithBufferOf
63 , writeChunksWithBufferOf
67import Control.Concurrent (threadWaitWrite, rtsSupportsBoundThreads)
68import Control.Exception (onException)
69import Control.Monad (forM_, when)
70import Control.Monad.Catch (MonadCatch, finally, MonadMask)
71import Control.Monad.IO.Class (MonadIO(..))
72import Data.Maybe (isNothing, fromJust)
73import Data.Word (Word8)
74import Foreign.Ptr (plusPtr, Ptr, castPtr)
75import Streamly.Internal.Data.Unboxed (Unboxed)
77 (Socket, SocketOption(..), Family(..), SockAddr(..),
78 ProtocolNumber, withSocketsDo, SocketType(..), socket, bind,
79 setSocketOption, sendBuf, recvBuf)
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.Internal.Data.Array.Unboxed.Type (Array(..))
90import Streamly.Internal.Data.Array.Unboxed.Stream (lpackArraysChunksOf)
91import Streamly.Internal.Data.Fold (Fold)
92import Streamly.Internal.Data.Stream.Type (Stream)
93import Streamly.Internal.Data.Unfold.Type (Unfold(..))
94-- import Streamly.String (encodeUtf8, decodeUtf8, foldLines)
95import Streamly.Internal.System.IO (defaultChunkSize)
97import qualified Streamly.Data.Array.Unboxed as A (reader, length, writeN)
98import qualified Streamly.Data.Fold as FL
99import qualified Streamly.Internal.Data.Array.Unboxed.Type as A
100 (unsafeFreeze, asPtrUnsafe, byteLength, writeNUnsafe)
101import qualified Streamly.Internal.Data.Array.Unboxed.Mut as MArray
102 (Array(..), newPinnedBytes, asPtrUnsafe)
103import qualified Streamly.Internal.Data.Array.Unboxed.Stream as AS
104import qualified Streamly.Internal.Data.Stream as S
105import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
106 (Stream(..), Step(..))
107import qualified Streamly.Data.Unfold as UF
108import qualified Streamly.Internal.Data.Unfold as UF (first, map)
109import qualified Streamly.Internal.Data.Stream.StreamK.Type as K (mkStream)
113-- >>> import Streamly.Internal.System.IO (defaultChunkSize)
114-- >>> import qualified Streamly.Internal.Network.Socket as Socket
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'.
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
130-- /Inhibits stream fusion/
133{-# INLINE withSocket #-}
134withSocket :: (MonadIO m, MonadCatch m) =>
135 Socket -> (Socket -> Stream m a) -> Stream m a
136withSocket sk f = S.finallyIO (Net.close sk) (f sk)
138-------------------------------------------------------------------------------
140-------------------------------------------------------------------------------
142-- XXX Protocol specific socket options should be separated from socket level
145-- | Specify the socket protocol details.
146data SockSpec = SockSpec
148 sockFamily :: !Family
149 , sockType :: !SocketType
150 , sockProto :: !ProtocolNumber
151 , sockOpts :: ![(SocketOption, Int)]
154initListener :: Int -> SockSpec -> SockAddr -> IO Socket
155initListener listenQLen SockSpec{..} addr =
157 sock <- socket sockFamily sockType sockProto
158 use sock `onException` Net.close sock
164 mapM_ (\(opt, val) -> setSocketOption sock opt val) sockOpts
166 Net.listen sock listenQLen
168{-# INLINE listenTuples #-}
169listenTuples :: MonadIO m
170 => Unfold m (Int, SockSpec, SockAddr) (Socket, SockAddr)
171listenTuples = Unfold step inject
173 inject (listenQLen, spec, addr) =
174 liftIO $ initListener listenQLen spec addr
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.
187{-# INLINE acceptor #-}
188acceptor :: MonadIO m => Unfold m (Int, SockSpec, SockAddr) Socket
189acceptor = 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
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.
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.
220{-# INLINE connectFrom #-}
221connectFrom :: SockSpec -> SockAddr -> SockAddr -> IO Socket
222connectFrom spec local = connectCommon spec (Just local)
224-------------------------------------------------------------------------------
226-------------------------------------------------------------------------------
228{-# INLINE recvConnectionTuplesWith #-}
229recvConnectionTuplesWith :: MonadIO m
230 => Int -> SockSpec -> SockAddr -> Stream m (Socket, SockAddr)
231recvConnectionTuplesWith tcpListenQ spec addr = S.unfoldrM step Nothing
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.
249accept :: MonadIO m => Int -> SockSpec -> SockAddr -> Stream m Socket
250accept tcpListenQ spec addr =
251 fst <$> recvConnectionTuplesWith tcpListenQ spec addr
253-------------------------------------------------------------------------------
255-------------------------------------------------------------------------------
257{-# INLINABLE readArrayUptoWith #-}
259 :: (h -> Ptr Word8 -> Int -> IO Int)
263readArrayUptoWith f size h = do
264 arr <- MArray.newPinnedBytes size
265 -- ptr <- mallocPlainForeignPtrAlignedBytes size (alignment (undefined :: Word8))
266 MArray.asPtrUnsafe arr $ \p -> do
268 let v = A.unsafeFreeze
269 $ arr { MArray.arrEnd = n, MArray.arrBound = size }
271 -- XXX shrink only if the diff is significant
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.
281{-# INLINABLE getChunk #-}
282getChunk :: Int -> Socket -> IO (Array Word8)
283getChunk = readArrayUptoWith recvBuf
285-------------------------------------------------------------------------------
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
296 let fd = fdSocket s in threadWaitWrite $ fromIntegral fd
298waitWhen0 _ _ = return ()
300sendAll :: Socket -> Ptr Word8 -> Int -> IO ()
301sendAll _ _ len | len <= 0 = return ()
303 sent <- sendBuf s p len
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 ())
314writeArrayWith _ _ arr | A.length arr == 0 = return ()
315writeArrayWith f h arr = A.asPtrUnsafe arr $ \ptr -> f h (castPtr ptr) aLen
319 aLen = A.byteLength arr
321-- | Write an Array to a file handle.
324{-# INLINABLE putChunk #-}
325putChunk :: Unboxed a => Socket -> Array a -> IO ()
326putChunk = 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
338 -- XXX use cons/nil instead
339 go = K.mkStream $ \_ yld _ stp -> do
340 arr <- liftIO $ f size h
345-- | @readChunksWith bufsize socket@ reads a stream of arrays from @socket@.
346-- The maximum size of a single array is limited to @bufsize@.
349{-# INLINE_NORMAL readChunksWith #-}
350readChunksWith :: MonadIO m => Int -> Socket -> Stream m (Array Word8)
351-- readChunksWith = _readChunksUptoWith readChunk
352readChunksWith size h = S.fromStreamD (D.Stream step ())
354 {-# INLINE_LATE step #-}
356 arr <- liftIO $ getChunk size h
362-- | Read a stream of byte arrays from a socket. The maximum size of a single
363-- array is limited to @defaultChunkSize@.
365-- >>> readChunks = Socket.readChunksWith defaultChunkSize
368{-# INLINE readChunks #-}
369readChunks :: MonadIO m => Socket -> Stream m (Array Word8)
370readChunks = readChunksWith defaultChunkSize
372-- | Unfold the tuple @(bufsize, socket)@ into a stream of 'Word8' arrays.
373-- Read requests to the socket are performed using a buffer of size @bufsize@.
374-- The size of an array in the resulting stream is always less than or equal to
378{-# INLINE_NORMAL chunkReaderWith #-}
379chunkReaderWith :: MonadIO m => Unfold m (Int, Socket) (Array Word8)
380chunkReaderWith = Unfold step return
382 {-# INLINE_LATE step #-}
384 arr <- liftIO $ getChunk size h
388 _ -> D.Yield arr (size, h)
390-- | Same as 'chunkReaderWith'
393{-# DEPRECATED readChunksWithBufferOf "Please use 'chunkReaderWith' instead" #-}
394{-# INLINE_NORMAL readChunksWithBufferOf #-}
395readChunksWithBufferOf :: MonadIO m => Unfold m (Int, Socket) (Array Word8)
396readChunksWithBufferOf = chunkReaderWith
398-- | Unfolds a socket into a stream of 'Word8' arrays. Requests to the socket
399-- are performed using a buffer of size
400-- 'Streamly.Internal.Data.Array.Unboxed.Type.defaultChunkSize'. The
401-- size of arrays in the resulting stream are therefore less than or equal to
402-- 'Streamly.Internal.Data.Array.Unboxed.Type.defaultChunkSize'.
405{-# INLINE chunkReader #-}
406chunkReader :: MonadIO m => Unfold m Socket (Array Word8)
407chunkReader = UF.first defaultChunkSize chunkReaderWith
409-------------------------------------------------------------------------------
410-- Read File to Stream
411-------------------------------------------------------------------------------
413-- | Generate a byte stream from a socket using a buffer of the given size.
416{-# INLINE readWith #-}
417readWith :: MonadIO m => Int -> Socket -> Stream m Word8
418readWith size = AS.concat . readChunksWith size
420-- | Generate a byte stream from a socket.
422-- >>> read = Socket.readWith defaultChunkSize
426read :: MonadIO m => Socket -> Stream m Word8
427read = readWith defaultChunkSize
429-- | Unfolds the tuple @(bufsize, socket)@ into a byte stream, read requests
430-- to the socket are performed using buffers of @bufsize@.
433{-# INLINE readerWith #-}
434readerWith :: MonadIO m => Unfold m (Int, Socket) Word8
435readerWith = UF.many A.reader chunkReaderWith
437-- | Same as 'readWith'
440{-# DEPRECATED readWithBufferOf "Please use 'readerWith' instead" #-}
441{-# INLINE readWithBufferOf #-}
442readWithBufferOf :: MonadIO m => Unfold m (Int, Socket) Word8
443readWithBufferOf = readerWith
445-- | Unfolds a 'Socket' into a byte stream. IO requests to the socket are
446-- performed in sizes of
447-- 'Streamly.Internal.Data.Array.Unboxed.Type.defaultChunkSize'.
451reader :: MonadIO m => Unfold m Socket Word8
452reader = UF.first defaultChunkSize readerWith
454-------------------------------------------------------------------------------
456-------------------------------------------------------------------------------
458-- | Write a stream of arrays to a handle.
461{-# INLINE putChunks #-}
462putChunks :: (MonadIO m, Unboxed a)
463 => Socket -> Stream m (Array a) -> m ()
464putChunks h = S.fold (FL.drainBy (liftIO . putChunk h))
466-- | Write a stream of arrays to a socket. Each array in the stream is written
467-- to the socket as a separate IO request.
470{-# INLINE writeChunks #-}
471writeChunks :: (MonadIO m, Unboxed a) => Socket -> Fold m (Array a) ()
472writeChunks h = FL.drainBy (liftIO . putChunk h)
474-- | @writeChunksWith bufsize socket@ writes a stream of arrays to
475-- @socket@ after coalescing the adjacent arrays in chunks of @bufsize@.
476-- Multiple arrays are coalesed as long as the total size remains below the
477-- specified size. It never splits an array, if a single array is bigger than
478-- the specified size it emitted as it is.
481{-# INLINE writeChunksWith #-}
482writeChunksWith :: (MonadIO m, Unboxed a)
483 => Int -> Socket -> Fold m (Array a) ()
484writeChunksWith n h = lpackArraysChunksOf n (writeChunks h)
486-- | Same as 'writeChunksWith'
489{-# DEPRECATED writeChunksWithBufferOf "Please use 'writeChunksWith' instead" #-}
490{-# INLINE writeChunksWithBufferOf #-}
491writeChunksWithBufferOf :: (MonadIO m, Unboxed a)
492 => Int -> Socket -> Fold m (Array a) ()
493writeChunksWithBufferOf = writeChunksWith
495-- GHC buffer size dEFAULT_FD_BUFFER_SIZE=8192 bytes.
498-- Note that if you use a chunk size less than 8K (GHC's default buffer
499-- size) then you are advised to use 'NOBuffering' mode on the 'Handle' in case you
500-- do not want buffering to occur at GHC level as well. Same thing applies to
503-- | Like 'write' but provides control over the write buffer. Output will
504-- be written to the IO device as soon as we collect the specified number of
508{-# INLINE putBytesWith #-}
509putBytesWith :: MonadIO m => Int -> Socket -> Stream m Word8 -> m ()
510putBytesWith n h m = putChunks h $ AS.arraysOf n m
512-- | Write a byte stream to a socket. Accumulates the input in chunks of
513-- specified number of bytes before writing.
516{-# INLINE writeWith #-}
517writeWith :: MonadIO m => Int -> Socket -> Fold m Word8 ()
518writeWith n h = FL.chunksOf n (A.writeNUnsafe n) (writeChunks h)
520-- | Same as 'writeWith'
523{-# DEPRECATED writeWithBufferOf "Please use 'writeWith' instead" #-}
524{-# INLINE writeWithBufferOf #-}
525writeWithBufferOf :: MonadIO m => Int -> Socket -> Fold m Word8 ()
526writeWithBufferOf = writeWith
528-- | Write a stream of 'Maybe' values. Keep buffering the 'Just' values in an
529-- array. Write the array to the 'Handle' as soon as a 'Nothing' is encountered
530-- or the buffer size exceeds the specified limit.
533{-# INLINE writeMaybesWith #-}
534writeMaybesWith :: (MonadIO m )
535 => Int -> Socket -> Fold m (Maybe Word8) ()
537 let writeNJusts = FL.lmap fromJust $ A.writeN n
538 writeOnNothing = FL.takeEndBy_ isNothing writeNJusts
539 in FL.many writeOnNothing (writeChunks h)
541-- > write = 'writeWith' defaultChunkSize
543-- | Write a byte stream to a file handle. Combines the bytes in chunks of size
544-- up to 'defaultChunkSize' before writing. Note that the write behavior
545-- depends on the 'IOMode' and the current seek position of the handle.
548{-# INLINE putBytes #-}
549putBytes :: MonadIO m => Socket -> Stream m Word8 -> m ()
550putBytes = putBytesWith defaultChunkSize
552-- | Write a byte stream to a socket. Accumulates the input in chunks of
553-- up to 'defaultChunkSize' bytes before writing.
556-- write = 'writeWith' 'defaultChunkSize'
561write :: MonadIO m => Socket -> Fold m Word8 ()
562write = writeWith defaultChunkSize
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