Struct smoldot::libp2p::Network[][src]

pub struct Network<TNow, TPeer, TConn> { /* fields omitted */ }

Data structure containing the list of all connections, pending or not, and their latest known state. See also the module-level documentation.

Implementations

impl<TNow, TPeer, TConn> Network<TNow, TPeer, TConn> where
    TNow: Clone + Add<Duration, Output = TNow> + Sub<TNow, Output = Duration> + Ord
[src]

pub fn new(config: Config<TPeer>) -> Self[src]

Initializes a new network data structure.

pub async fn num_established_connections(&self) -> usize[src]

Returns the number of established TCP connections, both incoming and outgoing.

pub fn noise_key(&self) -> &NoiseKey[src]

Returns the Noise key originalled passed as Config::noise_key.

pub fn overlay_networks(&self) -> impl ExactSizeIterator<Item = &OverlayNetwork>[src]

Returns the list the overlay networks originally passed as Config::overlay_networks.

pub fn request_response_protocols(
    &self
) -> impl ExactSizeIterator<Item = &ConfigRequestResponse>
[src]

Returns the list the request-response protocols originally passed as Config::request_response_protocols.

pub async fn peers_list_lock(&self) -> impl Iterator<Item = PeerId>[src]

Returns an iterator to the list of PeerIds that we have an established connection with.

pub async fn add_addresses(
    &self,
    or_insert: impl FnOnce() -> TPeer,
    overlay_network_index: usize,
    peer_id: PeerId,
    addrs: impl IntoIterator<Item = Multiaddr>
)
[src]

pub fn add_incoming_connection(
    &self,
    local_listen_address: &Multiaddr,
    remote_addr: Multiaddr,
    user_data: TConn
) -> ConnectionId
[src]

pub async fn request(
    &self,
    now: TNow,
    target: PeerId,
    protocol_index: usize,
    request_data: Vec<u8>
) -> Result<Vec<u8>, RequestError>
[src]

Sends a request to the given peer, and waits for a response.

This consists in:

  • Opening a substream on an established connection with the target.
  • Negotiating the requested protocol (protocol_index) on this substream using the multistream-select protocol.
  • Sending the request (request_data parameter), prefixed with its length.
  • Waiting for the response (prefixed with its length), which is then returned.

An error happens if there is no suitable connection for that request, if the connection closes while the request is in progress, if the request or response doesn’t respect the protocol limits (see ConfigRequestResponse), or if the remote takes too much time to answer.

As the API of this module is inherently subject to race conditions, it is never possible to guarantee that this function will succeed. RequestError::ConnectionClosed should be handled by retrying the same request again.

Note: This function doesn’t return before the remote has answered. It is strongly recommended to await the returned Future in the background, and not block any important task on this.

Panic

Panics if protocol_index isn’t a valid index in Config::request_response_protocols.

pub async fn queue_notification(
    &self,
    target: &PeerId,
    protocol_index: usize,
    notification: impl Into<Vec<u8>>
) -> Result<(), QueueNotificationError>
[src]

Adds a notification to the queue of notifications to send to the given peer.

Each substream maintains a queue of notifications to be sent to the remote. This method attempts to push a notification to this queue.

As the API of this module is inherently subject to race conditions, it is possible for connections and substreams to no longer exist with this peer. This is a normal situation, and this error should simply be ignored apart from diagnostic purposes.

An error is also returned if the queue exceeds a certain size in bytes, for two reasons:

  • Since the content of the queue is transferred at a limited rate, each notification pushed at the end of the queue will take more time than the previous one to reach the destination. Once the queue reaches a certain size, the time it would take for newly-pushed notifications to reach the destination would start being unreasonably large.

  • If the remote deliberately applies back-pressure on the substream, it is undesirable to increase the memory usage of the local node.

Similarly, the queue being full is a normal situations and notification protocols should be designed in such a way that discarding notifications shouldn’t have a too negative impact.

