2-- Module : Streamly.Internal.Network.Inet.TCP
3-- Copyright : (c) 2019 Composewell Technologies
6-- Maintainer : streamly@composewell.com
7-- Stability : experimental
10-- Combinators to build Inet/TCP clients and servers.
12module Streamly.Internal.Network.Inet.TCP
30 -- | IP Address based operations.
62 , putBytesWithBufferOf
74 -- These abstractions can be applied to any setting where we need to do a
75 -- sink processing of multiple streams e.g. output from multiple processes
76 -- or data coming from multiple files.
78 -- handle connections concurrently using a specified fold
79 -- , handleConnections
81 -- handle frames concurrently using a specified fold
84 -- merge frames from all connection into a single stream. Frames can be
85 -- created by a specified fold.
97import Control.Exception (onException)
98import Control.Monad.Catch (MonadCatch, MonadMask, bracket)
99import Control.Monad.IO.Class (MonadIO(..))
100import Data.Word (Word8)
102 (Socket, PortNumber, SocketOption(..), Family(..), SockAddr(..),
103 SocketType(..), defaultProtocol, maxListenQueue, tupleToHostAddress,
105import Prelude hiding (read)
107import Streamly.Internal.Control.Concurrent (MonadAsync)
108import Streamly.Internal.Control.ForkLifted (fork)
109import Streamly.Internal.Data.Array.Type (Array(..), writeNUnsafe)
110import Streamly.Internal.Data.Fold.Type (Fold(..))
111import Streamly.Data.Stream (Stream)
112import Streamly.Internal.Data.Tuple.Strict (Tuple'(..))
113import Streamly.Internal.Data.Unboxed (Unbox)
114import Streamly.Internal.Data.Unfold.Type (Unfold(..))
115import Streamly.Internal.Network.Socket (SockSpec(..), accept, acceptor)
116import Streamly.Internal.System.IO (defaultChunkSize)
118import qualified Control.Monad.Catch as MC
119import qualified Network.Socket as Net
121import qualified Streamly.Data.Array as A
122import qualified Streamly.Data.Fold as FL
123import qualified Streamly.Data.Unfold as UF
124import qualified Streamly.Internal.Data.Array.Type as A
125import qualified Streamly.Internal.Data.Unfold as UF (first, bracketIO)
126import qualified Streamly.Internal.Data.Fold.Type as FL (Step(..), reduce)
127import qualified Streamly.Internal.Data.Stream.StreamD as S
128import qualified Streamly.Internal.Data.Stream.Exception.Lifted as S
129import qualified Streamly.Internal.Network.Socket as ISK
131-------------------------------------------------------------------------------
133-------------------------------------------------------------------------------
135{-# INLINE acceptorOnAddrWith #-}
138 => [(SocketOption, Int)]
139 -> Unfold m ((Word8, Word8, Word8, Word8), PortNumber) Socket
140acceptorOnAddrWith opts = UF.lmap f acceptor
145 { sockFamily = AF_INET
147 , sockProto = defaultProtocol -- TCP
150 , SockAddrInet port (tupleToHostAddress addr)
153-- | Unfold a tuple @(ipAddr, port)@ into a stream of connected TCP sockets.
154-- @ipAddr@ is the local IP address and @port@ is the local port on which
155-- connections are accepted.
157{-# INLINE acceptorOnAddr #-}
160 => Unfold m ((Word8, Word8, Word8, Word8), PortNumber) Socket
161acceptorOnAddr = acceptorOnAddrWith []
163{-# INLINE acceptorOnPortWith #-}
164acceptorOnPortWith :: MonadIO m
165 => [(SocketOption, Int)]
166 -> Unfold m PortNumber Socket
167acceptorOnPortWith opts = UF.first (0,0,0,0) (acceptorOnAddrWith opts)
169-- | Like 'acceptorOnAddr' but binds on the IPv4 address @0.0.0.0@ i.e. on all
170-- IPv4 addresses/interfaces of the machine and listens for TCP connections on
171-- the specified port.
173-- > acceptorOnPort = UF.first acceptorOnAddr (0,0,0,0)
175{-# INLINE acceptorOnPort #-}
176acceptorOnPort :: MonadIO m => Unfold m PortNumber Socket
177acceptorOnPort = UF.first (0,0,0,0) acceptorOnAddr
179-- | Like 'acceptorOnAddr' but binds on the localhost IPv4 address @127.0.0.1@.
180-- The server can only be accessed from the local host, it cannot be accessed
181-- from other hosts on the network.
183-- > acceptorOnPortLocal = UF.first acceptorOnAddr (127,0,0,1)
185{-# INLINE acceptorOnPortLocal #-}
186acceptorOnPortLocal :: MonadIO m => Unfold m PortNumber Socket
187acceptorOnPortLocal = UF.first (127,0,0,1) acceptorOnAddr
189-------------------------------------------------------------------------------
191-------------------------------------------------------------------------------
193{-# INLINE acceptOnAddrWith #-}
196 => [(SocketOption, Int)]
197 -> (Word8, Word8, Word8, Word8)
200acceptOnAddrWith opts addr port =
201 accept maxListenQueue SockSpec
202 { sockFamily = AF_INET
204 , sockProto = defaultProtocol
207 (SockAddrInet port (tupleToHostAddress addr))
209-- | Like 'accept' but binds on the specified IPv4 address of the machine
210-- and listens for TCP connections on the specified port.
213{-# INLINE acceptOnAddr #-}
216 => (Word8, Word8, Word8, Word8)
219acceptOnAddr = acceptOnAddrWith []
221-- | Like 'accept' but binds on the IPv4 address @0.0.0.0@ i.e. on all
222-- IPv4 addresses/interfaces of the machine and listens for TCP connections on
223-- the specified port.
225-- > acceptOnPort = acceptOnAddr (0,0,0,0)
228{-# INLINE acceptOnPort #-}
229acceptOnPort :: MonadIO m => PortNumber -> Stream m Socket
230acceptOnPort = acceptOnAddr (0,0,0,0)
232-- | Like 'accept' but binds on the localhost IPv4 address @127.0.0.1@.
233-- The server can only be accessed from the local host, it cannot be accessed
234-- from other hosts on the network.
236-- > acceptOnPortLocal = acceptOnAddr (127,0,0,1)
239{-# INLINE acceptOnPortLocal #-}
240acceptOnPortLocal :: MonadIO m => PortNumber -> Stream m Socket
241acceptOnPortLocal = acceptOnAddr (127,0,0,1)
243-------------------------------------------------------------------------------
245-------------------------------------------------------------------------------
247-- | Connect to the specified IP address and port number. Returns a connected
248-- socket or throws an exception.
250connect :: (Word8, Word8, Word8, Word8) -> PortNumber -> IO Socket
251connect addr port = do
252 sock <- socket AF_INET Stream defaultProtocol
253 Net.connect sock (SockAddrInet port (Net.tupleToHostAddress addr))
254 `onException` Net.close sock
257-- | Connect to a remote host using IP address and port and run the supplied
258-- action on the resulting socket. 'withConnectionM' makes sure that the
259-- socket is closed on normal termination or in case of an exception. If
260-- closing the socket raises an exception, then this exception will be raised
261-- by 'withConnectionM'.
264{-# INLINABLE withConnectionM #-}
265withConnectionM :: (MonadMask m, MonadIO m)
266 => (Word8, Word8, Word8, Word8) -> PortNumber -> (Socket -> m ()) -> m ()
267withConnectionM addr port =
268 bracket (liftIO $ connect addr port) (liftIO . Net.close)
270-------------------------------------------------------------------------------
272-------------------------------------------------------------------------------
274-- | Transform an 'Unfold' from a 'Socket' to an unfold from a remote IP
275-- address and port. The resulting unfold opens a socket, uses it using the
276-- supplied unfold and then makes sure that the socket is closed on normal
277-- termination or in case of an exception. If closing the socket raises an
278-- exception, then this exception will be raised by 'usingConnection'.
281{-# INLINE usingConnection #-}
282usingConnection :: (MonadCatch m, MonadAsync m)
284 -> Unfold m ((Word8, Word8, Word8, Word8), PortNumber) a
285usingConnection = UF.bracketIO (\(addr, port) -> connect addr port) Net.close
287-------------------------------------------------------------------------------
289-------------------------------------------------------------------------------
291-- | @'withConnection' addr port act@ opens a connection to the specified IPv4
292-- host address and port and passes the resulting socket handle to the
293-- computation @act@. The handle will be closed on exit from 'withConnection',
294-- whether by normal termination or by raising an exception. If closing the
295-- handle raises an exception, then this exception will be raised by
296-- 'withConnection' rather than any exception raised by 'act'.
299{-# INLINE withConnection #-}
300withConnection :: (MonadCatch m, MonadAsync m)
301 => (Word8, Word8, Word8, Word8)
303 -> (Socket -> Stream m a)
305withConnection addr port = S.bracketIO (connect addr port) Net.close
307-------------------------------------------------------------------------------
308-- Read Addr to Stream
309-------------------------------------------------------------------------------
311-- | Read a stream from the supplied IPv4 host address and port number.
314reader :: (MonadCatch m, MonadAsync m)
315 => Unfold m ((Word8, Word8, Word8, Word8), PortNumber) Word8
316reader = UF.many A.reader (usingConnection ISK.chunkReader)
318{-# INLINE concatChunks #-}
319concatChunks :: (Monad m, Unbox a) => Stream m (Array a) -> Stream m a
320concatChunks = S.unfoldMany A.reader
322-- | Read a stream from the supplied IPv4 host address and port number.
326read :: (MonadCatch m, MonadAsync m)
327 => (Word8, Word8, Word8, Word8) -> PortNumber -> Stream m Word8
328read addr port = concatChunks $ withConnection addr port ISK.readChunks
330-------------------------------------------------------------------------------
332-------------------------------------------------------------------------------
334-- | Write a stream of arrays to the supplied IPv4 host address and port
338{-# INLINE putChunks #-}
340 :: (MonadCatch m, MonadAsync m)
341 => (Word8, Word8, Word8, Word8)
343 -> Stream m (Array Word8)
345putChunks addr port xs =
347 $ withConnection addr port (\sk -> S.fromEffect $ ISK.putChunks sk xs)
349-- | Write a stream of arrays to the supplied IPv4 host address and port
352{-# INLINE writeChunks #-}
354 :: (MonadIO m, MonadCatch m)
355 => (Word8, Word8, Word8, Word8)
357 -> Fold m (Array Word8) ()
358writeChunks addr port = Fold step initial extract
361 skt <- liftIO (connect addr port)
362 fld <- FL.reduce (ISK.writeChunks skt)
363 `MC.onException` liftIO (Net.close skt)
364 return $ FL.Partial (Tuple' fld skt)
365 step (Tuple' fld skt) x = do
366 r <- FL.addOne x fld `MC.onException` liftIO (Net.close skt)
367 return $ FL.Partial (Tuple' r skt)
368 extract (Tuple' (Fold _ initial1 extract1) skt) = do
369 liftIO $ Net.close skt
372 FL.Partial fs -> extract1 fs
373 FL.Done fb -> return fb
375-- | Like 'write' but provides control over the write buffer. Output will
376-- be written to the IO device as soon as we collect the specified number of
380{-# INLINE putBytesWithBufferOf #-}
382 :: (MonadCatch m, MonadAsync m)
384 -> (Word8, Word8, Word8, Word8)
388putBytesWithBufferOf n addr port m = putChunks addr port $ A.chunksOf n m
390-- | Like 'write' but provides control over the write buffer. Output will
391-- be written to the IO device as soon as we collect the specified number of
394{-# INLINE writeWithBufferOf #-}
396 :: (MonadIO m, MonadCatch m)
398 -> (Word8, Word8, Word8, Word8)
401writeWithBufferOf n addr port =
402 FL.groupsOf n (writeNUnsafe n) (writeChunks addr port)
404-- | Write a stream to the supplied IPv4 host address and port number.
407{-# INLINE putBytes #-}
408putBytes :: (MonadCatch m, MonadAsync m)
409 => (Word8, Word8, Word8, Word8) -> PortNumber -> Stream m Word8 -> m ()
410putBytes = putBytesWithBufferOf defaultChunkSize
412-- | Write a stream to the supplied IPv4 host address and port number.
415write :: (MonadIO m, MonadCatch m)
416 => (Word8, Word8, Word8, Word8) -> PortNumber -> Fold m Word8 ()
417write = writeWithBufferOf defaultChunkSize
419-------------------------------------------------------------------------------
421-------------------------------------------------------------------------------
423{-# INLINE withInputConnect #-}
425 :: (MonadCatch m, MonadAsync m)
426 => (Word8, Word8, Word8, Word8)
429 -> (Socket -> Stream m a)
431withInputConnect addr port input f = S.bracket pre post handler
436 sk <- liftIO $ connect addr port
437 tid <- fork (ISK.putBytes sk input)
440 handler (sk, _) = f sk
442 -- XXX kill the thread immediately?
443 post (sk, _) = liftIO $ Net.close sk
445-- | Send an input stream to a remote host and produce the output stream from
446-- the host. The server host just acts as a transformation function on the
447-- input stream. Both sending and receiving happen asynchronously.
451{-# INLINE pipeBytes #-}
453 :: (MonadAsync m, MonadCatch m)
454 => (Word8, Word8, Word8, Word8)
458pipeBytes addr port input = withInputConnect addr port input ISK.read
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list