#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
    , forSocketM
    , withSocket

    -- * Accept connections
    , accept
    , acceptor

    -- * Connect
    , connect
    , connectFrom

    -- * Read from connection
    , getChunk

    -- ** Streams
    , read
    , readWith
    , readChunks
    , readChunksWith

    -- ** Unfolds
    , reader
    , readerWith
    , chunkReader
    , chunkReaderWith

    -- * Write to connection
    , putChunk

    -- ** Folds
    , write
    , writeWith
    , writeChunks
    , writeChunksWith
    , writeMaybesWith

    -- ** Stream writes
    , putChunks
    , putBytesWith
    , putBytes

    -- reading/writing datagrams

    -- * Deprecated
    , readWithBufferOf
    , readChunksWithBufferOf
    , writeWithBufferOf
    , writeChunksWithBufferOf
    )
where

import Control.Concurrent (threadWaitWrite, rtsSupportsBoundThreads)
import Control.Exception (onException)
import Control.Monad (forM_, when)
import Control.Monad.Catch (MonadCatch, finally, MonadMask)
import Control.Monad.IO.Class (MonadIO(..))
import Data.Maybe (isNothing, fromJust)
import Data.Word (Word8)
import Foreign.Ptr (plusPtr, Ptr, castPtr)
import Streamly.Data.MutByteArray (Unbox)
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.Internal.Data.Array (Array(..))
import Streamly.Data.Fold (Fold)
import Streamly.Data.Stream (Stream)
import Streamly.Internal.Data.Unfold (Unfold(..))
-- import Streamly.String (encodeUtf8, decodeUtf8, foldLines)
import Streamly.Internal.System.IO (defaultChunkSize)

import qualified Streamly.Data.Array as A
import qualified Streamly.Data.Fold as FL
import qualified Streamly.Data.Stream as S
import qualified Streamly.Data.Unfold as UF
import qualified Streamly.Internal.Data.Array as A
    ( unsafeFreeze, unsafePinnedAsPtr, byteLength, pinnedChunksOf,
      pinnedCreateOf, unsafePinnedCreateOf, lCompactGE )
import qualified Streamly.Internal.Data.MutArray as MArray
    (MutArray(..), unsafePinnedAsPtr, pinnedEmptyOf)
import qualified Streamly.Internal.Data.Stream as S (fromStreamK, Stream(..), Step(..))
import qualified Streamly.Internal.Data.StreamK as K (mkStream)

-- $setup
-- >>> :m
-- >>> import Streamly.Internal.System.IO (defaultChunkSize)
-- >>> import qualified Streamly.Internal.Network.Socket as Socket

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

-- | Like 'forSocketM' but runs a streaming computation instead of a monadic
-- computation.
--
-- /Inhibits stream fusion/
--
-- /Internal/
{-# INLINE withSocket #-}
withSocket :: (MonadIO m, MonadCatch m) =>
    Socket -> (Socket -> Stream m a) -> Stream m a
withSocket :: forall (m :: * -> *) a.
(MonadIO m, MonadCatch m) =>
Socket -> (Socket -> Stream m a) -> Stream m a
withSocket Socket
sk Socket -> Stream m a
f = IO () -> Stream m a -> Stream m a
forall (m :: * -> *) b a.
(MonadIO m, MonadCatch m) =>
IO b -> Stream m a -> Stream m a
S.finallyIO (Socket -> IO ()
Net.close Socket
sk) (Socket -> Stream m a
f Socket
sk)

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

-- XXX Protocol specific socket options should be separated from socket level
-- options.
--
-- NOTE: the socket config is specified as a record and not by composing
-- functions because all the fields are mandatory except the sockOpts field.

-- | 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
Family
SocketType
sockFamily :: SockSpec -> Family
sockType :: SockSpec -> SocketType
sockProto :: SockSpec -> CInt
sockOpts :: SockSpec -> [(SocketOption, Int)]
sockFamily :: Family
sockType :: SocketType
sockProto :: CInt
sockOpts :: [(SocketOption, Int)]
..} SockAddr
addr =
  IO Socket -> IO Socket
forall a. IO a -> IO a
withSocketsDo (IO Socket -> IO Socket) -> IO Socket -> IO Socket
forall a b. (a -> b) -> a -> b
$ do
    Socket
