mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-29 17:51:35 +00:00
Run rustfmt on the code and use tabs
This commit is contained in:
@ -46,9 +46,9 @@
|
|||||||
use futures::{Future, Stream, Async, Poll};
|
use futures::{Future, Stream, Async, Poll};
|
||||||
use futures::stream::Fuse as StreamFuse;
|
use futures::stream::Fuse as StreamFuse;
|
||||||
use multiaddr::Multiaddr;
|
use multiaddr::Multiaddr;
|
||||||
|
use muxing::StreamMuxer;
|
||||||
use smallvec::SmallVec;
|
use smallvec::SmallVec;
|
||||||
use std::io::Error as IoError;
|
use std::io::Error as IoError;
|
||||||
use muxing::StreamMuxer;
|
|
||||||
use transport::{Transport, ConnectionUpgrade, UpgradedNode};
|
use transport::{Transport, ConnectionUpgrade, UpgradedNode};
|
||||||
|
|
||||||
/// Allows reusing the same muxed connection multiple times.
|
/// Allows reusing the same muxed connection multiple times.
|
||||||
@ -59,7 +59,7 @@ use transport::{Transport, ConnectionUpgrade, UpgradedNode};
|
|||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct ConnectionReuse<T, C>
|
pub struct ConnectionReuse<T, C>
|
||||||
where T: Transport,
|
where T: Transport,
|
||||||
C: ConnectionUpgrade<T::RawConn>,
|
C: ConnectionUpgrade<T::RawConn>
|
||||||
{
|
{
|
||||||
// Underlying transport and connection upgrade for when we need to dial or listen.
|
// Underlying transport and connection upgrade for when we need to dial or listen.
|
||||||
inner: UpgradedNode<T, C>,
|
inner: UpgradedNode<T, C>,
|
||||||
@ -67,13 +67,11 @@ pub struct ConnectionReuse<T, C>
|
|||||||
|
|
||||||
impl<T, C> From<UpgradedNode<T, C>> for ConnectionReuse<T, C>
|
impl<T, C> From<UpgradedNode<T, C>> for ConnectionReuse<T, C>
|
||||||
where T: Transport,
|
where T: Transport,
|
||||||
C: ConnectionUpgrade<T::RawConn>,
|
C: ConnectionUpgrade<T::RawConn>
|
||||||
{
|
{
|
||||||
#[inline]
|
#[inline]
|
||||||
fn from(node: UpgradedNode<T, C>) -> ConnectionReuse<T, C> {
|
fn from(node: UpgradedNode<T, C>) -> ConnectionReuse<T, C> {
|
||||||
ConnectionReuse {
|
ConnectionReuse { inner: node }
|
||||||
inner: node,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -82,19 +80,25 @@ impl<T, C> Transport for ConnectionReuse<T, C>
|
|||||||
C: ConnectionUpgrade<T::RawConn> + 'static, // TODO: 'static :(
|
C: ConnectionUpgrade<T::RawConn> + 'static, // TODO: 'static :(
|
||||||
C: Clone,
|
C: Clone,
|
||||||
C::Output: StreamMuxer + Clone,
|
C::Output: StreamMuxer + Clone,
|
||||||
C::NamesIter: Clone, // TODO: not elegant
|
C::NamesIter: Clone // TODO: not elegant
|
||||||
{
|
{
|
||||||
type RawConn = <C::Output as StreamMuxer>::Substream;
|
type RawConn = <C::Output as StreamMuxer>::Substream;
|
||||||
type Listener = ConnectionReuseListener<Box<Stream<Item = (C::Output, Multiaddr), Error = IoError>>, C::Output>;
|
type Listener = ConnectionReuseListener<
|
||||||
|
Box<
|
||||||
|
Stream<
|
||||||
|
Item = (C::Output, Multiaddr),
|
||||||
|
Error = IoError,
|
||||||
|
>,
|
||||||
|
>,
|
||||||
|
C::Output,
|
||||||
|
>;
|
||||||
type Dial = Box<Future<Item = Self::RawConn, Error = IoError>>;
|
type Dial = Box<Future<Item = Self::RawConn, Error = IoError>>;
|
||||||
|
|
||||||
fn listen_on(self, addr: Multiaddr) -> Result<(Self::Listener, Multiaddr), (Self, Multiaddr)> {
|
fn listen_on(self, addr: Multiaddr) -> Result<(Self::Listener, Multiaddr), (Self, Multiaddr)> {
|
||||||
let (listener, new_addr) = match self.inner.listen_on(addr.clone()) {
|
let (listener, new_addr) = match self.inner.listen_on(addr.clone()) {
|
||||||
Ok((l, a)) => (l, a),
|
Ok((l, a)) => (l, a),
|
||||||
Err((inner, addr)) => {
|
Err((inner, addr)) => {
|
||||||
return Err((ConnectionReuse {
|
return Err((ConnectionReuse { inner: inner }, addr));
|
||||||
inner: inner,
|
|
||||||
}, addr));
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -110,16 +114,11 @@ impl<T, C> Transport for ConnectionReuse<T, C>
|
|||||||
let dial = match self.inner.dial(addr) {
|
let dial = match self.inner.dial(addr) {
|
||||||
Ok(l) => l,
|
Ok(l) => l,
|
||||||
Err((inner, addr)) => {
|
Err((inner, addr)) => {
|
||||||
return Err((ConnectionReuse {
|
return Err((ConnectionReuse { inner: inner }, addr));
|
||||||
inner: inner,
|
|
||||||
}, addr));
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let future = dial
|
let future = dial.and_then(|dial| dial.outbound());
|
||||||
.and_then(|dial| {
|
|
||||||
dial.outbound()
|
|
||||||
});
|
|
||||||
Ok(Box::new(future) as Box<_>)
|
Ok(Box::new(future) as Box<_>)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -128,7 +127,7 @@ impl<T, C> Transport for ConnectionReuse<T, C>
|
|||||||
/// `ConnectionReuse` struct.
|
/// `ConnectionReuse` struct.
|
||||||
pub struct ConnectionReuseListener<S, M>
|
pub struct ConnectionReuseListener<S, M>
|
||||||
where S: Stream<Item = (M, Multiaddr), Error = IoError>,
|
where S: Stream<Item = (M, Multiaddr), Error = IoError>,
|
||||||
M: StreamMuxer,
|
M: StreamMuxer
|
||||||
{
|
{
|
||||||
listener: StreamFuse<S>,
|
listener: StreamFuse<S>,
|
||||||
connections: Vec<(M, <M as StreamMuxer>::InboundSubstream, Multiaddr)>,
|
connections: Vec<(M, <M as StreamMuxer>::InboundSubstream, Multiaddr)>,
|
||||||
@ -136,7 +135,7 @@ pub struct ConnectionReuseListener<S, M>
|
|||||||
|
|
||||||
impl<S, M> Stream for ConnectionReuseListener<S, M>
|
impl<S, M> Stream for ConnectionReuseListener<S, M>
|
||||||
where S: Stream<Item = (M, Multiaddr), Error = IoError>,
|
where S: Stream<Item = (M, Multiaddr), Error = IoError>,
|
||||||
M: StreamMuxer + Clone + 'static, // TODO: 'static :(
|
M: StreamMuxer + Clone + 'static // TODO: 'static :(
|
||||||
{
|
{
|
||||||
type Item = (M::Substream, Multiaddr);
|
type Item = (M::Substream, Multiaddr);
|
||||||
type Error = IoError;
|
type Error = IoError;
|
||||||
@ -146,18 +145,18 @@ impl<S, M> Stream for ConnectionReuseListener<S, M>
|
|||||||
Ok(Async::Ready(Some((upgrade, client_addr)))) => {
|
Ok(Async::Ready(Some((upgrade, client_addr)))) => {
|
||||||
let next_incoming = upgrade.clone().inbound();
|
let next_incoming = upgrade.clone().inbound();
|
||||||
self.connections.push((upgrade, next_incoming, client_addr));
|
self.connections.push((upgrade, next_incoming, client_addr));
|
||||||
},
|
}
|
||||||
Ok(Async::NotReady) => (),
|
Ok(Async::NotReady) => (),
|
||||||
Ok(Async::Ready(None)) => {
|
Ok(Async::Ready(None)) => {
|
||||||
if self.connections.is_empty() {
|
if self.connections.is_empty() {
|
||||||
return Ok(Async::Ready(None));
|
return Ok(Async::Ready(None));
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
if self.connections.is_empty() {
|
if self.connections.is_empty() {
|
||||||
return Err(err);
|
return Err(err);
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut connections_to_drop: SmallVec<[_; 8]> = SmallVec::new();
|
let mut connections_to_drop: SmallVec<[_; 8]> = SmallVec::new();
|
||||||
@ -170,11 +169,11 @@ impl<S, M> Stream for ConnectionReuseListener<S, M>
|
|||||||
let mut new_next = muxer.clone().inbound();
|
let mut new_next = muxer.clone().inbound();
|
||||||
*next_incoming = new_next;
|
*next_incoming = new_next;
|
||||||
return Ok(Async::Ready(Some((incoming, client_addr.clone()))));
|
return Ok(Async::Ready(Some((incoming, client_addr.clone()))));
|
||||||
},
|
}
|
||||||
Ok(Async::NotReady) => (),
|
Ok(Async::NotReady) => (),
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
connections_to_drop.push(index);
|
connections_to_drop.push(index);
|
||||||
},
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -42,7 +42,9 @@ pub trait StreamMuxer {
|
|||||||
fn outbound(self) -> Self::OutboundSubstream;
|
fn outbound(self) -> Self::OutboundSubstream;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> StreamMuxer for T where T: AsyncRead + AsyncWrite {
|
impl<T> StreamMuxer for T
|
||||||
|
where T: AsyncRead + AsyncWrite
|
||||||
|
{
|
||||||
type Substream = Self;
|
type Substream = Self;
|
||||||
type InboundSubstream = FutureResult<Self, IoError>; // TODO: use !
|
type InboundSubstream = FutureResult<Self, IoError>; // TODO: use !
|
||||||
type OutboundSubstream = FutureResult<Self, IoError>; // TODO: use !
|
type OutboundSubstream = FutureResult<Self, IoError>; // TODO: use !
|
||||||
|
@ -549,7 +549,10 @@ impl<'a, T, C> UpgradedNode<T, C>
|
|||||||
pub fn listen_on(
|
pub fn listen_on(
|
||||||
self,
|
self,
|
||||||
addr: Multiaddr,
|
addr: Multiaddr,
|
||||||
) -> Result<(Box<Stream<Item = (C::Output, Multiaddr), Error = IoError> + 'a>, Multiaddr), (Self, Multiaddr)>
|
) -> Result<
|
||||||
|
(Box<Stream<Item = (C::Output, Multiaddr), Error = IoError> + 'a>, Multiaddr),
|
||||||
|
(Self, Multiaddr),
|
||||||
|
>
|
||||||
where C::NamesIter: Clone, // TODO: not elegant
|
where C::NamesIter: Clone, // TODO: not elegant
|
||||||
C: Clone
|
C: Clone
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user