on master help sign in
Add combinators to specify socket options
4b242dbHarendra Kumar
src/Streamly/Internal/Network/Inet/TCP.hs+43−18
@@ -24,11 +24,14 @@ module Streamly.Internal.Network.Inet.TCP
24 -- * TCP Servers
24 -- * TCP Servers
25 -- ** Unfolds
25 -- ** Unfolds
26 acceptOnAddr
26 acceptOnAddr
27+ , acceptOnAddrWith
27 , acceptOnPort
28 , acceptOnPort
29+ , acceptOnPortWith
28 , acceptOnPortLocal
30 , acceptOnPortLocal
29
31
30 -- ** Streams
32 -- ** Streams
31 , connectionsOnAddr
33 , connectionsOnAddr
34+ , connectionsOnAddrWith
32 , connectionsOnPort
35 , connectionsOnPort
33 , connectionsOnLocalHost
36 , connectionsOnLocalHost
34
37
@@ -131,16 +134,12 @@ import qualified Streamly.Internal.Network.Socket as ISK
131-- Accept (unfolds)
134-- Accept (unfolds)
132-------------------------------------------------------------------------------
135-------------------------------------------------------------------------------
133
136
134−-- | Unfold a tuple @(ipAddr, port)@ into a stream of connected TCP sockets.
137+{-# INLINE acceptOnAddrWith #-}
135−-- @ipAddr@ is the local IP address and @port@ is the local port on which
138+acceptOnAddrWith
136−-- connections are accepted.
137−--
138−-- @since 0.7.0
139−{-# INLINE acceptOnAddr #-}
140−acceptOnAddr
141 :: MonadIO m
139 :: MonadIO m
140+ => [(SocketOption, Int)]
142− => Unfold m ((Word8, Word8, Word8, Word8), PortNumber) Socket
141+ -> Unfold m ((Word8, Word8, Word8, Word8), PortNumber) Socket
143−acceptOnAddr = UF.lmap f accept
142+acceptOnAddrWith opts = UF.lmap f accept
144 where
143 where
145 f (addr, port) =
144 f (addr, port) =
146 (maxListenQueue
145 (maxListenQueue
@@ -148,11 +147,28 @@ acceptOnAddr = UF.lmap f accept
148 { sockFamily = AF_INET
147 { sockFamily = AF_INET
149 , sockType = Stream
148 , sockType = Stream
150 , sockProto = defaultProtocol -- TCP
149 , sockProto = defaultProtocol -- TCP
151− , sockOpts = [(NoDelay,1), (ReuseAddr,1)]
150+ , sockOpts = opts
152 }
151 }
153 , SockAddrInet port (tupleToHostAddress addr)
152 , SockAddrInet port (tupleToHostAddress addr)
154 )
153 )
155
154
155+-- | Unfold a tuple @(ipAddr, port)@ into a stream of connected TCP sockets.
156+-- @ipAddr@ is the local IP address and @port@ is the local port on which
157+-- connections are accepted.
158+--
159+-- @since 0.7.0
160+{-# INLINE acceptOnAddr #-}
161+acceptOnAddr
162+ :: MonadIO m
163+ => Unfold m ((Word8, Word8, Word8, Word8), PortNumber) Socket
164+acceptOnAddr = acceptOnAddrWith []
165+
166+{-# INLINE acceptOnPortWith #-}
167+acceptOnPortWith :: MonadIO m
168+ => [(SocketOption, Int)]
169+ -> Unfold m PortNumber Socket
170+acceptOnPortWith opts = UF.supplyFirst (acceptOnAddrWith opts) (0,0,0,0)
171+
156-- | Like 'acceptOnAddr' but binds on the IPv4 address @0.0.0.0@ i.e. on all
172-- | Like 'acceptOnAddr' but binds on the IPv4 address @0.0.0.0@ i.e. on all
157-- IPv4 addresses/interfaces of the machine and listens for TCP connections on
173-- IPv4 addresses/interfaces of the machine and listens for TCP connections on
158-- the specified port.
174-- the specified port.
@@ -179,6 +195,22 @@ acceptOnPortLocal = UF.supplyFirst acceptOnAddr (127,0,0,1)
179-- Accept (streams)
195-- Accept (streams)
180-------------------------------------------------------------------------------
196-------------------------------------------------------------------------------
181
197
198+{-# INLINE connectionsOnAddrWith #-}
199+connectionsOnAddrWith
200+ :: MonadAsync m
201+ => [(SocketOption, Int)]
202+ -> (Word8, Word8, Word8, Word8)
203+ -> PortNumber
204+ -> SerialT m Socket
205+connectionsOnAddrWith opts addr port =
206+ connections maxListenQueue SockSpec
207+ { sockFamily = AF_INET
208+ , sockType = Stream
209+ , sockProto = defaultProtocol
210+ , sockOpts = opts
211+ }
212+ (SockAddrInet port (tupleToHostAddress addr))
213+
182-- | Like 'connections' but binds on the specified IPv4 address of the machine
214-- | Like 'connections' but binds on the specified IPv4 address of the machine
183-- and listens for TCP connections on the specified port.
215-- and listens for TCP connections on the specified port.
184--
216--
@@ -189,14 +221,7 @@ connectionsOnAddr
189 => (Word8, Word8, Word8, Word8)
221 => (Word8, Word8, Word8, Word8)
190 -> PortNumber
222 -> PortNumber
191 -> SerialT m Socket
223 -> SerialT m Socket
192−connectionsOnAddr addr port =
224+connectionsOnAddr = connectionsOnAddrWith []
193− connections maxListenQueue SockSpec
194− { sockFamily = AF_INET
195− , sockType = Stream
196− , sockProto = defaultProtocol
197− , sockOpts = [(NoDelay,1), (ReuseAddr,1)]
198− }
199− (SockAddrInet port (tupleToHostAddress addr))
200
225
201-- | Like 'connections' but binds on the IPv4 address @0.0.0.0@ i.e. on all
226-- | Like 'connections' but binds on the IPv4 address @0.0.0.0@ i.e. on all
202-- IPv4 addresses/interfaces of the machine and listens for TCP connections on
227-- IPv4 addresses/interfaces of the machine and listens for TCP connections on