sock <- Family -> SocketType -> CInt -> IO Socket
socket Family
sockFamily SocketType
sockType CInt
sockProto
    Socket -> IO ()
use Socket
sock IO () -> IO () -> IO ()
forall a b. IO a -> IO b -> IO a
`onException` Socket -> IO ()
Net.close Socket
sock
    Socket -> IO Socket
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Socket
sock

    where

    use :: Socket -> IO ()
use Socket
sock = do
        ((SocketOption, Int) -> IO ()) -> [(SocketOption, Int)] -> IO ()
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

{-# 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 = (Socket -> m (Step Socket (Socket, SockAddr)))
-> ((Int, SockSpec, SockAddr) -> m Socket)
-> Unfold m (Int, SockSpec, SockAddr) (Socket, SockAddr)
forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold Socket -> m (Step Socket (Socket, SockAddr))
forall {m :: * -> *}.
MonadIO m =>
Socket -> m (Step Socket (Socket, SockAddr))
step (Int, SockSpec, SockAddr) -> m Socket
forall {m :: * -> *}.
MonadIO m =>
(Int, SockSpec, SockAddr) -> m Socket
inject
    where
    inject :: (Int, SockSpec, SockAddr) -> m Socket
inject (Int
listenQLen, SockSpec
spec, SockAddr
addr) =
        IO Socket -> m Socket
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Socket -> m Socket) -> IO Socket -> m Socket
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 <- IO (Socket, SockAddr) -> m (Socket, SockAddr)
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Socket -> IO (Socket, SockAddr)
Net.accept Socket
listener IO (Socket, SockAddr) -> IO () -> IO (Socket, SockAddr)
forall a b. IO a -> IO b -> IO a
`onException` Socket -> IO ()
Net.close Socket
listener)
        Step Socket (Socket, SockAddr)
-> m (Step Socket (Socket, SockAddr))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step Socket (Socket, SockAddr)
 -> m (Step Socket (Socket, SockAddr)))
-> Step Socket (Socket, SockAddr)
-> m (Step Socket (Socket, SockAddr))
forall a b. (a -> b) -> a -> b
$ (Socket, SockAddr) -> Socket -> Step Socket (Socket, SockAddr)
forall s a. a -> s -> Step s a
S.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.
--
{-# INLINE acceptor #-}
acceptor :: MonadIO m => Unfold m (Int, SockSpec, SockAddr) Socket
acceptor :: forall (m :: * -> *).
MonadIO m =>
Unfold m (Int, SockSpec, SockAddr) Socket
acceptor = ((Socket, SockAddr) -> Socket)
-> Unfold m (Int, SockSpec, SockAddr) (Socket, SockAddr)
-> Unfold m (Int, SockSpec, SockAddr) Socket
forall a b.
(a -> b)
-> Unfold m (Int, SockSpec, SockAddr) a
-> Unfold m (Int, SockSpec, SockAddr) b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Socket, SockAddr) -> Socket
forall a b. (a, b) -> a
fst Unfold m (Int, SockSpec, SockAddr) (Socket, SockAddr)
forall (m :: * -> *).
MonadIO m =>
Unfold m (Int, SockSpec, SockAddr) (Socket, SockAddr)
listenTuples

