Module libp2p_core::upgrade[][src]

Contains everything related to upgrading a connection or a substream to use a protocol.

After a connection with a remote has been successfully established or a substream successfully opened, the next step is to upgrade this connection or substream to use a protocol.

This is where the UpgradeInfo, InboundUpgrade and OutboundUpgrade traits come into play. The InboundUpgrade and OutboundUpgrade traits are implemented on types that represent a collection of one or more possible protocols for respectively an ingoing or outgoing connection or substream.

Note: Multiple versions of the same protocol are treated as different protocols. For example, /foo/1.0.0 and /foo/1.1.0 are totally unrelated as far as upgrading is concerned.

Upgrade process

An upgrade is performed in two steps:

  • A protocol negotiation step. The UpgradeInfo::protocol_info method is called to determine which protocols are supported by the trait implementation. The multistream-select protocol is used in order to agree on which protocol to use amongst the ones supported.

  • A handshake. After a successful negotiation, the InboundUpgrade::upgrade_inbound or OutboundUpgrade::upgrade_outbound method is called. This method will return a Future that performs a handshake. This handshake is considered mandatory, however in practice it is possible for the trait implementation to return a dummy Future that doesn't perform any action and immediately succeeds.

After an upgrade is successful, an object of type InboundUpgrade::Output or OutboundUpgrade::Output is returned. The actual object depends on the implementation and there is no constraint on the traits that it should implement, however it is expected that it can be used by the user to control the behaviour of the protocol.

Note: You can use the apply_inbound or apply_outbound methods to try upgrade a connection or substream. However if you use the recommended Swarm or ProtocolsHandler APIs, the upgrade is automatically handled for you and you don't need to use these methods.

Re-exports

pub use crate::Negotiated;

Structs

DeniedUpgrade

Dummy implementation of UpgradeInfo/InboundUpgrade/OutboundUpgrade that doesn't support any protocol.

FromFnUpgrade

Implements the UpgradeInfo, InboundUpgrade and OutboundUpgrade traits.

InboundUpgradeApply

Future returned by apply_inbound. Drives the upgrade process.

MapInboundUpgrade

Wraps around an upgrade and applies a closure to the output.

MapInboundUpgradeErr

Wraps around an upgrade and applies a closure to the error.

MapOutboundUpgrade

Wraps around an upgrade and applies a closure to the output.

MapOutboundUpgradeErr

Wraps around an upgrade and applies a closure to the error.

NegotiatedComplete

A Future that waits on the completion of protocol negotiation.

OptionalUpgrade

Upgrade that can be disabled at runtime.

OutboundUpgradeApply

Future returned by apply_outbound. Drives the upgrade process.

SelectUpgrade

Upgrade that combines two upgrades into one. Supports all the protocols supported by either sub-upgrade.

Enums

EitherUpgrade

A type to represent two possible upgrade types (inbound or outbound).

NegotiationError

Error that can happen when negotiating a protocol with the remote.

ProtocolError

A protocol error.

ReadOneError

Error while reading one message.

UpgradeError

Error that can happen when upgrading a connection or substream to use a protocol.

Version

Supported multistream-select versions.

Traits

InboundUpgrade

Possible upgrade on an inbound connection or substream.

InboundUpgradeExt

Extension trait for InboundUpgrade. Automatically implemented on all types that implement InboundUpgrade.

OutboundUpgrade

Possible upgrade on an outbound connection or substream.

OutboundUpgradeExt

Extention trait for OutboundUpgrade. Automatically implemented on all types that implement OutboundUpgrade.

ProtocolName

Types serving as protocol names.

UpgradeInfo

Common trait for upgrades that can be applied on inbound substreams, outbound substreams, or both.

Functions

apply

Applies an upgrade to the inbound and outbound direction of a connection or substream.

apply_inbound

Tries to perform an upgrade on an inbound connection or substream.

apply_outbound

Tries to perform an upgrade on an outbound connection or substream.

from_fn

Initializes a new FromFnUpgrade.

read_one

Reads a length-prefixed message from the given socket.

read_varint

Reads a variable-length integer from the socket.

write_one

Send a message to the given socket, then shuts down the writing side.

write_varint

Writes a variable-length integer to the socket.

write_with_len_prefix

Send a message to the given socket with a length prefix appended to it. Also flushes the socket.