@@ -23,6 +23,8 @@ module Streamly.Internal.Network.Socket
23 -- * Accept connections
23 -- * Accept connections
25 , connections
25 , connections
27 -- * Read from connection
29 -- * Read from connection
@@ -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
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
195+ mapM_ (\(opt, val) -> setSocketOption sock opt val) sockOpts
197+ Nothing -> return ()
198+ Just addr -> bind sock addr
199+ Net.connect sock remote
201+-- | Connect to a remote host using the given socket specification and remote
202+-- address. Returns a connected socket or throws an exception.
206+{-# INLINE connect #-}
207+connect :: SockSpec -> SockAddr -> IO Socket
208+connect spec remote = connectCommon spec Nothing remote
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.
216+{-# INLINE connectFrom #-}
217+connectFrom :: SockSpec -> SockAddr -> SockAddr -> IO Socket
218+connectFrom spec local remote = connectCommon spec (Just local) remote
183-------------------------------------------------------------------------------
220-------------------------------------------------------------------------------
184-- Listen (Streams)
221-- Listen (Streams)
185-------------------------------------------------------------------------------
222-------------------------------------------------------------------------------