{-# INLINE connectCommon #-}
connectCommon :: SockSpec -> Maybe SockAddr -> SockAddr -> IO Socket
connectCommon :: SockSpec -> Maybe SockAddr -> SockAddr -> IO Socket
connectCommon SockSpec{[(SocketOption, Int)]
CInt
Family
SocketType
sockFamily :: SockSpec -> Family
sockType :: SockSpec -> SocketType
sockProto :: SockSpec -> CInt
sockOpts :: SockSpec -> [(SocketOption, Int)]
sockFamily :: Family
sockType :: SocketType
sockProto :: CInt
sockOpts :: [(SocketOption, Int)]
..} Maybe SockAddr
local SockAddr
remote = IO Socket -> IO Socket
forall a. IO a -> IO a
withSocketsDo (IO Socket -> IO Socket) -> IO Socket -> IO Socket
forall a b. (a -> b) -> a -> b
$ do
    Socket
sock <- Family -> SocketType -> CInt -> IO Socket
socket Family
sockFamily SocketType
sockType CInt
sockProto
    Socket -> IO ()
use Socket
sock IO () -> IO () -> IO ()
forall a b. IO a -> IO b -> IO a
`onException` Socket -> IO ()
Net.close Socket
sock
    Socket -> IO Socket
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Socket
sock

    where

    use :: Socket -> IO ()
use Socket
sock = do
        ((SocketOption, Int) -> IO ()) -> [(SocketOption, Int)] -> IO ()
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
        Maybe SockAddr -> (SockAddr -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ Maybe SockAddr
local (Socket -> SockAddr -> IO ()
bind Socket
sock)
        Socket -> SockAddr -> IO ()
Net.connect Socket
sock SockAddr
remote

-- | Connect to a remote host using the given socket specification and remote
-- address. Returns a connected socket or throws an exception.
--
-- /Pre-release/
--
{-# INLINE connect #-}
connect :: SockSpec -> SockAddr -> IO Socket
connect :: SockSpec -> SockAddr -> IO Socket
connect SockSpec
spec = SockSpec -> Maybe SockAddr -> SockAddr -> IO Socket
connectCommon SockSpec
spec Maybe SockAddr
forall a. Maybe a
Nothing

-- | Connect to a remote host using the given socket specification, a local
-- address to bind to and a remote address to connect to. Returns a connected
-- socket or throws an exception.
--
-- /Pre-release/
--
{-# INLINE connectFrom #-}
connectFrom :: SockSpec -> SockAddr -> SockAddr -> IO Socket
connectFrom :: SockSpec -> SockAddr -> SockAddr -> IO Socket
connectFrom SockSpec
spec SockAddr
local = SockSpec -> Maybe SockAddr -> SockAddr -> IO Socket
connectCommon SockSpec
spec (SockAddr -> Maybe SockAddr
forall a. a -> Maybe a
Just SockAddr
local)

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

{-# INLINE recvConnectionTuplesWith #-}
recvConnectionTuplesWith :: MonadIO m
    => Int -> SockSpec -> SockAddr -> Stream m (Socket, SockAddr)
recvConnectionTuplesWith :: forall (m :: * -> *).
MonadIO m =>
Int -> SockSpec -> SockAddr -> Stream m (Socket, SockAddr)
recvConnectionTuplesWith Int
tcpListenQ SockSpec
spec SockAddr
addr = (Maybe Socket -> m (Maybe ((Socket, SockAddr), Maybe Socket)))
-> Maybe Socket -> Stream m (Socket, SockAddr)
forall (m :: * -> *) s a.
Monad m =>
(s -> m (Maybe (a, s))) -> s -> Stream m a
S.unfoldrM Maybe Socket -> m (Maybe ((Socket, SockAddr), Maybe Socket))
forall {m :: * -> *}.
MonadIO m =>
Maybe Socket -> m (Maybe ((Socket, SockAddr), Maybe Socket))
step Maybe Socket
forall a. Maybe a
Nothing
    where
    step :: Maybe Socket -> m (Maybe ((Socket, SockAddr), Maybe Socket))
step Maybe Socket
Nothing = do
        Socket
listener <- IO Socket -> m Socket
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Socket -> m Socket) -> IO Socket -> m Socket
forall a b. (a -> b) -> a -> b
$ Int -> SockSpec -> SockAddr -> IO Socket
initListener Int
tcpListenQ SockSpec
spec SockAddr
addr
        (Socket, SockAddr)
r <- IO (Socket, SockAddr) -> m (Socket, SockAddr)
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Socket -> IO (Socket, SockAddr)
Net.accept Socket
listener IO (Socket, SockAddr) -> IO () -> IO (Socket, SockAddr)
forall a b. IO a -> IO b -> IO a
`onException` Socket -> IO ()
Net.close Socket
listener)
        Maybe ((Socket, SockAddr), Maybe Socket)
-> m (Maybe ((Socket, SockAddr), Maybe Socket))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe ((Socket, SockAddr), Maybe Socket)
 -> m (Maybe ((Socket, SockAddr), Maybe Socket)))
-> Maybe ((Socket, SockAddr), Maybe Socket)
-> m (Maybe ((Socket, SockAddr), Maybe Socket))
forall a b. (a -> b) -> a -> b
$ ((Socket, SockAddr), Maybe Socket)
-> Maybe ((Socket, SockAddr), Maybe Socket)
forall a. a -> Maybe a
Just ((Socket, SockAddr)
r, Socket -> Maybe Socket
forall a. a -> Maybe a
Just Socket
listener)

    step (Just Socket
listener) = do
        (Socket, SockAddr)
r <- IO (Socket, SockAddr) -> m (Socket, SockAddr)
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Socket -> IO (Socket, SockAddr)
Net.accept Socket
listener IO (Socket, SockAddr) -> IO () -> IO (Socket, SockAddr)
forall a b. IO a -> IO b -> IO a
`onException` Socket -> IO ()
Net.close Socket
listener)
        Maybe ((Socket, SockAddr), Maybe Socket)
-> m (Maybe ((Socket, SockAddr), Maybe Socket))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe ((Socket, SockAddr), Maybe Socket)
 -> m (Maybe ((Socket, SockAddr), Maybe Socket)))
