{-# LANGUAGE CPP              #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE RecordWildCards  #-}

#include "inline.hs"

-- |
-- Module      : Streamly.Internal.Network.Socket
-- Copyright   : (c) 2018 Composewell Technologies
--
-- License     : BSD3
-- Maintainer  : streamly@composewell.com
-- Stability   : experimental
-- Portability : GHC
--
module Streamly.Internal.Network.Socket
    (
    SockSpec (..)
    -- * Use a socket
    , handleWithM
    , handleWith

    -- * Accept connections
    , accept
    , connections

    -- * Read from connection
    , read
    , readWithBufferOf
    -- , readUtf8
    -- , readLines
    -- , readFrames
    -- , readByChunks

    -- -- * Array Read
    -- , readArrayUpto
    -- , readArrayOf

    -- , readChunksUpto
    , readChunksWithBufferOf
    , readChunks

    , toChunksWithBufferOf
    , toChunks
    , toBytes

    -- * Write to connection
    , write
    -- , writeUtf8
    -- , writeUtf8ByLines
    -- , writeByFrames
    , writeWithBufferOf

    , fromChunks
    , fromBytesWithBufferOf
    , fromBytes

    -- -- * Array Write
    , writeChunk
    , writeChunks
    , writeChunksWithBufferOf
    , writeStrings

    -- reading/writing datagrams
    )
where

import Control.Concurrent (threadWaitWrite, rtsSupportsBoundThreads)
import Control.Monad.Catch (MonadCatch, finally, MonadMask)
import Control.Monad.IO.Class (MonadIO(..))
import Control.Monad (when)
import Data.Word (Word8)
import Foreign.ForeignPtr (withForeignPtr)
import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
import Foreign.Ptr (minusPtr, plusPtr, Ptr, castPtr)
import Foreign.Storable (Storable(..))
import GHC.ForeignPtr (mallocPlainForeignPtrBytes)
import Network.Socket
       (Socket, SocketOption(..), Family(..), SockAddr(..),
        ProtocolNumber, withSocketsDo, SocketType(..), socket, bind,
        setSocketOption, sendBuf, recvBuf)
#if MIN_VERSION_network(3,1,0)
import Network.Socket (withFdSocket)
#else
import Network.Socket (fdSocket)
#endif
import Prelude hiding (read)

import qualified Network.Socket as Net

import Streamly (MonadAsync)
import Streamly.Internal.Data.Unfold.Types (Unfold(..))
import Streamly.Internal.Memory.Array.Types (Array(..), lpackArraysChunksOf)
import Streamly.Internal.Data.Stream.Serial (SerialT)
import Streamly.Internal.Data.Stream.StreamK.Type (IsStream, mkStream)
import Streamly.Data.Fold (Fold)
-- import Streamly.String (encodeUtf8, decodeUtf8, foldLines)

import qualified Streamly.Data.Fold as FL
import qualified Streamly.Internal.Data.Fold.Types as FL
import qualified Streamly.Internal.Data.Unfold as UF
import qualified Streamly.Internal.Memory.Array as IA
import qualified Streamly.Memory.Array as A
import qualified Streamly.Internal.Memory.ArrayStream as AS
import qualified Streamly.Internal.Memory.Array.Types as A
import qualified Streamly.Prelude as S
import qualified Streamly.Internal.Data.Stream.StreamD.Type as D

-- | @'handleWithM' socket act@ runs the monadic computation @act@ passing the
-- socket handle to it.  The handle will be closed on exit from 'handleWithM',
-- whether by normal termination or by raising an exception.  If closing the
-- handle raises an exception, then this exception will be raised by
-- 'handleWithM' rather than any exception raised by 'act'.
--
-- @since 0.7.0
{-# INLINE handleWithM #-}
handleWithM :: (MonadMask m, MonadIO m) => (Socket -> m ()) -> Socket -> m ()
handleWithM :: forall (m :: * -> *).
(MonadMask m, MonadIO m) =>
(Socket -> m ()) -> Socket -> m ()
handleWithM Socket -> m ()
f Socket
sk = forall (m :: * -> *) a b. MonadMask m => m a -> m b -> m a
finally (Socket -> m ()
f Socket
sk) (forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Socket -> IO ()
Net.close Socket
sk))

-- | Like 'handleWithM' but runs a streaming computation instead of a monadic
-- computation.
--
-- @since 0.7.0
{-# INLINE handleWith #-}
handleWith :: (IsStream t, MonadCatch m, MonadIO m)
    => Socket -> (Socket -> t m a) -> t m a
handleWith :: forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, MonadCatch m, MonadIO m) =>
Socket -> (Socket -> t m a) -> t m a
handleWith Socket
sk Socket -> t m a
f = forall (t :: (* -> *) -> * -> *) (m :: * -> *) b a.
(IsStream t, MonadCatch m) =>
m b -> t m a -> t m a
S.finally (forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ Socket -> IO ()
Net.close Socket
sk) (Socket -> t m a
f Socket
sk)

-------------------------------------------------------------------------------
-- Accept (Unfolds)
-------------------------------------------------------------------------------

-- XXX Protocol specific socket options should be separated from socket level
-- options.
--
-- | Specify the socket protocol details.
data SockSpec = SockSpec
    {
      SockSpec -> Family
sockFamily :: !Family
    , SockSpec -> SocketType
sockType   :: !SocketType
    , SockSpec -> CInt
sockProto  :: !ProtocolNumber
    , SockSpec -> [(SocketOption, Int)]
sockOpts   :: ![(SocketOption, Int)]
    }

initListener :: Int -> SockSpec -> SockAddr -> IO Socket
initListener :: Int -> SockSpec -> SockAddr -> IO Socket
initListener Int
listenQLen SockSpec{[(SocketOption, Int)]
CInt
SocketType
Family
sockOpts :: [(SocketOption, Int)]
sockProto :: CInt
sockType :: SocketType
sockFamily :: Family
sockOpts :: SockSpec -> [(SocketOption, Int)]
sockProto :: SockSpec -> CInt
sockType :: SockSpec -> SocketType
sockFamily :: SockSpec -> Family
..} SockAddr
addr =
  forall a. IO a -> IO a
withSocketsDo forall a b. (a -> b) -> a -> b
$ do
    Socket
sock <- Family -> SocketType -> CInt -> IO Socket
socket Family
sockFamily SocketType
sockType CInt
sockProto
    forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (\(SocketOption
opt, Int
val) -> Socket -> SocketOption -> Int -> IO ()
setSocketOption Socket
sock SocketOption
opt Int
val) [(SocketOption, Int)]
sockOpts
    Socket -> SockAddr -> IO ()
bind Socket
sock SockAddr
addr
    Socket -> Int -> IO ()
Net.listen Socket
sock Int
listenQLen
    forall (m :: * -> *) a. Monad m => a -> m a
return Socket
sock

{-# INLINE listenTuples #-}
listenTuples :: MonadIO m
    => Unfold m (Int, SockSpec, SockAddr) (Socket, SockAddr)
listenTuples :: forall (m :: * -> *).
MonadIO m =>
Unfold m (Int, SockSpec, SockAddr) (Socket, SockAddr)
listenTuples = forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold forall {m :: * -> *}.
MonadIO m =>
Socket -> m (Step Socket (Socket, SockAddr))
step forall {m :: * -> *}.
MonadIO m =>
(Int, SockSpec, SockAddr) -> m Socket
inject
    where
    inject :: (Int, SockSpec, SockAddr) -> m Socket
inject (Int
listenQLen, SockSpec
spec, SockAddr
addr) = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ Int -> SockSpec -> SockAddr -> IO Socket
initListener Int
listenQLen SockSpec
spec SockAddr
addr

    step :: Socket -> m (Step Socket (Socket, SockAddr))
step Socket
listener = do
        (Socket, SockAddr)
r <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ Socket -> IO (Socket, SockAddr)
Net.accept Socket
listener
        -- XXX error handling
        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
D.Yield (Socket, SockAddr)
r Socket
listener

-- | Unfold a three tuple @(listenQLen, spec, addr)@ into a stream of connected
-- protocol sockets corresponding to incoming connections. @listenQLen@ is the
-- maximum number of pending connections in the backlog. @spec@ is the socket
-- protocol and options specification and @addr@ is the protocol address where
-- the server listens for incoming connections.
--
-- @since 0.7.0
{-# INLINE accept #-}
accept :: MonadIO m => Unfold m (Int, SockSpec, SockAddr) Socket
accept :: forall (m :: * -> *).
MonadIO m =>
Unfold m (Int, SockSpec, SockAddr) Socket
accept = forall (m :: * -> *) b c a.
Monad m =>
(b -> c) -> Unfold m a b -> Unfold m a c
UF.map forall a b. (a, b) -> a
fst forall (m :: * -> *).
MonadIO m =>
Unfold m (Int, SockSpec, SockAddr) (Socket, SockAddr)
listenTuples

-------------------------------------------------------------------------------
-- Listen (Streams)
-------------------------------------------------------------------------------

{-# INLINE recvConnectionTuplesWith #-}
recvConnectionTuplesWith :: MonadAsync m
    => Int -> SockSpec -> SockAddr -> SerialT m (Socket, SockAddr)
recvConnectionTuplesWith :: forall (m :: * -> *).
MonadAsync m =>
Int -> SockSpec -> SockAddr -> SerialT m (Socket, SockAddr)
recvConnectionTuplesWith Int
tcpListenQ SockSpec
spec SockAddr
addr = forall (t :: (* -> *) -> * -> *) (m :: * -> *) b a.
(IsStream t, MonadAsync m) =>
(b -> m (Maybe (a, b))) -> b -> t m a
S.unfoldrM forall {m :: * -> *}.
MonadIO m =>
Maybe Socket -> m (Maybe ((Socket, SockAddr), Maybe Socket))
step forall a. Maybe a
Nothing
    where
    step :: Maybe Socket -> m (Maybe ((Socket, SockAddr), Maybe Socket))
step Maybe Socket
Nothing = do
        Socket
listener <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ Int -> SockSpec -> SockAddr -> IO Socket
initListener Int
tcpListenQ SockSpec
spec SockAddr
addr
        (Socket, SockAddr)
r <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ Socket -> IO (Socket, SockAddr)
Net.accept Socket
listener
        -- XXX error handling
        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a
Just ((Socket, SockAddr)
r, forall a. a -> Maybe a
Just Socket
listener)

    step (Just Socket
listener) = do
        (Socket, SockAddr)
r <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ Socket -> IO (Socket, SockAddr)
Net.accept Socket
listener
        -- XXX error handling
        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a
Just ((Socket, SockAddr)
r, forall a. a -> Maybe a
Just Socket
listener)

-- | Start a TCP stream server that listens for connections on the supplied
-- server address specification (address family, local interface IP address and
-- port). The server generates a stream of connected sockets.  The first
-- argument is the maximum number of pending connections in the backlog.
--
-- /Internal/
{-# INLINE connections #-}
connections :: MonadAsync m => Int -> SockSpec -> SockAddr -> SerialT m Socket
connections :: forall (m :: * -> *).
MonadAsync m =>
Int -> SockSpec -> SockAddr -> SerialT m Socket
connections Int
tcpListenQ SockSpec
spec SockAddr
addr = forall a b. (a, b) -> a
fst forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *).
MonadAsync m =>
Int -> SockSpec -> SockAddr -> SerialT m (Socket, SockAddr)
recvConnectionTuplesWith Int
tcpListenQ SockSpec
spec SockAddr
addr

-------------------------------------------------------------------------------
-- Array IO (Input)
-------------------------------------------------------------------------------

{-# INLINABLE readArrayUptoWith #-}
readArrayUptoWith
    :: (h -> Ptr Word8 -> Int -> IO Int)
    -> Int
    -> h
    -> IO (Array Word8)
readArrayUptoWith :: forall h.
(h -> Ptr Word8 -> Int -> IO Int) -> Int -> h -> IO (Array Word8)
readArrayUptoWith h -> Ptr Word8 -> Int -> IO Int
f Int
size h
h = do
    ForeignPtr Word8
ptr <- forall a. Int -> IO (ForeignPtr a)
mallocPlainForeignPtrBytes Int
size
    -- ptr <- mallocPlainForeignPtrAlignedBytes size (alignment (undefined :: Word8))
    forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr Word8
ptr forall a b. (a -> b) -> a -> b
$ \Ptr Word8
p -> do
        Int
n <- h -> Ptr Word8 -> Int -> IO Int
f h
h Ptr Word8
p Int
size
        let v :: Array Word8
v = Array
                { aStart :: ForeignPtr Word8
aStart = ForeignPtr Word8
ptr
                , aEnd :: Ptr Word8
aEnd   = Ptr Word8
p forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
n
                , aBound :: Ptr Word8
aBound = Ptr Word8
p forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
size
                }
        -- XXX shrink only if the diff is significant
        -- A.shrinkToFit v
        forall (m :: * -> *) a. Monad m => a -> m a
return Array Word8
v

-- | Read a 'ByteArray' from a file handle. If no data is available on the
-- handle it blocks until some data becomes available. If data is available
-- then it immediately returns that data without blocking. It reads a maximum
-- of up to the size requested.
{-# INLINABLE readArrayOf #-}
readArrayOf :: Int -> Socket -> IO (Array Word8)
readArrayOf :: Int -> Socket -> IO (Array Word8)
readArrayOf = forall h.
(h -> Ptr Word8 -> Int -> IO Int) -> Int -> h -> IO (Array Word8)
readArrayUptoWith Socket -> Ptr Word8 -> Int -> IO Int
recvBuf

-------------------------------------------------------------------------------
-- Array IO (output)
-------------------------------------------------------------------------------

waitWhen0 :: Int -> Socket -> IO ()
waitWhen0 :: Int -> Socket -> IO ()
waitWhen0 Int
0 Socket
s = forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
rtsSupportsBoundThreads forall a b. (a -> b) -> a -> b
$
#if MIN_VERSION_network(3,1,0)
    forall r. Socket -> (CInt -> IO r) -> IO r
withFdSocket Socket
s forall a b. (a -> b) -> a -> b
$ \CInt
fd -> Fd -> IO ()
threadWaitWrite forall a b. (a -> b) -> a -> b
$ forall a b. (Integral a, Num b) => a -> b
fromIntegral CInt
fd
#elif MIN_VERSION_network(3,0,0)
    fdSocket s >>= threadWaitWrite . fromIntegral
#else
    let fd = fdSocket s in threadWaitWrite $ fromIntegral fd
#endif
waitWhen0 Int
_ Socket
_ = forall (m :: * -> *) a. Monad m => a -> m a
return ()

sendAll :: Socket -> Ptr Word8 -> Int -> IO ()
sendAll :: Socket -> Ptr Word8 -> Int -> IO ()
sendAll Socket
_ Ptr Word8
_ Int
len | Int
len forall a. Ord a => a -> a -> Bool
<= Int
0 = forall (m :: * -> *) a. Monad m => a -> m a
return ()
sendAll Socket
s Ptr Word8
p Int
len = do
    Int
sent <- Socket -> Ptr Word8 -> Int -> IO Int
sendBuf Socket
s Ptr Word8
p Int
len
    Int -> Socket -> IO ()
waitWhen0 Int
sent Socket
s
    -- assert (sent <= len)
    forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
sent forall a. Ord a => a -> a -> Bool
>= Int
0) forall a b. (a -> b) -> a -> b
$ Socket -> Ptr Word8 -> Int -> IO ()
sendAll Socket
s (Ptr Word8
p forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
sent) (Int
len forall a. Num a => a -> a -> a
- Int
sent)

{-# INLINABLE writeArrayWith #-}
writeArrayWith :: Storable a
    => (h -> Ptr Word8 -> Int -> IO ())
    -> h
    -> Array a
    -> IO ()
writeArrayWith :: forall a h.
Storable a =>
(h -> Ptr Word8 -> Int -> IO ()) -> h -> Array a -> IO ()
writeArrayWith h -> Ptr Word8 -> Int -> IO ()
_ h
_ Array a
arr | forall a. Storable a => Array a -> Int
A.length Array a
arr forall a. Eq a => a -> a -> Bool
== Int
0 = forall (m :: * -> *) a. Monad m => a -> m a
return ()
writeArrayWith h -> Ptr Word8 -> Int -> IO ()
f h
h Array{Ptr a
ForeignPtr a
aBound :: Ptr a
aEnd :: Ptr a
aStart :: ForeignPtr a
aBound :: forall a. Array a -> Ptr a
aEnd :: forall a. Array a -> Ptr a
aStart :: forall a. Array a -> ForeignPtr a
..} = forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr a
aStart forall a b. (a -> b) -> a -> b
$ \Ptr a
p ->
    h -> Ptr Word8 -> Int -> IO ()
f h
h (forall a b. Ptr a -> Ptr b
castPtr Ptr a
p) Int
aLen
    where
    aLen :: Int
aLen =
        let p :: Ptr a
p = forall a. ForeignPtr a -> Ptr a
unsafeForeignPtrToPtr ForeignPtr a
aStart
        in Ptr a
aEnd forall a b. Ptr a -> Ptr b -> Int
`minusPtr` Ptr a
p

-- | Write an Array to a file handle.
--
-- @since 0.7.0
{-# INLINABLE writeChunk #-}
writeChunk :: Storable a => Socket -> Array a -> IO ()
writeChunk :: forall a. Storable a => Socket -> Array a -> IO ()
writeChunk = forall a h.
Storable a =>
(h -> Ptr Word8 -> Int -> IO ()) -> h -> Array a -> IO ()
writeArrayWith Socket -> Ptr Word8 -> Int -> IO ()
sendAll

-------------------------------------------------------------------------------
-- Stream of Arrays IO
-------------------------------------------------------------------------------

{-# INLINABLE _readChunksUptoWith #-}
_readChunksUptoWith :: (IsStream t, MonadIO m)
    => (Int -> h -> IO (Array Word8))
    -> Int -> h -> t m (Array Word8)
_readChunksUptoWith :: forall (t :: (* -> *) -> * -> *) (m :: * -> *) h.
(IsStream t, MonadIO m) =>
(Int -> h -> IO (Array Word8)) -> Int -> h -> t m (Array Word8)
_readChunksUptoWith Int -> h -> IO (Array Word8)
f Int
size h
h = t m (Array Word8)
go
  where
    -- XXX use cons/nil instead
    go :: t m (Array Word8)
go = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
IsStream t =>
(forall r.
 State Stream m a
 -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
-> t m a
mkStream forall a b. (a -> b) -> a -> b
$ \State Stream m (Array Word8)
_ Array Word8 -> t m (Array Word8) -> m r
yld Array Word8 -> m r
_ m r
stp -> do
        Array Word8
arr <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ Int -> h -> IO (Array Word8)
f Int
size h
h
        if forall a. Storable a => Array a -> Int
A.length Array Word8
arr forall a. Eq a => a -> a -> Bool
== Int
0
        then m r
stp
        else Array Word8 -> t m (Array Word8) -> m r
yld Array Word8
arr t m (Array Word8)
go

-- | @toChunksWithBufferOf size h@ reads a stream of arrays from file handle @h@.
-- The maximum size of a single array is limited to @size@.
-- 'fromHandleArraysUpto' ignores the prevailing 'TextEncoding' and 'NewlineMode'
-- on the 'Handle'.
{-# INLINE_NORMAL toChunksWithBufferOf #-}
toChunksWithBufferOf :: (IsStream t, MonadIO m)
    => Int -> Socket -> t m (Array Word8)
-- toChunksWithBufferOf = _readChunksUptoWith readArrayOf
toChunksWithBufferOf :: forall (t :: (* -> *) -> * -> *) (m :: * -> *).
(IsStream t, MonadIO m) =>
Int -> Socket -> t m (Array Word8)
toChunksWithBufferOf Int
size Socket
h = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
Stream m a -> t m a
D.fromStreamD (forall (m :: * -> *) a s.
(State Stream m a -> s -> m (Step s a)) -> s -> Stream m a
D.Stream forall {m :: * -> *} {p} {p}.
MonadIO m =>
p -> p -> m (Step () (Array Word8))
step ())
    where
    {-# INLINE_LATE step #-}
    step :: p -> p -> m (Step () (Array Word8))
step p
_ p
_ = do
        Array Word8
arr <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ Int -> Socket -> IO (Array Word8)
readArrayOf Int
size Socket
h
        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
            case forall a. Storable a => Array a -> Int
A.length Array Word8
arr of
                Int
0 -> forall s a. Step s a
D.Stop
                Int
_ -> forall s a. a -> s -> Step s a
D.Yield Array Word8
arr ()

-- XXX read 'Array a' instead of Word8
--
-- | @toChunks h@ reads a stream of arrays from socket handle @h@.
-- The maximum size of a single array is limited to @defaultChunkSize@.
--
-- @since 0.7.0
{-# INLINE toChunks #-}
toChunks :: (IsStream t, MonadIO m) => Socket -> t m (Array Word8)
toChunks :: forall (t :: (* -> *) -> * -> *) (m :: * -> *).
(IsStream t, MonadIO m) =>
Socket -> t m (Array Word8)
toChunks = forall (t :: (* -> *) -> * -> *) (m :: * -> *).
(IsStream t, MonadIO m) =>
Int -> Socket -> t m (Array Word8)
toChunksWithBufferOf Int
A.defaultChunkSize

-- | Unfold the tuple @(bufsize, socket)@ into a stream of 'Word8' arrays.
-- Read requests to the socket are performed using a buffer of size @bufsize@.
-- The size of an array in the resulting stream is always less than or equal to
-- @bufsize@.
--
-- @since 0.7.0
{-# INLINE_NORMAL readChunksWithBufferOf #-}
readChunksWithBufferOf :: MonadIO m => Unfold m (Int, Socket) (Array Word8)
readChunksWithBufferOf :: forall (m :: * -> *).
MonadIO m =>
Unfold m (Int, Socket) (Array Word8)
readChunksWithBufferOf = forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold forall {m :: * -> *}.
MonadIO m =>
(Int, Socket) -> m (Step (Int, Socket) (Array Word8))
step forall (m :: * -> *) a. Monad m => a -> m a
return
    where
    {-# INLINE_LATE step #-}
    step :: (Int, Socket) -> m (Step (Int, Socket) (Array Word8))
step (Int
size, Socket
h) = do
        Array Word8
arr <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ Int -> Socket -> IO (Array Word8)
readArrayOf Int
size Socket
h
        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
            case forall a. Storable a => Array a -> Int
A.length Array Word8
arr of
                Int
0 -> forall s a. Step s a
D.Stop
                Int
_ -> forall s a. a -> s -> Step s a
D.Yield Array Word8
arr (Int
size, Socket
h)

-- | Unfolds a socket into a stream of 'Word8' arrays. Requests to the socket
-- are performed using a buffer of size
-- 'Streamly.Internal.Memory.Array.Types.defaultChunkSize'. The
-- size of arrays in the resulting stream are therefore less than or equal to
-- 'Streamly.Internal.Memory.Array.Types.defaultChunkSize'.
--
-- @since 0.7.0
{-# INLINE readChunks #-}
readChunks :: MonadIO m => Unfold m Socket (Array Word8)
readChunks :: forall (m :: * -> *). MonadIO m => Unfold m Socket (Array Word8)
readChunks = forall (m :: * -> *) a b c. Unfold m (a, b) c -> a -> Unfold m b c
UF.supplyFirst forall (m :: * -> *).
MonadIO m =>
Unfold m (Int, Socket) (Array Word8)
readChunksWithBufferOf Int
A.defaultChunkSize

-------------------------------------------------------------------------------
-- Read File to Stream
-------------------------------------------------------------------------------

-- TODO for concurrent streams implement readahead IO. We can send multiple
-- read requests at the same time. For serial case we can use async IO. We can
-- also control the read throughput in mbps or IOPS.

{-
-- | @readWithBufferOf bufsize handle@ reads a byte stream from a file
-- handle, reads are performed in chunks of up to @bufsize@.  The stream ends
-- as soon as EOF is encountered.
--
{-# INLINE readWithBufferOf #-}
readWithBufferOf :: (IsStream t, MonadIO m) => Int -> Handle -> t m Word8
readWithBufferOf chunkSize h = A.flattenArrays $ readChunksUpto chunkSize h
-}

-- TODO
-- read :: (IsStream t, MonadIO m, Storable a) => Handle -> t m a
--
-- > read = 'readByChunks' A.defaultChunkSize
-- | Generate a stream of elements of the given type from a socket. The
-- stream ends when EOF is encountered.
--
-- @since 0.7.0
{-# INLINE toBytes #-}
toBytes :: (IsStream t, MonadIO m) => Socket -> t m Word8
toBytes :: forall (t :: (* -> *) -> * -> *) (m :: * -> *).
(IsStream t, MonadIO m) =>
Socket -> t m Word8
toBytes = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, MonadIO m, Storable a) =>
t m (Array a) -> t m a
AS.concat forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: (* -> *) -> * -> *) (m :: * -> *).
(IsStream t, MonadIO m) =>
Socket -> t m (Array Word8)
toChunks

-- | Unfolds the tuple @(bufsize, socket)@ into a byte stream, read requests
-- to the socket are performed using buffers of @bufsize@.
--
-- @since 0.7.0
{-# INLINE readWithBufferOf #-}
readWithBufferOf :: MonadIO m => Unfold m (Int, Socket) Word8
readWithBufferOf :: forall (m :: * -> *). MonadIO m => Unfold m (Int, Socket) Word8
readWithBufferOf = forall (m :: * -> *) a b c.
Monad m =>
Unfold m a b -> Unfold m b c -> Unfold m a c
UF.concat forall (m :: * -> *).
MonadIO m =>
Unfold m (Int, Socket) (Array Word8)
readChunksWithBufferOf forall (m :: * -> *) a.
(Monad m, Storable a) =>
Unfold m (Array a) a
A.read

-- | Unfolds a 'Socket' into a byte stream.  IO requests to the socket are
-- performed in sizes of
-- 'Streamly.Internal.Memory.Array.Types.defaultChunkSize'.
--
-- @since 0.7.0
{-# INLINE read #-}
read :: MonadIO m => Unfold m Socket Word8
read :: forall (m :: * -> *). MonadIO m => Unfold m Socket Word8
read = forall (m :: * -> *) a b c. Unfold m (a, b) c -> a -> Unfold m b c
UF.supplyFirst forall (m :: * -> *). MonadIO m => Unfold m (Int, Socket) Word8
readWithBufferOf Int
A.defaultChunkSize

-------------------------------------------------------------------------------
-- Writing
-------------------------------------------------------------------------------

-- | Write a stream of arrays to a handle.
--
-- @since 0.7.0
{-# INLINE fromChunks #-}
fromChunks :: (MonadIO m, Storable a)
    => Socket -> SerialT m (Array a) -> m ()
fromChunks :: forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Socket -> SerialT m (Array a) -> m ()
fromChunks Socket
h = forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> SerialT m a -> m ()
S.mapM_ (forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Storable a => Socket -> Array a -> IO ()
writeChunk Socket
h)

-- | Write a stream of arrays to a socket.  Each array in the stream is written
-- to the socket as a separate IO request.
--
-- @since 0.7.0
{-# INLINE writeChunks #-}
writeChunks :: (MonadIO m, Storable a) => Socket -> Fold m (Array a) ()
writeChunks :: forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Socket -> Fold m (Array a) ()
writeChunks Socket
h = forall (m :: * -> *) a b. Monad m => (a -> m b) -> Fold m a ()
FL.drainBy (forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Storable a => Socket -> Array a -> IO ()
writeChunk Socket
h)

-- | @writeChunksWithBufferOf bufsize socket@ writes a stream of arrays
-- to @socket@ after coalescing the adjacent arrays in chunks of @bufsize@.
-- We never split an array, if a single array is bigger than the specified size
-- it emitted as it is. Multiple arrays are coalesed as long as the total size
-- remains below the specified size.
--
-- @since 0.7.0
{-# INLINE writeChunksWithBufferOf #-}
writeChunksWithBufferOf :: (MonadIO m, Storable a)
    => Int -> Socket -> Fold m (Array a) ()
writeChunksWithBufferOf :: forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> Socket -> Fold m (Array a) ()
writeChunksWithBufferOf Int
n Socket
h = forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> Fold m (Array a) () -> Fold m (Array a) ()
lpackArraysChunksOf Int
n (forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Socket -> Fold m (Array a) ()
writeChunks Socket
h)

-- | Write a stream of strings to a socket in Latin1 encoding.  Output is
-- flushed to the socket for each string.
--
-- /Internal/
--
{-# INLINE writeStrings #-}
writeStrings :: MonadIO m
    => (SerialT m Char -> SerialT m Word8) -> Socket -> Fold m String ()
writeStrings :: forall (m :: * -> *).
MonadIO m =>
(SerialT m Char -> SerialT m Word8) -> Socket -> Fold m String ()
writeStrings SerialT m Char -> SerialT m Word8
encode Socket
h =
    forall (m :: * -> *) a b r.
Monad m =>
(a -> m b) -> Fold m b r -> Fold m a r
FL.lmapM (forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
SerialT m a -> m (Array a)
IA.fromStream forall b c a. (b -> c) -> (a -> b) -> a -> c
. SerialT m Char -> SerialT m Word8
encode forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) (t :: (* -> *) -> * -> *) a.
(Monad m, IsStream t) =>
[a] -> t m a
S.fromList) (forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Socket -> Fold m (Array a) ()
writeChunks Socket
h)

-- GHC buffer size dEFAULT_FD_BUFFER_SIZE=8192 bytes.
--
-- XXX test this
-- Note that if you use a chunk size less than 8K (GHC's default buffer
-- size) then you are advised to use 'NOBuffering' mode on the 'Handle' in case you
-- do not want buffering to occur at GHC level as well. Same thing applies to
-- writes as well.

-- | Like 'write' but provides control over the write buffer. Output will
-- be written to the IO device as soon as we collect the specified number of
-- input elements.
--
-- @since 0.7.0
{-# INLINE fromBytesWithBufferOf #-}
fromBytesWithBufferOf :: MonadIO m => Int -> Socket -> SerialT m Word8 -> m ()
fromBytesWithBufferOf :: forall (m :: * -> *).
MonadIO m =>
Int -> Socket -> SerialT m Word8 -> m ()
fromBytesWithBufferOf Int
n Socket
h SerialT m Word8
m = forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Socket -> SerialT m (Array a) -> m ()
fromChunks Socket
h forall a b. (a -> b) -> a -> b
$ forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, MonadIO m, Storable a) =>
Int -> t m a -> t m (Array a)
AS.arraysOf Int
n SerialT m Word8
m

-- | Write a byte stream to a socket. Accumulates the input in chunks of
-- specified number of bytes before writing.
--
-- @since 0.7.0
{-# INLINE writeWithBufferOf #-}
writeWithBufferOf :: MonadIO m => Int -> Socket -> Fold m Word8 ()
writeWithBufferOf :: forall (m :: * -> *). MonadIO m => Int -> Socket -> Fold m Word8 ()
writeWithBufferOf Int
n Socket
h = forall (m :: * -> *) a b c.
Monad m =>
Int -> Fold m a b -> Fold m b c -> Fold m a c
FL.lchunksOf Int
n (forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> Fold m a (Array a)
A.writeNUnsafe Int
n) (forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Socket -> Fold m (Array a) ()
writeChunks Socket
h)

-- > write = 'writeWithBufferOf' A.defaultChunkSize
--
-- | Write a byte stream to a file handle. Combines the bytes in chunks of size
-- up to 'A.defaultChunkSize' before writing.  Note that the write behavior
-- depends on the 'IOMode' and the current seek position of the handle.
--
-- @since 0.7.0
{-# INLINE fromBytes #-}
fromBytes :: MonadIO m => Socket -> SerialT m Word8 -> m ()
fromBytes :: forall (m :: * -> *).
MonadIO m =>
Socket -> SerialT m Word8 -> m ()
fromBytes = forall (m :: * -> *).
MonadIO m =>
Int -> Socket -> SerialT m Word8 -> m ()
fromBytesWithBufferOf Int
A.defaultChunkSize

-- | Write a byte stream to a socket. Accumulates the input in chunks of
-- up to 'A.defaultChunkSize' bytes before writing.
--
-- @
-- write = 'writeWithBufferOf' 'A.defaultChunkSize'
-- @
--
-- @since 0.7.0
{-# INLINE write #-}
write :: MonadIO m => Socket -> Fold m Word8 ()
write :: forall (m :: * -> *). MonadIO m => Socket -> Fold m Word8 ()
write = forall (m :: * -> *). MonadIO m => Int -> Socket -> Fold m Word8 ()
writeWithBufferOf Int
A.defaultChunkSize

{-
{-# INLINE write #-}
write :: (MonadIO m, Storable a) => Handle -> SerialT m a -> m ()
write = toHandleWith A.defaultChunkSize
-}

-------------------------------------------------------------------------------
-- IO with encoding/decoding Unicode characters
-------------------------------------------------------------------------------

{-
-- |
-- > readUtf8 = decodeUtf8 . read
--
-- Read a UTF8 encoded stream of unicode characters from a file handle.
--
-- @since 0.7.0
{-# INLINE readUtf8 #-}
readUtf8 :: (IsStream t, MonadIO m) => Handle -> t m Char
readUtf8 = decodeUtf8 . read

-- |
-- > writeUtf8 h s = write h $ encodeUtf8 s
--
-- Encode a stream of unicode characters to UTF8 and write it to the given file
-- handle. Default block buffering applies to the writes.
--
-- @since 0.7.0
{-# INLINE writeUtf8 #-}
writeUtf8 :: MonadIO m => Handle -> SerialT m Char -> m ()
writeUtf8 h s = write h $ encodeUtf8 s

-- | Write a stream of unicode characters after encoding to UTF-8 in chunks
-- separated by a linefeed character @'\n'@. If the size of the buffer exceeds
-- @defaultChunkSize@ and a linefeed is not yet found, the buffer is written
-- anyway.  This is similar to writing to a 'Handle' with the 'LineBuffering'
-- option.
--
-- @since 0.7.0
{-# INLINE writeUtf8ByLines #-}
writeUtf8ByLines :: (IsStream t, MonadIO m) => Handle -> t m Char -> m ()
writeUtf8ByLines = undefined

-- | Read UTF-8 lines from a file handle and apply the specified fold to each
-- line. This is similar to reading a 'Handle' with the 'LineBuffering' option.
--
-- @since 0.7.0
{-# INLINE readLines #-}
readLines :: (IsStream t, MonadIO m) => Handle -> Fold m Char b -> t m b
readLines h f = foldLines (readUtf8 h) f

-------------------------------------------------------------------------------
-- Framing on a sequence
-------------------------------------------------------------------------------

-- | Read a stream from a file handle and split it into frames delimited by
-- the specified sequence of elements. The supplied fold is applied on each
-- frame.
--
-- @since 0.7.0
{-# INLINE readFrames #-}
readFrames :: (IsStream t, MonadIO m, Storable a)
    => Array a -> Handle -> Fold m a b -> t m b
readFrames = undefined -- foldFrames . read

-- | Write a stream to the given file handle buffering up to frames separated
-- by the given sequence or up to a maximum of @defaultChunkSize@.
--
-- @since 0.7.0
{-# INLINE writeByFrames #-}
writeByFrames :: (IsStream t, MonadIO m, Storable a)
    => Array a -> Handle -> t m a -> m ()
writeByFrames = undefined
-}