2{-# LANGUAGE BangPatterns #-}
3{-# LANGUAGE FlexibleContexts #-}
4{-# LANGUAGE MagicHash #-}
5{-# LANGUAGE RecordWildCards #-}
6{-# LANGUAGE UnboxedTuples #-}
11-- Module : Streamly.Network.Client
12-- Copyright : (c) 2019 Composewell Technologies
15-- Maintainer : harendra.kumar@gmail.com
16-- Stability : experimental
19-- Combinators to build network clients.
21-- > import qualified Streamly.Network.Socket as Client
24module Streamly.Network.Client
27 -- | Socket based reads and writes.
58import Control.Monad.Catch (MonadCatch)
59import Control.Monad.IO.Class (MonadIO(..))
60import Data.Word (Word8)
62 (Socket, PortNumber, Family(..), SockAddr(..), SocketType(..),
63 socket, connect, defaultProtocol)
64import Prelude hiding (read)
66import qualified Network.Socket as Net
68import Streamly (MonadAsync)
69import Streamly.Fold.Types (Fold(..))
70import Streamly.Memory.Array.Types (Array(..), defaultChunkSize)
71-- import Streamly.Streams.Serial (SerialT)
72import Streamly.Streams.StreamK.Type (IsStream)
74import qualified Streamly.Fold.Types as FL
75import qualified Streamly.Memory.Array as A
76import qualified Streamly.Prelude as S
77import qualified Streamly.Network.Socket as SK
79-------------------------------------------------------------------------------
81-------------------------------------------------------------------------------
83openConnection :: (Word8, Word8, Word8, Word8) -> PortNumber -> IO Socket
84openConnection addr port = do
85 sock <- socket AF_INET Stream defaultProtocol
86 connect sock $ SockAddrInet port (Net.tupleToHostAddress addr)
89-- | @'withConnection' addr port act@ opens a connection to the specified IPv4
90-- host address and port and passes the resulting socket handle to the
91-- computation @act@. The handle will be closed on exit from 'withConnection',
92-- whether by normal termination or by raising an exception. If closing the
93-- handle raises an exception, then this exception will be raised by
94-- 'withConnection' rather than any exception raised by 'act'.
97{-# INLINABLE withConnection #-}
98withConnection :: (IsStream t, MonadCatch m, MonadIO m)
99 => (Word8, Word8, Word8, Word8) -> PortNumber -> (Socket -> t m a) -> t m a
100withConnection addr port =
101 S.bracket (liftIO $ openConnection addr port) (liftIO . Net.close)
103-------------------------------------------------------------------------------
104-- Read Addr to Stream
105-------------------------------------------------------------------------------
107-- | Read a stream from the supplied IPv4 host address and port number.
111read :: (IsStream t, MonadCatch m, MonadIO m)
112 => (Word8, Word8, Word8, Word8) -> PortNumber -> t m Word8
113read addr port = A.concat $ withConnection addr port SK.readArrays
115-------------------------------------------------------------------------------
117-------------------------------------------------------------------------------
120-- | Write a stream of arrays to the supplied IPv4 host address and port
124{-# INLINE writeArrays #-}
126 :: (MonadCatch m, MonadAsync m)
127 => (Word8, Word8, Word8, Word8)
129 -> SerialT m (Array Word8)
131writeArrays addr port xs =
132 S.drain $ withConnection addr port (\sk -> S.yieldM $ SK.writeArrays sk xs)
135-- | Write a stream of arrays to the supplied IPv4 host address and port
139{-# INLINE writeArrays #-}
142 => (Word8, Word8, Word8, Word8)
144 -> Fold m (Array Word8) ()
145writeArrays addr port = Fold step initial extract
148 skt <- liftIO (openConnection addr port)
149 FL.initialize (SK.writeArrays skt)
151 extract (Fold _ initial1 extract1) = initial1 >>= extract1
154-- | Like 'write' but provides control over the write buffer. Output will
155-- be written to the IO device as soon as we collect the specified number of
159{-# INLINE writeInChunksOf #-}
161 :: (MonadCatch m, MonadAsync m)
163 -> (Word8, Word8, Word8, Word8)
167writeInChunksOf n addr port m = writeArrays addr port $ AS.arraysOf n m
170-- | Like 'write' but provides control over the write buffer. Output will
171-- be written to the IO device as soon as we collect the specified number of
175{-# INLINE writeInChunksOf #-}
179 -> (Word8, Word8, Word8, Word8)
182writeInChunksOf n addr port = FL.lchunksOf n (A.writeN n) (writeArrays addr port)
185-- | Write a stream to the supplied IPv4 host address and port number.
189write :: (MonadCatch m, MonadAsync m)
190 => (Word8, Word8, Word8, Word8) -> PortNumber -> SerialT m Word8 -> m ()
191write = writeInChunksOf defaultChunkSize
194-- | Write a stream to the supplied IPv4 host address and port number.
199 => (Word8, Word8, Word8, Word8) -> PortNumber -> Fold m Word8 ()
200write = writeInChunksOf 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