Skip to content

Commit

Permalink
making getAddrInfo polymorphic
Browse files Browse the repository at this point in the history
  • Loading branch information
kazu-yamamoto committed Aug 30, 2024
1 parent 8c18f94 commit d97f57d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
7 changes: 3 additions & 4 deletions Network/Socket.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -112,8 +112,7 @@ module Network.Socket (
withSocketsDo,

-- * Address information
getAddrInfo,
getAddrInfoNE,
GetAddrInfo (..),

-- ** Types
HostName,
Expand Down
17 changes: 15 additions & 2 deletions Network/Socket/Info.hsc
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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 ->
Expand Down
2 changes: 1 addition & 1 deletion examples/EchoClient.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion examples/EchoServer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d97f57d

Please sign in to comment.