Wrap multistream-select streams under a Negotiated (#1001)

This commit is contained in:
Pierre Krieger 2019-03-19 17:27:30 +01:00 committed by GitHub
parent 63e9e39538
commit 96e559b503
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
24 changed files with 162 additions and 111 deletions

View File

@ -61,6 +61,7 @@
/// Multi-address re-export.
pub use multiaddr;
pub use multistream_select::Negotiated;
mod keys_proto;
mod peer_id;

View File

@ -188,7 +188,7 @@ where
self.pending_error = Some(error);
}
}
#[inline]
fn connection_keep_alive(&self) -> KeepAlive {
self.keep_alive

View File

@ -20,6 +20,7 @@
use crate::upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo};
use futures::future;
use multistream_select::Negotiated;
use std::iter;
use void::Void;
@ -42,7 +43,7 @@ impl<C> InboundUpgrade<C> for DeniedUpgrade {
type Error = Void;
type Future = future::Empty<Self::Output, Self::Error>;
fn upgrade_inbound(self, _: C, _: Self::Info) -> Self::Future {
fn upgrade_inbound(self, _: Negotiated<C>, _: Self::Info) -> Self::Future {
future::empty()
}
}
@ -52,7 +53,7 @@ impl<C> OutboundUpgrade<C> for DeniedUpgrade {
type Error = Void;
type Future = future::Empty<Self::Output, Self::Error>;
fn upgrade_outbound(self, _: C, _: Self::Info) -> Self::Future {
fn upgrade_outbound(self, _: Negotiated<C>, _: Self::Info) -> Self::Future {
future::empty()
}
}

View File

@ -22,6 +22,7 @@ use crate::{
either::{EitherOutput, EitherError, EitherFuture2, EitherName},
upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo}
};
use multistream_select::Negotiated;
/// A type to represent two possible upgrade types (inbound or outbound).
#[derive(Debug, Clone)]
@ -55,7 +56,7 @@ where
type Error = EitherError<EA, EB>;
type Future = EitherFuture2<A::Future, B::Future>;
fn upgrade_inbound(self, sock: C, info: Self::Info) -> Self::Future {
fn upgrade_inbound(self, sock: Negotiated<C>, info: Self::Info) -> Self::Future {
match (self, info) {
(EitherUpgrade::A(a), EitherName::A(info)) => EitherFuture2::A(a.upgrade_inbound(sock, info)),
(EitherUpgrade::B(b), EitherName::B(info)) => EitherFuture2::B(b.upgrade_inbound(sock, info)),
@ -73,7 +74,7 @@ where
type Error = EitherError<EA, EB>;
type Future = EitherFuture2<A::Future, B::Future>;
fn upgrade_outbound(self, sock: C, info: Self::Info) -> Self::Future {
fn upgrade_outbound(self, sock: Negotiated<C>, info: Self::Info) -> Self::Future {
match (self, info) {
(EitherUpgrade::A(a), EitherName::A(info)) => EitherFuture2::A(a.upgrade_outbound(sock, info)),
(EitherUpgrade::B(b), EitherName::B(info)) => EitherFuture2::B(b.upgrade_outbound(sock, info)),

View File

@ -20,6 +20,7 @@
use crate::upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo};
use futures::{prelude::*, try_ready};
use multistream_select::Negotiated;
/// Wraps around an upgrade and applies a closure to the output.
#[derive(Debug, Clone)]
@ -52,7 +53,7 @@ where
type Error = U::Error;
type Future = MapFuture<U::Future, F>;
fn upgrade_inbound(self, sock: C, info: Self::Info) -> Self::Future {
fn upgrade_inbound(self, sock: Negotiated<C>, info: Self::Info) -> Self::Future {
MapFuture {
inner: self.upgrade.upgrade_inbound(sock, info),
map: Some(self.fun)
@ -68,7 +69,7 @@ where
type Error = U::Error;
type Future = U::Future;
fn upgrade_outbound(self, sock: C, info: Self::Info) -> Self::Future {
fn upgrade_outbound(self, sock: Negotiated<C>, info: Self::Info) -> Self::Future {
self.upgrade.upgrade_outbound(sock, info)
}
}
@ -103,7 +104,7 @@ where
type Error = U::Error;
type Future = U::Future;
fn upgrade_inbound(self, sock: C, info: Self::Info) -> Self::Future {
fn upgrade_inbound(self, sock: Negotiated<C>, info: Self::Info) -> Self::Future {
self.upgrade.upgrade_inbound(sock, info)
}
}
@ -117,7 +118,7 @@ where
type Error = U::Error;
type Future = MapFuture<U::Future, F>;
fn upgrade_outbound(self, sock: C, info: Self::Info) -> Self::Future {
fn upgrade_outbound(self, sock: Negotiated<C>, info: Self::Info) -> Self::Future {
MapFuture {
inner: self.upgrade.upgrade_outbound(sock, info),
map: Some(self.fun)
@ -156,7 +157,7 @@ where
type Error = T;
type Future = MapErrFuture<U::Future, F>;
fn upgrade_inbound(self, sock: C, info: Self::Info) -> Self::Future {
fn upgrade_inbound(self, sock: Negotiated<C>, info: Self::Info) -> Self::Future {
MapErrFuture {
fut: self.upgrade.upgrade_inbound(sock, info),
fun: Some(self.fun)
@ -172,7 +173,7 @@ where
type Error = U::Error;
type Future = U::Future;
fn upgrade_outbound(self, sock: C, info: Self::Info) -> Self::Future {
fn upgrade_outbound(self, sock: Negotiated<C>, info: Self::Info) -> Self::Future {
self.upgrade.upgrade_outbound(sock, info)
}
}
@ -208,7 +209,7 @@ where
type Error = T;
type Future = MapErrFuture<U::Future, F>;
fn upgrade_outbound(self, sock: C, info: Self::Info) -> Self::Future {
fn upgrade_outbound(self, sock: Negotiated<C>, info: Self::Info) -> Self::Future {
MapErrFuture {
fut: self.upgrade.upgrade_outbound(sock, info),
fun: Some(self.fun)
@ -224,7 +225,7 @@ where
type Error = U::Error;
type Future = U::Future;
fn upgrade_inbound(self, sock: C, info: Self::Info) -> Self::Future {
fn upgrade_inbound(self, sock: Negotiated<C>, info: Self::Info) -> Self::Future {
self.upgrade.upgrade_inbound(sock, info)
}
}

View File

@ -67,6 +67,7 @@ mod transfer;
use futures::future::Future;
pub use multistream_select::Negotiated;
pub use self::{
apply::{apply, apply_inbound, apply_outbound, InboundUpgradeApply, OutboundUpgradeApply},
denied::DeniedUpgrade,
@ -114,7 +115,7 @@ pub trait InboundUpgrade<C>: UpgradeInfo {
/// method is called to start the handshake.
///
/// The `info` is the identifier of the protocol, as produced by `protocol_info`.
fn upgrade_inbound(self, socket: C, info: Self::Info) -> Self::Future;
fn upgrade_inbound(self, socket: Negotiated<C>, info: Self::Info) -> Self::Future;
}
/// Extension trait for `InboundUpgrade`. Automatically implemented on all types that implement
@ -154,7 +155,7 @@ pub trait OutboundUpgrade<C>: UpgradeInfo {
/// method is called to start the handshake.
///
/// The `info` is the identifier of the protocol, as produced by `protocol_info`.
fn upgrade_outbound(self, socket: C, info: Self::Info) -> Self::Future;
fn upgrade_outbound(self, socket: Negotiated<C>, info: Self::Info) -> Self::Future;
}
/// Extention trait for `OutboundUpgrade`. Automatically implemented on all types that implement

View File

@ -22,6 +22,7 @@ use crate::{
either::{EitherOutput, EitherError, EitherFuture2, EitherName},
upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo}
};
use multistream_select::Negotiated;
/// Upgrade that combines two upgrades into one. Supports all the protocols supported by either
/// sub-upgrade.
@ -64,7 +65,7 @@ where
type Error = EitherError<EA, EB>;
type Future = EitherFuture2<A::Future, B::Future>;
fn upgrade_inbound(self, sock: C, info: Self::Info) -> Self::Future {
fn upgrade_inbound(self, sock: Negotiated<C>, info: Self::Info) -> Self::Future {
match info {
EitherName::A(info) => EitherFuture2::A(self.0.upgrade_inbound(sock, info)),
EitherName::B(info) => EitherFuture2::B(self.1.upgrade_inbound(sock, info))
@ -81,7 +82,7 @@ where
type Error = EitherError<EA, EB>;
type Future = EitherFuture2<A::Future, B::Future>;
fn upgrade_outbound(self, sock: C, info: Self::Info) -> Self::Future {
fn upgrade_outbound(self, sock: Negotiated<C>, info: Self::Info) -> Self::Future {
match info {
EitherName::A(info) => EitherFuture2::A(self.0.upgrade_outbound(sock, info)),
EitherName::B(info) => EitherFuture2::B(self.1.upgrade_outbound(sock, info))

View File

@ -31,7 +31,7 @@ use crate::protocol::{
use log::trace;
use std::mem;
use tokio_io::{AsyncRead, AsyncWrite};
use crate::ProtocolChoiceError;
use crate::{Negotiated, ProtocolChoiceError};
/// Future, returned by `dialer_select_proto`, which selects a protocol and dialer
/// either sequentially of by considering all protocols in parallel.
@ -125,7 +125,7 @@ where
I: Iterator,
I::Item: AsRef<[u8]> + Clone
{
type Item = (I::Item, R);
type Item = (I::Item, Negotiated<R>);
type Error = ProtocolChoiceError;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
@ -207,7 +207,7 @@ where
ListenerToDialerMessage::ProtocolAck { ref name }
if name.as_ref() == proto_name.as_ref() =>
{
return Ok(Async::Ready((proto_name, r.into_inner())))
return Ok(Async::Ready((proto_name, Negotiated(r.into_inner()))))
}
ListenerToDialerMessage::NotAvailable => {
let proto_name = protocols.next()
@ -300,7 +300,7 @@ where
I: Iterator,
I::Item: AsRef<[u8]> + Clone
{
type Item = (I::Item, R);
type Item = (I::Item, Negotiated<R>);
type Error = ProtocolChoiceError;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
@ -423,7 +423,7 @@ where
Some(ListenerToDialerMessage::ProtocolAck { ref name })
if name.as_ref() == proto_name.as_ref() =>
{
return Ok(Async::Ready((proto_name, dialer.into_inner())))
return Ok(Async::Ready((proto_name, Negotiated(dialer.into_inner()))))
}
_ => return Err(ProtocolChoiceError::UnexpectedMessage)
}

View File

@ -74,6 +74,49 @@ mod tests;
mod protocol;
use futures::prelude::*;
use std::io;
pub use self::dialer_select::{dialer_select_proto, DialerSelectFuture};
pub use self::error::ProtocolChoiceError;
pub use self::listener_select::{listener_select_proto, ListenerSelectFuture};
/// A stream after it has been negotiated.
pub struct Negotiated<TInner>(pub(crate) TInner);
impl<TInner> io::Read for Negotiated<TInner>
where
TInner: io::Read
{
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.0.read(buf)
}
}
impl<TInner> tokio_io::AsyncRead for Negotiated<TInner>
where
TInner: tokio_io::AsyncRead
{
}
impl<TInner> io::Write for Negotiated<TInner>
where
TInner: io::Write
{
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.0.write(buf)
}
fn flush(&mut self) -> io::Result<()> {
self.0.flush()
}
}
impl<TInner> tokio_io::AsyncWrite for Negotiated<TInner>
where
TInner: tokio_io::AsyncWrite
{
fn shutdown(&mut self) -> Poll<(), io::Error> {
self.0.shutdown()
}
}

View File

@ -31,7 +31,7 @@ use crate::protocol::{
use log::{debug, trace};
use std::mem;
use tokio_io::{AsyncRead, AsyncWrite};
use crate::ProtocolChoiceError;
use crate::{Negotiated, ProtocolChoiceError};
/// Helps selecting a protocol amongst the ones supported.
///
@ -99,7 +99,7 @@ where
for<'a> &'a I: IntoIterator<Item = X>,
X: AsRef<[u8]> + Clone
{
type Item = (X, R, I);
type Item = (X, Negotiated<R>, I);
type Error = ProtocolChoiceError;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
@ -171,7 +171,7 @@ where
}
};
if let Some(p) = outcome {
return Ok(Async::Ready((p, listener.into_inner(), protocols)))
return Ok(Async::Ready((p, Negotiated(listener.into_inner()), protocols)))
} else {
let stream = listener.into_future();
self.inner = ListenerSelectState::Incoming { stream, protocols }

View File

@ -27,7 +27,7 @@ use bytes::Bytes;
use libp2p_core::{
Endpoint,
StreamMuxer,
upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo}
upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo, Negotiated}
};
use log::{debug, trace};
use parking_lot::Mutex;
@ -158,11 +158,11 @@ impl<C> InboundUpgrade<C> for MplexConfig
where
C: AsyncRead + AsyncWrite,
{
type Output = Multiplex<C>;
type Output = Multiplex<Negotiated<C>>;
type Error = IoError;
type Future = future::FutureResult<Self::Output, IoError>;
fn upgrade_inbound(self, socket: C, _: Self::Info) -> Self::Future {
fn upgrade_inbound(self, socket: Negotiated<C>, _: Self::Info) -> Self::Future {
future::ok(self.upgrade(socket))
}
}
@ -171,11 +171,11 @@ impl<C> OutboundUpgrade<C> for MplexConfig
where
C: AsyncRead + AsyncWrite,
{
type Output = Multiplex<C>;
type Output = Multiplex<Negotiated<C>>;
type Error = IoError;
type Future = future::FutureResult<Self::Output, IoError>;
fn upgrade_outbound(self, socket: C, _: Self::Info) -> Self::Future {
fn upgrade_outbound(self, socket: Negotiated<C>, _: Self::Info) -> Self::Future {
future::ok(self.upgrade(socket))
}
}

View File

@ -22,7 +22,7 @@
//! [specification](https://github.com/hashicorp/yamux/blob/master/spec.md).
use futures::{future::{self, FutureResult}, prelude::*};
use libp2p_core::upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo};
use libp2p_core::upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo, Negotiated};
use log::debug;
use std::{io, iter, sync::atomic};
use std::io::{Error as IoError};
@ -155,11 +155,11 @@ impl<C> InboundUpgrade<C> for Config
where
C: AsyncRead + AsyncWrite + 'static,
{
type Output = Yamux<C>;
type Output = Yamux<Negotiated<C>>;
type Error = io::Error;
type Future = FutureResult<Yamux<C>, io::Error>;
type Future = FutureResult<Yamux<Negotiated<C>>, io::Error>;
fn upgrade_inbound(self, i: C, _: Self::Info) -> Self::Future {
fn upgrade_inbound(self, i: Negotiated<C>, _: Self::Info) -> Self::Future {
future::ok(Yamux::new(i, self.0, yamux::Mode::Server))
}
}
@ -168,11 +168,11 @@ impl<C> OutboundUpgrade<C> for Config
where
C: AsyncRead + AsyncWrite + 'static,
{
type Output = Yamux<C>;
type Output = Yamux<Negotiated<C>>;
type Error = io::Error;
type Future = FutureResult<Yamux<C>, io::Error>;
type Future = FutureResult<Yamux<Negotiated<C>>, io::Error>;
fn upgrade_outbound(self, i: C, _: Self::Info) -> Self::Future {
fn upgrade_outbound(self, i: Negotiated<C>, _: Self::Info) -> Self::Future {
future::ok(Yamux::new(i, self.0, yamux::Mode::Client))
}
}

View File

@ -53,10 +53,10 @@ where
{
type Output = FloodsubRpc;
type Error = FloodsubDecodeError;
type Future = upgrade::ReadOneThen<TSocket, (), fn(Vec<u8>, ()) -> Result<FloodsubRpc, FloodsubDecodeError>>;
type Future = upgrade::ReadOneThen<upgrade::Negotiated<TSocket>, (), fn(Vec<u8>, ()) -> Result<FloodsubRpc, FloodsubDecodeError>>;
#[inline]
fn upgrade_inbound(self, socket: TSocket, _: Self::Info) -> Self::Future {
fn upgrade_inbound(self, socket: upgrade::Negotiated<TSocket>, _: Self::Info) -> Self::Future {
upgrade::read_one_then(socket, 2048, (), |packet, ()| {
let mut rpc: rpc_proto::RPC = protobuf::parse_from_bytes(&packet)?;
@ -168,10 +168,10 @@ where
{
type Output = ();
type Error = io::Error;
type Future = upgrade::WriteOne<TSocket>;
type Future = upgrade::WriteOne<upgrade::Negotiated<TSocket>>;
#[inline]
fn upgrade_outbound(self, socket: TSocket, _: Self::Info) -> Self::Future {
fn upgrade_outbound(self, socket: upgrade::Negotiated<TSocket>, _: Self::Info) -> Self::Future {
let bytes = self.into_bytes();
upgrade::write_one(socket, bytes)
}

View File

@ -24,7 +24,7 @@ use crate::protocol::{IdentifyInfo, IdentifySender, IdentifySenderFuture};
use futures::prelude::*;
use libp2p_core::protocols_handler::{ProtocolsHandler, ProtocolsHandlerSelect, ProtocolsHandlerUpgrErr};
use libp2p_core::swarm::{ConnectedPoint, NetworkBehaviour, NetworkBehaviourAction, PollParameters};
use libp2p_core::{Multiaddr, PeerId, PublicKey, either::EitherOutput};
use libp2p_core::{Multiaddr, PeerId, PublicKey, either::EitherOutput, upgrade::Negotiated};
use smallvec::SmallVec;
use std::{collections::HashMap, collections::VecDeque, io};
use tokio_io::{AsyncRead, AsyncWrite};
@ -42,9 +42,9 @@ pub struct Identify<TSubstream> {
/// For each peer we're connected to, the observed address to send back to it.
observed_addresses: HashMap<PeerId, Multiaddr>,
/// List of senders to answer, with the observed multiaddr.
to_answer: SmallVec<[(PeerId, IdentifySender<TSubstream>, Multiaddr); 4]>,
to_answer: SmallVec<[(PeerId, IdentifySender<Negotiated<TSubstream>>, Multiaddr); 4]>,
/// List of futures that send back information back to remotes.
futures: SmallVec<[(PeerId, IdentifySenderFuture<TSubstream>); 4]>,
futures: SmallVec<[(PeerId, IdentifySenderFuture<Negotiated<TSubstream>>); 4]>,
/// Events that need to be produced outside when polling..
events: VecDeque<NetworkBehaviourAction<EitherOutput<Void, Void>, IdentifyEvent>>,
}

View File

@ -22,7 +22,7 @@ use crate::protocol::{IdentifySender, IdentifyProtocolConfig};
use futures::prelude::*;
use libp2p_core::{
protocols_handler::{KeepAlive, ProtocolsHandler, ProtocolsHandlerEvent, ProtocolsHandlerUpgrErr},
upgrade::{DeniedUpgrade, InboundUpgrade, OutboundUpgrade}
upgrade::{DeniedUpgrade, InboundUpgrade, OutboundUpgrade, Negotiated}
};
use smallvec::SmallVec;
use tokio_io::{AsyncRead, AsyncWrite};
@ -34,7 +34,7 @@ pub struct IdentifyListenHandler<TSubstream> {
config: IdentifyProtocolConfig,
/// List of senders to yield to the user.
pending_result: SmallVec<[IdentifySender<TSubstream>; 4]>,
pending_result: SmallVec<[IdentifySender<Negotiated<TSubstream>>; 4]>,
}
impl<TSubstream> IdentifyListenHandler<TSubstream> {
@ -53,7 +53,7 @@ where
TSubstream: AsyncRead + AsyncWrite,
{
type InEvent = Void;
type OutEvent = IdentifySender<TSubstream>;
type OutEvent = IdentifySender<Negotiated<TSubstream>>;
type Error = Void;
type Substream = TSubstream;
type InboundProtocol = IdentifyProtocolConfig;

View File

@ -24,7 +24,7 @@ use futures::{future::{self, FutureResult}, Async, AsyncSink, Future, Poll, Sink
use futures::try_ready;
use libp2p_core::{
Multiaddr, PublicKey,
upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo}
upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo, Negotiated}
};
use log::{debug, trace};
use protobuf::Message as ProtobufMessage;
@ -150,11 +150,11 @@ impl<C> InboundUpgrade<C> for IdentifyProtocolConfig
where
C: AsyncRead + AsyncWrite,
{
type Output = IdentifySender<C>;
type Output = IdentifySender<Negotiated<C>>;
type Error = IoError;
type Future = FutureResult<Self::Output, IoError>;
fn upgrade_inbound(self, socket: C, _: Self::Info) -> Self::Future {
fn upgrade_inbound(self, socket: Negotiated<C>, _: Self::Info) -> Self::Future {
trace!("Upgrading inbound connection");
let socket = Framed::new(socket, codec::UviBytes::default());
let sender = IdentifySender { inner: socket };
@ -168,9 +168,9 @@ where
{
type Output = RemoteInfo;
type Error = IoError;
type Future = IdentifyOutboundFuture<C>;
type Future = IdentifyOutboundFuture<Negotiated<C>>;
fn upgrade_outbound(self, socket: C, _: Self::Info) -> Self::Future {
fn upgrade_outbound(self, socket: Negotiated<C>, _: Self::Info) -> Self::Future {
IdentifyOutboundFuture {
inner: Framed::new(socket, codec::UviBytes::<BytesMut>::default()),
shutdown: false,

View File

@ -24,7 +24,7 @@ use crate::protocol::{
};
use futures::prelude::*;
use libp2p_core::protocols_handler::{KeepAlive, ProtocolsHandler, ProtocolsHandlerEvent, ProtocolsHandlerUpgrErr};
use libp2p_core::{upgrade, either::EitherOutput, InboundUpgrade, OutboundUpgrade, PeerId};
use libp2p_core::{upgrade, either::EitherOutput, InboundUpgrade, OutboundUpgrade, PeerId, upgrade::Negotiated};
use multihash::Multihash;
use std::{error, fmt, io, time::Duration, time::Instant};
use tokio_io::{AsyncRead, AsyncWrite};
@ -49,7 +49,7 @@ where
next_connec_unique_id: UniqueConnecId,
/// List of active substreams with the state they are in.
substreams: Vec<SubstreamState<TSubstream, TUserData>>,
substreams: Vec<SubstreamState<Negotiated<TSubstream>, TUserData>>,
/// Until when to keep the connection alive.
keep_alive: KeepAlive,

View File

@ -29,7 +29,7 @@
use bytes::BytesMut;
use crate::protobuf_structs;
use futures::{future, sink, stream, Sink, Stream};
use libp2p_core::{InboundUpgrade, Multiaddr, OutboundUpgrade, PeerId, UpgradeInfo};
use libp2p_core::{InboundUpgrade, Multiaddr, OutboundUpgrade, PeerId, UpgradeInfo, upgrade::Negotiated};
use multihash::Multihash;
use protobuf::{self, Message};
use std::io::{Error as IoError, ErrorKind as IoErrorKind};
@ -153,12 +153,12 @@ impl<C> InboundUpgrade<C> for KademliaProtocolConfig
where
C: AsyncRead + AsyncWrite,
{
type Output = KadInStreamSink<C>;
type Output = KadInStreamSink<Negotiated<C>>;
type Future = future::FutureResult<Self::Output, IoError>;
type Error = IoError;
#[inline]
fn upgrade_inbound(self, incoming: C, _: Self::Info) -> Self::Future {
fn upgrade_inbound(self, incoming: Negotiated<C>, _: Self::Info) -> Self::Future {
let mut codec = codec::UviBytes::default();
codec.set_max_len(4096);
@ -182,12 +182,12 @@ impl<C> OutboundUpgrade<C> for KademliaProtocolConfig
where
C: AsyncRead + AsyncWrite,
{
type Output = KadOutStreamSink<C>;
type Output = KadOutStreamSink<Negotiated<C>>;
type Future = future::FutureResult<Self::Output, IoError>;
type Error = IoError;
#[inline]
fn upgrade_outbound(self, incoming: C, _: Self::Info) -> Self::Future {
fn upgrade_outbound(self, incoming: Negotiated<C>, _: Self::Info) -> Self::Future {
let mut codec = codec::UviBytes::default();
codec.set_max_len(4096);

View File

@ -58,7 +58,7 @@ pub use io::NoiseOutput;
pub use protocol::{Keypair, PublicKey, Protocol, ProtocolParams, IX, IK, XX};
pub use protocol::x25519::X25519;
use libp2p_core::{UpgradeInfo, InboundUpgrade, OutboundUpgrade};
use libp2p_core::{UpgradeInfo, InboundUpgrade, OutboundUpgrade, upgrade::Negotiated};
use tokio_io::{AsyncRead, AsyncWrite};
use zeroize::Zeroize;
@ -127,11 +127,11 @@ where
NoiseConfig<IX, C>: UpgradeInfo,
C: Protocol<C> + AsRef<[u8]> + Zeroize
{
type Output = (PublicKey<C>, NoiseOutput<T>);
type Output = (PublicKey<C>, NoiseOutput<Negotiated<T>>);
type Error = NoiseError;
type Future = rt1::NoiseInboundFuture<T, C>;
type Future = rt1::NoiseInboundFuture<Negotiated<T>, C>;
fn upgrade_inbound(self, socket: T, _: Self::Info) -> Self::Future {
fn upgrade_inbound(self, socket: Negotiated<T>, _: Self::Info) -> Self::Future {
let session = self.params.into_builder()
.local_private_key(self.keys.secret().as_ref())
.build_responder()
@ -146,11 +146,11 @@ where
NoiseConfig<IX, C>: UpgradeInfo,
C: Protocol<C> + AsRef<[u8]> + Zeroize
{
type Output = (PublicKey<C>, NoiseOutput<T>);
type Output = (PublicKey<C>, NoiseOutput<Negotiated<T>>);
type Error = NoiseError;
type Future = rt1::NoiseOutboundFuture<T, C>;
type Future = rt1::NoiseOutboundFuture<Negotiated<T>, C>;
fn upgrade_outbound(self, socket: T, _: Self::Info) -> Self::Future {
fn upgrade_outbound(self, socket: Negotiated<T>, _: Self::Info) -> Self::Future {
let session = self.params.into_builder()
.local_private_key(self.keys.secret().as_ref())
.build_initiator()
@ -167,11 +167,11 @@ where
NoiseConfig<XX, C>: UpgradeInfo,
C: Protocol<C> + AsRef<[u8]> + Zeroize
{
type Output = (PublicKey<C>, NoiseOutput<T>);
type Output = (PublicKey<C>, NoiseOutput<Negotiated<T>>);
type Error = NoiseError;
type Future = rt15::NoiseInboundFuture<T, C>;
type Future = rt15::NoiseInboundFuture<Negotiated<T>, C>;
fn upgrade_inbound(self, socket: T, _: Self::Info) -> Self::Future {
fn upgrade_inbound(self, socket: Negotiated<T>, _: Self::Info) -> Self::Future {
let session = self.params.into_builder()
.local_private_key(self.keys.secret().as_ref())
.build_responder()
@ -186,11 +186,11 @@ where
NoiseConfig<XX, C>: UpgradeInfo,
C: Protocol<C> + AsRef<[u8]> + Zeroize
{
type Output = (PublicKey<C>, NoiseOutput<T>);
type Output = (PublicKey<C>, NoiseOutput<Negotiated<T>>);
type Error = NoiseError;
type Future = rt15::NoiseOutboundFuture<T, C>;
type Future = rt15::NoiseOutboundFuture<Negotiated<T>, C>;
fn upgrade_outbound(self, socket: T, _: Self::Info) -> Self::Future {
fn upgrade_outbound(self, socket: Negotiated<T>, _: Self::Info) -> Self::Future {
let session = self.params.into_builder()
.local_private_key(self.keys.secret().as_ref())
.build_initiator()
@ -207,11 +207,11 @@ where
NoiseConfig<IK, C>: UpgradeInfo,
C: Protocol<C> + AsRef<[u8]> + Zeroize
{
type Output = (PublicKey<C>, NoiseOutput<T>);
type Output = (PublicKey<C>, NoiseOutput<Negotiated<T>>);
type Error = NoiseError;
type Future = rt1::NoiseInboundFuture<T, C>;
type Future = rt1::NoiseInboundFuture<Negotiated<T>, C>;
fn upgrade_inbound(self, socket: T, _: Self::Info) -> Self::Future {
fn upgrade_inbound(self, socket: Negotiated<T>, _: Self::Info) -> Self::Future {
let session = self.params.into_builder()
.local_private_key(self.keys.secret().as_ref())
.build_responder()
@ -226,11 +226,11 @@ where
NoiseConfig<IK, C, PublicKey<C>>: UpgradeInfo,
C: Protocol<C> + AsRef<[u8]> + Zeroize
{
type Output = (PublicKey<C>, NoiseOutput<T>);
type Output = (PublicKey<C>, NoiseOutput<Negotiated<T>>);
type Error = NoiseError;
type Future = rt1::NoiseOutboundFuture<T, C>;
type Future = rt1::NoiseOutboundFuture<Negotiated<T>, C>;
fn upgrade_outbound(self, socket: T, _: Self::Info) -> Self::Future {
fn upgrade_outbound(self, socket: Negotiated<T>, _: Self::Info) -> Self::Future {
let session = self.params.into_builder()
.local_private_key(self.keys.secret().as_ref())
.remote_public_key(self.remote.as_ref())

View File

@ -23,7 +23,7 @@
use bytes::Bytes;
use futures::{future, prelude::*};
use libp2p_core::{Multiaddr, upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo}};
use libp2p_core::{Multiaddr, upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo, Negotiated}};
use std::{io, iter};
use tokio_codec::{FramedRead, FramedWrite};
use tokio_io::{AsyncRead, AsyncWrite};
@ -51,11 +51,11 @@ impl<C> InboundUpgrade<C> for Observed
where
C: AsyncRead + AsyncWrite + Send + 'static
{
type Output = Sender<C>;
type Output = Sender<Negotiated<C>>;
type Error = io::Error;
type Future = Box<dyn Future<Item=Self::Output, Error=Self::Error> + Send>;
fn upgrade_inbound(self, conn: C, _: Self::Info) -> Self::Future {
fn upgrade_inbound(self, conn: Negotiated<C>, _: Self::Info) -> Self::Future {
let io = FramedWrite::new(conn, UviBytes::default());
Box::new(future::ok(Sender { io }))
}
@ -69,10 +69,10 @@ where
type Error = io::Error;
type Future = Box<dyn Future<Item=Self::Output, Error=Self::Error> + Send>;
fn upgrade_outbound(self, conn: C, _: Self::Info) -> Self::Future {
fn upgrade_outbound(self, conn: Negotiated<C>, _: Self::Info) -> Self::Future {
let io = FramedRead::new(conn, UviBytes::default());
let future = io.into_future()
.map_err(|(e, _): (io::Error, FramedRead<C, UviBytes>)| e)
.map_err(|(e, _): (io::Error, FramedRead<Negotiated<C>, UviBytes>)| e)
.and_then(move |(bytes, _)| {
if let Some(b) = bytes {
let ma = Multiaddr::from_bytes(b.to_vec())
@ -100,7 +100,7 @@ impl<C: AsyncWrite> Sender<C> {
#[cfg(test)]
mod tests {
use libp2p_core::{Multiaddr, upgrade::{InboundUpgrade, OutboundUpgrade}};
use libp2p_core::{Multiaddr, upgrade::{apply_inbound, apply_outbound}};
use tokio::runtime::current_thread;
use tokio::net::{TcpListener, TcpStream};
use super::*;
@ -115,17 +115,19 @@ mod tests {
let server = server.incoming()
.into_future()
.map_err(|(e, _)| e.into())
.map_err(|_| panic!())
.and_then(move |(conn, _)| {
Observed::new().upgrade_inbound(conn.unwrap(), b"/paritytech/observed-address/0.1.0")
apply_inbound(conn.unwrap(), Observed::new())
})
.map_err(|_| panic!())
.and_then(move |sender| sender.send_address(observed_addr1));
let client = TcpStream::connect(&server_addr)
.map_err(|e| e.into())
.map_err(|_| panic!())
.and_then(|conn| {
Observed::new().upgrade_outbound(conn, b"/paritytech/observed-address/0.1.0")
apply_outbound(conn, Observed::new())
})
.map_err(|_| panic!())
.map(move |addr| {
eprintln!("{} {}", addr, observed_addr2);
assert_eq!(addr, observed_addr2)
@ -133,7 +135,7 @@ mod tests {
current_thread::block_on_all(future::lazy(move || {
current_thread::spawn(server.map_err(|e| panic!("server error: {}", e)).map(|_| ()));
client.map_err(|e| panic!("client error: {}", e))
client
}))
.unwrap();
}

View File

@ -19,7 +19,7 @@
// DEALINGS IN THE SOFTWARE.
use futures::{prelude::*, future, try_ready};
use libp2p_core::{InboundUpgrade, OutboundUpgrade, UpgradeInfo};
use libp2p_core::{InboundUpgrade, OutboundUpgrade, UpgradeInfo, upgrade::Negotiated};
use log::debug;
use rand::{distributions::Standard, prelude::*, rngs::EntropyRng};
use std::{io, iter, time::Duration, time::Instant};
@ -53,10 +53,10 @@ where
{
type Output = ();
type Error = io::Error;
type Future = future::Map<future::AndThen<future::AndThen<future::AndThen<tokio_io::io::ReadExact<TSocket, [u8; 32]>, tokio_io::io::WriteAll<TSocket, [u8; 32]>, fn((TSocket, [u8; 32])) -> tokio_io::io::WriteAll<TSocket, [u8; 32]>>, tokio_io::io::Flush<TSocket>, fn((TSocket, [u8; 32])) -> tokio_io::io::Flush<TSocket>>, tokio_io::io::Shutdown<TSocket>, fn(TSocket) -> tokio_io::io::Shutdown<TSocket>>, fn(TSocket) -> ()>;
type Future = future::Map<future::AndThen<future::AndThen<future::AndThen<tokio_io::io::ReadExact<Negotiated<TSocket>, [u8; 32]>, tokio_io::io::WriteAll<Negotiated<TSocket>, [u8; 32]>, fn((Negotiated<TSocket>, [u8; 32])) -> tokio_io::io::WriteAll<Negotiated<TSocket>, [u8; 32]>>, tokio_io::io::Flush<Negotiated<TSocket>>, fn((Negotiated<TSocket>, [u8; 32])) -> tokio_io::io::Flush<Negotiated<TSocket>>>, tokio_io::io::Shutdown<Negotiated<TSocket>>, fn(Negotiated<TSocket>) -> tokio_io::io::Shutdown<Negotiated<TSocket>>>, fn(Negotiated<TSocket>) -> ()>;
#[inline]
fn upgrade_inbound(self, socket: TSocket, _: Self::Info) -> Self::Future {
fn upgrade_inbound(self, socket: Negotiated<TSocket>, _: Self::Info) -> Self::Future {
tokio_io::io::read_exact(socket, [0; 32])
.and_then::<fn(_) -> _, _>(|(socket, buffer)| tokio_io::io::write_all(socket, buffer))
.and_then::<fn(_) -> _, _>(|(socket, _)| tokio_io::io::flush(socket))
@ -71,10 +71,10 @@ where
{
type Output = Duration;
type Error = io::Error;
type Future = PingDialer<TSocket>;
type Future = PingDialer<Negotiated<TSocket>>;
#[inline]
fn upgrade_outbound(self, socket: TSocket, _: Self::Info) -> Self::Future {
fn upgrade_outbound(self, socket: Negotiated<TSocket>, _: Self::Info) -> Self::Future {
let payload: [u8; 32] = EntropyRng::default().sample(Standard);
debug!("Preparing for ping with payload {:?}", payload);

View File

@ -19,7 +19,7 @@
// DEALINGS IN THE SOFTWARE.
use futures::future::{self, FutureResult};
use libp2p_core::{InboundUpgrade, OutboundUpgrade, UpgradeInfo};
use libp2p_core::{InboundUpgrade, OutboundUpgrade, UpgradeInfo, upgrade::Negotiated};
use std::iter;
use void::Void;
@ -36,21 +36,21 @@ impl UpgradeInfo for PlainTextConfig {
}
impl<C> InboundUpgrade<C> for PlainTextConfig {
type Output = C;
type Output = Negotiated<C>;
type Error = Void;
type Future = FutureResult<C, Self::Error>;
type Future = FutureResult<Negotiated<C>, Self::Error>;
fn upgrade_inbound(self, i: C, _: Self::Info) -> Self::Future {
fn upgrade_inbound(self, i: Negotiated<C>, _: Self::Info) -> Self::Future {
future::ok(i)
}
}
impl<C> OutboundUpgrade<C> for PlainTextConfig {
type Output = C;
type Output = Negotiated<C>;
type Error = Void;
type Future = FutureResult<C, Self::Error>;
type Future = FutureResult<Negotiated<C>, Self::Error>;
fn upgrade_outbound(self, i: C, _: Self::Info) -> Self::Future {
fn upgrade_outbound(self, i: Negotiated<C>, _: Self::Info) -> Self::Future {
future::ok(i)
}
}

View File

@ -84,7 +84,7 @@ pub use self::error::SecioError;
use bytes::BytesMut;
use futures::stream::MapErr as StreamMapErr;
use futures::{Future, Poll, Sink, StartSend, Stream};
use libp2p_core::{PublicKey, identity, upgrade::{UpgradeInfo, InboundUpgrade, OutboundUpgrade}};
use libp2p_core::{PublicKey, identity, upgrade::{UpgradeInfo, InboundUpgrade, OutboundUpgrade, Negotiated}};
use log::debug;
use rw_stream_sink::RwStreamSink;
use std::io::{Error as IoError, ErrorKind as IoErrorKind};
@ -195,11 +195,11 @@ impl<T> InboundUpgrade<T> for SecioConfig
where
T: AsyncRead + AsyncWrite + Send + 'static
{
type Output = SecioOutput<T>;
type Output = SecioOutput<Negotiated<T>>;
type Error = SecioError;
type Future = Box<dyn Future<Item = Self::Output, Error = Self::Error> + Send>;
fn upgrade_inbound(self, socket: T, _: Self::Info) -> Self::Future {
fn upgrade_inbound(self, socket: Negotiated<T>, _: Self::Info) -> Self::Future {
Box::new(self.handshake(socket))
}
}
@ -208,11 +208,11 @@ impl<T> OutboundUpgrade<T> for SecioConfig
where
T: AsyncRead + AsyncWrite + Send + 'static
{
type Output = SecioOutput<T>;
type Output = SecioOutput<Negotiated<T>>;
type Error = SecioError;
type Future = Box<dyn Future<Item = Self::Output, Error = Self::Error> + Send>;
fn upgrade_outbound(self, socket: T, _: Self::Info) -> Self::Future {
fn upgrade_outbound(self, socket: Negotiated<T>, _: Self::Info) -> Self::Future {
Box::new(self.handshake(socket))
}
}

View File

@ -18,7 +18,7 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
use crate::core::upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo};
use crate::core::upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo, Negotiated};
use bytes::Bytes;
use futures::{future::FromErr, prelude::*};
use std::{iter, io::Error as IoError, sync::Arc};
@ -70,7 +70,7 @@ impl<F> UpgradeInfo for SimpleProtocol<F> {
impl<C, F, O> InboundUpgrade<C> for SimpleProtocol<F>
where
C: AsyncRead + AsyncWrite,
F: Fn(C) -> O,
F: Fn(Negotiated<C>) -> O,
O: IntoFuture<Error = IoError>
{
type Output = O::Item;
@ -78,7 +78,7 @@ where
type Future = FromErr<O::Future, IoError>;
#[inline]
fn upgrade_inbound(self, socket: C, _: Self::Info) -> Self::Future {
fn upgrade_inbound(self, socket: Negotiated<C>, _: Self::Info) -> Self::Future {
let upgrade = &self.upgrade;
upgrade(socket).into_future().from_err()
}
@ -87,7 +87,7 @@ where
impl<C, F, O> OutboundUpgrade<C> for SimpleProtocol<F>
where
C: AsyncRead + AsyncWrite,
F: Fn(C) -> O,
F: Fn(Negotiated<C>) -> O,
O: IntoFuture<Error = IoError>
{
type Output = O::Item;
@ -95,7 +95,7 @@ where
type Future = FromErr<O::Future, IoError>;
#[inline]
fn upgrade_outbound(self, socket: C, _: Self::Info) -> Self::Future {
fn upgrade_outbound(self, socket: Negotiated<C>, _: Self::Info) -> Self::Future {
let upgrade = &self.upgrade;
upgrade(socket).into_future().from_err()
}