{-# LANGUAGE CPP #-}
#include "HsNetDef.h"
module Network.Socket.Wait (
withReadable,
acceptWith,
recvBufWith,
recvBufFromWith,
isClosedSocketError,
isTransientAcceptError,
) where
import Control.Concurrent.STM
import qualified Control.Exception as E
import Foreign.C.Error (
Errno (..),
eCONNABORTED,
eHOSTDOWN,
eHOSTUNREACH,
eNETDOWN,
eNETUNREACH,
eNONET,
eNOPROTOOPT,
ePROTO,
)
import GHC.IO.Exception (IOErrorType (..), ioe_errno)
import System.IO.Error (ioeGetErrorType)
import Network.Socket.Buffer
#if defined(mingw32_HOST_OS)
import Network.Socket.Cbits
import Network.Socket.Win32.Cancel
#else
import Network.Socket.STM
#endif
import Network.Socket.Imports
import Network.Socket.Syscall
import Network.Socket.Types
withReadable :: Socket -> STM a -> IO b -> IO (Either a b)
withReadable :: forall a b. Socket -> STM a -> IO b -> IO (Either a b)
withReadable Socket
s STM a
giveUp IO b
act = do
Maybe a
gaveUp <- STM (Maybe a) -> IO (Maybe a)
forall a. STM a -> IO a
atomically (STM (Maybe a) -> IO (Maybe a)) -> STM (Maybe a) -> IO (Maybe a)
forall a b. (a -> b) -> a -> b
$ (a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> STM a -> STM (Maybe a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> STM a
giveUp) STM (Maybe a) -> STM (Maybe a) -> STM (Maybe a)
forall a. STM a -> STM a -> STM a
`orElse` Maybe a -> STM (Maybe a)
forall a. a -> STM a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing
case Maybe a
gaveUp of
Just a
a -> Either a b -> IO (Either a b)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Either a b -> IO (Either a b)) -> Either a b -> IO (Either a b)
forall a b. (a -> b) -> a -> b
$ a -> Either a b
forall a b. a -> Either a b
Left a
a
Maybe a
Nothing -> IO (Either a b)
wait
where
#if !defined(mingw32_HOST_OS)
wait :: IO (Either a b)
wait = do
(STM ()
readable, IO ()
cancelWait) <- Socket -> IO (STM (), IO ())
waitAndCancelReadSocketSTM Socket
s
Either a ()
ev <-
STM (Either a ()) -> IO (Either a ())
forall a. STM a -> IO a
atomically ((a -> Either a ()
forall a b. a -> Either a b
Left (a -> Either a ()) -> STM a -> STM (Either a ())
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> STM a
giveUp) STM (Either a ()) -> STM (Either a ()) -> STM (Either a ())
forall a. STM a -> STM a -> STM a
`orElse` (() -> Either a ()
forall a b. b -> Either a b
Right (() -> Either a ()) -> STM () -> STM (Either a ())
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> STM ()
readable))
IO (Either a ()) -> IO () -> IO (Either a ())
forall a b. IO a -> IO b -> IO a
`E.finally` (IO ()
cancelWait IO () -> (IOException -> IO ()) -> IO ()
forall e a. Exception e => IO a -> (e -> IO a) -> IO a
`E.catch` IOException -> IO ()
gone)
(a -> IO (Either a b))
-> (() -> IO (Either a b)) -> Either a () -> IO (Either a b)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Either a b -> IO (Either a b)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Either a b -> IO (Either a b))
-> (a -> Either a b) -> a -> IO (Either a b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Either a b
forall a b. a -> Either a b
Left) (IO (Either a b) -> () -> IO (Either a b)
forall a b. a -> b -> a
const (IO (Either a b) -> () -> IO (Either a b))
-> IO (Either a b) -> () -> IO (Either a b)
forall a b. (a -> b) -> a -> b
$ b -> Either a b
forall a b. b -> Either a b
Right (b -> Either a b) -> IO b -> IO (Either a b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO b
act) Either a ()
ev
gone :: E.IOException -> IO ()
gone :: IOException -> IO ()
gone IOException
_ = () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
#else
wait = do
(completed, cancelAct) <- windowsViaSTM s act
ev <-
atomically ((Right <$> completed) `orElse` (Left <$> giveUp))
`E.onException` cancelAct
case ev of
Right b -> return $ Right b
Left a -> do
cancelAct
return $ Left a
#endif
acceptWith :: SocketAddress sa => Socket -> STM a -> IO (Either a (Socket, sa))
acceptWith :: forall sa a.
SocketAddress sa =>
Socket -> STM a -> IO (Either a (Socket, sa))
acceptWith Socket
s STM a
giveUp = Socket -> STM a -> IO (Socket, sa) -> IO (Either a (Socket, sa))
forall a b. Socket -> STM a -> IO b -> IO (Either a b)
withReadable Socket
s STM a
giveUp (IO (Socket, sa) -> IO (Either a (Socket, sa)))
-> IO (Socket, sa) -> IO (Either a (Socket, sa))
forall a b. (a -> b) -> a -> b
$ Socket -> IO (Socket, sa)
forall sa. SocketAddress sa => Socket -> IO (Socket, sa)
accept Socket
s
recvBufWith :: Socket -> Ptr Word8 -> Int -> STM a -> IO (Either a Int)
recvBufWith :: forall a. Socket -> Ptr Word8 -> Int -> STM a -> IO (Either a Int)
recvBufWith Socket
s Ptr Word8
ptr Int
nbytes STM a
giveUp = Socket -> STM a -> IO Int -> IO (Either a Int)
forall a b. Socket -> STM a -> IO b -> IO (Either a b)
withReadable Socket
s STM a
giveUp (IO Int -> IO (Either a Int)) -> IO Int -> IO (Either a Int)
forall a b. (a -> b) -> a -> b
$ Socket -> Ptr Word8 -> Int -> IO Int
recvBuf Socket
s Ptr Word8
ptr Int
nbytes
recvBufFromWith
:: SocketAddress sa
=> Socket -> Ptr Word8 -> Int -> STM a -> IO (Either a (Int, sa))
recvBufFromWith :: forall sa a.
SocketAddress sa =>
Socket -> Ptr Word8 -> Int -> STM a -> IO (Either a (Int, sa))
recvBufFromWith Socket
s Ptr Word8
ptr Int
nbytes STM a
giveUp =
Socket -> STM a -> IO (Int, sa) -> IO (Either a (Int, sa))
forall a b. Socket -> STM a -> IO b -> IO (Either a b)
withReadable Socket
s STM a
giveUp (IO (Int, sa) -> IO (Either a (Int, sa)))
-> IO (Int, sa) -> IO (Either a (Int, sa))
forall a b. (a -> b) -> a -> b
$ Socket -> Ptr Word8 -> Int -> IO (Int, sa)
forall sa a.
SocketAddress sa =>
Socket -> Ptr a -> Int -> IO (Int, sa)
recvBufFrom Socket
s Ptr Word8
ptr Int
nbytes
isClosedSocketError :: E.IOException -> Bool
isClosedSocketError :: IOException -> Bool
isClosedSocketError IOException
e = IOException -> IOErrorType
ioeGetErrorType IOException
e IOErrorType -> IOErrorType -> Bool
forall a. Eq a => a -> a -> Bool
== IOErrorType
InvalidArgument Bool -> Bool -> Bool
|| IOException -> Bool
forall {p}. p -> Bool
isWinsock IOException
e
where
#if defined(mingw32_HOST_OS)
isWinsock x = ioe_errno x `elem` map Just [wsaEINTR, wsaENOTSOCK]
#else
isWinsock :: p -> Bool
isWinsock p
_ = Bool
False
#endif
isTransientAcceptError :: E.IOException -> Bool
isTransientAcceptError :: IOException -> Bool
isTransientAcceptError IOException
e =
IOException -> IOErrorType
ioeGetErrorType IOException
e IOErrorType -> IOErrorType -> Bool
forall a. Eq a => a -> a -> Bool
== IOErrorType
Interrupted Bool -> Bool -> Bool
|| (Errno -> Bool) -> [Errno] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any Errno -> Bool
is [Errno]
errnos Bool -> Bool -> Bool
|| IOException -> Bool
forall {p}. p -> Bool
isWinsock IOException
e
where
is :: Errno -> Bool
is (Errno CInt
n) = CInt
n CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
/= -CInt
1 Bool -> Bool -> Bool
&& IOException -> Maybe CInt
ioe_errno IOException
e Maybe CInt -> Maybe CInt -> Bool
forall a. Eq a => a -> a -> Bool
== CInt -> Maybe CInt
forall a. a -> Maybe a
Just CInt
n
errnos :: [Errno]
errnos =
[ Errno
eCONNABORTED
, Errno
eHOSTDOWN
, Errno
eHOSTUNREACH
, Errno
eNETDOWN
, Errno
eNETUNREACH
, Errno
eNONET
, Errno
eNOPROTOOPT
, Errno
ePROTO
]
#if defined(mingw32_HOST_OS)
isWinsock x = ioe_errno x `elem` map Just codes
codes =
[ wsaECONNABORTED
, wsaECONNRESET
, wsaEHOSTDOWN
, wsaEHOSTUNREACH
, wsaENETDOWN
, wsaENETRESET
, wsaENETUNREACH
, wsaENOPROTOOPT
]
#else
isWinsock :: p -> Bool
isWinsock p
_ = Bool
False
#endif