Skip to content

Commit 05afe85

Browse files
complyuekazu-yamamoto
authored andcommitted
use openSocket in echo examples
1 parent fe3a66c commit 05afe85

File tree

3 files changed

+18
-26
lines changed

3 files changed

+18
-26
lines changed

Network/Socket.hs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,12 @@
5252
-- > , addrSocketType = Stream
5353
-- > }
5454
-- > head <$> getAddrInfo (Just hints) mhost (Just port)
55-
-- > open addr = E.bracketOnError
56-
-- > (socket (addrFamily addr) (addrSocketType addr) (addrProtocol addr))
57-
-- > close $ \sock -> do
58-
-- > setSocketOption sock ReuseAddr 1
59-
-- > withFdSocket sock setCloseOnExecIfNeeded
60-
-- > bind sock $ addrAddress addr
61-
-- > listen sock 1024
62-
-- > return sock
55+
-- > open addr = E.bracketOnError (openSocket addr) close $ \sock -> do
56+
-- > setSocketOption sock ReuseAddr 1
57+
-- > withFdSocket sock setCloseOnExecIfNeeded
58+
-- > bind sock $ addrAddress addr
59+
-- > listen sock 1024
60+
-- > return sock
6361
-- > loop sock = forever $ E.bracketOnError (accept sock) (close . fst)
6462
-- > $ \(conn, _peer) -> void $
6563
-- > -- 'forkFinally' alone is unlikely to fail thus leaking @conn@,
@@ -93,11 +91,9 @@
9391
-- > resolve = do
9492
-- > let hints = defaultHints { addrSocketType = Stream }
9593
-- > head <$> getAddrInfo (Just hints) (Just host) (Just port)
96-
-- > open addr = E.bracketOnError
97-
-- > (socket (addrFamily addr) (addrSocketType addr) (addrProtocol addr))
98-
-- > close $ \sock -> do
99-
-- > connect sock $ addrAddress addr
100-
-- > return sock
94+
-- > open addr = E.bracketOnError (openSocket addr) close $ \sock -> do
95+
-- > connect sock $ addrAddress addr
96+
-- > return sock
10197
--
10298
-- The proper programming model is that one 'Socket' is handled by
10399
-- a single thread. If multiple threads use one 'Socket' concurrently,

examples/EchoClient.hs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ runTCPClient host port client = withSocketsDo $ do
2323
resolve = do
2424
let hints = defaultHints { addrSocketType = Stream }
2525
head <$> getAddrInfo (Just hints) (Just host) (Just port)
26-
open addr = E.bracketOnError
27-
(socket (addrFamily addr) (addrSocketType addr) (addrProtocol addr))
28-
close $ \sock -> do
29-
connect sock $ addrAddress addr
30-
return sock
26+
open addr = E.bracketOnError (openSocket addr) close $ \sock -> do
27+
connect sock $ addrAddress addr
28+
return sock

examples/EchoServer.hs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,12 @@ runTCPServer mhost port server = withSocketsDo $ do
2929
, addrSocketType = Stream
3030
}
3131
head <$> getAddrInfo (Just hints) mhost (Just port)
32-
open addr = E.bracketOnError
33-
(socket (addrFamily addr) (addrSocketType addr) (addrProtocol addr))
34-
close $ \sock -> do
35-
setSocketOption sock ReuseAddr 1
36-
withFdSocket sock setCloseOnExecIfNeeded
37-
bind sock $ addrAddress addr
38-
listen sock 1024
39-
return sock
32+
open addr = E.bracketOnError (openSocket addr) close $ \sock -> do
33+
setSocketOption sock ReuseAddr 1
34+
withFdSocket sock setCloseOnExecIfNeeded
35+
bind sock $ addrAddress addr
36+
listen sock 1024
37+
return sock
4038
loop sock = forever $ E.bracketOnError (accept sock) (close . fst)
4139
$ \(conn, _peer) -> void $
4240
-- 'forkFinally' alone is unlikely to fail thus leaking @conn@,

0 commit comments

Comments
 (0)