libp2p-hs-0.1.0.0: Haskell implementation of the libp2p networking stack
Safe HaskellNone
LanguageGHC2021

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

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.