1{-# OPTIONS_HADDOCK hide #-}
3{-# LANGUAGE BangPatterns #-}
4{-# LANGUAGE FlexibleContexts #-}
5{-# LANGUAGE MagicHash #-}
6{-# LANGUAGE RecordWildCards #-}
7{-# LANGUAGE UnboxedTuples #-}
12-- Module : Streamly.Internal.Network.Inet.TCP
13-- Copyright : (c) 2019 Composewell Technologies
16-- Maintainer : streamly@composewell.com
17-- Stability : experimental
20-- Combinators to build Inet/TCP clients and servers.
22module Streamly.Internal.Network.Inet.TCP
33 , connectionsOnLocalHost
36 -- | IP Address based operations.
68 , fromBytesWithBufferOf
77 -- These abstractions can be applied to any setting where we need to do a
78 -- sink processing of multiple streams e.g. output from multiple processes
79 -- or data coming from multiple files.
81 -- handle connections concurrently using a specified fold
82 -- , handleConnections
84 -- handle frames concurrently using a specified fold
87 -- merge frames from all connection into a single stream. Frames can be
88 -- created by a specified fold.
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 (MonadAsync)
108import Streamly.Internal.Data.Fold.Types (Fold(..))
109import Streamly.Internal.Data.Unfold.Types (Unfold(..))
110import Streamly.Internal.Network.Socket (SockSpec(..), accept, connections)
111import Streamly.Streams.Serial (SerialT)
112import Streamly.Internal.Memory.Array.Types (Array(..), defaultChunkSize, writeNUnsafe)
113import Streamly.Streams.StreamK.Type (IsStream)
115import qualified Control.Monad.Catch as MC
116import qualified Network.Socket as Net
118import qualified Streamly.Internal.Data.Unfold as UF
119import qualified Streamly.Internal.Memory.Array as A
120import qualified Streamly.Internal.Memory.ArrayStream as AS
121import qualified Streamly.Internal.Data.Fold.Types as FL
122import qualified Streamly.Prelude as S
123import qualified Streamly.Network.Socket as SK
124import qualified Streamly.Internal.Network.Socket as ISK
126-------------------------------------------------------------------------------
128-------------------------------------------------------------------------------
130-- | Unfold a tuple @(ipAddr, port)@ into a stream of connected TCP sockets.
131-- @ipAddr@ is the local IP address and @port@ is the local port on which
132-- connections are accepted.
135{-# INLINE acceptOnAddr #-}
138 => Unfold m ((Word8, Word8, Word8, Word8), PortNumber) Socket
139acceptOnAddr = UF.lmap f accept
144 { sockFamily = AF_INET
146 , sockProto = defaultProtocol -- TCP
147 , sockOpts = [(NoDelay,1), (ReuseAddr,1)]
149 , SockAddrInet port (tupleToHostAddress addr)
152-- | Like 'acceptOnAddr' but binds on the IPv4 address @0.0.0.0@ i.e. on all
153-- IPv4 addresses/interfaces of the machine and listens for TCP connections on
154-- the specified port.
156-- > acceptOnPort = UF.supplyFirst acceptOnAddr (0,0,0,0)
159{-# INLINE acceptOnPort #-}
160acceptOnPort :: MonadIO m => Unfold m PortNumber Socket
161acceptOnPort = UF.supplyFirst acceptOnAddr (0,0,0,0)
163-- | Like 'acceptOnAddr' but binds on the localhost IPv4 address @127.0.0.1@.
164-- The server can only be accessed from the local host, it cannot be accessed
165-- from other hosts on the network.
167-- > acceptOnPortLocal = UF.supplyFirst acceptOnAddr (127,0,0,1)
170{-# INLINE acceptOnPortLocal #-}
171acceptOnPortLocal :: MonadIO m => Unfold m PortNumber Socket
172acceptOnPortLocal = UF.supplyFirst acceptOnAddr (127,0,0,1)
174-------------------------------------------------------------------------------
176-------------------------------------------------------------------------------
178-- | Like 'connections' but binds on the specified IPv4 address of the machine
179-- and listens for TCP connections on the specified port.
182{-# INLINE connectionsOnAddr #-}
185 => (Word8, Word8, Word8, Word8)
188connectionsOnAddr addr port =
189 connections maxListenQueue SockSpec
190 { sockFamily = AF_INET
192 , sockProto = defaultProtocol
193 , sockOpts = [(NoDelay,1), (ReuseAddr,1)]
195 (SockAddrInet port (tupleToHostAddress addr))
197-- | Like 'connections' but binds on the IPv4 address @0.0.0.0@ i.e. on all
198-- IPv4 addresses/interfaces of the machine and listens for TCP connections on
199-- the specified port.
201-- > connectionsOnPort = connectionsOnAddr (0,0,0,0)
204{-# INLINE connectionsOnPort #-}
205connectionsOnPort :: MonadAsync m => PortNumber -> SerialT m Socket
206connectionsOnPort = connectionsOnAddr (0,0,0,0)
208-- | Like 'connections' but binds on the localhost IPv4 address @127.0.0.1@.
209-- The server can only be accessed from the local host, it cannot be accessed
210-- from other hosts on the network.
212-- > connectionsOnLocalHost = connectionsOnAddr (127,0,0,1)
215{-# INLINE connectionsOnLocalHost #-}
216connectionsOnLocalHost :: MonadAsync m => PortNumber -> SerialT m Socket
217connectionsOnLocalHost = connectionsOnAddr (127,0,0,1)
219-------------------------------------------------------------------------------
221-------------------------------------------------------------------------------
223-- | Connect to the specified IP address and port number.
226connect :: (Word8, Word8, Word8, Word8) -> PortNumber -> IO Socket
227connect addr port = do
228 sock <- socket AF_INET Stream defaultProtocol
229 Net.connect sock $ SockAddrInet port (Net.tupleToHostAddress addr)
232-- | Connect to a remote host using IP address and port and run the supplied
233-- action on the resulting socket. 'withConnectionM' makes sure that the
234-- socket is closed on normal termination or in case of an exception. If
235-- closing the socket raises an exception, then this exception will be raised
236-- by 'withConnectionM'.
239{-# INLINABLE withConnectionM #-}
240withConnectionM :: (MonadMask m, MonadIO m)
241 => (Word8, Word8, Word8, Word8) -> PortNumber -> (Socket -> m ()) -> m ()
242withConnectionM addr port =
243 bracket (liftIO $ connect addr port) (liftIO . Net.close)
245-------------------------------------------------------------------------------
247-------------------------------------------------------------------------------
249-- | Transform an 'Unfold' from a 'Socket' to an unfold from a remote IP
250-- address and port. The resulting unfold opens a socket, uses it using the
251-- supplied unfold and then makes sure that the socket is closed on normal
252-- termination or in case of an exception. If closing the socket raises an
253-- exception, then this exception will be raised by 'usingConnection'.
256{-# INLINABLE usingConnection #-}
257usingConnection :: (MonadCatch m, MonadIO m)
259 -> Unfold m ((Word8, Word8, Word8, Word8), PortNumber) a
261 UF.bracket (\(addr, port) -> liftIO $ connect addr port)
264-------------------------------------------------------------------------------
266-------------------------------------------------------------------------------
268-- | @'withConnection' addr port act@ opens a connection to the specified IPv4
269-- host address and port and passes the resulting socket handle to the
270-- computation @act@. The handle will be closed on exit from 'withConnection',
271-- whether by normal termination or by raising an exception. If closing the
272-- handle raises an exception, then this exception will be raised by
273-- 'withConnection' rather than any exception raised by 'act'.
276{-# INLINABLE withConnection #-}
277withConnection :: (IsStream t, MonadCatch m, MonadIO m)
278 => (Word8, Word8, Word8, Word8) -> PortNumber -> (Socket -> t m a) -> t m a
279withConnection addr port =
280 S.bracket (liftIO $ connect addr port) (liftIO . Net.close)
282-------------------------------------------------------------------------------
283-- Read Addr to Stream
284-------------------------------------------------------------------------------
286-- | Read a stream from the supplied IPv4 host address and port number.
290read :: (MonadCatch m, MonadIO m)
291 => Unfold m ((Word8, Word8, Word8, Word8), PortNumber) Word8
292read = UF.concat (usingConnection ISK.readChunks) A.read
294-- | Read a stream from the supplied IPv4 host address and port number.
297{-# INLINE toBytes #-}
298toBytes :: (IsStream t, MonadCatch m, MonadIO m)
299 => (Word8, Word8, Word8, Word8) -> PortNumber -> t m Word8
300toBytes addr port = AS.concat $ withConnection addr port ISK.toChunks
302-------------------------------------------------------------------------------
304-------------------------------------------------------------------------------
306-- | Write a stream of arrays to the supplied IPv4 host address and port
310{-# INLINE fromChunks #-}
312 :: (MonadCatch m, MonadAsync m)
313 => (Word8, Word8, Word8, Word8)
315 -> SerialT m (Array Word8)
317fromChunks addr port xs =
318 S.drain $ withConnection addr port (\sk -> S.yieldM $ ISK.fromChunks sk xs)
320-- | Write a stream of arrays to the supplied IPv4 host address and port
324{-# INLINE writeChunks #-}
326 :: (MonadAsync m, MonadCatch m)
327 => (Word8, Word8, Word8, Word8)
329 -> Fold m (Array Word8) ()
330writeChunks addr port = Fold step initial extract
333 skt <- liftIO (connect addr port)
334 fld <- FL.initialize (SK.writeChunks skt)
335 `MC.onException` (liftIO $ Net.close skt)
337 step (fld, skt) x = do
338 r <- FL.runStep fld x `MC.onException` (liftIO $ Net.close skt)
340 extract ((Fold _ initial1 extract1), skt) = do
341 liftIO $ Net.close skt
342 initial1 >>= extract1
344-- | Like 'write' but provides control over the write buffer. Output will
345-- be written to the IO device as soon as we collect the specified number of
349{-# INLINE fromBytesWithBufferOf #-}
351 :: (MonadCatch m, MonadAsync m)
353 -> (Word8, Word8, Word8, Word8)
357fromBytesWithBufferOf n addr port m = fromChunks addr port $ AS.arraysOf n m
359-- | Like 'write' but provides control over the write buffer. Output will
360-- be written to the IO device as soon as we collect the specified number of
364{-# INLINE writeWithBufferOf #-}
366 :: (MonadAsync m, MonadCatch m)
368 -> (Word8, Word8, Word8, Word8)
371writeWithBufferOf n addr port =
372 FL.lchunksOf n (writeNUnsafe n) (writeChunks addr port)
374-- | Write a stream to the supplied IPv4 host address and port number.
377{-# INLINE fromBytes #-}
378fromBytes :: (MonadCatch m, MonadAsync m)
379 => (Word8, Word8, Word8, Word8) -> PortNumber -> SerialT m Word8 -> m ()
380fromBytes = fromBytesWithBufferOf defaultChunkSize
382-- | Write a stream to the supplied IPv4 host address and port number.
386write :: (MonadAsync m, MonadCatch m)
387 => (Word8, Word8, Word8, Word8) -> PortNumber -> Fold m Word8 ()
388write = writeWithBufferOf defaultChunkSize
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