mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-26 00:01:33 +00:00
Remove or_upgrade and let users build OrUpgrade manually
This commit is contained in:
@ -29,7 +29,7 @@ extern crate tokio_io;
|
|||||||
|
|
||||||
use bytes::BytesMut;
|
use bytes::BytesMut;
|
||||||
use futures::{Future, Sink, Stream};
|
use futures::{Future, Sink, Stream};
|
||||||
use swarm::{SimpleProtocol, Transport};
|
use swarm::{UpgradeExt, SimpleProtocol, Transport};
|
||||||
use tcp::TcpConfig;
|
use tcp::TcpConfig;
|
||||||
use tokio_core::reactor::Core;
|
use tokio_core::reactor::Core;
|
||||||
use tokio_io::codec::length_delimited;
|
use tokio_io::codec::length_delimited;
|
||||||
@ -44,13 +44,18 @@ fn main() {
|
|||||||
|
|
||||||
// On top of TCP/IP, we will use either the plaintext protocol or the secio protocol,
|
// On top of TCP/IP, we will use either the plaintext protocol or the secio protocol,
|
||||||
// depending on which one the remote supports.
|
// depending on which one the remote supports.
|
||||||
.with_upgrade(swarm::PlainTextConfig)
|
.with_upgrade({
|
||||||
.or_upgrade({
|
let plain_text = swarm::PlainTextConfig;
|
||||||
let private_key = include_bytes!("test-private-key.pk8");
|
|
||||||
let public_key = include_bytes!("test-public-key.der").to_vec();
|
let secio = {
|
||||||
secio::SecioConfig {
|
let private_key = include_bytes!("test-private-key.pk8");
|
||||||
key: secio::SecioKeyPair::rsa_from_pkcs8(private_key, public_key).unwrap(),
|
let public_key = include_bytes!("test-public-key.der").to_vec();
|
||||||
}
|
secio::SecioConfig {
|
||||||
|
key: secio::SecioKeyPair::rsa_from_pkcs8(private_key, public_key).unwrap(),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
plain_text.or_upgrade(secio)
|
||||||
})
|
})
|
||||||
|
|
||||||
.with_upgrade(multiplex::MultiplexConfig);
|
.with_upgrade(multiplex::MultiplexConfig);
|
||||||
|
@ -28,7 +28,7 @@ extern crate tokio_io;
|
|||||||
|
|
||||||
use futures::future::{Future, IntoFuture, loop_fn, Loop};
|
use futures::future::{Future, IntoFuture, loop_fn, Loop};
|
||||||
use futures::{Stream, Sink};
|
use futures::{Stream, Sink};
|
||||||
use swarm::{Transport, SimpleProtocol};
|
use swarm::{Transport, UpgradeExt, SimpleProtocol};
|
||||||
use tcp::TcpConfig;
|
use tcp::TcpConfig;
|
||||||
use tokio_core::reactor::Core;
|
use tokio_core::reactor::Core;
|
||||||
use tokio_io::codec::length_delimited;
|
use tokio_io::codec::length_delimited;
|
||||||
@ -43,15 +43,20 @@ fn main() {
|
|||||||
|
|
||||||
// On top of TCP/IP, we will use either the plaintext protocol or the secio protocol,
|
// On top of TCP/IP, we will use either the plaintext protocol or the secio protocol,
|
||||||
// depending on which one the remote supports.
|
// depending on which one the remote supports.
|
||||||
.with_upgrade(swarm::PlainTextConfig)
|
.with_upgrade({
|
||||||
.or_upgrade({
|
let plain_text = swarm::PlainTextConfig;
|
||||||
let private_key = include_bytes!("test-private-key.pk8");
|
|
||||||
let public_key = include_bytes!("test-public-key.der").to_vec();
|
let secio = {
|
||||||
secio::SecioConfig {
|
let private_key = include_bytes!("test-private-key.pk8");
|
||||||
key: secio::SecioKeyPair::rsa_from_pkcs8(private_key, public_key).unwrap(),
|
let public_key = include_bytes!("test-public-key.der").to_vec();
|
||||||
}
|
secio::SecioConfig {
|
||||||
|
key: secio::SecioKeyPair::rsa_from_pkcs8(private_key, public_key).unwrap(),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
plain_text.or_upgrade(secio)
|
||||||
})
|
})
|
||||||
|
|
||||||
// On top of plaintext or secio, we use the "echo" protocol, which is a custom protocol
|
// On top of plaintext or secio, we use the "echo" protocol, which is a custom protocol
|
||||||
// just for this example.
|
// just for this example.
|
||||||
// For this purpose, we create a `SimpleProtocol` struct.
|
// For this purpose, we create a `SimpleProtocol` struct.
|
||||||
|
@ -181,4 +181,4 @@ pub use self::connection_reuse::ConnectionReuse;
|
|||||||
pub use self::multiaddr::Multiaddr;
|
pub use self::multiaddr::Multiaddr;
|
||||||
pub use self::muxing::StreamMuxer;
|
pub use self::muxing::StreamMuxer;
|
||||||
pub use self::transport::{ConnectionUpgrade, PlainTextConfig, Transport, UpgradedNode, OrUpgrade};
|
pub use self::transport::{ConnectionUpgrade, PlainTextConfig, Transport, UpgradedNode, OrUpgrade};
|
||||||
pub use self::transport::{Endpoint, SimpleProtocol, MuxedTransport};
|
pub use self::transport::{Endpoint, SimpleProtocol, MuxedTransport, UpgradeExt};
|
||||||
|
@ -529,6 +529,21 @@ pub enum Endpoint {
|
|||||||
Listener,
|
Listener,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Extension trait for `ConnectionUpgrade`. Automatically implemented on everything.
|
||||||
|
pub trait UpgradeExt {
|
||||||
|
/// Builds a struct that will choose an upgrade between `self` and `other`, depending on what
|
||||||
|
/// the remote supports.
|
||||||
|
fn or_upgrade<T>(self, other: T) -> OrUpgrade<Self, T>
|
||||||
|
where Self: Sized;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> UpgradeExt for T {
|
||||||
|
#[inline]
|
||||||
|
fn or_upgrade<U>(self, other: U) -> OrUpgrade<Self, U> {
|
||||||
|
OrUpgrade(self, other)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// See `or_upgrade()`.
|
/// See `or_upgrade()`.
|
||||||
#[derive(Debug, Copy, Clone)]
|
#[derive(Debug, Copy, Clone)]
|
||||||
pub struct OrUpgrade<A, B>(A, B);
|
pub struct OrUpgrade<A, B>(A, B);
|
||||||
@ -691,22 +706,6 @@ where
|
|||||||
T: Transport + 'a,
|
T: Transport + 'a,
|
||||||
C: ConnectionUpgrade<T::RawConn> + 'a,
|
C: ConnectionUpgrade<T::RawConn> + 'a,
|
||||||
{
|
{
|
||||||
/// Builds a new struct that implements `ConnectionUpgrade` that contains both `self` and
|
|
||||||
/// `other_upg`.
|
|
||||||
///
|
|
||||||
/// The returned object will try to negotiate either the protocols of `self` or the protocols
|
|
||||||
/// of `other_upg`, then upgrade the connection to the negogiated protocol.
|
|
||||||
#[inline]
|
|
||||||
pub fn or_upgrade<D>(self, other_upg: D) -> UpgradedNode<T, OrUpgrade<C, D>>
|
|
||||||
where
|
|
||||||
D: ConnectionUpgrade<T::RawConn> + 'a,
|
|
||||||
{
|
|
||||||
UpgradedNode {
|
|
||||||
transports: self.transports,
|
|
||||||
upgrade: OrUpgrade(self.upgrade, other_upg),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Tries to dial on the `Multiaddr` using the transport that was passed to `new`, then upgrade
|
/// Tries to dial on the `Multiaddr` using the transport that was passed to `new`, then upgrade
|
||||||
/// the connection.
|
/// the connection.
|
||||||
///
|
///
|
||||||
|
Reference in New Issue
Block a user