2018-08-31 10:31:34 +02:00
|
|
|
// Copyright 2018 Parity Technologies (UK) Ltd.
|
2017-11-28 12:20:28 +01:00
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the "Software"),
|
|
|
|
// to deal in the Software without restriction, including without limitation
|
|
|
|
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
// and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
// Software is furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
// DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2018-11-16 12:24:12 +01:00
|
|
|
//! Muxing is the process of splitting a connection into multiple substreams.
|
|
|
|
//!
|
|
|
|
//! The main item of this module is the `StreamMuxer` trait. An implementation of `StreamMuxer`
|
2022-06-23 13:52:11 +02:00
|
|
|
//! has ownership of a connection, lets you open and close substreams.
|
2018-11-16 12:24:12 +01:00
|
|
|
//!
|
|
|
|
//! > **Note**: You normally don't need to use the methods of the `StreamMuxer` directly, as this
|
|
|
|
//! > is managed by the library's internals.
|
|
|
|
//!
|
|
|
|
//! Each substream of a connection is an isolated stream of data. All the substreams are muxed
|
|
|
|
//! together so that the data read from or written to each substream doesn't influence the other
|
|
|
|
//! substreams.
|
|
|
|
//!
|
|
|
|
//! In the context of libp2p, each substream can use a different protocol. Contrary to opening a
|
|
|
|
//! connection, opening a substream is almost free in terms of resources. This means that you
|
|
|
|
//! shouldn't hesitate to rapidly open and close substreams, and to design protocols that don't
|
|
|
|
//! require maintaining long-lived channels of communication.
|
|
|
|
//!
|
|
|
|
//! > **Example**: The Kademlia protocol opens a new substream for each request it wants to
|
|
|
|
//! > perform. Multiple requests can be performed simultaneously by opening multiple
|
|
|
|
//! > substreams, without having to worry about associating responses with the
|
|
|
|
//! > right request.
|
|
|
|
//!
|
|
|
|
//! # Implementing a muxing protocol
|
|
|
|
//!
|
|
|
|
//! In order to implement a muxing protocol, create an object that implements the `UpgradeInfo`,
|
|
|
|
//! `InboundUpgrade` and `OutboundUpgrade` traits. See the `upgrade` module for more information.
|
|
|
|
//! The `Output` associated type of the `InboundUpgrade` and `OutboundUpgrade` traits should be
|
|
|
|
//! identical, and should be an object that implements the `StreamMuxer` trait.
|
|
|
|
//!
|
|
|
|
//! The upgrade process will take ownership of the connection, which makes it possible for the
|
|
|
|
//! implementation of `StreamMuxer` to control everything that happens on the wire.
|
|
|
|
|
2022-06-23 13:52:11 +02:00
|
|
|
use futures::{task::Context, task::Poll, AsyncRead, AsyncWrite};
|
2020-06-30 17:10:53 +02:00
|
|
|
use multiaddr::Multiaddr;
|
2017-11-28 12:20:28 +01:00
|
|
|
|
2022-06-15 17:25:31 +02:00
|
|
|
pub use self::boxed::StreamMuxerBox;
|
2022-06-23 13:52:11 +02:00
|
|
|
pub use self::boxed::SubstreamBox;
|
2019-04-23 15:08:59 +02:00
|
|
|
pub use self::singleton::SingletonMuxer;
|
|
|
|
|
2022-06-15 17:25:31 +02:00
|
|
|
mod boxed;
|
2019-04-23 15:08:59 +02:00
|
|
|
mod singleton;
|
|
|
|
|
2022-06-23 13:52:11 +02:00
|
|
|
/// Provides multiplexing for a connection by allowing users to open substreams.
|
2019-03-11 17:19:50 +01:00
|
|
|
///
|
2022-06-23 13:52:11 +02:00
|
|
|
/// A substream created by a [`StreamMuxer`] is a type that implements [`AsyncRead`] and [`AsyncWrite`].
|
2019-03-11 17:19:50 +01:00
|
|
|
///
|
2022-06-23 13:52:11 +02:00
|
|
|
/// Inbound substreams are reported via [`StreamMuxer::poll_event`].
|
|
|
|
/// Outbound substreams can be opened via [`StreamMuxer::open_outbound`] and subsequent polling via
|
|
|
|
/// [`StreamMuxer::poll_outbound`].
|
2017-11-28 12:20:28 +01:00
|
|
|
pub trait StreamMuxer {
|
2018-03-07 16:20:55 +01:00
|
|
|
/// Type of the object that represents the raw substream where data can be read and written.
|
2022-06-23 13:52:11 +02:00
|
|
|
type Substream: AsyncRead + AsyncWrite;
|
2018-05-14 14:49:29 +02:00
|
|
|
|
2018-03-07 16:20:55 +01:00
|
|
|
/// Future that will be resolved when the outgoing substream is open.
|
2018-08-31 10:31:34 +02:00
|
|
|
type OutboundSubstream;
|
2017-11-28 12:20:28 +01:00
|
|
|
|
2019-04-28 14:42:18 +03:00
|
|
|
/// Error type of the muxer
|
2022-06-24 08:26:49 +02:00
|
|
|
type Error: std::error::Error;
|
2019-04-28 14:42:18 +03:00
|
|
|
|
2020-06-30 17:10:53 +02:00
|
|
|
/// Polls for a connection-wide event.
|
2018-08-31 10:31:34 +02:00
|
|
|
///
|
|
|
|
/// This function behaves the same as a `Stream`.
|
2018-09-11 11:17:44 +02:00
|
|
|
///
|
2019-09-16 11:08:44 +02:00
|
|
|
/// If `Pending` is returned, then the current task will be notified once the muxer
|
2018-09-11 11:17:44 +02:00
|
|
|
/// is ready to be polled, similar to the API of `Stream::poll()`.
|
2019-03-11 17:19:50 +01:00
|
|
|
/// Only the latest task that was used to call this method may be notified.
|
|
|
|
///
|
2020-11-24 17:36:44 +01:00
|
|
|
/// It is permissible and common to use this method to perform background
|
|
|
|
/// work, such as processing incoming packets and polling timers.
|
|
|
|
///
|
2019-03-11 17:19:50 +01:00
|
|
|
/// An error can be generated if the connection has been closed.
|
2021-08-11 13:12:12 +02:00
|
|
|
fn poll_event(
|
|
|
|
&self,
|
|
|
|
cx: &mut Context<'_>,
|
|
|
|
) -> Poll<Result<StreamMuxerEvent<Self::Substream>, Self::Error>>;
|
2017-11-28 12:20:28 +01:00
|
|
|
|
2019-03-11 17:19:50 +01:00
|
|
|
/// Opens a new outgoing substream, and produces the equivalent to a future that will be
|
|
|
|
/// resolved when it becomes available.
|
|
|
|
///
|
|
|
|
/// The API of `OutboundSubstream` is totally opaque, and the object can only be interfaced
|
|
|
|
/// through the methods on the `StreamMuxer` trait.
|
2018-08-31 10:31:34 +02:00
|
|
|
fn open_outbound(&self) -> Self::OutboundSubstream;
|
|
|
|
|
|
|
|
/// Polls the outbound substream.
|
|
|
|
///
|
2019-09-16 11:08:44 +02:00
|
|
|
/// If `Pending` is returned, then the current task will be notified once the substream
|
2018-09-11 11:17:44 +02:00
|
|
|
/// is ready to be polled, similar to the API of `Future::poll()`.
|
|
|
|
/// However, for each individual outbound substream, only the latest task that was used to
|
|
|
|
/// call this method may be notified.
|
2018-09-14 13:18:10 +02:00
|
|
|
///
|
|
|
|
/// May panic or produce an undefined result if an earlier polling of the same substream
|
|
|
|
/// returned `Ready` or `Err`.
|
2021-08-11 13:12:12 +02:00
|
|
|
fn poll_outbound(
|
|
|
|
&self,
|
|
|
|
cx: &mut Context<'_>,
|
|
|
|
s: &mut Self::OutboundSubstream,
|
|
|
|
) -> Poll<Result<Self::Substream, Self::Error>>;
|
2018-08-31 10:31:34 +02:00
|
|
|
|
2019-03-11 17:19:50 +01:00
|
|
|
/// Destroys an outbound substream future. Use this after the outbound substream has finished,
|
|
|
|
/// or if you want to interrupt it.
|
2018-10-11 10:35:14 +02:00
|
|
|
fn destroy_outbound(&self, s: Self::OutboundSubstream);
|
2018-08-31 10:31:34 +02:00
|
|
|
|
2019-03-11 17:19:50 +01:00
|
|
|
/// Closes this `StreamMuxer`.
|
|
|
|
///
|
2019-09-16 11:08:44 +02:00
|
|
|
/// After this has returned `Poll::Ready(Ok(()))`, the muxer has become useless. All
|
2019-03-11 17:19:50 +01:00
|
|
|
/// subsequent reads must return either `EOF` or an error. All subsequent writes, shutdowns,
|
|
|
|
/// or polls must generate an error or be ignored.
|
2018-10-11 10:35:14 +02:00
|
|
|
///
|
2019-03-11 17:19:50 +01:00
|
|
|
/// > **Note**: You are encouraged to call this method and wait for it to return `Ready`, so
|
|
|
|
/// > that the remote is properly informed of the shutdown. However, apart from
|
|
|
|
/// > properly informing the remote, there is no difference between this and
|
|
|
|
/// > immediately dropping the muxer.
|
2022-05-29 16:27:40 +02:00
|
|
|
fn poll_close(&self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>>;
|
2018-08-31 10:31:34 +02:00
|
|
|
}
|
|
|
|
|
2020-06-30 17:10:53 +02:00
|
|
|
/// Event about a connection, reported by an implementation of [`StreamMuxer`].
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
pub enum StreamMuxerEvent<T> {
|
|
|
|
/// Remote has opened a new substream. Contains the substream in question.
|
|
|
|
InboundSubstream(T),
|
|
|
|
|
|
|
|
/// Address to the remote has changed. The previous one is now obsolete.
|
|
|
|
///
|
|
|
|
/// > **Note**: This can for example happen when using the QUIC protocol, where the two nodes
|
|
|
|
/// > can change their IP address while retaining the same QUIC connection.
|
|
|
|
AddressChange(Multiaddr),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> StreamMuxerEvent<T> {
|
|
|
|
/// If `self` is a [`StreamMuxerEvent::InboundSubstream`], returns the content. Otherwise
|
|
|
|
/// returns `None`.
|
|
|
|
pub fn into_inbound_substream(self) -> Option<T> {
|
|
|
|
if let StreamMuxerEvent::InboundSubstream(s) = self {
|
|
|
|
Some(s)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2022-06-15 21:33:32 +02:00
|
|
|
|
|
|
|
/// Map the stream within [`StreamMuxerEvent::InboundSubstream`] to a new type.
|
|
|
|
pub fn map_inbound_stream<O>(self, map: impl FnOnce(T) -> O) -> StreamMuxerEvent<O> {
|
|
|
|
match self {
|
|
|
|
StreamMuxerEvent::InboundSubstream(stream) => {
|
|
|
|
StreamMuxerEvent::InboundSubstream(map(stream))
|
|
|
|
}
|
|
|
|
StreamMuxerEvent::AddressChange(addr) => StreamMuxerEvent::AddressChange(addr),
|
|
|
|
}
|
|
|
|
}
|
2020-06-30 17:10:53 +02:00
|
|
|
}
|