libp2p::swarm::behaviour

Enum ToSwarm

#[non_exhaustive]
pub enum ToSwarm<TOutEvent, TInEvent> { GenerateEvent(TOutEvent), Dial { opts: DialOpts, }, ListenOn { opts: ListenOpts, }, RemoveListener { id: ListenerId, }, NotifyHandler { peer_id: PeerId, handler: NotifyHandler, event: TInEvent, }, NewExternalAddrCandidate(Multiaddr), ExternalAddrConfirmed(Multiaddr), ExternalAddrExpired(Multiaddr), CloseConnection { peer_id: PeerId, connection: CloseConnection, }, NewExternalAddrOfPeer { peer_id: PeerId, address: Multiaddr, }, }
Expand description

A command issued from a NetworkBehaviour for the Swarm.

Variants (Non-exhaustive)§

This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future 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.

Fields

§

ListenOn

Instructs the Swarm to listen on the provided address.

Fields

§

RemoveListener

Instructs the Swarm to remove the listener.

Fields

§

NotifyHandler

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.

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.

§

NewExternalAddrCandidate(Multiaddr)

Reports a new candidate for an external address to the Swarm.

The emphasis on a new candidate is important. Protocols MUST take care to only emit a candidate once per “source”. For example, the observed address of a TCP connection does not change throughout its lifetime. Thus, only one candidate should be emitted per connection.

This makes the report frequency of an address a meaningful data-point for consumers of this event. This address will be shared with all NetworkBehaviours via FromSwarm::NewExternalAddrCandidate.

This address could come from a variety of sources:

  • A protocol such as identify obtained it from a remote.
  • The user provided it based on configuration.
  • We made an educated guess based on one of our listen addresses.
§

ExternalAddrConfirmed(Multiaddr)

Indicates to the Swarm that the provided address is confirmed to be externally reachable.

This is intended to be issued in response to a FromSwarm::NewExternalAddrCandidate if we are indeed externally reachable on this address. This address will be shared with all NetworkBehaviours via FromSwarm::ExternalAddrConfirmed.

§

ExternalAddrExpired(Multiaddr)

Indicates to the Swarm that we are no longer externally reachable under the provided address.

This expires an address that was earlier confirmed via ToSwarm::ExternalAddrConfirmed. This address will be shared with all NetworkBehaviours via FromSwarm::ExternalAddrExpired.

§

CloseConnection

Instructs the Swarm to initiate a graceful close of one or all connections with the given peer.

Closing a connection via ToSwarm::CloseConnection will poll ConnectionHandler::poll_close to completion. In most cases, stopping to “use” a connection is enough to have it closed. The keep-alive algorithm will close a connection automatically once all ConnectionHandlers are idle.

Use this command if you want to close a connection despite it still being in use by one or more handlers.

Fields

§peer_id: PeerId

The peer to disconnect.

§connection: CloseConnection

Whether to close a specific or all connections to the given peer.

§

NewExternalAddrOfPeer

Reports external address of a remote peer to the Swarm and through that to other NetworkBehaviours.

Fields

§peer_id: PeerId
§address: Multiaddr

Implementations§

§

impl<TOutEvent, TInEventOld> ToSwarm<TOutEvent, TInEventOld>

pub fn map_in<TInEventNew>( self, f: impl FnOnce(TInEventOld) -> TInEventNew, ) -> ToSwarm<TOutEvent, TInEventNew>

Map the handler event.

§

impl<TOutEvent, THandlerIn> ToSwarm<TOutEvent, THandlerIn>

pub fn map_out<E>( self, f: impl FnOnce(TOutEvent) -> E, ) -> ToSwarm<E, THandlerIn>

Map the event the swarm will return.

Trait Implementations§

§

impl<TOutEvent, TInEvent> Debug for ToSwarm<TOutEvent, TInEvent>
where TOutEvent: Debug, TInEvent: Debug,

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<TOutEvent, TInEvent> Freeze for ToSwarm<TOutEvent, TInEvent>
where TOutEvent: Freeze, TInEvent: Freeze,

§

impl<TOutEvent, TInEvent> RefUnwindSafe for ToSwarm<TOutEvent, TInEvent>
where TOutEvent: RefUnwindSafe, TInEvent: RefUnwindSafe,

§

impl<TOutEvent, TInEvent> Send for ToSwarm<TOutEvent, TInEvent>
where TOutEvent: Send, TInEvent: Send,

§

impl<TOutEvent, TInEvent> Sync for ToSwarm<TOutEvent, TInEvent>
where TOutEvent: Sync, TInEvent: Sync,

§

impl<TOutEvent, TInEvent> Unpin for ToSwarm<TOutEvent, TInEvent>
where TOutEvent: Unpin, TInEvent: Unpin,

§

impl<TOutEvent, TInEvent> UnwindSafe for ToSwarm<TOutEvent, TInEvent>
where TOutEvent: UnwindSafe, TInEvent: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

§

impl<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more