| Safe Haskell | None |
|---|---|
| Language | GHC2021 |
Network.LibP2P.Switch.Listen
Description
Listen loop for the Switch (docs/08-switch.md §Listening).
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]
Connection gating
data ConnectionGater Source #
Connection gater: policy-based admission control (docs/08-switch.md §Connection Gating).
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).