-> Maybe ((Socket, SockAddr), Maybe Socket)
-> m (Maybe ((Socket, SockAddr), Maybe Socket))
forall a b. (a -> b) -> a -> b
$ ((Socket, SockAddr), Maybe Socket)
-> Maybe ((Socket, SockAddr), Maybe Socket)
forall a. a -> Maybe a
Just ((Socket, SockAddr)
r, Socket -> Maybe Socket
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.
--
-- /Pre-release/
{-# INLINE accept #-}
accept :: MonadIO m => Int -> SockSpec -> SockAddr -> Stream m Socket
accept :: forall (m :: * -> *).
MonadIO m =>
Int -> SockSpec -> SockAddr -> Stream m Socket
accept Int
tcpListenQ SockSpec
spec SockAddr
addr =
    (Socket, SockAddr) -> Socket
forall a b. (a, b) -> a
fst ((Socket, SockAddr) -> Socket)
-> Stream m (Socket, SockAddr) -> Stream m Socket
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> SockSpec -> SockAddr -> Stream m (Socket, SockAddr)
forall (m :: * -> *).
MonadIO m =>
Int -> SockSpec -> SockAddr -> Stream m (Socket, SockAddr)
recvConnectionTuplesWith Int
tcpListenQ SockSpec
spec SockAddr
addr

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

-- XXX add an API that compacts the arrays to an exact size.

{-# 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
    MutArray Word8
arr <- Int -> IO (MutArray Word8)
forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> m (MutArray a)
MArray.pinnedEmptyOf Int
size
    -- ptr <- mallocPlainForeignPtrAlignedBytes size (alignment (undefined :: Word8))
    MutArray Word8
-> (Ptr Word8 -> IO (Array Word8)) -> IO (Array Word8)
forall (m :: * -> *) a b.
MonadIO m =>
MutArray a -> (Ptr a -> m b) -> m b
MArray.unsafePinnedAsPtr MutArray Word8
arr ((Ptr Word8 -> IO (Array Word8)) -> IO (Array Word8))
-> (Ptr Word8 -> IO (Array Word8)) -> IO (Array Word8)
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 a
v = MutArray a -> Array a
forall a. MutArray a -> Array a
A.unsafeFreeze
                (MutArray a -> Array a) -> MutArray a -> Array a
forall a b. (a -> b) -> a -> b
$ MutArray Word8
arr { MArray.arrEnd = n, MArray.arrBound = size }

        -- XXX shrink only if the diff is significant
        -- A.shrinkToFit v
        Array Word8 -> IO (Array Word8)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Array Word8
forall {a}. Array a
v

-- | Read a byte array from a file handle up to a maximum of the requested
-- size. 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.
--
{-# INLINABLE getChunk #-}
getChunk :: Int -> Socket -> IO (Array Word8)
getChunk :: Int -> Socket -> IO (Array Word8)
getChunk = (Socket -> Ptr Word8 -> Int -> IO Int)
-> Int -> Socket -> IO (Array Word8)
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 = Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
rtsSupportsBoundThreads (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$
#if MIN_VERSION_network(3,1,0)
    Socket -> (CInt -> IO ()) -> IO ()
forall r. Socket -> (CInt -> IO r) -> IO r
withFdSocket Socket
s ((CInt -> IO ()) -> IO ()) -> (CInt -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \CInt
fd -> Fd -> IO ()
threadWaitWrite (Fd -> IO ()) -> Fd -> IO ()
forall a b. (a -> b) -> a -> b
$ CInt -> Fd
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
_ = () -> IO ()
forall a. a -> IO a
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 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = () -> IO ()
forall a. a -> IO a
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)
    Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
sent Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ Socket -> Ptr Word8 -> Int -> IO ()
sendAll Socket
s (Ptr Word8
p Ptr Word8 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
sent) (Int
len Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
sent)

{-# INLINABLE writeArrayWith #-}
writeArrayWith :: Unbox a
    => (h -> Ptr Word8 -> Int -> IO ())
    -> h
    -> Array a
    -> IO ()
writeArrayWith :: forall a h.
Unbox a =>
(h -> Ptr Word8 -> Int -> IO ()) -> h -> Array a -> IO ()
writeArrayWith h -> Ptr Word8 -> Int -> IO ()
_ h
_ Array a
arr | Array a -> Int
forall a. Unbox a => Array a -> Int
A.length Array a
arr Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 = () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
writeArrayWith h -> Ptr Word8 -> Int -> IO ()
f h
h Array a
arr = Array a -> (Ptr a -> IO ()) -> IO ()
forall (m :: * -> *) a b.
MonadIO m =>
Array a -> (Ptr a -> m b) -> m b
A.unsafePinnedAsPtr Array a
arr ((Ptr a -> IO ()) -> IO ()) -> (Ptr a -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr a
ptr -> h -> Ptr Word8 -> Int -> IO ()
f h
h (Ptr a -> Ptr Word8
forall a b. Ptr a -> Ptr b
castPtr Ptr a
ptr) Int
aLen

    where

    aLen :: Int
aLen = Array a -> Int
forall a. Array a -> Int
A.byteLength Array a
arr

-- | Write an Array to a socket.
--
{-# INLINABLE putChunk #-}
putChunk :: Unbox a => Socket -> Array a -> IO ()
putChunk :: forall a. Unbox a => Socket -> Array a -> IO ()
putChunk = (Socket -> Ptr Word8 -> Int -> IO ()) -> Socket -> Array a -> IO ()
forall a h.
Unbox a =>
(h -> Ptr Word8 -> Int -> IO ()) -> h -> Array a -> IO ()
writeArrayWith Socket -> Ptr Word8 -> Int -> IO ()
sendAll

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

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

-- | @readChunksWith bufsize socket@ reads a stream of arrays from @socket@.
-- The maximum size of a single array is limited to @bufsize@.
--
-- /Pre-release/
{-# INLINE_NORMAL readChunksWith #-}
readChunksWith :: MonadIO m => Int -> Socket -> Stream m (Array Word8)
-- readChunksWith = _readChunksUptoWith readChunk
readChunksWith :: forall (m :: * -> *).
MonadIO m =>
Int -> Socket -> Stream m (Array Word8)
readChunksWith Int
size Socket
h = (State StreamK m (Array Word8) -> () -> m (Step () (Array Word8)))
-> () -> Stream m (Array Word8)
forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
S.Stream State StreamK m (Array Word8) -> () -> m (Step () (Array Word8))
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 <- IO (Array Word8) -> m (Array Word8)
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Array Word8) -> m (Array Word8))
-> IO (Array Word8) -> m (Array Word8)
forall a b. (a -> b) -> a -> b
$ Int -> Socket -> IO (Array Word8)
getChunk Int
size Socket
h
        Step () (Array Word8) -> m (Step () (Array Word8))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step () (Array Word8) -> m (Step () (Array Word8)))
-> Step () (Array Word8) -> m (Step () (Array Word8))
forall a b. (a -> b) -> a -> b
$
            case Array Word8 -> Int
forall a. Unbox a => Array a -> Int
A.length Array Word8
arr of
                Int
0 -> Step () (Array Word8)
forall s a. Step s a
S.Stop
                Int
_ -> Array Word8 -> () -> Step () (Array Word8)
forall s a. a -> s -> Step s a
S.Yield Array Word8
arr ()

-- | Read a stream of byte arrays from a socket. The maximum size of a single
-- array is limited to @defaultChunkSize@.
--
-- >>> readChunks = Socket.readChunksWith defaultChunkSize
--
-- /Pre-release/
{-# INLINE readChunks #-}
readChunks :: MonadIO m => Socket -> Stream m (Array Word8)
readChunks :: forall (m :: * -> *). MonadIO m => Socket -> Stream m (Array Word8)
readChunks = Int -> Socket -> Stream m (Array Word8)
forall (m :: * -> *).
MonadIO m =>
Int -> Socket -> Stream m (Array Word8)
readChunksWith Int
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@.
--
{-# INLINE_NORMAL chunkReaderWith #-}
chunkReaderWith :: MonadIO m => Unfold m (Int, Socket) (Array Word8)
chunkReaderWith :: forall (m :: * -> *).
MonadIO m =>
Unfold m (Int, Socket) (Array Word8)
chunkReaderWith = ((Int, Socket) -> m (Step (Int, Socket) (Array Word8)))
-> ((Int, Socket) -> m (Int, Socket))
-> Unfold m (Int, Socket) (Array Word8)
forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold (Int, Socket) -> m (Step (Int, Socket) (Array Word8))
forall {m :: * -> *}.
MonadIO m =>
(Int, Socket) -> m (Step (Int, Socket) (Array Word8))
step (Int, Socket) -> m (Int, Socket)
forall a. a -> m a
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 <- IO (Array Word8) -> m (Array Word8)
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Array Word8) -> m (Array Word8))
-> IO (Array Word8) -> m (Array Word8)
forall a b. (a -> b) -> a -> b
$ Int -> Socket -> IO (Array Word8)
getChunk Int
size Socket
h
        Step (Int, Socket) (Array Word8)
-> m (Step (Int, Socket) (Array Word8))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Int, Socket) (Array Word8)
 -> m (Step (Int, Socket) (Array Word8)))
-> Step (Int, Socket) (Array Word8)
-> m (Step (Int, Socket) (Array Word8))
forall a b. (a -> b) -> a -> b
$
            case Array Word8 -> Int
forall a. Unbox a => Array a -> Int
A.length Array Word8
arr of
                Int
0 -> Step (Int, Socket) (Array Word8)
forall s a. Step s a
S.Stop
                Int
_ -> Array Word8 -> (Int, Socket) -> Step (Int, Socket) (Array Word8)
forall s a. a -> s -> Step s a
S.Yield Array Word8
arr (Int
size, Socket
h)

-- | Same as 'chunkReaderWith'
--
{-# DEPRECATED readChunksWithBufferOf "Please use 'chunkReaderWith' instead" #-}
{-# INLINE_NORMAL readChunksWithBufferOf #-}
readChunksWithBufferOf :: MonadIO m => Unfold m (Int, Socket) (Array Word8)
readChunksWithBufferOf :: forall (m :: * -> *).
MonadIO m =>
Unfold m (Int, Socket) (Array Word8)
readChunksWithBufferOf = Unfold m (Int, Socket) (Array Word8)
forall (m :: * -> *).
MonadIO m =>
Unfold m (Int, Socket) (Array Word8)
chunkReaderWith

-- | Unfolds a socket into a stream of 'Word8' arrays. Requests to the socket
-- are performed using a buffer of size
-- 'Streamly.Internal.Data.Array.Type.defaultChunkSize'. The
-- size of arrays in the resulting stream are therefore less than or equal to
-- 'Streamly.Internal.Data.Array.Type.defaultChunkSize'.
--
{-# INLINE chunkReader #-}
chunkReader :: MonadIO m => Unfold m Socket (Array Word8)
chunkReader :: forall (m :: * -> *). MonadIO m => Unfold m Socket (Array Word8)
chunkReader = Int
-> Unfold m (Int, Socket) (Array Word8)
-> Unfold m Socket (Array Word8)
forall a (m :: * -> *) b c. a -> Unfold m (a, b) c -> Unfold m b c
UF.first Int
defaultChunkSize Unfold m (Int, Socket) (Array Word8)
forall (m :: * -> *).
MonadIO m =>
Unfold m (Int, Socket) (Array Word8)
chunkReaderWith

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

{-# INLINE concatChunks #-}
concatChunks :: (Monad m, Unbox a) => Stream m (Array a) -> Stream m a
concatChunks :: forall (m :: * -> *) a.
(Monad m, Unbox a) =>
Stream m (Array a) -> Stream m a
concatChunks = Unfold m (Array a) a -> Stream m (Array a) -> Stream m a
forall (m :: * -> *) a b.
Monad m =>
Unfold m a b -> Stream m a -> Stream m b
S.unfoldMany Unfold m (Array a) a
forall (m :: * -> *) a. (Monad m, Unbox a) => Unfold m (Array a) a
A.reader

-- | Generate a byte stream from a socket using a buffer of the given size.
--
-- /Pre-release/
{-# INLINE readWith #-}
readWith :: MonadIO m => Int -> Socket -> Stream m Word8
readWith :: forall (m :: * -> *). MonadIO m => Int -> Socket -> Stream m Word8
readWith Int
size = Stream m (Array Word8) -> Stream m Word8
forall (m :: * -> *) a.
(Monad m, Unbox a) =>
Stream m (Array a) -> Stream m a
concatChunks (Stream m (Array Word8) -> Stream m Word8)
-> (Socket -> Stream m (Array Word8)) -> Socket -> Stream m Word8
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Socket -> Stream m (Array Word8)
forall (m :: * -> *).
MonadIO m =>
Int -> Socket -> Stream m (Array Word8)
readChunksWith Int
size

-- | Generate a byte stream from a socket.
--
-- >>> read = Socket.readWith defaultChunkSize
--
-- /Pre-release/
{-# INLINE read #-}
read :: MonadIO m => Socket -> Stream m Word8
read :: forall (m :: * -> *). MonadIO m => Socket -> Stream m Word8
read = Int -> Socket -> Stream m Word8
forall (m :: * -> *). MonadIO m => Int -> Socket -> Stream m Word8
readWith Int
defaultChunkSize

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

-- | Same as 'readWith'
--
{-# DEPRECATED readWithBufferOf "Please use 'readerWith' instead" #-}
{-# INLINE readWithBufferOf #-}
readWithBufferOf :: MonadIO m => Unfold m (Int, Socket) Word8
readWithBufferOf :: forall (m :: * -> *). MonadIO m => Unfold m (Int, Socket) Word8
readWithBufferOf = Unfold m (Int, Socket) Word8
forall (m :: * -> *). MonadIO m => Unfold m (Int, Socket) Word8
readerWith

-- | Unfolds a 'Socket' into a byte stream.  IO requests to the socket are
-- performed in sizes of
-- 'Streamly.Internal.Data.Array.Type.defaultChunkSize'.
--
{-# INLINE reader #-}
reader :: MonadIO m => Unfold m Socket Word8
reader :: forall (m :: * -> *). MonadIO m => Unfold m Socket Word8
reader = Int -> Unfold m (Int, Socket) Word8 -> Unfold m Socket Word8
forall a (m :: * -> *) b c. a -> Unfold m (a, b) c -> Unfold m b c
UF.first Int
defaultChunkSize Unfold m (Int, Socket) Word8
forall (m :: * -> *). MonadIO m => Unfold m (Int, Socket) Word8
readerWith

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

-- | Write a stream of arrays to a handle.
--
{-# INLINE putChunks #-}
putChunks :: (MonadIO m, Unbox a)
    => Socket -> Stream m (Array a) -> m ()
putChunks :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Socket -> Stream m (Array a) -> m ()
putChunks Socket
h = Fold m (Array a) () -> Stream m (Array a) -> m ()
forall (m :: * -> *) a b.
Monad m =>
Fold m a b -> Stream m a -> m b
S.fold ((Array a -> m ()) -> Fold m (Array a) ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> Fold m a ()
FL.drainMapM (IO () -> m ()
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> (Array a -> IO ()) -> Array a -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Socket -> Array a -> IO ()
forall a. Unbox a => Socket -> Array a -> IO ()
putChunk 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.
--
{-# INLINE writeChunks #-}
writeChunks :: (MonadIO m, Unbox a) => Socket -> Fold m (Array a) ()
writeChunks :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Socket -> Fold m (Array a) ()
writeChunks Socket
h = (Array a -> m ()) -> Fold m (Array a) ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> Fold m a ()
FL.drainMapM (IO () -> m ()
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> (Array a -> IO ()) -> Array a -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Socket -> Array a -> IO ()
forall a. Unbox a => Socket -> Array a -> IO ()
putChunk Socket
h)

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

-- | Same as 'writeChunksWith'
--
{-# DEPRECATED writeChunksWithBufferOf "Please use 'writeChunksWith' instead" #-}
{-# INLINE writeChunksWithBufferOf #-}
writeChunksWithBufferOf :: (MonadIO m, Unbox a)
    => Int -> Socket -> Fold m (Array a) ()
writeChunksWithBufferOf :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> Socket -> Fold m (Array a) ()
writeChunksWithBufferOf = Int -> Socket -> Fold m (Array a) ()
forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> Socket -> Fold m (Array a) ()
writeChunksWith

-- 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.
--
{-# INLINE putBytesWith #-}
putBytesWith :: MonadIO m => Int -> Socket -> Stream m Word8 -> m ()
putBytesWith :: forall (m :: * -> *).
MonadIO m =>
Int -> Socket -> Stream m Word8 -> m ()
putBytesWith Int
n Socket
h Stream m Word8
m = Socket -> Stream m (Array Word8) -> m ()
forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Socket -> Stream m (Array a) -> m ()
putChunks Socket
h (Stream m (Array Word8) -> m ()) -> Stream m (Array Word8) -> m ()
forall a b. (a -> b) -> a -> b
$ Int -> Stream m Word8 -> Stream m (Array Word8)
forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> Stream m a -> Stream m (Array a)
A.pinnedChunksOf Int
n Stream m Word8
m

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

-- | Same as 'writeWith'
--
{-# DEPRECATED writeWithBufferOf "Please use 'writeWith' instead" #-}
{-# INLINE writeWithBufferOf #-}
writeWithBufferOf :: MonadIO m => Int -> Socket -> Fold m Word8 ()
writeWithBufferOf :: forall (m :: * -> *). MonadIO m => Int -> Socket -> Fold m Word8 ()
writeWithBufferOf = Int -> Socket -> Fold m Word8 ()
forall (m :: * -> *). MonadIO m => Int -> Socket -> Fold m Word8 ()
writeWith

-- | Write a stream of 'Maybe' values. Keep buffering the 'Just' values in an
-- array. Write the array to the 'Handle' as soon as a 'Nothing' is encountered
-- or the buffer size exceeds the specified limit.
--
-- /Pre-release/
{-# INLINE writeMaybesWith #-}
writeMaybesWith :: (MonadIO m )
    => Int -> Socket -> Fold m (Maybe Word8) ()
writeMaybesWith :: forall (m :: * -> *).
MonadIO m =>
Int -> Socket -> Fold m (Maybe Word8) ()
writeMaybesWith Int
n Socket
h =
    let writeNJusts :: Fold m (Maybe Word8) (Array Word8)
writeNJusts = (Maybe Word8 -> Word8)
-> Fold m Word8 (Array Word8) -> Fold m (Maybe Word8) (Array Word8)
forall a b (m :: * -> *) r. (a -> b) -> Fold m b r -> Fold m a r
FL.lmap Maybe Word8 -> Word8
forall a. HasCallStack => Maybe a -> a
fromJust (Fold m Word8 (Array Word8) -> Fold m (Maybe Word8) (Array Word8))
-> Fold m Word8 (Array Word8) -> Fold m (Maybe Word8) (Array Word8)
forall a b. (a -> b) -> a -> b
$ Int -> Fold m Word8 (Array Word8)
forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> Fold m a (Array a)
A.pinnedCreateOf Int
n
        writeOnNothing :: Fold m (Maybe Word8) (Array Word8)
writeOnNothing = (Maybe Word8 -> Bool)
-> Fold m (Maybe Word8) (Array Word8)
-> Fold m (Maybe Word8) (Array Word8)
forall (m :: * -> *) a b.
Monad m =>
(a -> Bool) -> Fold m a b -> Fold m a b
FL.takeEndBy_ Maybe Word8 -> Bool
forall a. Maybe a -> Bool
isNothing Fold m (Maybe Word8) (Array Word8)
writeNJusts
    in Fold m (Maybe Word8) (Array Word8)
-> Fold m (Array Word8) () -> Fold m (Maybe Word8) ()
forall (m :: * -> *) a b c.
Monad m =>
Fold m a b -> Fold m b c -> Fold m a c
FL.many Fold m (Maybe Word8) (Array Word8)
writeOnNothing (Socket -> Fold m (Array Word8) ()
forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Socket -> Fold m (Array a) ()
writeChunks Socket
h)

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

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