Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Extend to both muxed and non-muxed versions
  • Loading branch information
tomaka committed Sep 6, 2018
commit fcaeb430dc3fde5643f34ca790c8218c778771ae
78 changes: 73 additions & 5 deletions core/src/transport/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ use std::io::Error as IoError;
use std::sync::Arc;
use transport::{MuxedTransport, Transport};

/// See the `Transport::boxed` method.
#[inline]
pub fn boxed<T>(transport: T) -> Boxed<T::Output>
where T: Transport + Clone + Send + Sync + 'static,
T::Dial: Send + 'static,
T::Listener: Send + 'static,
T::ListenerUpgrade: Send + 'static,
T::MultiaddrFuture: Send + 'static,
{
Boxed {
inner: Arc::new(transport) as Arc<_>
}
}
/// See the `Transport::boxed_muxed` method.
#[inline]
pub fn boxed_muxed<T>(transport: T) -> BoxedMuxed<T::Output>
Expand Down Expand Up @@ -51,17 +64,14 @@ trait Abstract<O> {
fn listen_on(&self, addr: Multiaddr) -> Result<(Listener<O>, Multiaddr), Multiaddr>;
fn dial(&self, addr: Multiaddr) -> Result<Dial<O>, Multiaddr>;
fn nat_traversal(&self, server: &Multiaddr, observed: &Multiaddr) -> Option<Multiaddr>;
fn next_incoming(&self) -> Incoming<O>;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this trait?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the same as Transport, but since it doesn't have associated types and doesn't take by ownership, it can be put in a Box.


impl<T, O> Abstract<O> for T
where T: MuxedTransport<Output = O> + Clone + 'static,
where T: Transport<Output = O> + Clone + 'static,
T::Dial: Send + 'static,
T::Listener: Send + 'static,
T::ListenerUpgrade: Send + 'static,
T::MultiaddrFuture: Send + 'static,
T::Incoming: Send + 'static,
T::IncomingUpgrade: Send + 'static,
{
fn listen_on(&self, addr: Multiaddr) -> Result<(Listener<O>, Multiaddr), Multiaddr> {
let (listener, new_addr) = Transport::listen_on(self.clone(), addr)
Expand All @@ -85,7 +95,21 @@ where T: MuxedTransport<Output = O> + Clone + 'static,
fn nat_traversal(&self, server: &Multiaddr, observed: &Multiaddr) -> Option<Multiaddr> {
Transport::nat_traversal(self, server, observed)
}
}

trait AbstractMuxed<O>: Abstract<O> {
fn next_incoming(&self) -> Incoming<O>;
}

impl<T, O> AbstractMuxed<O> for T
where T: MuxedTransport<Output = O> + Clone + 'static,
T::Dial: Send + 'static,
T::Listener: Send + 'static,
T::ListenerUpgrade: Send + 'static,
T::MultiaddrFuture: Send + 'static,
T::Incoming: Send + 'static,
T::IncomingUpgrade: Send + 'static,
{
fn next_incoming(&self) -> Incoming<O> {
let fut = MuxedTransport::next_incoming(self.clone())
.map(|upgrade| {
Expand All @@ -98,10 +122,54 @@ where T: MuxedTransport<Output = O> + Clone + 'static,

/// See the `Transport::boxed` method.
// TODO: implement Debug
pub struct BoxedMuxed<O> {
pub struct Boxed<O> {
inner: Arc<Abstract<O> + Send + Sync>,
}

impl<O> Clone for Boxed<O> {
#[inline]
fn clone(&self) -> Self {
Boxed {
inner: self.inner.clone(),
}
}
}

impl<O> Transport for Boxed<O> {
type Output = O;
type MultiaddrFuture = MultiaddrFuture;
type Listener = Listener<O>;
type ListenerUpgrade = ListenerUpgrade<O>;
type Dial = Dial<O>;

#[inline]
fn listen_on(self, addr: Multiaddr) -> Result<(Self::Listener, Multiaddr), (Self, Multiaddr)> {
match self.inner.listen_on(addr) {
Ok(listen) => Ok(listen),
Err(addr) => Err((self, addr))
}
}

#[inline]
fn dial(self, addr: Multiaddr) -> Result<Self::Dial, (Self, Multiaddr)> {
match self.inner.dial(addr) {
Ok(dial) => Ok(dial),
Err(addr) => Err((self, addr))
}
}

#[inline]
fn nat_traversal(&self, server: &Multiaddr, observed: &Multiaddr) -> Option<Multiaddr> {
self.inner.nat_traversal(server, observed)
}
}

/// See the `Transport::boxed_muxed` method.
// TODO: implement Debug
pub struct BoxedMuxed<O> {
inner: Arc<AbstractMuxed<O> + Send + Sync>,
}

impl<O> Clone for BoxedMuxed<O> {
#[inline]
fn clone(&self) -> Self {
Expand Down
14 changes: 14 additions & 0 deletions core/src/transport/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,20 @@ pub trait Transport {

/// Turns this `Transport` into an abstract boxed transport.
#[inline]
fn boxed(self) -> boxed::Boxed<Self::Output>
where Self: Sized + MuxedTransport + Clone + Send + Sync + 'static,
Self::Dial: Send + 'static,
Self::Listener: Send + 'static,
Self::ListenerUpgrade: Send + 'static,
Self::MultiaddrFuture: Send + 'static,
{
boxed::boxed(self)
}

/// Turns this `Transport` into an abstract boxed transport.
///
/// This is the version if the transport supports muxing.
#[inline]
fn boxed_muxed(self) -> boxed::BoxedMuxed<Self::Output>
where Self: Sized + MuxedTransport + Clone + Send + Sync + 'static,
Self::Dial: Send + 'static,
Expand Down