diff --git a/Network/Socket.hs b/Network/Socket.hs index 7276c61a..a3a75d51 100644 --- a/Network/Socket.hs +++ b/Network/Socket.hs @@ -57,7 +57,7 @@ -- > addrFlags = [AI_PASSIVE] -- > , addrSocketType = Stream -- > } --- > NE.head <$> getAddrInfoNE (Just hints) mhost (Just port) +-- > NE.head <$> getAddrInfo (Just hints) mhost (Just port) -- > open addr = E.bracketOnError (openSocket addr) close $ \sock -> do -- > setSocketOption sock ReuseAddr 1 -- > withFdSocket sock setCloseOnExecIfNeeded @@ -97,7 +97,7 @@ -- > where -- > resolve = do -- > let hints = defaultHints { addrSocketType = Stream } --- > NE.head <$> getAddrInfoNE (Just hints) (Just host) (Just port) +-- > NE.head <$> getAddrInfo (Just hints) (Just host) (Just port) -- > open addr = E.bracketOnError (openSocket addr) close $ \sock -> do -- > connect sock $ addrAddress addr -- > return sock @@ -112,8 +112,7 @@ module Network.Socket ( withSocketsDo, -- * Address information - getAddrInfo, - getAddrInfoNE, + GetAddrInfo (..), -- ** Types HostName, diff --git a/Network/Socket/Info.hsc b/Network/Socket/Info.hsc index 9eb92551..e9e40791 100644 --- a/Network/Socket/Info.hsc +++ b/Network/Socket/Info.hsc @@ -201,6 +201,19 @@ defaultHints = AddrInfo { , addrCanonName = Nothing } +class GetAddrInfo t where + getAddrInfo + :: Maybe AddrInfo -- ^ preferred socket type or protocol + -> Maybe HostName -- ^ host name to look up + -> Maybe ServiceName -- ^ service name to look up + -> IO (t AddrInfo) -- ^ resolved addresses, with "best" first + +instance GetAddrInfo [] where + getAddrInfo = getAddrInfoList + +instance GetAddrInfo NE.NonEmpty where + getAddrInfo = getAddrInfoNE + ----------------------------------------------------------------------------- -- | Resolve a host or service name to one or more addresses. -- The 'AddrInfo' values that this function returns contain 'SockAddr' @@ -242,12 +255,12 @@ defaultHints = AddrInfo { -- >>> addrAddress addr -- 127.0.0.1:80 -getAddrInfo +getAddrInfoList :: Maybe AddrInfo -- ^ preferred socket type or protocol -> Maybe HostName -- ^ host name to look up -> Maybe ServiceName -- ^ service name to look up -> IO [AddrInfo] -- ^ resolved addresses, with "best" first -getAddrInfo hints node service = alloc getaddrinfo +getAddrInfoList hints node service = alloc getaddrinfo where alloc body = withSocketsDo $ maybeWith withCString node $ \c_node -> maybeWith withCString service $ \c_service -> diff --git a/examples/EchoClient.hs b/examples/EchoClient.hs index a9e63568..ec72bc7e 100644 --- a/examples/EchoClient.hs +++ b/examples/EchoClient.hs @@ -24,7 +24,7 @@ runTCPClient host port client = withSocketsDo $ do where resolve = do let hints = defaultHints{addrSocketType = Stream} - NE.head <$> getAddrInfoNE (Just hints) (Just host) (Just port) + NE.head <$> getAddrInfo (Just hints) (Just host) (Just port) open addr = E.bracketOnError (openSocket addr) close $ \sock -> do connect sock $ addrAddress addr return sock diff --git a/examples/EchoServer.hs b/examples/EchoServer.hs index 9b242ae5..e760b46f 100644 --- a/examples/EchoServer.hs +++ b/examples/EchoServer.hs @@ -30,7 +30,7 @@ runTCPServer mhost port server = withSocketsDo $ do { addrFlags = [AI_PASSIVE] , addrSocketType = Stream } - NE.head <$> getAddrInfoNE (Just hints) mhost (Just port) + NE.head <$> getAddrInfo (Just hints) mhost (Just port) open addr = E.bracketOnError (openSocket addr) close $ \sock -> do setSocketOption sock ReuseAddr 1 withFdSocket sock setCloseOnExecIfNeeded