on fa75413 help sign in
561 lines 19 kB history raw
1{-# OPTIONS_HADDOCK hide #-}
2{-# LANGUAGE CPP #-}
3{-# LANGUAGE BangPatterns #-}
4{-# LANGUAGE FlexibleContexts #-}
5{-# LANGUAGE MagicHash #-}
6{-# LANGUAGE RecordWildCards #-}
7{-# LANGUAGE UnboxedTuples #-}
9#include "inline.hs"
11-- |
12-- Module : Streamly.Internal.Network.Socket
13-- Copyright : (c) 2018 Composewell Technologies
14--
15-- License : BSD3
16-- Maintainer : streamly@composewell.com
17-- Stability : experimental
18-- Portability : GHC
19--
20module Streamly.Internal.Network.Socket
21 (
22 SockSpec (..)
23 -- * Use a socket
24 , handleWithM
25 , handleWith
27 -- * Accept connections
28 , accept
29 , connections
31 -- * Read from connection
32 , read
33 , readWithBufferOf
34 -- , readUtf8
35 -- , readLines
36 -- , readFrames
37 -- , readByChunks
39 -- -- * Array Read
40 -- , readArrayUpto
41 -- , readArrayOf
43 -- , readChunksUpto
44 , readChunksWithBufferOf
45 , readChunks
47 , toChunksWithBufferOf
48 , toChunks
49 , toBytes
51 -- * Write to connection
52 , write
53 -- , writeUtf8
54 -- , writeUtf8ByLines
55 -- , writeByFrames
56 , writeWithBufferOf
58 , fromChunks
59 , fromBytesWithBufferOf
60 , fromBytes
62 -- -- * Array Write
63 , writeChunk
64 , writeChunks
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(..))
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-- | Write a stream of strings to a socket in Latin1 encoding. Output is
430-- flushed to the socket for each string.
431--
432-- /Internal/
433--
434{-# INLINE writeStrings #-}
435writeStrings :: MonadIO m
436 => (SerialT m Char -> SerialT m Word8) -> Socket -> Fold m String ()
437writeStrings encode h =
438 FL.lmapM (IA.fromStream . encode . S.fromList) (writeChunks h)
440-- GHC buffer size dEFAULT_FD_BUFFER_SIZE=8192 bytes.
441--
442-- XXX test this
443-- Note that if you use a chunk size less than 8K (GHC's default buffer
444-- size) then you are advised to use 'NOBuffering' mode on the 'Handle' in case you
445-- do not want buffering to occur at GHC level as well. Same thing applies to
446-- writes as well.
448-- | Like 'write' but provides control over the write buffer. Output will
449-- be written to the IO device as soon as we collect the specified number of
450-- input elements.
451--
452-- @since 0.7.0
453{-# INLINE fromBytesWithBufferOf #-}
454fromBytesWithBufferOf :: MonadIO m => Int -> Socket -> SerialT m Word8 -> m ()
455fromBytesWithBufferOf n h m = fromChunks h $ AS.arraysOf n m
457-- | Write a byte stream to a socket. Accumulates the input in chunks of
458-- specified number of bytes before writing.
459--
460-- @since 0.7.0
461{-# INLINE writeWithBufferOf #-}
462writeWithBufferOf :: MonadIO m => Int -> Socket -> Fold m Word8 ()
463writeWithBufferOf n h = FL.lchunksOf n (A.writeNUnsafe n) (writeChunks h)
465-- > write = 'writeWithBufferOf' A.defaultChunkSize
466--
467-- | Write a byte stream to a file handle. Combines the bytes in chunks of size
468-- up to 'A.defaultChunkSize' before writing. Note that the write behavior
469-- depends on the 'IOMode' and the current seek position of the handle.
470--
471-- @since 0.7.0
472{-# INLINE fromBytes #-}
473fromBytes :: MonadIO m => Socket -> SerialT m Word8 -> m ()
474fromBytes = fromBytesWithBufferOf A.defaultChunkSize
476-- | Write a byte stream to a socket. Accumulates the input in chunks of
477-- up to 'A.defaultChunkSize' bytes before writing.
478--
479-- @
480-- write = 'writeWithBufferOf' 'A.defaultChunkSize'
481-- @
482--
483-- @since 0.7.0
484{-# INLINE write #-}
485write :: MonadIO m => Socket -> Fold m Word8 ()
486write = writeWithBufferOf A.defaultChunkSize
488{-
489{-# INLINE write #-}
490write :: (MonadIO m, Storable a) => Handle -> SerialT m a -> m ()
491write = toHandleWith A.defaultChunkSize
492-}
494-------------------------------------------------------------------------------
495-- IO with encoding/decoding Unicode characters
496-------------------------------------------------------------------------------
498{-
499-- |
500-- > readUtf8 = decodeUtf8 . read
501--
502-- Read a UTF8 encoded stream of unicode characters from a file handle.
503--
504-- @since 0.7.0
505{-# INLINE readUtf8 #-}
506readUtf8 :: (IsStream t, MonadIO m) => Handle -> t m Char
507readUtf8 = decodeUtf8 . read
509-- |
510-- > writeUtf8 h s = write h $ encodeUtf8 s
511--
512-- Encode a stream of unicode characters to UTF8 and write it to the given file
513-- handle. Default block buffering applies to the writes.
514--
515-- @since 0.7.0
516{-# INLINE writeUtf8 #-}
517writeUtf8 :: MonadIO m => Handle -> SerialT m Char -> m ()
518writeUtf8 h s = write h $ encodeUtf8 s
520-- | Write a stream of unicode characters after encoding to UTF-8 in chunks
521-- separated by a linefeed character @'\n'@. If the size of the buffer exceeds
522-- @defaultChunkSize@ and a linefeed is not yet found, the buffer is written
523-- anyway. This is similar to writing to a 'Handle' with the 'LineBuffering'
524-- option.
525--
526-- @since 0.7.0
527{-# INLINE writeUtf8ByLines #-}
528writeUtf8ByLines :: (IsStream t, MonadIO m) => Handle -> t m Char -> m ()
529writeUtf8ByLines = undefined
531-- | Read UTF-8 lines from a file handle and apply the specified fold to each
532-- line. This is similar to reading a 'Handle' with the 'LineBuffering' option.
533--
534-- @since 0.7.0
535{-# INLINE readLines #-}
536readLines :: (IsStream t, MonadIO m) => Handle -> Fold m Char b -> t m b
537readLines h f = foldLines (readUtf8 h) f
539-------------------------------------------------------------------------------
540-- Framing on a sequence
541-------------------------------------------------------------------------------
543-- | Read a stream from a file handle and split it into frames delimited by
544-- the specified sequence of elements. The supplied fold is applied on each
545-- frame.
546--
547-- @since 0.7.0
548{-# INLINE readFrames #-}
549readFrames :: (IsStream t, MonadIO m, Storable a)
550 => Array a -> Handle -> Fold m a b -> t m b
551readFrames = undefined -- foldFrames . read
553-- | Write a stream to the given file handle buffering up to frames separated
554-- by the given sequence or up to a maximum of @defaultChunkSize@.
555--
556-- @since 0.7.0
557{-# INLINE writeByFrames #-}
558writeByFrames :: (IsStream t, MonadIO m, Storable a)
559 => Array a -> Handle -> t m a -> m ()
560writeByFrames = undefined
561-}