Regardless of the success of this function, no guarantee exists about the successful delivery of notifications.

pub async fn pending_outcome_ok(
    &self,
    id: PendingId,
    user_data: TConn
) -> ConnectionId
[src]

After calling Network::fill_out_slots, notifies the Network of the success of the dialing attempt.

See also Network::pending_outcome_err.

Panic

Panics if the PendingId is invalid.

pub async fn pending_outcome_err(&self, id: PendingId)[src]

After calling Network::fill_out_slots, notifies the Network of the failure of the dialing attempt.

See also Network::pending_outcome_ok.

Panic

Panics if the PendingId is invalid.

pub async fn accept_notifications_in(
    &self,
    id: ConnectionId,
    overlay_network_index: usize,
    handshake: Vec<u8>
)
[src]

pub async fn respond_in_request(
    &self,
    id: ConnectionId,
    substream_id: SubstreamId,
    response: Result<Vec<u8>, ()>
)
[src]

Responds to an incoming request. Must be called in response to a Event::RequestIn.

Passing an Err corresponds, on the other side, to a established::RequestError::SubstreamClosed.

Has no effect if the connection has been closed in the meanwhile.

pub async fn next_event(&self) -> Event<TConn>[src]

Returns the next event produced by the service.

This function should be called at a high enough rate that Network::read_write can continue pushing events to the internal buffer of events. Failure to call this function often enough will lead to connections being back-pressured. See also Config::pending_api_events_buffer_size.

It is technically possible to call this function multiple times simultaneously, in which case the events will be distributed amongst the multiple calls in an unspecified way. Keep in mind that some Events have logic attached to the order in which they are produced, and calling this function multiple times is therefore discouraged.

pub async fn read_write<'a>(
    &self,
    connection_id: ConnectionId,
    now: TNow,
    incoming_buffer: Option<&[u8]>,
    outgoing_buffer: (&'a mut [u8], &'a mut [u8])
) -> Result<ReadWrite<TNow>, ConnectionError>
[src]

Panic

Panics if connection_id isn’t a valid connection.

pub async fn open_next_substream(
    &self
) -> Option<SubstreamOpen<'_, TNow, TPeer, TConn>>
[src]

pub async fn fill_out_slots<'a>(
    &self,
    overlay_network_index: usize
) -> Option<StartConnect>
[src]

Spawns new outgoing connections in order to fill empty outgoing slots.

Auto Trait Implementations

impl<TNow, TPeer, TConn> !RefUnwindSafe for Network<TNow, TPeer, TConn>

impl<TNow, TPeer, TConn> Send for Network<TNow, TPeer, TConn> where
    TConn: Send,
    TNow: Send,
    TPeer: Send

impl<TNow, TPeer, TConn> Sync for Network<TNow, TPeer, TConn> where
    TConn: Send,
    TNow: Send,
    TPeer: Send

impl<TNow, TPeer, TConn> Unpin for Network<TNow, TPeer, TConn> where
    TPeer: Unpin

impl<TNow, TPeer, TConn> !UnwindSafe for Network<TNow, TPeer, TConn>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Conv for T

impl<T> Conv for T

impl<T> Downcast for T where
    T: Any

impl<T> DowncastSync for T where
    T: Send + Sync + Any

impl<T> FmtForward for T

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> Pipe for T where
    T: ?Sized

impl<T> Pipe for T

impl<T> PipeAsRef for T

impl<T> PipeBorrow for T

impl<T> PipeDeref for T

impl<T> PipeRef for T

impl<T> Pointable for T

type Init = T

The type for initializers.

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T> Tap for T

impl<T> Tap for T

impl<T, U> TapAsRef<U> for T where
    U: ?Sized

impl<T, U> TapBorrow<U> for T where
    U: ?Sized

impl<T> TapDeref for T

impl<T> TryConv for T

impl<T> TryConv for T

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

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