Enum libp2p_swarm::behaviour::ToSwarm
source · pub enum ToSwarm<TOutEvent, TInEvent> {
GenerateEvent(TOutEvent),
Dial {
opts: DialOpts,
},
NotifyHandler {
peer_id: PeerId,
handler: NotifyHandler,
event: TInEvent,
},
ReportObservedAddr {
address: Multiaddr,
score: AddressScore,
},
CloseConnection {
peer_id: PeerId,
connection: CloseConnection,
},
}
Expand description
A command issued from a NetworkBehaviour
for the Swarm
.
Variants§
GenerateEvent(TOutEvent)
Instructs the Swarm
to return an event when it is being polled.
Dial
Instructs the swarm to start a dial.
On success, NetworkBehaviour::on_swarm_event
with ConnectionEstablished
is invoked.
On failure, NetworkBehaviour::on_swarm_event
with DialFailure
is invoked.
DialOpts
provides access to the ConnectionId
via DialOpts::connection_id
.
This ConnectionId
will be used throughout the connection’s lifecycle to associate events with it.
This allows a NetworkBehaviour
to identify a connection that resulted out of its own dial request.
NotifyHandler
Fields
peer_id: PeerId
The peer for whom a ConnectionHandler
should be notified.
handler: NotifyHandler
The options w.r.t. which connection handler to notify of the event.
event: TInEvent
The event to send.
Instructs the Swarm
to send an event to the handler dedicated to a
connection with a peer.
If the Swarm
is connected to the peer, the message is delivered to the
ConnectionHandler
instance identified by the peer ID and connection ID.
If the specified connection no longer exists, the event is silently dropped.
Typically the connection ID given is the same as the one passed to
NetworkBehaviour::on_connection_handler_event
, i.e. whenever the behaviour wishes to
respond to a request on the same connection (and possibly the same
substream, as per the implementation of ConnectionHandler
).
Note that even if the peer is currently connected, connections can get closed at any time and thus the event may not reach a handler.
ReportObservedAddr
Fields
address: Multiaddr
The observed address of the local node.
score: AddressScore
The score to associate with this observation, i.e. an indicator for the trusworthiness of this address relative to other observed addresses.
Informs the Swarm
about an address observed by a remote for
the local node by which the local node is supposedly publicly
reachable.
It is advisable to issue ReportObservedAddr
actions at a fixed frequency
per node. This way address information will be more accurate over time
and individual outliers carry less weight.
CloseConnection
Fields
connection: CloseConnection
Whether to close a specific or all connections to the given peer.
Instructs the Swarm
to initiate a graceful close of one or all connections
with the given peer.
Note: Closing a connection via
ToSwarm::CloseConnection
does not inform the
corresponding ConnectionHandler
.
Closing a connection via a ConnectionHandler
can be done
either in a collaborative manner across ConnectionHandler
s
with ConnectionHandler::connection_keep_alive
or directly with
ConnectionHandlerEvent::Close
.