Skip to content

Commit 8c18f94

Browse files
committed
providing getAddrInfoNE
1 parent a521e19 commit 8c18f94

File tree

5 files changed

+22
-6
lines changed

5 files changed

+22
-6
lines changed

Network/Socket.hs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
-- > import qualified Control.Exception as E
3434
-- > import Control.Monad (unless, forever, void)
3535
-- > import qualified Data.ByteString as S
36+
-- > import qualified Data.List.NonEmpty as NE
3637
-- > import Network.Socket
3738
-- > import Network.Socket.ByteString (recv, sendAll)
3839
-- >
@@ -56,7 +57,7 @@
5657
-- > addrFlags = [AI_PASSIVE]
5758
-- > , addrSocketType = Stream
5859
-- > }
59-
-- > head <$> getAddrInfo (Just hints) mhost (Just port)
60+
-- > NE.head <$> getAddrInfoNE (Just hints) mhost (Just port)
6061
-- > open addr = E.bracketOnError (openSocket addr) close $ \sock -> do
6162
-- > setSocketOption sock ReuseAddr 1
6263
-- > withFdSocket sock setCloseOnExecIfNeeded
@@ -77,6 +78,7 @@
7778
-- >
7879
-- > import qualified Control.Exception as E
7980
-- > import qualified Data.ByteString.Char8 as C
81+
-- > import qualified Data.List.NonEmpty as NE
8082
-- > import Network.Socket
8183
-- > import Network.Socket.ByteString (recv, sendAll)
8284
-- >
@@ -95,7 +97,7 @@
9597
-- > where
9698
-- > resolve = do
9799
-- > let hints = defaultHints { addrSocketType = Stream }
98-
-- > head <$> getAddrInfo (Just hints) (Just host) (Just port)
100+
-- > NE.head <$> getAddrInfoNE (Just hints) (Just host) (Just port)
99101
-- > open addr = E.bracketOnError (openSocket addr) close $ \sock -> do
100102
-- > connect sock $ addrAddress addr
101103
-- > return sock
@@ -111,6 +113,7 @@ module Network.Socket (
111113

112114
-- * Address information
113115
getAddrInfo,
116+
getAddrInfoNE,
114117

115118
-- ** Types
116119
HostName,

Network/Socket/Info.hsc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
module Network.Socket.Info where
99

10+
import qualified Data.List.NonEmpty as NE
1011
import Foreign.Marshal.Alloc (alloca, allocaBytes)
1112
import Foreign.Marshal.Utils (maybeWith, with)
1213
import GHC.IO.Exception (IOErrorType(NoSuchThing))
@@ -290,6 +291,15 @@ getAddrInfo hints node service = alloc getaddrinfo
290291
filteredHints = hints
291292
#endif
292293

294+
getAddrInfoNE
295+
:: Maybe AddrInfo
296+
-> Maybe HostName
297+
-> Maybe ServiceName
298+
-> IO (NE.NonEmpty AddrInfo)
299+
getAddrInfoNE hints node service =
300+
-- getAddrInfo never returns an empty list.
301+
NE.fromList <$> getAddrInfo hints node service
302+
293303
followAddrInfo :: Ptr AddrInfo -> IO [AddrInfo]
294304
followAddrInfo ptr_ai
295305
| ptr_ai == nullPtr = return []

examples/EchoClient.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module Main (main) where
55

66
import qualified Control.Exception as E
77
import qualified Data.ByteString.Char8 as C
8+
import qualified Data.List.NonEmpty as NE
89
import Network.Socket
910
import Network.Socket.ByteString (recv, sendAll)
1011

@@ -23,7 +24,7 @@ runTCPClient host port client = withSocketsDo $ do
2324
where
2425
resolve = do
2526
let hints = defaultHints{addrSocketType = Stream}
26-
head <$> getAddrInfo (Just hints) (Just host) (Just port)
27+
NE.head <$> getAddrInfoNE (Just hints) (Just host) (Just port)
2728
open addr = E.bracketOnError (openSocket addr) close $ \sock -> do
2829
connect sock $ addrAddress addr
2930
return sock

examples/EchoServer.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Control.Concurrent (forkFinally)
55
import qualified Control.Exception as E
66
import Control.Monad (forever, unless, void)
77
import qualified Data.ByteString as S
8+
import qualified Data.List.NonEmpty as NE
89
import Network.Socket
910
import Network.Socket.ByteString (recv, sendAll)
1011

@@ -29,7 +30,7 @@ runTCPServer mhost port server = withSocketsDo $ do
2930
{ addrFlags = [AI_PASSIVE]
3031
, addrSocketType = Stream
3132
}
32-
head <$> getAddrInfo (Just hints) mhost (Just port)
33+
NE.head <$> getAddrInfoNE (Just hints) mhost (Just port)
3334
open addr = E.bracketOnError (openSocket addr) close $ \sock -> do
3435
setSocketOption sock ReuseAddr 1
3536
withFdSocket sock setCloseOnExecIfNeeded

tests/Network/Test/Common.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import qualified Control.Exception as E
3535
import Control.Monad
3636
import Data.ByteString (ByteString)
3737
import qualified Data.ByteString.Lazy as L
38+
import qualified Data.List.NonEmpty as NE
3839
import Network.Socket
3940
import System.Directory
4041
import System.Timeout (timeout)
@@ -244,7 +245,7 @@ bracketWithReraise tid setup teardown thing =
244245

245246
resolveClient :: SocketType -> HostName -> PortNumber -> IO AddrInfo
246247
resolveClient socketType host port =
247-
head <$> getAddrInfo (Just hints) (Just host) (Just $ show port)
248+
NE.head <$> getAddrInfoNE (Just hints) (Just host) (Just $ show port)
248249
where
249250
hints = defaultHints {
250251
addrSocketType = socketType
@@ -253,7 +254,7 @@ resolveClient socketType host port =
253254

254255
resolveServer :: SocketType -> HostName -> IO AddrInfo
255256
resolveServer socketType host =
256-
head <$> getAddrInfo (Just hints) (Just host) Nothing
257+
NE.head <$> getAddrInfoNE (Just hints) (Just host) Nothing
257258
where
258259
hints = defaultHints {
259260
addrSocketType = socketType

0 commit comments

Comments
 (0)