mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-22 14:21:33 +00:00
Fix concerns
This commit is contained in:
@ -28,7 +28,7 @@ extern crate tokio_io;
|
||||
|
||||
use bytes::BytesMut;
|
||||
use futures::{Stream, Sink, Future};
|
||||
use swarm::Transport;
|
||||
use swarm::{Transport, SimpleProtocol};
|
||||
use tcp::TcpConfig;
|
||||
use tokio_core::reactor::Core;
|
||||
use tokio_io::codec::length_delimited;
|
||||
@ -47,9 +47,9 @@ fn main() {
|
||||
}
|
||||
});
|
||||
|
||||
let with_echo = with_secio.with_simple_protocol_upgrade("/echo/1.0.0", |socket| {
|
||||
let with_echo = with_secio.with_upgrade(SimpleProtocol::new("/echo/1.0.0", |socket| {
|
||||
Ok(length_delimited::Framed::<_, BytesMut>::new(socket))
|
||||
});
|
||||
}));
|
||||
|
||||
let dialer = with_echo.dial(swarm::multiaddr::Multiaddr::new("/ip4/127.0.0.1/tcp/10333").unwrap())
|
||||
.unwrap_or_else(|_| panic!())
|
||||
|
@ -26,12 +26,9 @@ extern crate libp2p_tcp_transport as tcp;
|
||||
extern crate tokio_core;
|
||||
extern crate tokio_io;
|
||||
|
||||
use bytes::BytesMut;
|
||||
use futures::future::{Future, IntoFuture, loop_fn, Loop};
|
||||
use futures::{Stream, Sink};
|
||||
use std::io::Error as IoError;
|
||||
use std::iter;
|
||||
use swarm::{Transport, ConnectionUpgrade};
|
||||
use swarm::{Transport, SimpleProtocol};
|
||||
use tcp::TcpConfig;
|
||||
use tokio_core::reactor::Core;
|
||||
use tokio_io::codec::length_delimited;
|
||||
@ -50,9 +47,9 @@ fn main() {
|
||||
}
|
||||
});
|
||||
|
||||
let with_echo = with_secio.with_simple_protocol_upgrade("/echo/1.0.0", |socket| {
|
||||
Ok(length_delimited::Framed::<_, BytesMut>::new(socket))
|
||||
});
|
||||
let with_echo = with_secio.with_upgrade(SimpleProtocol::new("/echo/1.0.0", |socket| {
|
||||
Ok(length_delimited::Framed::new(socket))
|
||||
}));
|
||||
|
||||
let future = with_echo.listen_on(swarm::multiaddr::Multiaddr::new("/ip4/0.0.0.0/tcp/10333").unwrap())
|
||||
.unwrap_or_else(|_| panic!()).0
|
||||
|
@ -149,3 +149,4 @@ pub use self::connection_reuse::ConnectionReuse;
|
||||
pub use self::multiaddr::Multiaddr;
|
||||
pub use self::muxing::StreamMuxer;
|
||||
pub use self::transport::{ConnectionUpgrade, PlainTextConfig, Transport, UpgradedNode, OrUpgrade};
|
||||
pub use self::transport::SimpleProtocol;
|
||||
|
@ -102,25 +102,6 @@ pub trait Transport {
|
||||
upgrade: upgrade,
|
||||
}
|
||||
}
|
||||
|
||||
/// Wraps this transport inside an upgrade. Similar to `with_upgrade`, but more convenient to
|
||||
/// use for small protocols.
|
||||
#[inline]
|
||||
fn with_simple_protocol_upgrade<N, F, O>(self, name: N, upgrade: F)
|
||||
-> UpgradedNode<Self, SimpleProtocolUpgrade<F>>
|
||||
where Self: Sized,
|
||||
N: Into<Bytes>,
|
||||
F: Fn(Self::RawConn) -> O,
|
||||
O: IntoFuture<Error = IoError>,
|
||||
{
|
||||
UpgradedNode {
|
||||
transports: self,
|
||||
upgrade: SimpleProtocolUpgrade {
|
||||
name: name.into(),
|
||||
upgrade: Arc::new(upgrade),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Dummy implementation of `Transport` that just denies every single attempt.
|
||||
@ -184,26 +165,39 @@ impl<A, B> Transport for OrTransport<A, B>
|
||||
}
|
||||
}
|
||||
|
||||
/// Implementation of `ConnetionUpgrade`. See `with_simple_protocol_upgrade`.
|
||||
/// Implementation of `ConnectionUpgrade`. Convenient to use with small protocols.
|
||||
#[derive(Debug)]
|
||||
pub struct SimpleProtocolUpgrade<F> {
|
||||
pub struct SimpleProtocol<F> {
|
||||
name: Bytes,
|
||||
// Note: we put the closure `F` in an `Arc` because Rust closures aren't automatically clonable
|
||||
// yet.
|
||||
upgrade: Arc<F>,
|
||||
}
|
||||
|
||||
impl<F> Clone for SimpleProtocolUpgrade<F> {
|
||||
impl<F> SimpleProtocol<F> {
|
||||
/// Builds a `SimpleProtocol`.
|
||||
#[inline]
|
||||
pub fn new<N>(name: N, upgrade: F) -> SimpleProtocol<F>
|
||||
where N: Into<Bytes>
|
||||
{
|
||||
SimpleProtocol {
|
||||
name: name.into(),
|
||||
upgrade: Arc::new(upgrade),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<F> Clone for SimpleProtocol<F> {
|
||||
#[inline]
|
||||
fn clone(&self) -> Self {
|
||||
SimpleProtocolUpgrade {
|
||||
SimpleProtocol {
|
||||
name: self.name.clone(),
|
||||
upgrade: self.upgrade.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<C, F, O> ConnectionUpgrade<C> for SimpleProtocolUpgrade<F>
|
||||
impl<C, F, O> ConnectionUpgrade<C> for SimpleProtocol<F>
|
||||
where C: AsyncRead + AsyncWrite,
|
||||
F: Fn(C) -> O,
|
||||
O: IntoFuture<Error = IoError>,
|
||||
|
Reference in New Issue
Block a user