hs-libp2p-0.2.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 #

Policy hooks applied to inbound and outbound connection establishment.

Constructors

ConnectionGater 

Fields

defaultConnectionGater :: ConnectionGater Source #

Default gater that allows all connections.

Inbound connection handling

handleInbound :: Switch -> 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.

Stores the supplied gater on the Switch, then selects a transport for each address, binds a listener, and spawns an accept loop. The same policy applies to subsequent outbound dials. Returns the actual bound addresses. Fails if the switch is already closed.

acceptLoop :: Switch -> 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).