on master help sign in
Add connect and connectFrom
commite84612f179876baaac98e2fe1f210a73de206183 authorHarendra Kumar <harendra@composewell.com> dateTue 3 Mar 2020, 17:03 (7 years ago) parent96aa91d
1 file changed+37
src/Streamly/Internal/Network/Socket.hs+37
src/Streamly/Internal/Network/Socket.hs+37
@@ -23,6 +23,8 @@ module Streamly.Internal.Network.Socket
2323 -- * Accept connections
2424 , accept
2525 , connections
26+ , connect
27+ , connectFrom
2628
2729 -- * Read from connection
2830 , read
@@ -180,6 +182,41 @@ listenTuples = Unfold step inject
180182accept :: MonadIO m => Unfold m (Int, SockSpec, SockAddr) Socket
181183accept = UF.map fst listenTuples
182184
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+
183220-------------------------------------------------------------------------------
184221-- Listen (Streams)
185222-------------------------------------------------------------------------------