| Safe Haskell | None |
|---|---|
| Language | GHC2021 |
LibP2P.Switch.Listen
Description
Listen loop for the Switch.
Accepts inbound connections, applies connection gating policy, upgrades to secure multiplexed connections, and dispatches inbound streams to registered protocol handlers.
Synopsis
- data ConnectionGater = ConnectionGater {
- gateAccept :: !(Multiaddr -> IO Bool)
- gateSecured :: !(PeerId -> IO Bool)
- defaultConnectionGater :: ConnectionGater
- handleInbound :: Switch -> ConnectionGater -> RawConnection -> IO ()
- streamAcceptLoop :: Switch -> Connection -> IO ()
- dispatchStream :: Switch -> Connection -> StreamIO -> IO ()
- switchListen :: Switch -> ConnectionGater -> [Multiaddr] -> IO [Multiaddr]
- acceptLoop :: Switch -> ConnectionGater -> Listener -> IO ()
- switchListenAddrs :: Switch -> IO [Multiaddr]
- switchWithdrawListener :: Switch -> Multiaddr -> IO ()
Connection gating
data ConnectionGater Source #
Connection gater: policy-based admission control.
Called at multiple points during connection establishment to allow or deny based on policy (IP blocklist, Peer ID allowlist, etc.).
Constructors
| ConnectionGater | |
Fields
| |
defaultConnectionGater :: ConnectionGater Source #
Default gater that allows all connections.
Inbound connection handling
handleInbound :: Switch -> ConnectionGater -> RawConnection -> IO () Source #
Handle a single inbound connection: gate → upgrade → pool → stream accept loop.
This function blocks until the connection closes. Each accepted connection should be spawned in its own async thread from the accept loop.
Stream dispatch
streamAcceptLoop :: Switch -> Connection -> IO () Source #
Accept inbound streams and dispatch to registered protocol handlers.
Runs forever, accepting streams from the muxer and spawning a handler thread for each. Uses multistream-select to negotiate the protocol, then dispatches to the registered StreamHandler.
dispatchStream :: Switch -> Connection -> StreamIO -> IO () Source #
Dispatch a single inbound stream to the appropriate protocol handler.
Runs multistream-select as responder to determine which protocol the remote peer wants, then looks up and invokes the registered handler.
Listen orchestration
switchListen :: Switch -> ConnectionGater -> [Multiaddr] -> IO [Multiaddr] Source #
Start listening on the given addresses.
For each address, selects a matching transport, binds a listener, and spawns an accept loop that handles inbound connections. Returns the actual bound addresses (port 0 resolved to actual port). Fails if the switch is already closed.
acceptLoop :: Switch -> ConnectionGater -> Listener -> IO () Source #
Accept loop: forever accepts connections and spawns handleInbound threads. Catches exceptions from individual connections without stopping the loop. Stops when the listener is closed (accept throws).
switchListenAddrs :: Switch -> IO [Multiaddr] Source #
Get the current listen addresses from all active listeners.
switchWithdrawListener :: Switch -> Multiaddr -> IO () Source #
Withdraw the listener bound to the given address: cancel its accept
loop, close the underlying transport listener, and remove it from the
Switch's listener set so switchListenAddrs stops reporting it.
For listeners whose reachability guarantee can lapse independently of the Switch's own lifetime (e.g. a circuit relay reservation that expired or was refused on refresh), this withdraws just that one address without tearing down the rest of the Switch. A no-op if no listener is bound to the address (already withdrawn, or never was).