Split ConnectionUpgrade. (#642)

Introduce `InboundUpgrade` and `OutboundUpgrade`.
This commit is contained in:
Toralf Wittner
2018-11-15 17:41:11 +01:00
committed by Pierre Krieger
parent 466385a58a
commit 2e549884ef
52 changed files with 2010 additions and 1658 deletions

View File

@ -19,10 +19,10 @@
// DEALINGS IN THE SOFTWARE.
use bytes::{BufMut, Bytes, BytesMut};
use crate::rpc_proto;
use futures::future;
use libp2p_core::{ConnectionUpgrade, Endpoint, PeerId};
use libp2p_core::{InboundUpgrade, OutboundUpgrade, UpgradeInfo, PeerId};
use protobuf::Message as ProtobufMessage;
use rpc_proto;
use std::{io, iter};
use tokio_codec::{Decoder, Encoder, Framed};
use tokio_io::{AsyncRead, AsyncWrite};
@ -41,29 +41,41 @@ impl FloodsubConfig {
}
}
impl<TSocket> ConnectionUpgrade<TSocket> for FloodsubConfig
where
TSocket: AsyncRead + AsyncWrite,
{
type NamesIter = iter::Once<(Bytes, Self::UpgradeIdentifier)>;
type UpgradeIdentifier = ();
impl UpgradeInfo for FloodsubConfig {
type UpgradeId = ();
type NamesIter = iter::Once<(Bytes, Self::UpgradeId)>;
#[inline]
fn protocol_names(&self) -> Self::NamesIter {
iter::once(("/floodsub/1.0.0".into(), ()))
}
}
impl<TSocket> InboundUpgrade<TSocket> for FloodsubConfig
where
TSocket: AsyncRead + AsyncWrite,
{
type Output = Framed<TSocket, FloodsubCodec>;
type Future = future::FutureResult<Self::Output, io::Error>;
type Error = io::Error;
type Future = future::FutureResult<Self::Output, Self::Error>;
#[inline]
fn upgrade(self, socket: TSocket, _: Self::UpgradeIdentifier, _: Endpoint) -> Self::Future {
future::ok(Framed::new(
socket,
FloodsubCodec {
length_prefix: Default::default(),
},
))
fn upgrade_inbound(self, socket: TSocket, _: Self::UpgradeId) -> Self::Future {
future::ok(Framed::new(socket, FloodsubCodec { length_prefix: Default::default() }))
}
}
impl<TSocket> OutboundUpgrade<TSocket> for FloodsubConfig
where
TSocket: AsyncRead + AsyncWrite,
{
type Output = Framed<TSocket, FloodsubCodec>;
type Error = io::Error;
type Future = future::FutureResult<Self::Output, Self::Error>;
#[inline]
fn upgrade_outbound(self, socket: TSocket, _: Self::UpgradeId) -> Self::Future {
future::ok(Framed::new(socket, FloodsubCodec { length_prefix: Default::default() }))
}
}