4-- Module : Streamly.Internal.Network.Inet.TCP
5-- Copyright : (c) 2019 Composewell Technologies
8-- Maintainer : streamly@composewell.com
9-- Stability : experimental
12-- Combinators to build Inet/TCP clients and servers.
14module Streamly.Internal.Network.Inet.TCP
26 , connectionsOnAddrWith
28 , connectionsOnLocalHost
31 -- | IP Address based operations.
63 , putBytesWithBufferOf
75 -- These abstractions can be applied to any setting where we need to do a
76 -- sink processing of multiple streams e.g. output from multiple processes
77 -- or data coming from multiple files.
79 -- handle connections concurrently using a specified fold
80 -- , handleConnections
82 -- handle frames concurrently using a specified fold
85 -- merge frames from all connection into a single stream. Frames can be
86 -- created by a specified fold.
96import Control.Exception (onException)
97import Control.Monad.Catch (MonadCatch, MonadMask, bracket)
98import Control.Monad.IO.Class (MonadIO(..))
99import Data.Word (Word8)
101 (Socket, PortNumber, SocketOption(..), Family(..), SockAddr(..),
102 SocketType(..), defaultProtocol, maxListenQueue, tupleToHostAddress,
104import Prelude hiding (read)
106import Streamly.Internal.Control.Concurrent (MonadAsync)
107import Streamly.Internal.Control.ForkLifted (fork)
108import Streamly.Internal.Data.Array.Unboxed.Type (Array(..), writeNUnsafe)
109import Streamly.Internal.Data.Fold.Type (Fold(..))
110import Streamly.Data.Stream (Stream)
111import Streamly.Internal.Data.Tuple.Strict (Tuple'(..))
112import Streamly.Internal.Data.Unfold.Type (Unfold(..))
113import Streamly.Internal.Network.Socket (SockSpec(..), accept, connections)
114import Streamly.Internal.System.IO (defaultChunkSize)
116import qualified Control.Monad.Catch as MC
117import qualified Network.Socket as Net
119import qualified Streamly.Data.Array.Unboxed as A
120import qualified Streamly.Data.Fold as FL
121import qualified Streamly.Data.Unfold as UF
122import qualified Streamly.Internal.Data.Unfold as UF (bracket, first)
123import qualified Streamly.Internal.Data.Array.Unboxed.Stream as AS
124import qualified Streamly.Internal.Data.Fold.Type as FL
125 (initialize, snoc, Step(..))
126import qualified Streamly.Internal.Data.Stream.IsStream as S
127import qualified Streamly.Internal.Network.Socket as ISK
129-------------------------------------------------------------------------------
131-------------------------------------------------------------------------------
133{-# INLINE acceptOnAddrWith #-}
136 => [(SocketOption, Int)]
137 -> Unfold m ((Word8, Word8, Word8, Word8), PortNumber) Socket
138acceptOnAddrWith opts = UF.lmap f accept
143 { sockFamily = AF_INET
145 , sockProto = defaultProtocol -- TCP
148 , SockAddrInet port (tupleToHostAddress addr)
151-- | Unfold a tuple @(ipAddr, port)@ into a stream of connected TCP sockets.
152-- @ipAddr@ is the local IP address and @port@ is the local port on which
153-- connections are accepted.
156{-# INLINE acceptOnAddr #-}
159 => Unfold m ((Word8, Word8, Word8, Word8), PortNumber) Socket
160acceptOnAddr = acceptOnAddrWith []
162{-# INLINE acceptOnPortWith #-}
163acceptOnPortWith :: MonadIO m
164 => [(SocketOption, Int)]
165 -> Unfold m PortNumber Socket
166acceptOnPortWith opts = UF.first (0,0,0,0) (acceptOnAddrWith opts)
168-- | Like 'acceptOnAddr' but binds on the IPv4 address @0.0.0.0@ i.e. on all
169-- IPv4 addresses/interfaces of the machine and listens for TCP connections on
170-- the specified port.
172-- > acceptOnPort = UF.first acceptOnAddr (0,0,0,0)
175{-# INLINE acceptOnPort #-}
176acceptOnPort :: MonadIO m => Unfold m PortNumber Socket
177acceptOnPort = UF.first (0,0,0,0) acceptOnAddr
179-- | Like 'acceptOnAddr' 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-- > acceptOnPortLocal = UF.first acceptOnAddr (127,0,0,1)
186{-# INLINE acceptOnPortLocal #-}
187acceptOnPortLocal :: MonadIO m => Unfold m PortNumber Socket
188acceptOnPortLocal = UF.first (127,0,0,1) acceptOnAddr
190-------------------------------------------------------------------------------
192-------------------------------------------------------------------------------
194{-# INLINE connectionsOnAddrWith #-}
197 => [(SocketOption, Int)]
198 -> (Word8, Word8, Word8, Word8)
201connectionsOnAddrWith opts addr port =
202 connections maxListenQueue SockSpec
203 { sockFamily = AF_INET
205 , sockProto = defaultProtocol
208 (SockAddrInet port (tupleToHostAddress addr))
210-- | Like 'connections' but binds on the specified IPv4 address of the machine
211-- and listens for TCP connections on the specified port.
214{-# INLINE connectionsOnAddr #-}
217 => (Word8, Word8, Word8, Word8)
220connectionsOnAddr = connectionsOnAddrWith []
222-- | Like 'connections' but binds on the IPv4 address @0.0.0.0@ i.e. on all
223-- IPv4 addresses/interfaces of the machine and listens for TCP connections on
224-- the specified port.
226-- > connectionsOnPort = connectionsOnAddr (0,0,0,0)
229{-# INLINE connectionsOnPort #-}
230connectionsOnPort :: MonadAsync m => PortNumber -> Stream m Socket
231connectionsOnPort = connectionsOnAddr (0,0,0,0)
233-- | Like 'connections' but binds on the localhost IPv4 address @127.0.0.1@.
234-- The server can only be accessed from the local host, it cannot be accessed
235-- from other hosts on the network.
237-- > connectionsOnLocalHost = connectionsOnAddr (127,0,0,1)
240{-# INLINE connectionsOnLocalHost #-}
241connectionsOnLocalHost :: MonadAsync m => PortNumber -> Stream m Socket
242connectionsOnLocalHost = connectionsOnAddr (127,0,0,1)
244-------------------------------------------------------------------------------
246-------------------------------------------------------------------------------
248-- | Connect to the specified IP address and port number. Returns a connected
249-- socket or throws an exception.
252connect :: (Word8, Word8, Word8, Word8) -> PortNumber -> IO Socket
253connect addr port = do
254 sock <- socket AF_INET Stream defaultProtocol
255 Net.connect sock (SockAddrInet port (Net.tupleToHostAddress addr))
256 `onException` Net.close sock
259-- | Connect to a remote host using IP address and port and run the supplied
260-- action on the resulting socket. 'withConnectionM' makes sure that the
261-- socket is closed on normal termination or in case of an exception. If
262-- closing the socket raises an exception, then this exception will be raised
263-- by 'withConnectionM'.
266{-# INLINABLE withConnectionM #-}
267withConnectionM :: (MonadMask m, MonadIO m)
268 => (Word8, Word8, Word8, Word8) -> PortNumber -> (Socket -> m ()) -> m ()
269withConnectionM addr port =
270 bracket (liftIO $ connect addr port) (liftIO . Net.close)
272-------------------------------------------------------------------------------
274-------------------------------------------------------------------------------
276-- | Transform an 'Unfold' from a 'Socket' to an unfold from a remote IP
277-- address and port. The resulting unfold opens a socket, uses it using the
278-- supplied unfold and then makes sure that the socket is closed on normal
279-- termination or in case of an exception. If closing the socket raises an
280-- exception, then this exception will be raised by 'usingConnection'.
283{-# INLINE usingConnection #-}
284usingConnection :: (MonadCatch m, MonadAsync m)
286 -> Unfold m ((Word8, Word8, Word8, Word8), PortNumber) a
288 UF.bracket (\(addr, port) -> liftIO $ connect addr port)
291-------------------------------------------------------------------------------
293-------------------------------------------------------------------------------
295-- | @'withConnection' addr port act@ opens a connection to the specified IPv4
296-- host address and port and passes the resulting socket handle to the
297-- computation @act@. The handle will be closed on exit from 'withConnection',
298-- whether by normal termination or by raising an exception. If closing the
299-- handle raises an exception, then this exception will be raised by
300-- 'withConnection' rather than any exception raised by 'act'.
303{-# INLINE withConnection #-}
304withConnection :: (MonadCatch m, MonadAsync m)
305 => (Word8, Word8, Word8, Word8)
307 -> (Socket -> Stream m a)
309withConnection addr port =
310 S.bracket (liftIO $ connect addr port) (liftIO . Net.close)
312-------------------------------------------------------------------------------
313-- Read Addr to Stream
314-------------------------------------------------------------------------------
316-- | Read a stream from the supplied IPv4 host address and port number.
320read :: (MonadCatch m, MonadAsync m)
321 => Unfold m ((Word8, Word8, Word8, Word8), PortNumber) Word8
322read = UF.many A.read (usingConnection ISK.readChunks)
324-- | Read a stream from the supplied IPv4 host address and port number.
327{-# INLINE toBytes #-}
328toBytes :: (MonadCatch m, MonadAsync m)
329 => (Word8, Word8, Word8, Word8) -> PortNumber -> Stream m Word8
330toBytes addr port = AS.concat $ withConnection addr port ISK.toChunks
332-------------------------------------------------------------------------------
334-------------------------------------------------------------------------------
336-- | Write a stream of arrays to the supplied IPv4 host address and port
340{-# INLINE putChunks #-}
342 :: (MonadCatch m, MonadAsync m)
343 => (Word8, Word8, Word8, Word8)
345 -> Stream m (Array Word8)
347putChunks addr port xs =
348 S.drain $ withConnection addr port (\sk -> S.fromEffect $ ISK.putChunks sk xs)
350-- | Write a stream of arrays to the supplied IPv4 host address and port
354{-# INLINE writeChunks #-}
356 :: (MonadAsync m, MonadCatch m)
357 => (Word8, Word8, Word8, Word8)
359 -> Fold m (Array Word8) ()
360writeChunks addr port = Fold step initial extract
363 skt <- liftIO (connect addr port)
364 fld <- FL.initialize (ISK.writeChunks skt)
365 `MC.onException` liftIO (Net.close skt)
366 return $ FL.Partial (Tuple' fld skt)
367 step (Tuple' fld skt) x = do
368 r <- FL.snoc fld x `MC.onException` liftIO (Net.close skt)
369 return $ FL.Partial (Tuple' r skt)
370 extract (Tuple' (Fold _ initial1 extract1) skt) = do
371 liftIO $ Net.close skt
374 FL.Partial fs -> extract1 fs
375 FL.Done fb -> return fb
377-- | Like 'write' but provides control over the write buffer. Output will
378-- be written to the IO device as soon as we collect the specified number of
382{-# INLINE putBytesWithBufferOf #-}
384 :: (MonadCatch m, MonadAsync m)
386 -> (Word8, Word8, Word8, Word8)
390putBytesWithBufferOf n addr port m = putChunks addr port $ AS.arraysOf n m
392-- | Like 'write' but provides control over the write buffer. Output will
393-- be written to the IO device as soon as we collect the specified number of
397{-# INLINE writeWithBufferOf #-}
399 :: (MonadAsync m, MonadCatch m)
401 -> (Word8, Word8, Word8, Word8)
404writeWithBufferOf n addr port =
405 FL.chunksOf n (writeNUnsafe n) (writeChunks addr port)
407-- | Write a stream to the supplied IPv4 host address and port number.
410{-# INLINE putBytes #-}
411putBytes :: (MonadCatch m, MonadAsync m)
412 => (Word8, Word8, Word8, Word8) -> PortNumber -> Stream m Word8 -> m ()
413putBytes = putBytesWithBufferOf defaultChunkSize
415-- | Write a stream to the supplied IPv4 host address and port number.
419write :: (MonadAsync m, MonadCatch m)
420 => (Word8, Word8, Word8, Word8) -> PortNumber -> Fold m Word8 ()
421write = writeWithBufferOf defaultChunkSize
423-------------------------------------------------------------------------------
425-------------------------------------------------------------------------------
427{-# INLINE withInputConnect #-}
429 :: (MonadCatch m, MonadAsync m)
430 => (Word8, Word8, Word8, Word8)
433 -> (Socket -> Stream m a)
435withInputConnect addr port input f = S.bracket pre post handler
440 sk <- liftIO $ connect addr port
441 tid <- fork (ISK.putBytes sk input)
444 handler (sk, _) = f sk
446 -- XXX kill the thread immediately?
447 post (sk, _) = liftIO $ Net.close sk
449-- | Send an input stream to a remote host and produce the output stream from
450-- the host. The server host just acts as a transformation function on the
451-- input stream. Both sending and receiving happen asynchronously.
455{-# INLINE processBytes #-}
457 :: (MonadAsync m, MonadCatch m)
458 => (Word8, Word8, Word8, Word8)
462processBytes addr port input = withInputConnect addr port input ISK.toBytes
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