Use network's connectTo when available.

This commit is contained in:
Scott Fleischman 2019-05-30 10:02:06 -07:00
parent 21cf023854
commit 17287b5556

View file

@ -3,19 +3,38 @@
module Database.MongoDB.Internal.Network (PortID(..), N.HostName, connectTo) where module Database.MongoDB.Internal.Network (PortID(..), N.HostName, connectTo) where
import Control.Exception (bracketOnError)
import Network.BSD as BSD
import System.IO (Handle, IOMode(ReadWriteMode))
#if !MIN_VERSION_network(2, 8, 0) #if !MIN_VERSION_network(2, 8, 0)
import qualified Network as N import qualified Network as N
import System.IO (Handle)
#else #else
import Control.Exception (bracketOnError)
import Network.BSD as BSD
import qualified Network.Socket as N import qualified Network.Socket as N
import System.IO (Handle, IOMode(ReadWriteMode))
#endif #endif
-- | Wraps network's 'PortNumber'
-- Used to easy compatibility between older and newer network versions.
newtype PortID = PortNumber N.PortNumber deriving (Show, Eq, Ord) newtype PortID = PortNumber N.PortNumber deriving (Show, Eq, Ord)
-- Taken from network 2.8's connectTo
#if !MIN_VERSION_network(2, 8, 0)
-- Unwrap our newtype and use network's PortID and connectTo
connectTo :: N.HostName -- Hostname
-> PortID -- Port Identifier
-> IO Handle -- Connected Socket
connectTo hostname (PortNumber port) = N.connectTo hostname (N.PortNumber port)
#else
-- Copied implementation from network 2.8's 'connectTo', but using our 'PortID' newtype.
-- https://github.com/haskell/network/blob/e73f0b96c9da924fe83f3c73488f7e69f712755f/Network.hs#L120-L129 -- https://github.com/haskell/network/blob/e73f0b96c9da924fe83f3c73488f7e69f712755f/Network.hs#L120-L129
connectTo :: N.HostName -- Hostname connectTo :: N.HostName -- Hostname
-> PortID -- Port Identifier -> PortID -- Port Identifier
@ -30,3 +49,4 @@ connectTo hostname (PortNumber port) = do
N.connect sock (N.SockAddrInet port (hostAddress he)) N.connect sock (N.SockAddrInet port (hostAddress he))
N.socketToHandle sock ReadWriteMode N.socketToHandle sock ReadWriteMode
) )
#endif