Crate libp2p_swarm

Source
Expand description

High-level network manager.

A Swarm contains the state of the network as a whole. The entire behaviour of a libp2p network can be controlled through the Swarm. The Swarm struct contains all active and pending connections to remotes and manages the state of all the substreams that have been opened, and all the upgrades that were built upon these substreams.

§Initializing a Swarm

Creating a Swarm requires three things:

  1. A network identity of the local node in form of a PeerId.
  2. An implementation of the Transport trait. This is the type that will be used in order to reach nodes on the network based on their address. See the transport module for more information.
  3. An implementation of the NetworkBehaviour trait. This is a state machine that defines how the swarm should behave once it is connected to a node.

§Network Behaviour

The NetworkBehaviour trait is implemented on types that indicate to the swarm how it should behave. This includes which protocols are supported and which nodes to try to connect to. It is the NetworkBehaviour that controls what happens on the network. Multiple types that implement NetworkBehaviour can be composed into a single behaviour.

§Protocols Handler

The ConnectionHandler trait defines how each active connection to a remote should behave: how to handle incoming substreams, which protocols are supported, when to open a new outbound substream, etc.

Re-exports§

pub use behaviour::AddressChange;
pub use behaviour::CloseConnection;
pub use behaviour::ConnectionClosed;
pub use behaviour::DialFailure;
pub use behaviour::ExpiredListenAddr;
pub use behaviour::ExternalAddrExpired;
pub use behaviour::ExternalAddresses;
pub use behaviour::FromSwarm;
pub use behaviour::ListenAddresses;
pub use behaviour::ListenFailure;
pub use behaviour::ListenerClosed;
pub use behaviour::ListenerError;
pub use behaviour::NetworkBehaviour;
pub use behaviour::NewExternalAddrCandidate;
pub use behaviour::NewExternalAddrOfPeer;
pub use behaviour::NewListenAddr;
pub use behaviour::NotifyHandler;
pub use behaviour::PeerAddresses;
pub use behaviour::ToSwarm;
pub use handler::ConnectionHandler;
pub use handler::ConnectionHandlerEvent;
pub use handler::ConnectionHandlerSelect;
pub use handler::OneShotHandler;
pub use handler::OneShotHandlerConfig;
pub use handler::StreamUpgradeError;
pub use handler::SubstreamProtocol;

Modules§

behaviour
dial_opts
dummy
handler
Once a connection to a remote peer is established, a ConnectionHandler negotiates and handles one or more specific protocols on the connection.

Structs§

Config
ConnectionCounters
Network connection information.
ConnectionDenied
A connection was denied.
ConnectionId
Connection identifier.
InvalidProtocol
ListenOpts
NetworkInfo
Information about the connections obtained by Swarm::network_info().
Stream
StreamProtocol
Identifies a protocol for a stream.
SupportedProtocols
Swarm
Contains the state of the network, plus the way it should behave.

Enums§

ConnectionError
Errors that can occur in the context of an established Connection.
DialError
Possible errors when trying to establish or upgrade an outbound connection.
ListenError
Possible errors when upgrading an inbound connection.
SwarmEvent
Event generated by the Swarm.

Traits§

Executor
Implemented on objects that can run a Future in the background.

Type Aliases§

THandler
ConnectionHandler of the NetworkBehaviour for all the protocols the NetworkBehaviour supports.
THandlerInEvent
Custom event that can be received by the ConnectionHandler of the NetworkBehaviour.
THandlerOutEvent
Custom event that can be produced by the ConnectionHandler of the NetworkBehaviour.

Derive Macros§

NetworkBehaviourmacros
Generates a delegating NetworkBehaviour implementation for the struct this is used for. See the trait documentation for better description.