on master help sign in
Move Network.Client to Network.Inet.TCP
31c1788Harendra Kumar
examples/EchoServer.hs+1−1
@@ -5,7 +5,7 @@ import Control.Monad.IO.Class (liftIO)
5import Streamly
5import Streamly
6import Streamly.Network.Socket
6import Streamly.Network.Socket
7import qualified Network.Socket as Net
7import qualified Network.Socket as Net
8−import qualified Streamly.Network.Socket.Inet.TCP as TCP
8+import qualified Streamly.Network.Inet.TCP as TCP
9import qualified Streamly.Prelude as S
9import qualified Streamly.Prelude as S
10
10
11main :: IO ()
11main :: IO ()
examples/FileSinkServer.hs+1−1
@@ -14,7 +14,7 @@ import Streamly.Data.String
14import qualified Streamly.FileSystem.Handle as FH
14import qualified Streamly.FileSystem.Handle as FH
15import qualified Streamly.Memory.Array as A
15import qualified Streamly.Memory.Array as A
16import qualified Streamly.Network.Socket as NS
16import 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
18import qualified Streamly.Prelude as S
18import qualified Streamly.Prelude as S
19
19
20import System.IO (withFile, IOMode(..))
20import System.IO (withFile, IOMode(..))
examples/FromFileClient.hs+2−2
@@ -8,7 +8,7 @@ import System.Environment (getArgs)
8import Streamly
8import Streamly
9import qualified Streamly.Prelude as S
9import qualified Streamly.Prelude as S
10import qualified Streamly.Internal.FileSystem.Handle as IFH
10import 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
12
12
13import System.IO (withFile, IOMode(..))
13import System.IO (withFile, IOMode(..))
14
14
@@ -16,6 +16,6 @@ main :: IO ()
16main =
16main =
17 let sendFile file =
17 let sendFile file =
18 withFile file ReadMode $ \src ->
18 withFile file ReadMode $ \src ->
19− S.fold (Client.writeArrays (127, 0, 0, 1) 8090)
19+ S.fold (TCP.writeArrays (127, 0, 0, 1) 8090)
20 $ IFH.toStreamArrays src
20 $ IFH.toStreamArrays src
21 in getArgs >>= S.drain . parallely . S.mapM sendFile . S.fromList
21 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 @@
8#include "inline.hs"
8#include "inline.hs"
9
9
10-- |
10-- |
11−-- Module : Streamly.Internal.Network.Client
11+-- Module : Streamly.Internal.Network.Inet.TCP
12-- Copyright : (c) 2019 Composewell Technologies
12-- Copyright : (c) 2019 Composewell Technologies
13--
13--
14-- License : BSD3
14-- License : BSD3
@@ -16,18 +16,28 @@
16-- Stability : experimental
16-- Stability : experimental
17-- Portability : GHC
17-- Portability : GHC
18--
18--
19−-- Combinators to build network clients.
19+-- Combinators to build Inet/TCP clients and servers.
20−--
21−-- > import qualified Streamly.Network.Socket as Client
22−--
23
20
24−module Streamly.Internal.Network.Client
21+module Streamly.Internal.Network.Inet.TCP
25 (
22 (
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
27 -- | Socket based reads and writes.
37 -- | Socket based reads and writes.
28− withConnection
38+ , withConnection
29
39
30− -- * Source
40+ -- ** Source
31 , read
41 , read
32 -- , readUtf8
42 -- , readUtf8
33 -- , readLines
43 -- , readLines
@@ -42,7 +52,7 @@ module Streamly.Internal.Network.Client
42 -- , readArraysOf
52 -- , readArraysOf
43 -- , readArrays
53 -- , readArrays
44
54
45− -- * Sink
55+ -- ** Sink
46 , write
56 , write
47 -- , writeUtf8
57 -- , writeUtf8
48 -- , writeUtf8ByLines
58 -- , writeUtf8ByLines
@@ -52,6 +62,27 @@ module Streamly.Internal.Network.Client
52 -- -- * Array Write
62 -- -- * Array Write
53 -- , writeArray
63 -- , writeArray
54 , writeArrays
64 , 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+ -}
55 )
86 )
56where
87where
57
88
@@ -59,24 +90,124 @@ import Control.Monad.Catch (MonadCatch)
59import Control.Monad.IO.Class (MonadIO(..))
90import Control.Monad.IO.Class (MonadIO(..))
60import Data.Word (Word8)
91import Data.Word (Word8)
61import Network.Socket
92import Network.Socket
62− (Socket, PortNumber, Family(..), SockAddr(..), SocketType(..),
93+ (Socket, PortNumber, SocketOption(..), Family(..), SockAddr(..),
94+ SocketType(..), defaultProtocol, maxListenQueue, tupleToHostAddress,
63− socket, connect, defaultProtocol)
95+ socket, connect)
64import Prelude hiding (read)
96import Prelude hiding (read)
65
97
66−import qualified Network.Socket as Net
67−
68import Streamly (MonadAsync)
98import Streamly (MonadAsync)
69import Streamly.Internal.Data.Fold.Types (Fold(..))
99import 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)
70import Streamly.Internal.Memory.Array.Types (Array(..), defaultChunkSize, writeNUnsafe)
103import Streamly.Internal.Memory.Array.Types (Array(..), defaultChunkSize, writeNUnsafe)
71−-- import Streamly.Streams.Serial (SerialT)
72import Streamly.Streams.StreamK.Type (IsStream)
104import Streamly.Streams.StreamK.Type (IsStream)
73
105
106+import qualified Network.Socket as Net
107+import qualified Streamly.Internal.Data.Unfold as UF
74import qualified Streamly.Internal.Memory.ArrayStream as AS
108import qualified Streamly.Internal.Memory.ArrayStream as AS
75import qualified Streamly.Internal.Data.Fold.Types as FL
109import qualified Streamly.Internal.Data.Fold.Types as FL
76import qualified Streamly.Prelude as S
110import qualified Streamly.Prelude as S
77import qualified Streamly.Network.Socket as SK
111import qualified Streamly.Network.Socket as SK
78import qualified Streamly.Internal.Network.Socket as ISK
112import qualified Streamly.Internal.Network.Socket as ISK
79
113
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+
80-------------------------------------------------------------------------------
211-------------------------------------------------------------------------------
81-- Connect
212-- Connect
82-------------------------------------------------------------------------------
213-------------------------------------------------------------------------------
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 @@
7-- Stability : experimental
7-- Stability : experimental
8-- Portability : GHC
8-- Portability : GHC
9--
9--
10−-- Combinators to build TCP servers.
10+-- Combinators to build Inet/TCP clients and servers.
11--
11--
12−-- > import qualified Streamly.Network.Server as Server
12+-- > import qualified Streamly.Network.Inet.TCP as TCP
13--
13--
14
14
15−module Streamly.Network.Socket.Inet.TCP
15+module Streamly.Network.Inet.TCP
16 (
16 (
17 -- * Accept Connections
17 -- * Accept Connections
18 listenOnAddr
18 listenOnAddr
@@ -43,4 +43,4 @@ module Streamly.Network.Socket.Inet.TCP
43 )
43 )
44where
44where
45
45
46−import Streamly.Internal.Network.Server
46+import Streamly.Internal.Network.Inet.TCP
streamly.cabal+2−4
@@ -316,12 +316,10 @@ library
316 if !impl(ghcjs)
316 if !impl(ghcjs)
317 exposed-modules:
317 exposed-modules:
318 Streamly.Network.Socket
318 Streamly.Network.Socket
319− , Streamly.Network.Socket.Inet.TCP
319+ , Streamly.Network.Inet.TCP
320− , Streamly.Network.Client
321
320
322 , Streamly.Internal.Network.Socket
321 , Streamly.Internal.Network.Socket
323− , Streamly.Internal.Network.Server
322+ , Streamly.Internal.Network.Inet.TCP
324− , Streamly.Internal.Network.Client
325
323
326 if flag(benchmark)
324 if flag(benchmark)
327 exposed-modules:
325 exposed-modules: