on master help sign in
Move Network.Client to Network.Inet.TCP
commit31c17886bf69c1db46ff6256bdd0d57a4ae984c2 authorHarendra Kumar <harendra.kumar@gmail.com> dateMon 14 Oct 2019, 13:19 (7 years ago) parent49d3698
8 files changed+156−241
examples/EchoServer.hs+1−1 examples/FileSinkServer.hs+1−1 examples/FromFileClient.hs+2−2 src/Streamly/Internal/Network/Client.hs → src/Streamly/Internal/Network/Inet/TCP.hs+146−15 src/Streamly/Internal/Network/Server.hs−164 src/Streamly/Network/Client.hs−50 src/Streamly/Network/Socket/Inet/TCP.hs → src/Streamly/Network/Inet/TCP.hs+4−4 streamly.cabal+2−4
examples/EchoServer.hs+1−1
@@ -5,7 +5,7 @@ import Control.Monad.IO.Class (liftIO)
55import Streamly
66import Streamly.Network.Socket
77import qualified Network.Socket as Net
8−import qualified Streamly.Network.Socket.Inet.TCP as TCP
8+import qualified Streamly.Network.Inet.TCP as TCP
99import qualified Streamly.Prelude as S
1010
1111main :: IO ()
examples/FileSinkServer.hs+1−1
@@ -14,7 +14,7 @@ import Streamly.Data.String
1414import qualified Streamly.FileSystem.Handle as FH
1515import qualified Streamly.Memory.Array as A
1616import qualified Streamly.Network.Socket as NS
17−import qualified Streamly.Network.Socket.Inet.TCP as TCP
17+import qualified Streamly.Network.Inet.TCP as TCP
1818import qualified Streamly.Prelude as S
1919
2020import System.IO (withFile, IOMode(..))
examples/FromFileClient.hs+2−2
@@ -8,7 +8,7 @@ import System.Environment (getArgs)
88import Streamly
99import qualified Streamly.Prelude as S
1010import qualified Streamly.Internal.FileSystem.Handle as IFH
11−import qualified Streamly.Network.Client as Client
11+import qualified Streamly.Internal.Network.Inet.TCP as TCP
1212
1313import System.IO (withFile, IOMode(..))
1414
@@ -16,6 +16,6 @@ main :: IO ()
1616main =
1717 let sendFile file =
1818 withFile file ReadMode $ \src ->
19− S.fold (Client.writeArrays (127, 0, 0, 1) 8090)
19+ S.fold (TCP.writeArrays (127, 0, 0, 1) 8090)
2020 $ IFH.toStreamArrays src
2121 in getArgs >>= S.drain . parallely . S.mapM sendFile . S.fromList
src/Streamly/Internal/Network/Client.hs → src/Streamly/Internal/Network/Inet/TCP.hsrenamed+146−15
@@ -8,7 +8,7 @@
88#include "inline.hs"
99
1010-- |
11−-- Module : Streamly.Internal.Network.Client
11+-- Module : Streamly.Internal.Network.Inet.TCP
1212-- Copyright : (c) 2019 Composewell Technologies
1313--
1414-- License : BSD3
@@ -16,18 +16,28 @@
1616-- Stability : experimental
1717-- Portability : GHC
1818--
19−-- Combinators to build network clients.
20−--
21−-- > import qualified Streamly.Network.Socket as Client
22−--
19+-- Combinators to build Inet/TCP clients and servers.
2320
24−module Streamly.Internal.Network.Client
21+module Streamly.Internal.Network.Inet.TCP
2522 (
26− -- * Interact
23+ -- * TCP Servers
24+ -- ** Connected sockets
25+ -- *** Unfolds
26+ listenOnAddr
27+ , listenOnPort
28+ , listenOnPortLocal
29+
30+ -- *** Streams
31+ , connections
32+ , connectionsOnAddr
33+ , connectionsOnAllAddrs
34+ , connectionsOnLocalHost
35+
36+ -- * TCP clients
2737 -- | Socket based reads and writes.
28− withConnection
38+ , withConnection
2939
30− -- * Source
40+ -- ** Source
3141 , read
3242 -- , readUtf8
3343 -- , readLines
@@ -42,7 +52,7 @@ module Streamly.Internal.Network.Client
4252 -- , readArraysOf
4353 -- , readArrays
4454
45− -- * Sink
55+ -- ** Sink
4656 , write
4757 -- , writeUtf8
4858 -- , writeUtf8ByLines
@@ -52,6 +62,27 @@ module Streamly.Internal.Network.Client
5262 -- -- * Array Write
5363 -- , writeArray
5464 , writeArrays
65+ {-
66+ -- ** Sink Servers
67+
68+ -- These abstractions can be applied to any setting where we need to do a
69+ -- sink processing of multiple streams e.g. output from multiple processes
70+ -- or data coming from multiple files.
71+
72+ -- handle connections concurrently using a specified fold
73+ -- , handleConnections
74+
75+ -- handle frames concurrently using a specified fold
76+ , handleFrames
77+
78+ -- merge frames from all connection into a single stream. Frames can be
79+ -- created by a specified fold.
80+ , mergeFrames
81+
82+ -- * UDP Servers
83+ , datagrams
84+ , datagramsOn
85+ -}
5586 )
5687where
5788
@@ -59,24 +90,124 @@ import Control.Monad.Catch (MonadCatch)
5990import Control.Monad.IO.Class (MonadIO(..))
6091import Data.Word (Word8)
6192import Network.Socket
62− (Socket, PortNumber, Family(..), SockAddr(..), SocketType(..),
63− socket, connect, defaultProtocol)
93+ (Socket, PortNumber, SocketOption(..), Family(..), SockAddr(..),
94+ SocketType(..), defaultProtocol, maxListenQueue, tupleToHostAddress,
95+ socket, connect)
6496import Prelude hiding (read)
6597
66−import qualified Network.Socket as Net
67−
6898import Streamly (MonadAsync)
6999import Streamly.Internal.Data.Fold.Types (Fold(..))
100+import Streamly.Internal.Data.Unfold.Types (Unfold(..))
101+import Streamly.Internal.Network.Socket (SockSpec(..), listen, connections)
102+import Streamly.Streams.Serial (SerialT)
70103import Streamly.Internal.Memory.Array.Types (Array(..), defaultChunkSize, writeNUnsafe)
71−-- import Streamly.Streams.Serial (SerialT)
72104import Streamly.Streams.StreamK.Type (IsStream)
73105
106+import qualified Network.Socket as Net
107+import qualified Streamly.Internal.Data.Unfold as UF
74108import qualified Streamly.Internal.Memory.ArrayStream as AS
75109import qualified Streamly.Internal.Data.Fold.Types as FL
76110import qualified Streamly.Prelude as S
77111import qualified Streamly.Network.Socket as SK
78112import qualified Streamly.Internal.Network.Socket as ISK
79113
114+-------------------------------------------------------------------------------
115+-- TCP Servers
116+-------------------------------------------------------------------------------
117+
118+-- | Unfold a tuple @(ipAddr, port)@ into a stream of connected TCP sockets.
119+-- @ipAddr@ is the local IP address and @port@ is the local port on which
120+-- connections are accepted.
121+--
122+-- @since 0.7.0
123+{-# INLINE listenOnAddr #-}
124+listenOnAddr
125+ :: MonadIO m
126+ => Unfold m ((Word8, Word8, Word8, Word8), PortNumber) Socket
127+listenOnAddr = UF.lmap f listen
128+ where
129+ f (addr, port) =
130+ (maxListenQueue
131+ , SockSpec
132+ { sockFamily = AF_INET
133+ , sockType = Stream
134+ , sockProto = defaultProtocol -- TCP
135+ , sockOpts = [(NoDelay,1), (ReuseAddr,1)]
136+ }
137+ , SockAddrInet port (tupleToHostAddress addr)
138+ )
139+
140+-- | Like 'listenOnAddr' but binds on the IPv4 address @0.0.0.0@ i.e. on all
141+-- IPv4 addresses/interfaces of the machine and listens for TCP connections on
142+-- the specified port.
143+--
144+-- > listenOnPort = UF.supplyFirst listenOnAddr (0,0,0,0)
145+--
146+-- @since 0.7.0
147+{-# INLINE listenOnPort #-}
148+listenOnPort :: MonadIO m => Unfold m PortNumber Socket
149+listenOnPort = UF.supplyFirst listenOnAddr (0,0,0,0)
150+
151+-- | Like 'listenOnAddr' but binds on the localhost IPv4 address @127.0.0.1@.
152+-- The server can only be accessed from the local host, it cannot be accessed
153+-- from other hosts on the network.
154+--
155+-- > listenOnPortLocal = UF.supplyFirst listenOnAddr (127,0,0,1)
156+--
157+-- @since 0.7.0
158+{-# INLINE listenOnPortLocal #-}
159+listenOnPortLocal :: MonadIO m => Unfold m PortNumber Socket
160+listenOnPortLocal = UF.supplyFirst listenOnAddr (127,0,0,1)
161+
162+-------------------------------------------------------------------------------
163+-- Listen
164+-------------------------------------------------------------------------------
165+
166+-- | Like 'connections' but binds on the specified IPv4 address of the machine
167+-- and listens for TCP connections on the specified port.
168+--
169+-- /Internal/
170+{-# INLINE connectionsOnAddr #-}
171+connectionsOnAddr
172+ :: MonadAsync m
173+ => (Word8, Word8, Word8, Word8)
174+ -> PortNumber
175+ -> SerialT m Socket
176+connectionsOnAddr addr port =
177+ connections maxListenQueue SockSpec
178+ { sockFamily = AF_INET
179+ , sockType = Stream
180+ , sockProto = defaultProtocol
181+ , sockOpts = [(NoDelay,1), (ReuseAddr,1)]
182+ }
183+ (SockAddrInet port (tupleToHostAddress addr))
184+
185+-- | Like 'connections' but binds on the IPv4 address @0.0.0.0@ i.e. on all
186+-- IPv4 addresses/interfaces of the machine and listens for TCP connections on
187+-- the specified port.
188+--
189+-- > connectionsOnAllAddrs = connectionsOnAddr (0,0,0,0)
190+--
191+-- /Internal/
192+{-# INLINE connectionsOnAllAddrs #-}
193+connectionsOnAllAddrs :: MonadAsync m => PortNumber -> SerialT m Socket
194+connectionsOnAllAddrs = connectionsOnAddr (0,0,0,0)
195+
196+-- | Like 'connections' but binds on the localhost IPv4 address @127.0.0.1@.
197+-- The server can only be accessed from the local host, it cannot be accessed
198+-- from other hosts on the network.
199+--
200+-- > connectionsOnLocalHost = connectionsOnAddr (127,0,0,1)
201+--
202+-- /Internal/
203+{-# INLINE connectionsOnLocalHost #-}
204+connectionsOnLocalHost :: MonadAsync m => PortNumber -> SerialT m Socket
205+connectionsOnLocalHost = connectionsOnAddr (127,0,0,1)
206+
207+-------------------------------------------------------------------------------
208+-- TCP Clients
209+-------------------------------------------------------------------------------
210+
80211-------------------------------------------------------------------------------
81212-- Connect
82213-------------------------------------------------------------------------------
src/Streamly/Internal/Network/Server.hsdeleted−164
@@ -1,164 +0,0 @@
1−{-# LANGUAGE CPP #-}
2−{-# LANGUAGE BangPatterns #-}
3−{-# LANGUAGE FlexibleContexts #-}
4−{-# LANGUAGE MagicHash #-}
5−{-# LANGUAGE RecordWildCards #-}
6−{-# LANGUAGE UnboxedTuples #-}
7−
8−#include "inline.hs"
9−
10−-- |
11−-- Module : Streamly.Internal.Network.Server
12−-- Copyright : (c) 2019 Composewell Technologies
13−--
14−-- License : BSD3
15−-- Maintainer : streamly@composewell.com
16−-- Stability : experimental
17−-- Portability : GHC
18−--
19−-- Combinators to build network servers.
20−--
21−-- > import qualified Streamly.Internal.Network.Server as Server
22−--
23−
24−module Streamly.Internal.Network.Server
25− (
26− -- * TCP Servers
27− -- ** Connected sockets
28− -- *** Unfolds
29− listenOnAddr
30− , listenOnPort
31− , listenOnPortLocal
32−
33− -- *** Streams
34− , connections
35− , connectionsOnAddr
36− , connectionsOnAllAddrs
37− , connectionsOnLocalHost
38−
39− {-
40− -- ** Sink Servers
41−
42− -- These abstractions can be applied to any setting where we need to do a
43− -- sink processing of multiple streams e.g. output from multiple processes
44− -- or data coming from multiple files.
45−
46− -- handle connections concurrently using a specified fold
47− -- , handleConnections
48−
49− -- handle frames concurrently using a specified fold
50− , handleFrames
51−
52− -- merge frames from all connection into a single stream. Frames can be
53− -- created by a specified fold.
54− , mergeFrames
55−
56− -- * UDP Servers
57− , datagrams
58− , datagramsOn
59− -}
60− )
61−where
62−
63−import Control.Monad.IO.Class (MonadIO(..))
64−import Data.Word (Word8)
65−import Network.Socket
66− (Socket, PortNumber, SocketOption(..), Family(..), SockAddr(..),
67− SocketType(..), defaultProtocol, maxListenQueue, tupleToHostAddress)
68−import Prelude hiding (read)
69−
70−import Streamly (MonadAsync)
71−import Streamly.Internal.Data.Unfold.Types (Unfold(..))
72−import Streamly.Internal.Network.Socket (SockSpec(..), listen, connections)
73−import Streamly.Streams.Serial (SerialT)
74−
75−import qualified Streamly.Internal.Data.Unfold as UF
76−
77−-- | Unfold a tuple @(ipAddr, port)@ into a stream of connected TCP sockets.
78−-- @ipAddr@ is the local IP address and @port@ is the local port on which
79−-- connections are accepted.
80−--
81−-- @since 0.7.0
82−{-# INLINE listenOnAddr #-}
83−listenOnAddr
84− :: MonadIO m
85− => Unfold m ((Word8, Word8, Word8, Word8), PortNumber) Socket
86−listenOnAddr = UF.lmap f listen
87− where
88− f (addr, port) =
89− (maxListenQueue
90− , SockSpec
91− { sockFamily = AF_INET
92− , sockType = Stream
93− , sockProto = defaultProtocol -- TCP
94− , sockOpts = [(NoDelay,1), (ReuseAddr,1)]
95− }
96− , SockAddrInet port (tupleToHostAddress addr)
97− )
98−
99−-- | Like 'listenOnAddr' but binds on the IPv4 address @0.0.0.0@ i.e. on all
100−-- IPv4 addresses/interfaces of the machine and listens for TCP connections on
101−-- the specified port.
102−--
103−-- > listenOnPort = UF.supplyFirst listenOnAddr (0,0,0,0)
104−--
105−-- @since 0.7.0
106−{-# INLINE listenOnPort #-}
107−listenOnPort :: MonadIO m => Unfold m PortNumber Socket
108−listenOnPort = UF.supplyFirst listenOnAddr (0,0,0,0)
109−
110−-- | Like 'listenOnAddr' but binds on the localhost IPv4 address @127.0.0.1@.
111−-- The server can only be accessed from the local host, it cannot be accessed
112−-- from other hosts on the network.
113−--
114−-- > listenOnPortLocal = UF.supplyFirst listenOnAddr (127,0,0,1)
115−--
116−-- @since 0.7.0
117−{-# INLINE listenOnPortLocal #-}
118−listenOnPortLocal :: MonadIO m => Unfold m PortNumber Socket
119−listenOnPortLocal = UF.supplyFirst listenOnAddr (127,0,0,1)
120−
121−-------------------------------------------------------------------------------
122−-- Listen
123−-------------------------------------------------------------------------------
124−
125−-- | Like 'connections' but binds on the specified IPv4 address of the machine
126−-- and listens for TCP connections on the specified port.
127−--
128−-- /Internal/
129−{-# INLINE connectionsOnAddr #-}
130−connectionsOnAddr
131− :: MonadAsync m
132− => (Word8, Word8, Word8, Word8)
133− -> PortNumber
134− -> SerialT m Socket
135−connectionsOnAddr addr port =
136− connections maxListenQueue SockSpec
137− { sockFamily = AF_INET
138− , sockType = Stream
139− , sockProto = defaultProtocol
140− , sockOpts = [(NoDelay,1), (ReuseAddr,1)]
141− }
142− (SockAddrInet port (tupleToHostAddress addr))
143−
144−-- | Like 'connections' but binds on the IPv4 address @0.0.0.0@ i.e. on all
145−-- IPv4 addresses/interfaces of the machine and listens for TCP connections on
146−-- the specified port.
147−--
148−-- > connectionsOnAllAddrs = connectionsOnAddr (0,0,0,0)
149−--
150−-- /Internal/
151−{-# INLINE connectionsOnAllAddrs #-}
152−connectionsOnAllAddrs :: MonadAsync m => PortNumber -> SerialT m Socket
153−connectionsOnAllAddrs = connectionsOnAddr (0,0,0,0)
154−
155−-- | Like 'connections' but binds on the localhost IPv4 address @127.0.0.1@.
156−-- The server can only be accessed from the local host, it cannot be accessed
157−-- from other hosts on the network.
158−--
159−-- > connectionsOnLocalHost = connectionsOnAddr (127,0,0,1)
160−--
161−-- /Internal/
162−{-# INLINE connectionsOnLocalHost #-}
163−connectionsOnLocalHost :: MonadAsync m => PortNumber -> SerialT m Socket
164−connectionsOnLocalHost = connectionsOnAddr (127,0,0,1)
src/Streamly/Network/Client.hsdeleted−50
@@ -1,50 +0,0 @@
1−-- |
2−-- Module : Streamly.Network.Client
3−-- Copyright : (c) 2019 Composewell Technologies
4−--
5−-- License : BSD3
6−-- Maintainer : streamly@composewell.com
7−-- Stability : experimental
8−-- Portability : GHC
9−--
10−-- Combinators to build network clients.
11−--
12−-- > import qualified Streamly.Network.Socket as Client
13−--
14−
15−module Streamly.Network.Client
16− (
17− -- * Interact
18− -- | Socket based reads and writes.
19− withConnection
20−
21− -- * Source
22− , read
23− -- , readUtf8
24− -- , readLines
25− -- , readFrames
26− -- , readByChunks
27−
28− -- -- * Array Read
29− -- , readArrayUpto
30− -- , readArrayOf
31−
32− -- , readArraysUpto
33− -- , readArraysOf
34− -- , readArrays
35−
36− -- * Sink
37− , write
38− -- , writeUtf8
39− -- , writeUtf8ByLines
40− -- , writeByFrames
41− -- , writeInChunksOf
42−
43− -- -- * Array Write
44− -- , writeArray
45− , writeArrays
46− )
47−where
48−
49−import Streamly.Internal.Network.Client
50−import Prelude hiding (read)
src/Streamly/Network/Socket/Inet/TCP.hs → src/Streamly/Network/Inet/TCP.hsrenamed+4−4
@@ -7,12 +7,12 @@
77-- Stability : experimental
88-- Portability : GHC
99--
10−-- Combinators to build TCP servers.
10+-- Combinators to build Inet/TCP clients and servers.
1111--
12−-- > import qualified Streamly.Network.Server as Server
12+-- > import qualified Streamly.Network.Inet.TCP as TCP
1313--
1414
15−module Streamly.Network.Socket.Inet.TCP
15+module Streamly.Network.Inet.TCP
1616 (
1717 -- * Accept Connections
1818 listenOnAddr
@@ -43,4 +43,4 @@ module Streamly.Network.Socket.Inet.TCP
4343 )
4444where
4545
46−import Streamly.Internal.Network.Server
46+import Streamly.Internal.Network.Inet.TCP
streamly.cabal+2−4
@@ -316,12 +316,10 @@ library
316316 if !impl(ghcjs)
317317 exposed-modules:
318318 Streamly.Network.Socket
319− , Streamly.Network.Socket.Inet.TCP
320− , Streamly.Network.Client
319+ , Streamly.Network.Inet.TCP
321320
322321 , Streamly.Internal.Network.Socket
323− , Streamly.Internal.Network.Server
324− , Streamly.Internal.Network.Client
322+ , Streamly.Internal.Network.Inet.TCP
325323
326324 if flag(benchmark)
327325 exposed-modules: