on master help sign in
Add connect and connectFrom
e84612fHarendra Kumar
src/Streamly/Internal/Network/Socket.hs+37
@@ -23,6 +23,8 @@ module Streamly.Internal.Network.Socket
23 -- * Accept connections
23 -- * Accept connections
24 , accept
24 , accept
25 , connections
25 , connections
26+ , connect
27+ , connectFrom
26
28
27 -- * Read from connection
29 -- * Read from connection
28 , read
30 , read
@@ -180,6 +182,41 @@ listenTuples = Unfold step inject
180accept :: MonadIO m => Unfold m (Int, SockSpec, SockAddr) Socket
182accept :: MonadIO m => Unfold m (Int, SockSpec, SockAddr) Socket
181accept = UF.map fst listenTuples
183accept = UF.map fst listenTuples
182
184
185+{-# INLINE connectCommon #-}
186+connectCommon :: SockSpec -> Maybe SockAddr -> SockAddr -> IO Socket
187+connectCommon SockSpec{..} local remote = withSocketsDo $ do
188+ sock <- socket sockFamily sockType sockProto
189+ use sock `onException` Net.close sock
190+ return sock
191+
192+ where
193+
194+ use sock = do
195+ mapM_ (\(opt, val) -> setSocketOption sock opt val) sockOpts
196+ case local of
197+ Nothing -> return ()
198+ Just addr -> bind sock addr
199+ Net.connect sock remote
200+
201+-- | Connect to a remote host using the given socket specification and remote
202+-- address. Returns a connected socket or throws an exception.
203+--
204+-- /Internal/
205+--
206+{-# INLINE connect #-}
207+connect :: SockSpec -> SockAddr -> IO Socket
208+connect spec remote = connectCommon spec Nothing remote
209+
210+-- | Connect to a remote host using the given socket specification, a local
211+-- address to bind to and a remote address to connect to. Returns a connected
212+-- socket or throws an exception.
213+--
214+-- /Internal/
215+--
216+{-# INLINE connectFrom #-}
217+connectFrom :: SockSpec -> SockAddr -> SockAddr -> IO Socket
218+connectFrom spec local remote = connectCommon spec (Just local) remote
219+
183-------------------------------------------------------------------------------
220-------------------------------------------------------------------------------
184-- Listen (Streams)
221-- Listen (Streams)
185-------------------------------------------------------------------------------
222-------------------------------------------------------------------------------