Skip to content

Commit 17287b5

Browse files
Use network's connectTo when available.
1 parent 21cf023 commit 17287b5

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

Database/MongoDB/Internal/Network.hs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,38 @@
33

44
module Database.MongoDB.Internal.Network (PortID(..), N.HostName, connectTo) where
55

6-
import Control.Exception (bracketOnError)
7-
import Network.BSD as BSD
8-
import System.IO (Handle, IOMode(ReadWriteMode))
96

107
#if !MIN_VERSION_network(2, 8, 0)
8+
119
import qualified Network as N
10+
import System.IO (Handle)
11+
1212
#else
13+
14+
import Control.Exception (bracketOnError)
15+
import Network.BSD as BSD
1316
import qualified Network.Socket as N
17+
import System.IO (Handle, IOMode(ReadWriteMode))
18+
1419
#endif
1520

21+
22+
-- | Wraps network's 'PortNumber'
23+
-- Used to easy compatibility between older and newer network versions.
1624
newtype PortID = PortNumber N.PortNumber deriving (Show, Eq, Ord)
1725

18-
-- Taken from network 2.8's connectTo
26+
27+
#if !MIN_VERSION_network(2, 8, 0)
28+
29+
-- Unwrap our newtype and use network's PortID and connectTo
30+
connectTo :: N.HostName -- Hostname
31+
-> PortID -- Port Identifier
32+
-> IO Handle -- Connected Socket
33+
connectTo hostname (PortNumber port) = N.connectTo hostname (N.PortNumber port)
34+
35+
#else
36+
37+
-- Copied implementation from network 2.8's 'connectTo', but using our 'PortID' newtype.
1938
-- https://github.com/haskell/network/blob/e73f0b96c9da924fe83f3c73488f7e69f712755f/Network.hs#L120-L129
2039
connectTo :: N.HostName -- Hostname
2140
-> PortID -- Port Identifier
@@ -30,3 +49,4 @@ connectTo hostname (PortNumber port) = do
3049
N.connect sock (N.SockAddrInet port (hostAddress he))
3150
N.socketToHandle sock ReadWriteMode
3251
)
52+
#endif

0 commit comments

Comments
 (0)