Trait libp2p::swarm::NetworkBehaviour [−][src]
pub trait NetworkBehaviour: 'static + Send {
type ProtocolsHandler: IntoProtocolsHandler;
type OutEvent: 'static + Send;
Show 18 methods
fn new_handler(&mut self) -> Self::ProtocolsHandler;
fn inject_event(
&mut self,
peer_id: PeerId,
connection: ConnectionId,
event: <<Self::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutEvent
);
fn poll(
&mut self,
cx: &mut Context<'_>,
params: &mut impl PollParameters
) -> Poll<NetworkBehaviourAction<Self::OutEvent, Self::ProtocolsHandler, <<Self::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InEvent>>;
fn addresses_of_peer(&mut self, &PeerId) -> Vec<Multiaddr, Global> { ... }
fn inject_connected(&mut self, &PeerId) { ... }
fn inject_disconnected(&mut self, &PeerId) { ... }
fn inject_connection_established(
&mut self,
_peer_id: &PeerId,
_connection_id: &ConnectionId,
_endpoint: &ConnectedPoint,
_failed_addresses: Option<&Vec<Multiaddr, Global>>
) { ... }
fn inject_connection_closed(
&mut self,
&PeerId,
&ConnectionId,
&ConnectedPoint,
<Self::ProtocolsHandler as IntoProtocolsHandler>::Handler
) { ... }
fn inject_address_change(
&mut self,
&PeerId,
&ConnectionId,
_old: &ConnectedPoint,
_new: &ConnectedPoint
) { ... }
fn inject_dial_failure(
&mut self,
_peer_id: Option<PeerId>,
_handler: Self::ProtocolsHandler,
_error: &DialError
) { ... }
fn inject_listen_failure(
&mut self,
_local_addr: &Multiaddr,
_send_back_addr: &Multiaddr,
_handler: Self::ProtocolsHandler
) { ... }
fn inject_new_listener(&mut self, _id: ListenerId) { ... }
fn inject_new_listen_addr(&mut self, _id: ListenerId, _addr: &Multiaddr) { ... }
fn inject_expired_listen_addr(&mut self, _id: ListenerId, _addr: &Multiaddr) { ... }
fn inject_listener_error(
&mut self,
_id: ListenerId,
_err: &(dyn Error + 'static)
) { ... }
fn inject_listener_closed(
&mut self,
_id: ListenerId,
_reason: Result<(), &Error>
) { ... }
fn inject_new_external_addr(&mut self, _addr: &Multiaddr) { ... }
fn inject_expired_external_addr(&mut self, _addr: &Multiaddr) { ... }
}
Expand description
A behaviour for the network. Allows customizing the swarm.
This trait has been designed to be composable. Multiple implementations can be combined into one that handles all the behaviours at once.
Deriving NetworkBehaviour
Crate users can implement this trait by using the the #[derive(NetworkBehaviour)]
proc macro re-exported by the libp2p
crate. The macro generates a delegating trait
implementation for the struct
, which delegates method calls to all trait members.
By default the derive sets the NetworkBehaviour::OutEvent
as ()
but this can be overridden
with #[behaviour(out_event = "AnotherType")]
.
Struct members that don’t implement NetworkBehaviour
must be annotated with #[behaviour(ignore)]
.
By default, events generated by the remaining members are delegated to NetworkBehaviourEventProcess
implementations. Those must be provided by the user on the type that NetworkBehaviour
is
derived on.
Alternatively, users can specify #[behaviour(event_process = false)]
. In this case, users
should provide a custom out_event
and implement From
for each of the event types generated
by the struct members.
Not processing events within the derived NetworkBehaviour
will cause them to be emitted as
part of polling the swarm in SwarmEvent::Behaviour
.
Optionally one can provide a custom poll
function through the #[behaviour(poll_method = "poll")]
attribute.
This function must have the same signature as the NetworkBehaviour
function and will
be called last within the generated NetworkBehaviour
implementation.
Associated Types
Handler for all the protocols the network behaviour supports.
Required methods
fn new_handler(&mut self) -> Self::ProtocolsHandler
fn new_handler(&mut self) -> Self::ProtocolsHandler
Creates a new ProtocolsHandler
for a connection with a peer.
Every time an incoming connection is opened, and every time another NetworkBehaviour
emitted a dial request, this method is called.
The returned object is a handler for that specific connection, and will be moved to a background task dedicated to that connection.
The network behaviour (ie. the implementation of this trait) and the handlers it has spawned
(ie. the objects returned by new_handler
) can communicate by passing messages. Messages
sent from the handler to the behaviour are injected with NetworkBehaviour::inject_event
,
and the behaviour can send a message to the handler by making NetworkBehaviour::poll
return NetworkBehaviourAction::NotifyHandler
.
Note that the handler is returned to the NetworkBehaviour
on connection failure and
connection closing.
fn inject_event(
&mut self,
peer_id: PeerId,
connection: ConnectionId,
event: <<Self::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutEvent
)
fn inject_event(
&mut self,
peer_id: PeerId,
connection: ConnectionId,
event: <<Self::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutEvent
)
Informs the behaviour about an event generated by the handler dedicated to the peer identified by peer_id
.
for the behaviour.
The peer_id
is guaranteed to be in a connected state. In other words, inject_connected
has previously been called with this PeerId
.
fn poll(
&mut self,
cx: &mut Context<'_>,
params: &mut impl PollParameters
) -> Poll<NetworkBehaviourAction<Self::OutEvent, Self::ProtocolsHandler, <<Self::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InEvent>>
fn poll(
&mut self,
cx: &mut Context<'_>,
params: &mut impl PollParameters
) -> Poll<NetworkBehaviourAction<Self::OutEvent, Self::ProtocolsHandler, <<Self::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InEvent>>
Polls for things that swarm should do.
This API mimics the API of the Stream
trait. The method may register the current task in
order to wake it up at a later point in time.
Provided methods
Addresses that this behaviour is aware of for this specific peer, and that may allow reaching the peer.
The addresses will be tried in the order returned by this function, which means that they should be ordered by decreasing likelihood of reachability. In other words, the first address should be the most likely to be reachable.
fn inject_connected(&mut self, &PeerId)
fn inject_connected(&mut self, &PeerId)
Indicate to the behaviour that we connected to the node with the given peer id.
This node now has a handler (as spawned by new_handler
) running in the background.
This method is only called when the first connection to the peer is established, preceded by
inject_connection_established
.
fn inject_disconnected(&mut self, &PeerId)
fn inject_disconnected(&mut self, &PeerId)
Indicates to the behaviour that we disconnected from the node with the given peer id.
There is no handler running anymore for this node. Any event that has been sent to it may or may not have been processed by the handler.
This method is only called when the last established connection to the peer is closed,
preceded by inject_connection_closed
.
fn inject_connection_established(
&mut self,
_peer_id: &PeerId,
_connection_id: &ConnectionId,
_endpoint: &ConnectedPoint,
_failed_addresses: Option<&Vec<Multiaddr, Global>>
)
fn inject_connection_established(
&mut self,
_peer_id: &PeerId,
_connection_id: &ConnectionId,
_endpoint: &ConnectedPoint,
_failed_addresses: Option<&Vec<Multiaddr, Global>>
)
Informs the behaviour about a newly established connection to a peer.
fn inject_connection_closed(
&mut self,
&PeerId,
&ConnectionId,
&ConnectedPoint,
<Self::ProtocolsHandler as IntoProtocolsHandler>::Handler
)
fn inject_connection_closed(
&mut self,
&PeerId,
&ConnectionId,
&ConnectedPoint,
<Self::ProtocolsHandler as IntoProtocolsHandler>::Handler
)
Informs the behaviour about a closed connection to a peer.
A call to this method is always paired with an earlier call to
inject_connection_established
with the same peer ID, connection ID and
endpoint.
fn inject_address_change(
&mut self,
&PeerId,
&ConnectionId,
_old: &ConnectedPoint,
_new: &ConnectedPoint
)
fn inject_address_change(
&mut self,
&PeerId,
&ConnectionId,
_old: &ConnectedPoint,
_new: &ConnectedPoint
)
Informs the behaviour that the ConnectedPoint
of an existing connection has changed.
fn inject_dial_failure(
&mut self,
_peer_id: Option<PeerId>,
_handler: Self::ProtocolsHandler,
_error: &DialError
)
fn inject_dial_failure(
&mut self,
_peer_id: Option<PeerId>,
_handler: Self::ProtocolsHandler,
_error: &DialError
)
Indicates to the behaviour that the dial to a known or unknown node failed.
fn inject_listen_failure(
&mut self,
_local_addr: &Multiaddr,
_send_back_addr: &Multiaddr,
_handler: Self::ProtocolsHandler
)
fn inject_listen_failure(
&mut self,
_local_addr: &Multiaddr,
_send_back_addr: &Multiaddr,
_handler: Self::ProtocolsHandler
)
Indicates to the behaviour that an error happened on an incoming connection during its initial handshake.
This can include, for example, an error during the handshake of the encryption layer, or the connection unexpectedly closed.
fn inject_new_listener(&mut self, _id: ListenerId)
fn inject_new_listener(&mut self, _id: ListenerId)
Indicates to the behaviour that a new listener was created.
fn inject_new_listen_addr(&mut self, _id: ListenerId, _addr: &Multiaddr)
fn inject_new_listen_addr(&mut self, _id: ListenerId, _addr: &Multiaddr)
Indicates to the behaviour that we have started listening on a new multiaddr.
fn inject_expired_listen_addr(&mut self, _id: ListenerId, _addr: &Multiaddr)
fn inject_expired_listen_addr(&mut self, _id: ListenerId, _addr: &Multiaddr)
Indicates to the behaviour that a multiaddr we were listening on has expired, which means that we are no longer listening in it.
fn inject_listener_error(
&mut self,
_id: ListenerId,
_err: &(dyn Error + 'static)
)
fn inject_listener_error(
&mut self,
_id: ListenerId,
_err: &(dyn Error + 'static)
)
A listener experienced an error.
fn inject_listener_closed(
&mut self,
_id: ListenerId,
_reason: Result<(), &Error>
)
fn inject_listener_closed(
&mut self,
_id: ListenerId,
_reason: Result<(), &Error>
)
A listener closed.
fn inject_new_external_addr(&mut self, _addr: &Multiaddr)
fn inject_new_external_addr(&mut self, _addr: &Multiaddr)
Indicates to the behaviour that we have discovered a new external address for us.
fn inject_expired_external_addr(&mut self, _addr: &Multiaddr)
fn inject_expired_external_addr(&mut self, _addr: &Multiaddr)
Indicates to the behaviour that an external address was removed.