mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-28 17:21:34 +00:00
Merge the changes from polkadot-2 into master (#446)
This commit is contained in:
committed by
Benjamin Kampmann
parent
35e005879b
commit
f787c80a42
File diff suppressed because it is too large
Load Diff
@ -29,8 +29,10 @@
|
|||||||
//! `UpgradedNode::or_upgrade` methods, you can combine multiple transports and/or upgrades
|
//! `UpgradedNode::or_upgrade` methods, you can combine multiple transports and/or upgrades
|
||||||
//! together in a complex chain of protocols negotiation.
|
//! together in a complex chain of protocols negotiation.
|
||||||
|
|
||||||
|
use connection_reuse::ConnectionReuse;
|
||||||
use futures::prelude::*;
|
use futures::prelude::*;
|
||||||
use multiaddr::Multiaddr;
|
use multiaddr::Multiaddr;
|
||||||
|
use muxing::StreamMuxer;
|
||||||
use std::io::Error as IoError;
|
use std::io::Error as IoError;
|
||||||
use tokio_io::{AsyncRead, AsyncWrite};
|
use tokio_io::{AsyncRead, AsyncWrite};
|
||||||
use upgrade::{ConnectionUpgrade, Endpoint};
|
use upgrade::{ConnectionUpgrade, Endpoint};
|
||||||
@ -116,7 +118,7 @@ pub trait Transport {
|
|||||||
/// implementation of `Transport` is only responsible for handling the protocols it supports.
|
/// implementation of `Transport` is only responsible for handling the protocols it supports.
|
||||||
///
|
///
|
||||||
/// Returns `None` if nothing can be determined. This happens if this trait implementation
|
/// Returns `None` if nothing can be determined. This happens if this trait implementation
|
||||||
/// doesn't recognize the protocols, or if `server` and `observed` are unrelated.
|
/// doesn't recognize the protocols, or if `server` and `observed` are related.
|
||||||
fn nat_traversal(&self, server: &Multiaddr, observed: &Multiaddr) -> Option<Multiaddr>;
|
fn nat_traversal(&self, server: &Multiaddr, observed: &Multiaddr) -> Option<Multiaddr>;
|
||||||
|
|
||||||
/// Applies a function on the output of the `Transport`.
|
/// Applies a function on the output of the `Transport`.
|
||||||
@ -207,6 +209,17 @@ pub trait Transport {
|
|||||||
DummyMuxing::new(self)
|
DummyMuxing::new(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Turns this `Transport` into a `ConnectionReuse`. If the `Output` implements the
|
||||||
|
/// `StreamMuxer` trait, the returned object will implement `Transport` and `MuxedTransport`.
|
||||||
|
#[inline]
|
||||||
|
fn into_connection_reuse<D, M>(self) -> ConnectionReuse<Self, D, M>
|
||||||
|
where
|
||||||
|
Self: Sized + Transport<Output = (D, M)>,
|
||||||
|
M: StreamMuxer,
|
||||||
|
{
|
||||||
|
ConnectionReuse::new(self)
|
||||||
|
}
|
||||||
|
|
||||||
/// Wraps around the `Transport` and makes it interruptible.
|
/// Wraps around the `Transport` and makes it interruptible.
|
||||||
#[inline]
|
#[inline]
|
||||||
fn interruptible(self) -> (interruptible::Interruptible<Self>, interruptible::Interrupt)
|
fn interruptible(self) -> (interruptible::Interruptible<Self>, interruptible::Interrupt)
|
||||||
|
@ -18,10 +18,8 @@
|
|||||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||||
// DEALINGS IN THE SOFTWARE.
|
// DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
use connection_reuse::ConnectionReuse;
|
|
||||||
use futures::prelude::*;
|
use futures::prelude::*;
|
||||||
use multiaddr::Multiaddr;
|
use multiaddr::Multiaddr;
|
||||||
use muxing::StreamMuxer;
|
|
||||||
use std::io::Error as IoError;
|
use std::io::Error as IoError;
|
||||||
use tokio_io::{AsyncRead, AsyncWrite};
|
use tokio_io::{AsyncRead, AsyncWrite};
|
||||||
use transport::{MuxedTransport, Transport};
|
use transport::{MuxedTransport, Transport};
|
||||||
@ -52,16 +50,6 @@ where
|
|||||||
T::Output: AsyncRead + AsyncWrite,
|
T::Output: AsyncRead + AsyncWrite,
|
||||||
C: ConnectionUpgrade<T::Output, T::MultiaddrFuture> + 'a,
|
C: ConnectionUpgrade<T::Output, T::MultiaddrFuture> + 'a,
|
||||||
{
|
{
|
||||||
/// Turns this upgraded node into a `ConnectionReuse`. If the `Output` implements the
|
|
||||||
/// `StreamMuxer` trait, the returned object will implement `Transport` and `MuxedTransport`.
|
|
||||||
#[inline]
|
|
||||||
pub fn into_connection_reuse(self) -> ConnectionReuse<T, C>
|
|
||||||
where
|
|
||||||
C::Output: StreamMuxer,
|
|
||||||
{
|
|
||||||
From::from(self)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns a reference to the inner `Transport`.
|
/// Returns a reference to the inner `Transport`.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn transport(&self) -> &T {
|
pub fn transport(&self) -> &T {
|
||||||
|
@ -78,7 +78,9 @@ fn client_to_server_outbound() {
|
|||||||
let bg_thread = thread::spawn(move || {
|
let bg_thread = thread::spawn(move || {
|
||||||
let future = rx
|
let future = rx
|
||||||
.with_upgrade(multiplex::MplexConfig::new())
|
.with_upgrade(multiplex::MplexConfig::new())
|
||||||
|
.map(|val, _| ((), val))
|
||||||
.into_connection_reuse()
|
.into_connection_reuse()
|
||||||
|
.map(|((), val), _| val)
|
||||||
.listen_on("/memory".parse().unwrap())
|
.listen_on("/memory".parse().unwrap())
|
||||||
.unwrap_or_else(|_| panic!()).0
|
.unwrap_or_else(|_| panic!()).0
|
||||||
.into_future()
|
.into_future()
|
||||||
@ -124,7 +126,9 @@ fn connection_reused_for_dialing() {
|
|||||||
let bg_thread = thread::spawn(move || {
|
let bg_thread = thread::spawn(move || {
|
||||||
let future = OnlyOnce::from(rx)
|
let future = OnlyOnce::from(rx)
|
||||||
.with_upgrade(multiplex::MplexConfig::new())
|
.with_upgrade(multiplex::MplexConfig::new())
|
||||||
|
.map(|val, _| ((), val))
|
||||||
.into_connection_reuse()
|
.into_connection_reuse()
|
||||||
|
.map(|((), val), _| val)
|
||||||
.listen_on("/memory".parse().unwrap())
|
.listen_on("/memory".parse().unwrap())
|
||||||
.unwrap_or_else(|_| panic!()).0
|
.unwrap_or_else(|_| panic!()).0
|
||||||
.into_future()
|
.into_future()
|
||||||
@ -160,7 +164,9 @@ fn connection_reused_for_dialing() {
|
|||||||
|
|
||||||
let transport = OnlyOnce::from(tx)
|
let transport = OnlyOnce::from(tx)
|
||||||
.with_upgrade(multiplex::MplexConfig::new())
|
.with_upgrade(multiplex::MplexConfig::new())
|
||||||
.into_connection_reuse();
|
.map(|val, _| ((), val))
|
||||||
|
.into_connection_reuse()
|
||||||
|
.map(|((), val), _| val);
|
||||||
|
|
||||||
let future = transport
|
let future = transport
|
||||||
.clone()
|
.clone()
|
||||||
@ -229,7 +235,9 @@ fn use_opened_listen_to_dial() {
|
|||||||
|
|
||||||
let transport = OnlyOnce::from(tx)
|
let transport = OnlyOnce::from(tx)
|
||||||
.with_upgrade(multiplex::MplexConfig::new())
|
.with_upgrade(multiplex::MplexConfig::new())
|
||||||
.into_connection_reuse();
|
.map(|val, _| ((), val))
|
||||||
|
.into_connection_reuse()
|
||||||
|
.map(|((), val), _| val);
|
||||||
|
|
||||||
let future = transport
|
let future = transport
|
||||||
.clone()
|
.clone()
|
||||||
|
@ -75,7 +75,9 @@ fn main() {
|
|||||||
// `Transport` because the output of the upgrade is not a stream but a controller for
|
// `Transport` because the output of the upgrade is not a stream but a controller for
|
||||||
// muxing. We have to explicitly call `into_connection_reuse()` in order to turn this into
|
// muxing. We have to explicitly call `into_connection_reuse()` in order to turn this into
|
||||||
// a `Transport`.
|
// a `Transport`.
|
||||||
.into_connection_reuse();
|
.map(|val, _| ((), val))
|
||||||
|
.into_connection_reuse()
|
||||||
|
.map(|((), val), _| val);
|
||||||
|
|
||||||
// Building a struct that represents the protocol that we are going to use for dialing.
|
// Building a struct that represents the protocol that we are going to use for dialing.
|
||||||
let proto = SimpleProtocol::new("/echo/1.0.0", |socket| {
|
let proto = SimpleProtocol::new("/echo/1.0.0", |socket| {
|
||||||
|
@ -75,7 +75,9 @@ fn main() {
|
|||||||
// `Transport` because the output of the upgrade is not a stream but a controller for
|
// `Transport` because the output of the upgrade is not a stream but a controller for
|
||||||
// muxing. We have to explicitly call `into_connection_reuse()` in order to turn this into
|
// muxing. We have to explicitly call `into_connection_reuse()` in order to turn this into
|
||||||
// a `Transport`.
|
// a `Transport`.
|
||||||
.into_connection_reuse();
|
.map(|val, _| ((), val))
|
||||||
|
.into_connection_reuse()
|
||||||
|
.map(|((), val), _| val);
|
||||||
|
|
||||||
// We now have a `transport` variable that can be used either to dial nodes or listen to
|
// We now have a `transport` variable that can be used either to dial nodes or listen to
|
||||||
// incoming connections, and that will automatically apply secio and multiplex on top
|
// incoming connections, and that will automatically apply secio and multiplex on top
|
||||||
|
@ -76,7 +76,9 @@ fn main() {
|
|||||||
// `Transport` because the output of the upgrade is not a stream but a controller for
|
// `Transport` because the output of the upgrade is not a stream but a controller for
|
||||||
// muxing. We have to explicitly call `into_connection_reuse()` in order to turn this into
|
// muxing. We have to explicitly call `into_connection_reuse()` in order to turn this into
|
||||||
// a `Transport`.
|
// a `Transport`.
|
||||||
.into_connection_reuse();
|
.map(|val, _| ((), val))
|
||||||
|
.into_connection_reuse()
|
||||||
|
.map(|((), val), _| val);
|
||||||
|
|
||||||
// We now have a `transport` variable that can be used either to dial nodes or listen to
|
// We now have a `transport` variable that can be used either to dial nodes or listen to
|
||||||
// incoming connections, and that will automatically apply secio and multiplex on top
|
// incoming connections, and that will automatically apply secio and multiplex on top
|
||||||
|
@ -18,6 +18,8 @@
|
|||||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||||
// DEALINGS IN THE SOFTWARE.
|
// DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
#![type_length_limit = "2097152"]
|
||||||
|
|
||||||
extern crate bigint;
|
extern crate bigint;
|
||||||
extern crate bytes;
|
extern crate bytes;
|
||||||
extern crate env_logger;
|
extern crate env_logger;
|
||||||
@ -84,7 +86,9 @@ fn main() {
|
|||||||
// `Transport` because the output of the upgrade is not a stream but a controller for
|
// `Transport` because the output of the upgrade is not a stream but a controller for
|
||||||
// muxing. We have to explicitly call `into_connection_reuse()` in order to turn this into
|
// muxing. We have to explicitly call `into_connection_reuse()` in order to turn this into
|
||||||
// a `Transport`.
|
// a `Transport`.
|
||||||
.into_connection_reuse();
|
.map(|val, _| ((), val))
|
||||||
|
.into_connection_reuse()
|
||||||
|
.map(|((), val), _| val);
|
||||||
|
|
||||||
let addr_resolver = {
|
let addr_resolver = {
|
||||||
let peer_store = peer_store.clone();
|
let peer_store = peer_store.clone();
|
||||||
|
@ -68,7 +68,9 @@ fn main() {
|
|||||||
// `Transport` because the output of the upgrade is not a stream but a controller for
|
// `Transport` because the output of the upgrade is not a stream but a controller for
|
||||||
// muxing. We have to explicitly call `into_connection_reuse()` in order to turn this into
|
// muxing. We have to explicitly call `into_connection_reuse()` in order to turn this into
|
||||||
// a `Transport`.
|
// a `Transport`.
|
||||||
.into_connection_reuse();
|
.map(|val, _| ((), val))
|
||||||
|
.into_connection_reuse()
|
||||||
|
.map(|((), val), _| val);
|
||||||
|
|
||||||
// Let's put this `transport` into a *swarm*. The swarm will handle all the incoming
|
// Let's put this `transport` into a *swarm*. The swarm will handle all the incoming
|
||||||
// connections for us. The second parameter we pass is the connection upgrade that is accepted
|
// connections for us. The second parameter we pass is the connection upgrade that is accepted
|
||||||
|
@ -128,7 +128,9 @@ fn run_dialer(opts: DialerOpts) -> Result<(), Box<Error>> {
|
|||||||
let transport = {
|
let transport = {
|
||||||
let tcp = TcpConfig::new()
|
let tcp = TcpConfig::new()
|
||||||
.with_upgrade(libp2p_yamux::Config::default())
|
.with_upgrade(libp2p_yamux::Config::default())
|
||||||
.into_connection_reuse();
|
.map(|val, _| ((), val))
|
||||||
|
.into_connection_reuse()
|
||||||
|
.map(|((), val), _| val);
|
||||||
RelayTransport::new(opts.me, tcp, store, iter::once(opts.relay)).with_dummy_muxing()
|
RelayTransport::new(opts.me, tcp, store, iter::once(opts.relay)).with_dummy_muxing()
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -161,7 +163,9 @@ fn run_listener(opts: ListenerOpts) -> Result<(), Box<Error>> {
|
|||||||
|
|
||||||
let transport = TcpConfig::new()
|
let transport = TcpConfig::new()
|
||||||
.with_upgrade(libp2p_yamux::Config::default())
|
.with_upgrade(libp2p_yamux::Config::default())
|
||||||
.into_connection_reuse();
|
.map(|val, _| ((), val))
|
||||||
|
.into_connection_reuse()
|
||||||
|
.map(|((), val), _| val);
|
||||||
|
|
||||||
let relay = RelayConfig::new(opts.me, transport.clone(), store);
|
let relay = RelayConfig::new(opts.me, transport.clone(), store);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user