{-# language BangPatterns #-}
{-# language LambdaCase #-}
{-# language GADTSyntax #-}
{-# language KindSignatures #-}
{-# language DataKinds #-}
module Socket.Datagram.Interruptible.MutableBytes
(
send
, sendToIPv4
, receive
, receiveFromIPv4
, MM.receiveMany
, MM.receiveManyFromIPv4
) where
import Control.Concurrent.STM (TVar)
import Data.Bytes.Types (MutableBytes)
import Data.Primitive.PrimArray.Offset (MutablePrimArrayOffset)
import GHC.Exts (RealWorld)
import Posix.Socket (SocketAddressInternet)
import Socket (Connectedness(..),Family(..),Version(..),Interruptibility(Interruptible))
import Socket.Datagram (Socket(..),SendException,ReceiveException)
import Socket.IPv4 (Peer)
import qualified Socket.Datagram.Interruptible.MutableBytes.Many as MM
import qualified Socket.Datagram.Interruptible.MutableBytes.Receive.Connected as CR
import qualified Socket.Datagram.Interruptible.MutableBytes.Send.Connected as CS
import qualified Socket.Datagram.Interruptible.MutableBytes.Send.IPv4 as V4S
import qualified Socket.Datagram.Interruptible.MutableBytes.Receive.IPv4 as V4R
send ::
TVar Bool
-> Socket 'Connected a
-> MutableBytes RealWorld
-> IO (Either (SendException 'Interruptible) ())
send !intr (Socket !sock) !buf =
CS.send intr () sock buf
receive ::
TVar Bool
-> Socket c a
-> MutableBytes RealWorld
-> IO (Either (ReceiveException 'Interruptible) Int)
receive !intr (Socket !sock) !buf =
CR.receive intr sock buf () >>= \case
Right sz -> pure (Right sz)
Left err -> pure (Left err)
sendToIPv4 ::
TVar Bool
-> Socket 'Unconnected ('Internet 'V4)
-> Peer
-> MutableBytes RealWorld
-> IO (Either (SendException 'Interruptible) ())
sendToIPv4 !intr (Socket !sock) !dst !buf =
V4S.send intr dst sock buf
receiveFromIPv4 ::
TVar Bool
-> Socket 'Unconnected ('Internet 'V4)
-> MutableBytes RealWorld
-> MutablePrimArrayOffset RealWorld SocketAddressInternet
-> IO (Either (ReceiveException 'Interruptible) Int)
receiveFromIPv4 !intr (Socket !sock) !buf !addr =
V4R.receive intr sock buf addr