module Network.Transport.Util (spawn) where
import Network.Transport
( Transport
, EndPoint(..)
, EndPointAddress
, newEndPoint
)
import Control.Exception (throwIO)
import Control.Concurrent (forkIO)
spawn :: Transport -> (EndPoint -> IO ()) -> IO EndPointAddress
spawn :: Transport -> (EndPoint -> IO ()) -> IO EndPointAddress
spawn Transport
transport EndPoint -> IO ()
proc = do
Either (TransportError NewEndPointErrorCode) EndPoint
mEndPoint <- Transport
-> IO (Either (TransportError NewEndPointErrorCode) EndPoint)
newEndPoint Transport
transport
case Either (TransportError NewEndPointErrorCode) EndPoint
mEndPoint of
Left TransportError NewEndPointErrorCode
err -> TransportError NewEndPointErrorCode -> IO EndPointAddress
forall e a. Exception e => e -> IO a
throwIO TransportError NewEndPointErrorCode
err
Right EndPoint
endPoint -> do
IO () -> IO ThreadId
forkIO (IO () -> IO ThreadId) -> IO () -> IO ThreadId
forall a b. (a -> b) -> a -> b
$ EndPoint -> IO ()
proc EndPoint
endPoint
EndPointAddress -> IO EndPointAddress
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (EndPointAddress -> IO EndPointAddress)
-> EndPointAddress -> IO EndPointAddress
forall a b. (a -> b) -> a -> b
$ EndPoint -> EndPointAddress
address EndPoint
endPoint