#include "inline.hs"
module Streamly.Internal.Network.Socket
(
SockSpec (..)
, forSocketM
, withSocket
, accept
, connections
, connect
, connectFrom
, read
, readWithBufferOf
, readChunk
, readChunks
, readChunksWithBufferOf
, toChunksWithBufferOf
, toChunks
, toBytes
, write
, writeWithBufferOf
, writeMaybesWithBufferOf
, putChunks
, putBytesWithBufferOf
, putBytes
, writeChunk
, writeChunks
, writeChunksWithBufferOf
)
where
import Control.Concurrent (threadWaitWrite, rtsSupportsBoundThreads)
import Control.Exception (onException)
import Control.Monad.Catch (MonadCatch, finally, MonadMask)
import Control.Monad.IO.Class (MonadIO(..))
import Control.Monad (forM_, when)
import Data.Maybe (isNothing, fromJust)
import Data.Word (Word8)
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.Internal.BaseCompat
import Streamly.Internal.Control.Concurrent (MonadAsync)
import Streamly.Internal.Data.Array.Foreign.Mut.Type
(fromForeignPtrUnsafe, touch)
import Streamly.Internal.Data.Array.Foreign.Type (Array(..))
import Streamly.Internal.Data.Array.Stream.Foreign (lpackArraysChunksOf)
import Streamly.Internal.Data.Fold (Fold)
import Streamly.Internal.Data.Stream.IsStream.Type
(IsStream, mkStream, fromStreamD)
import Streamly.Internal.Data.Stream.Serial (SerialT)
import Streamly.Internal.Data.Unfold.Type (Unfold(..))
import Streamly.Internal.System.IO (defaultChunkSize)
import qualified Streamly.Internal.Data.Array.Foreign as A
import qualified Streamly.Internal.Data.Array.Foreign.Type as A
import qualified Streamly.Internal.Data.Array.Stream.Foreign as AS
import qualified Streamly.Internal.Data.Fold as FL
import qualified Streamly.Internal.Data.Stream.IsStream as S
import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
import qualified Streamly.Internal.Data.Unfold as UF
{-# 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 = 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))
{-# INLINE withSocket #-}
withSocket :: (IsStream t, MonadAsync m, MonadCatch m)
=> Socket -> (Socket -> t m a) -> t m a
withSocket :: forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, MonadAsync m, MonadCatch m) =>
Socket -> (Socket -> t m a) -> t m a
withSocket Socket
sk Socket -> t m a
f = forall (t :: (* -> *) -> * -> *) (m :: * -> *) b a.
(IsStream t, MonadAsync m, 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)
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
Socket -> IO ()
use Socket
sock forall a b. IO a -> IO b -> IO a
`onException` Socket -> IO ()
Net.close Socket
sock
forall (m :: * -> *) a. Monad m => a -> m a
return Socket
sock
where
use :: Socket -> IO ()
use Socket
sock = do
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 = 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 (Socket -> IO (Socket, SockAddr)
Net.accept Socket
listener forall a b. IO a -> IO b -> IO a
`onException` Socket -> IO ()
Net.close Socket
listener)
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
{-# 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.
Functor 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
{-# INLINE connectCommon #-}
connectCommon :: SockSpec -> Maybe SockAddr -> SockAddr -> IO Socket
connectCommon :: SockSpec -> Maybe SockAddr -> SockAddr -> IO Socket
connectCommon 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
..} Maybe SockAddr
local SockAddr
remote = 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
Socket -> IO ()
use Socket
sock forall a b. IO a -> IO b -> IO a
`onException` Socket -> IO ()
Net.close Socket
sock
forall (m :: * -> *) a. Monad m => a -> m a
return Socket
sock
where
use :: Socket -> IO ()
use Socket
sock = do
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
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
{-# INLINE connect #-}
connect :: SockSpec -> SockAddr -> IO Socket
connect :: SockSpec -> SockAddr -> IO Socket
connect SockSpec
spec = SockSpec -> Maybe SockAddr -> SockAddr -> IO Socket
connectCommon SockSpec
spec forall a. Maybe a
Nothing
{-# 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 (forall a. a -> Maybe a
Just SockAddr
local)
{-# 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 (Socket -> IO (Socket, SockAddr)
Net.accept Socket
listener forall a b. IO a -> IO b -> IO a
`onException` Socket -> IO ()
Net.close Socket
listener)
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 (Socket -> IO (Socket, SockAddr)
Net.accept Socket
listener forall a b. IO a -> IO b -> IO a
`onException` Socket -> IO ()
Net.close Socket
listener)
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)
{-# 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
{-# 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
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
unsafeWithForeignPtr 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 = forall a. Array a -> Array a
A.unsafeFreeze
forall a b. (a -> b) -> a -> b
$ forall a. ForeignPtr a -> Ptr a -> Ptr a -> Array a
fromForeignPtrUnsafe ForeignPtr Word8
ptr (Ptr Word8
p forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
n) (Ptr Word8
p forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
size)
forall (m :: * -> *) a. Monad m => a -> m a
return Array Word8
v
{-# INLINABLE readChunk #-}
readChunk :: Int -> Socket -> IO (Array Word8)
readChunk :: Int -> Socket -> IO (Array Word8)
readChunk = forall h.
(h -> Ptr Word8 -> Int -> IO Int) -> Int -> h -> IO (Array Word8)
readArrayUptoWith Socket -> Ptr Word8 -> Int -> IO Int
recvBuf
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
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
ArrayContents
aEnd :: forall a. Array a -> Ptr a
arrStart :: forall a. Array a -> Ptr a
arrContents :: forall a. Array a -> ArrayContents
aEnd :: Ptr a
arrStart :: Ptr a
arrContents :: ArrayContents
..} =
h -> Ptr Word8 -> Int -> IO ()
f h
h (forall a b. Ptr a -> Ptr b
castPtr Ptr a
arrStart) Int
aLen forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ArrayContents -> IO ()
touch ArrayContents
arrContents
where
aLen :: Int
aLen = Ptr a
aEnd forall a b. Ptr a -> Ptr b -> Int
`minusPtr` Ptr a
arrStart
{-# 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
{-# INLINE _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
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
{-# INLINE_NORMAL toChunksWithBufferOf #-}
toChunksWithBufferOf :: (IsStream t, MonadIO m)
=> Int -> Socket -> t m (Array Word8)
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
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)
readChunk 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 ()
{-# 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
defaultChunkSize
{-# 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)
readChunk 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)
{-# INLINE readChunks #-}
readChunks :: MonadIO m => Unfold m Socket (Array Word8)
readChunks :: forall (m :: * -> *). MonadIO m => Unfold m Socket (Array Word8)
readChunks = forall a (m :: * -> *) b c. a -> Unfold m (a, b) c -> Unfold m b c
UF.supplyFirst Int
defaultChunkSize forall (m :: * -> *).
MonadIO m =>
Unfold m (Int, Socket) (Array Word8)
readChunksWithBufferOf
{-# 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, Monad 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
{-# 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.many 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
{-# INLINE read #-}
read :: MonadIO m => Unfold m Socket Word8
read :: forall (m :: * -> *). MonadIO m => Unfold m Socket Word8
read = forall a (m :: * -> *) b c. a -> Unfold m (a, b) c -> Unfold m b c
UF.supplyFirst Int
defaultChunkSize forall (m :: * -> *). MonadIO m => Unfold m (Int, Socket) Word8
readWithBufferOf
{-# INLINE putChunks #-}
putChunks :: (MonadIO m, Storable a)
=> Socket -> SerialT m (Array a) -> m ()
putChunks :: forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Socket -> SerialT m (Array a) -> m ()
putChunks 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)
{-# 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)
{-# 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)
{-# INLINE putBytesWithBufferOf #-}
putBytesWithBufferOf :: MonadIO m => Int -> Socket -> SerialT m Word8 -> m ()
putBytesWithBufferOf :: forall (m :: * -> *).
MonadIO m =>
Int -> Socket -> SerialT m Word8 -> m ()
putBytesWithBufferOf Int
n Socket
h SerialT m Word8
m = forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Socket -> SerialT m (Array a) -> m ()
putChunks 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
{-# 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.chunksOf 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)
{-# INLINE writeMaybesWithBufferOf #-}
writeMaybesWithBufferOf :: (MonadIO m )
=> Int -> Socket -> Fold m (Maybe Word8) ()
writeMaybesWithBufferOf :: forall (m :: * -> *).
MonadIO m =>
Int -> Socket -> Fold m (Maybe Word8) ()
writeMaybesWithBufferOf Int
n Socket
h =
let writeNJusts :: Fold m (Maybe Word8) (Array Word8)
writeNJusts = forall a b (m :: * -> *) r. (a -> b) -> Fold m b r -> Fold m a r
FL.lmap forall a. HasCallStack => Maybe a -> a
fromJust forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> Fold m a (Array a)
A.writeN Int
n
writeOnNothing :: Fold m (Maybe Word8) (Array Word8)
writeOnNothing = forall (m :: * -> *) a b.
Monad m =>
(a -> Bool) -> Fold m a b -> Fold m a b
FL.takeEndBy_ forall a. Maybe a -> Bool
isNothing Fold m (Maybe Word8) (Array Word8)
writeNJusts
in 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 (forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Socket -> Fold m (Array a) ()
writeChunks Socket
h)
{-# INLINE putBytes #-}
putBytes :: MonadIO m => Socket -> SerialT m Word8 -> m ()
putBytes :: forall (m :: * -> *).
MonadIO m =>
Socket -> SerialT m Word8 -> m ()
putBytes = forall (m :: * -> *).
MonadIO m =>
Int -> Socket -> SerialT m Word8 -> m ()
putBytesWithBufferOf Int
defaultChunkSize
{-# 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
defaultChunkSize