on d7d42be help sign in
573 lines 20 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 SockSpec (..)
22 -- * Use a socket
23 , handleWithM
24 , handleWith
26 -- * Accept connections
27 , accept
28 , connections
30 -- * Read from connection
31 , read
32 , readWithBufferOf
33 -- , readUtf8
34 -- , readLines
35 -- , readFrames
36 -- , readByChunks
38 -- -- * Array Read
39 -- , readArrayUpto
40 -- , readArrayOf
42 -- , readChunksUpto
43 , readChunksWithBufferOf
44 , readChunks
46 , toChunksWithBufferOf
47 , toChunks
48 , toBytes
50 -- * Write to connection
51 , write
52 -- , writeUtf8
53 -- , writeUtf8ByLines
54 -- , writeByFrames
55 , writeWithBufferOf
57 , fromChunks
58 , fromBytesWithBufferOf
59 , fromBytes
61 -- -- * Array Write
62 , writeChunk
63 , writeChunks
64 , writeChunksWithBufferOf
65 , writeStrings
67 -- reading/writing datagrams
68 )
69where
71import Control.Concurrent (threadWaitWrite, rtsSupportsBoundThreads)
72import Control.Monad.Catch (MonadCatch, finally, MonadMask)
73import Control.Monad.IO.Class (MonadIO(..))
74import Control.Monad (when)
75import Data.Word (Word8)
76import Foreign.ForeignPtr (withForeignPtr)
77import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
78import Foreign.Ptr (minusPtr, plusPtr, Ptr, castPtr)
79import Foreign.Storable (Storable(..))
80import GHC.ForeignPtr (mallocPlainForeignPtrBytes)
81import Network.Socket (sendBuf, recvBuf)
82import Network.Socket
83 (Socket, SocketOption(..), Family(..), SockAddr(..),
84 ProtocolNumber, withSocketsDo, SocketType(..), socket, bind,
85 setSocketOption)
86#if MIN_VERSION_network(3,1,0)
87import Network.Socket (withFdSocket)
88#else
89import Network.Socket (fdSocket)
90#endif
91import Prelude hiding (read)
93import qualified Network.Socket as Net
95import Streamly (MonadAsync)
96import Streamly.Internal.Data.Unfold.Types (Unfold(..))
97import Streamly.Internal.Memory.Array.Types (Array(..), lpackArraysChunksOf)
98import Streamly.Internal.Data.Stream.Serial (SerialT)
99import Streamly.Internal.Data.Stream.StreamK.Type (IsStream, mkStream)
100import Streamly.Data.Fold (Fold)
101-- import Streamly.String (encodeUtf8, decodeUtf8, foldLines)
103import qualified Streamly.Data.Fold as FL
104import qualified Streamly.Internal.Data.Fold.Types as FL
105import qualified Streamly.Internal.Data.Unfold as UF
106import qualified Streamly.Internal.Memory.Array as IA
107import qualified Streamly.Memory.Array as A
108import qualified Streamly.Internal.Memory.ArrayStream as AS
109import qualified Streamly.Internal.Memory.Array.Types as A
110import qualified Streamly.Prelude as S
111import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
113-- | @'handleWithM' socket act@ runs the monadic computation @act@ passing the
114-- socket handle to it. The handle will be closed on exit from 'handleWithM',
115-- whether by normal termination or by raising an exception. If closing the
116-- handle raises an exception, then this exception will be raised by
117-- 'handleWithM' rather than any exception raised by 'act'.
118--
119-- @since 0.7.0
120{-# INLINE handleWithM #-}
121handleWithM :: (MonadMask m, MonadIO m) => (Socket -> m ()) -> Socket -> m ()
122handleWithM f sk = finally (f sk) (liftIO (Net.close sk))
124-- | Like 'handleWithM' but runs a streaming computation instead of a monadic
125-- computation.
126--
127-- @since 0.7.0
128{-# INLINE handleWith #-}
129handleWith :: (IsStream t, MonadCatch m, MonadIO m)
130 => Socket -> (Socket -> t m a) -> t m a
131handleWith sk f = S.finally (liftIO $ Net.close sk) (f sk)
133-------------------------------------------------------------------------------
134-- Accept (Unfolds)
135-------------------------------------------------------------------------------
137-- XXX Protocol specific socket options should be separated from socket level
138-- options.
139--
140-- | Specify the socket protocol details.
141data SockSpec = SockSpec
142 {
143 sockFamily :: !Family
144 , sockType :: !SocketType
145 , sockProto :: !ProtocolNumber
146 , sockOpts :: ![(SocketOption, Int)]
147 }
149initListener :: Int -> SockSpec -> SockAddr -> IO Socket
150initListener listenQLen SockSpec{..} addr =
151 withSocketsDo $ do
152 sock <- socket sockFamily sockType sockProto
153 mapM_ (\(opt, val) -> setSocketOption sock opt val) sockOpts
154 bind sock addr
155 Net.listen sock listenQLen
156 return sock
158{-# INLINE listenTuples #-}
159listenTuples :: MonadIO m
160 => Unfold m (Int, SockSpec, SockAddr) (Socket, SockAddr)
161listenTuples = Unfold step inject
162 where
163 inject (listenQLen, spec, addr) = do
164 listener <- liftIO $ initListener listenQLen spec addr
165 return listener
167 step listener = do
168 r <- liftIO $ Net.accept listener
169 -- XXX error handling
170 return $ D.Yield r listener
172-- | Unfold a three tuple @(listenQLen, spec, addr)@ into a stream of connected
173-- protocol sockets corresponding to incoming connections. @listenQLen@ is the
174-- maximum number of pending connections in the backlog. @spec@ is the socket
175-- protocol and options specification and @addr@ is the protocol address where
176-- the server listens for incoming connections.
177--
178-- @since 0.7.0
179{-# INLINE accept #-}
180accept :: MonadIO m => Unfold m (Int, SockSpec, SockAddr) Socket
181accept = UF.map fst listenTuples
183-------------------------------------------------------------------------------
184-- Listen (Streams)
185-------------------------------------------------------------------------------
187{-# INLINE recvConnectionTuplesWith #-}
188recvConnectionTuplesWith :: MonadAsync m
189 => Int -> SockSpec -> SockAddr -> SerialT m (Socket, SockAddr)
190recvConnectionTuplesWith tcpListenQ spec addr = S.unfoldrM step Nothing
191 where
192 step Nothing = do
193 listener <- liftIO $ initListener tcpListenQ spec addr
194 r <- liftIO $ Net.accept listener
195 -- XXX error handling
196 return $ Just (r, Just listener)
198 step (Just listener) = do
199 r <- liftIO $ Net.accept listener
200 -- XXX error handling
201 return $ Just (r, Just listener)
203-- | Start a TCP stream server that listens for connections on the supplied
204-- server address specification (address family, local interface IP address and
205-- port). The server generates a stream of connected sockets. The first
206-- argument is the maximum number of pending connections in the backlog.
207--
208-- /Internal/
209{-# INLINE connections #-}
210connections :: MonadAsync m => Int -> SockSpec -> SockAddr -> SerialT m Socket
211connections tcpListenQ spec addr = fmap fst $
212 recvConnectionTuplesWith tcpListenQ spec addr
214-------------------------------------------------------------------------------
215-- Array IO (Input)
216-------------------------------------------------------------------------------
218{-# INLINABLE readArrayUptoWith #-}
219readArrayUptoWith
220 :: (h -> Ptr Word8 -> Int -> IO Int)
221 -> Int
222 -> h
223 -> IO (Array Word8)
224readArrayUptoWith f size h = do
225 ptr <- mallocPlainForeignPtrBytes size
226 -- ptr <- mallocPlainForeignPtrAlignedBytes size (alignment (undefined :: Word8))
227 withForeignPtr ptr $ \p -> do
228 n <- f h p size
229 let v = Array
230 { aStart = ptr
231 , aEnd = p `plusPtr` n
232 , aBound = p `plusPtr` size
233 }
234 -- XXX shrink only if the diff is significant
235 -- A.shrinkToFit v
236 return v
238-- | Read a 'ByteArray' from a file handle. If no data is available on the
239-- handle it blocks until some data becomes available. If data is available
240-- then it immediately returns that data without blocking. It reads a maximum
241-- of up to the size requested.
242{-# INLINABLE readArrayOf #-}
243readArrayOf :: Int -> Socket -> IO (Array Word8)
244readArrayOf = readArrayUptoWith recvBuf
246-------------------------------------------------------------------------------
247-- Array IO (output)
248-------------------------------------------------------------------------------
250waitWhen0 :: Int -> Socket -> IO ()
251waitWhen0 0 s = when rtsSupportsBoundThreads $
252#if MIN_VERSION_network(3,1,0)
253 withFdSocket s $ \fd -> threadWaitWrite $ fromIntegral fd
254#else
255 let fd = fdSocket s in threadWaitWrite $ fromIntegral fd
256#endif
257waitWhen0 _ _ = return ()
259sendAll :: Socket -> Ptr Word8 -> Int -> IO ()
260sendAll _ _ len | len <= 0 = return ()
261sendAll s p len = do
262 sent <- sendBuf s p len
263 waitWhen0 sent s
264 -- assert (sent <= len)
265 when (sent >= 0) $ sendAll s (p `plusPtr` sent) (len - sent)
267{-# INLINABLE writeArrayWith #-}
268writeArrayWith :: Storable a
269 => (h -> Ptr Word8 -> Int -> IO ())
270 -> h
271 -> Array a
272 -> IO ()
273writeArrayWith _ _ arr | A.length arr == 0 = return ()
274writeArrayWith f h Array{..} = withForeignPtr aStart $ \p ->
275 f h (castPtr p) aLen
276 where
277 aLen =
278 let p = unsafeForeignPtrToPtr aStart
279 in aEnd `minusPtr` p
281-- | Write an Array to a file handle.
282--
283-- @since 0.7.0
284{-# INLINABLE writeChunk #-}
285writeChunk :: Storable a => Socket -> Array a -> IO ()
286writeChunk = writeArrayWith sendAll
288-------------------------------------------------------------------------------
289-- Stream of Arrays IO
290-------------------------------------------------------------------------------
292{-# INLINABLE _readChunksUptoWith #-}
293_readChunksUptoWith :: (IsStream t, MonadIO m)
294 => (Int -> h -> IO (Array Word8))
295 -> Int -> h -> t m (Array Word8)
296_readChunksUptoWith f size h = go
297 where
298 -- XXX use cons/nil instead
299 go = mkStream $ \_ yld _ stp -> do
300 arr <- liftIO $ f size h
301 if A.length arr == 0
302 then stp
303 else yld arr go
305-- | @toChunksWithBufferOf size h@ reads a stream of arrays from file handle @h@.
306-- The maximum size of a single array is limited to @size@.
307-- 'fromHandleArraysUpto' ignores the prevailing 'TextEncoding' and 'NewlineMode'
308-- on the 'Handle'.
309{-# INLINE_NORMAL toChunksWithBufferOf #-}
310toChunksWithBufferOf :: (IsStream t, MonadIO m)
311 => Int -> Socket -> t m (Array Word8)
312-- toChunksWithBufferOf = _readChunksUptoWith readArrayOf
313toChunksWithBufferOf size h = D.fromStreamD (D.Stream step ())
314 where
315 {-# INLINE_LATE step #-}
316 step _ _ = do
317 arr <- liftIO $ readArrayOf size h
318 return $
319 case A.length arr of
320 0 -> D.Stop
321 _ -> D.Yield arr ()
323-- XXX read 'Array a' instead of Word8
324--
325-- | @toChunks h@ reads a stream of arrays from socket handle @h@.
326-- The maximum size of a single array is limited to @defaultChunkSize@.
327--
328-- @since 0.7.0
329{-# INLINE toChunks #-}
330toChunks :: (IsStream t, MonadIO m) => Socket -> t m (Array Word8)
331toChunks = toChunksWithBufferOf A.defaultChunkSize
333-- | Unfold the tuple @(bufsize, socket)@ into a stream of 'Word8' arrays.
334-- Read requests to the socket are performed using a buffer of size @bufsize@.
335-- The size of an array in the resulting stream is always less than or equal to
336-- @bufsize@.
337--
338-- @since 0.7.0
339{-# INLINE_NORMAL readChunksWithBufferOf #-}
340readChunksWithBufferOf :: MonadIO m => Unfold m (Int, Socket) (Array Word8)
341readChunksWithBufferOf = Unfold step return
342 where
343 {-# INLINE_LATE step #-}
344 step (size, h) = do
345 arr <- liftIO $ readArrayOf size h
346 return $
347 case A.length arr of
348 0 -> D.Stop
349 _ -> D.Yield arr (size, h)
351-- | Unfolds a socket into a stream of 'Word8' arrays. Requests to the socket
352-- are performed using a buffer of size
353-- 'Streamly.Internal.Memory.Array.Types.defaultChunkSize'. The
354-- size of arrays in the resulting stream are therefore less than or equal to
355-- 'Streamly.Internal.Memory.Array.Types.defaultChunkSize'.
356--
357-- @since 0.7.0
358{-# INLINE readChunks #-}
359readChunks :: MonadIO m => Unfold m Socket (Array Word8)
360readChunks = UF.supplyFirst readChunksWithBufferOf A.defaultChunkSize
362-------------------------------------------------------------------------------
363-- Read File to Stream
364-------------------------------------------------------------------------------
366-- TODO for concurrent streams implement readahead IO. We can send multiple
367-- read requests at the same time. For serial case we can use async IO. We can
368-- also control the read throughput in mbps or IOPS.
370{-
371-- | @readWithBufferOf bufsize handle@ reads a byte stream from a file
372-- handle, reads are performed in chunks of up to @bufsize@. The stream ends
373-- as soon as EOF is encountered.
374--
375{-# INLINE readWithBufferOf #-}
376readWithBufferOf :: (IsStream t, MonadIO m) => Int -> Handle -> t m Word8
377readWithBufferOf chunkSize h = A.flattenArrays $ readChunksUpto chunkSize h
378-}
380-- TODO
381-- read :: (IsStream t, MonadIO m, Storable a) => Handle -> t m a
382--
383-- > read = 'readByChunks' A.defaultChunkSize
384-- | Generate a stream of elements of the given type from a socket. The
385-- stream ends when EOF is encountered.
386--
387-- @since 0.7.0
388{-# INLINE toBytes #-}
389toBytes :: (IsStream t, MonadIO m) => Socket -> t m Word8
390toBytes = AS.concat . toChunks
392-- | Unfolds the tuple @(bufsize, socket)@ into a byte stream, read requests
393-- to the socket are performed using buffers of @bufsize@.
394--
395-- @since 0.7.0
396{-# INLINE readWithBufferOf #-}
397readWithBufferOf :: MonadIO m => Unfold m (Int, Socket) Word8
398readWithBufferOf = UF.concat readChunksWithBufferOf A.read
400-- | Unfolds a 'Socket' into a byte stream. IO requests to the socket are
401-- performed in sizes of
402-- 'Streamly.Internal.Memory.Array.Types.defaultChunkSize'.
403--
404-- @since 0.7.0
405{-# INLINE read #-}
406read :: MonadIO m => Unfold m Socket Word8
407read = UF.supplyFirst readWithBufferOf A.defaultChunkSize
409-------------------------------------------------------------------------------
410-- Writing
411-------------------------------------------------------------------------------
413-- | Write a stream of arrays to a handle.
414--
415-- @since 0.7.0
416{-# INLINE fromChunks #-}
417fromChunks :: (MonadIO m, Storable a)
418 => Socket -> SerialT m (Array a) -> m ()
419fromChunks h m = S.mapM_ (liftIO . writeChunk h) m
421-- | Write a stream of arrays to a socket. Each array in the stream is written
422-- to the socket as a separate IO request.
423--
424-- @since 0.7.0
425{-# INLINE writeChunks #-}
426writeChunks :: (MonadIO m, Storable a) => Socket -> Fold m (Array a) ()
427writeChunks h = FL.drainBy (liftIO . writeChunk h)
429-- | @writeChunksWithBufferOf bufsize socket@ writes a stream of arrays
430-- to @socket@ after coalescing the adjacent arrays in chunks of @bufsize@.
431-- We never split an array, if a single array is bigger than the specified size
432-- it emitted as it is. Multiple arrays are coalesed as long as the total size
433-- remains below the specified size.
434--
435-- @since 0.7.0
436{-# INLINE writeChunksWithBufferOf #-}
437writeChunksWithBufferOf :: (MonadIO m, Storable a)
438 => Int -> Socket -> Fold m (Array a) ()
439writeChunksWithBufferOf n h = lpackArraysChunksOf n (writeChunks h)
441-- | Write a stream of strings to a socket in Latin1 encoding. Output is
442-- flushed to the socket for each string.
443--
444-- /Internal/
445--
446{-# INLINE writeStrings #-}
447writeStrings :: MonadIO m
448 => (SerialT m Char -> SerialT m Word8) -> Socket -> Fold m String ()
449writeStrings encode h =
450 FL.lmapM (IA.fromStream . encode . S.fromList) (writeChunks h)
452-- GHC buffer size dEFAULT_FD_BUFFER_SIZE=8192 bytes.
453--
454-- XXX test this
455-- Note that if you use a chunk size less than 8K (GHC's default buffer
456-- size) then you are advised to use 'NOBuffering' mode on the 'Handle' in case you
457-- do not want buffering to occur at GHC level as well. Same thing applies to
458-- writes as well.
460-- | Like 'write' but provides control over the write buffer. Output will
461-- be written to the IO device as soon as we collect the specified number of
462-- input elements.
463--
464-- @since 0.7.0
465{-# INLINE fromBytesWithBufferOf #-}
466fromBytesWithBufferOf :: MonadIO m => Int -> Socket -> SerialT m Word8 -> m ()
467fromBytesWithBufferOf n h m = fromChunks h $ AS.arraysOf n m
469-- | Write a byte stream to a socket. Accumulates the input in chunks of
470-- specified number of bytes before writing.
471--
472-- @since 0.7.0
473{-# INLINE writeWithBufferOf #-}
474writeWithBufferOf :: MonadIO m => Int -> Socket -> Fold m Word8 ()
475writeWithBufferOf n h = FL.lchunksOf n (A.writeNUnsafe n) (writeChunks h)
477-- > write = 'writeWithBufferOf' A.defaultChunkSize
478--
479-- | Write a byte stream to a file handle. Combines the bytes in chunks of size
480-- up to 'A.defaultChunkSize' before writing. Note that the write behavior
481-- depends on the 'IOMode' and the current seek position of the handle.
482--
483-- @since 0.7.0
484{-# INLINE fromBytes #-}
485fromBytes :: MonadIO m => Socket -> SerialT m Word8 -> m ()
486fromBytes = fromBytesWithBufferOf A.defaultChunkSize
488-- | Write a byte stream to a socket. Accumulates the input in chunks of
489-- up to 'A.defaultChunkSize' bytes before writing.
490--
491-- @
492-- write = 'writeWithBufferOf' 'A.defaultChunkSize'
493-- @
494--
495-- @since 0.7.0
496{-# INLINE write #-}
497write :: MonadIO m => Socket -> Fold m Word8 ()
498write = writeWithBufferOf A.defaultChunkSize
500{-
501{-# INLINE write #-}
502write :: (MonadIO m, Storable a) => Handle -> SerialT m a -> m ()
503write = toHandleWith A.defaultChunkSize
504-}
506-------------------------------------------------------------------------------
507-- IO with encoding/decoding Unicode characters
508-------------------------------------------------------------------------------
510{-
511-- |
512-- > readUtf8 = decodeUtf8 . read
513--
514-- Read a UTF8 encoded stream of unicode characters from a file handle.
515--
516-- @since 0.7.0
517{-# INLINE readUtf8 #-}
518readUtf8 :: (IsStream t, MonadIO m) => Handle -> t m Char
519readUtf8 = decodeUtf8 . read
521-- |
522-- > writeUtf8 h s = write h $ encodeUtf8 s
523--
524-- Encode a stream of unicode characters to UTF8 and write it to the given file
525-- handle. Default block buffering applies to the writes.
526--
527-- @since 0.7.0
528{-# INLINE writeUtf8 #-}
529writeUtf8 :: MonadIO m => Handle -> SerialT m Char -> m ()
530writeUtf8 h s = write h $ encodeUtf8 s
532-- | Write a stream of unicode characters after encoding to UTF-8 in chunks
533-- separated by a linefeed character @'\n'@. If the size of the buffer exceeds
534-- @defaultChunkSize@ and a linefeed is not yet found, the buffer is written
535-- anyway. This is similar to writing to a 'Handle' with the 'LineBuffering'
536-- option.
537--
538-- @since 0.7.0
539{-# INLINE writeUtf8ByLines #-}
540writeUtf8ByLines :: (IsStream t, MonadIO m) => Handle -> t m Char -> m ()
541writeUtf8ByLines = undefined
543-- | Read UTF-8 lines from a file handle and apply the specified fold to each
544-- line. This is similar to reading a 'Handle' with the 'LineBuffering' option.
545--
546-- @since 0.7.0
547{-# INLINE readLines #-}
548readLines :: (IsStream t, MonadIO m) => Handle -> Fold m Char b -> t m b
549readLines h f = foldLines (readUtf8 h) f
551-------------------------------------------------------------------------------
552-- Framing on a sequence
553-------------------------------------------------------------------------------
555-- | Read a stream from a file handle and split it into frames delimited by
556-- the specified sequence of elements. The supplied fold is applied on each
557-- frame.
558--
559-- @since 0.7.0
560{-# INLINE readFrames #-}
561readFrames :: (IsStream t, MonadIO m, Storable a)
562 => Array a -> Handle -> Fold m a b -> t m b
563readFrames = undefined -- foldFrames . read
565-- | Write a stream to the given file handle buffering up to frames separated
566-- by the given sequence or up to a maximum of @defaultChunkSize@.
567--
568-- @since 0.7.0
569{-# INLINE writeByFrames #-}
570writeByFrames :: (IsStream t, MonadIO m, Storable a)
571 => Array a -> Handle -> t m a -> m ()
572writeByFrames = undefined
573-}