From 2783c5713e04557b3bef9b68e174c3ba5e71cc25 Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Wed, 10 Jan 2018 18:24:58 +0100 Subject: [PATCH] Fix concerns --- libp2p-tcp-transport/src/lib.rs | 1 - libp2p-websocket/README.md | 8 +++++--- libp2p-websocket/src/browser.rs | 36 ++++++++++++++++----------------- libp2p-websocket/src/lib.rs | 13 ++++++++---- 4 files changed, 32 insertions(+), 26 deletions(-) diff --git a/libp2p-tcp-transport/src/lib.rs b/libp2p-tcp-transport/src/lib.rs index db341307..f7e9ed85 100644 --- a/libp2p-tcp-transport/src/lib.rs +++ b/libp2p-tcp-transport/src/lib.rs @@ -109,7 +109,6 @@ impl Transport for TcpConfig { let future = future::result(listener).map(|listener| { // Pull out a stream of sockets for incoming connections listener.incoming().map(|(sock, addr)| { - println!("incoming tcp stream"); let addr = addr.to_multiaddr() .expect("generating a multiaddr from a socket addr never fails"); (Ok(sock).into_future(), addr) diff --git a/libp2p-websocket/README.md b/libp2p-websocket/README.md index 3f3b762d..04d8c86f 100644 --- a/libp2p-websocket/README.md +++ b/libp2p-websocket/README.md @@ -18,7 +18,7 @@ which uses `ws` on top of TCP/IP will automatically use the `XMLHttpRequest` Jav use libp2p_websocket::WsConfig; let ws_config = WsConfig::new(); -// let _ = ws_config.dial(Multiaddr::new("/ip4/40.41.42.43/tcp/12345/ws").unwrap()); +// let _ = ws_config.dial("/ip4/40.41.42.43/tcp/12345/ws".parse().unwrap()); ``` # Other operating systems @@ -29,15 +29,17 @@ plugged on top of another implementation of `Transport` such as TCP/IP. This underlying transport must be passed to the `WsConfig::new()` function. ```rust +extern crate libp2p_swarm; extern crate libp2p_tcp_transport; extern crate libp2p_websocket; extern crate tokio_core; -use libp2p_websocket::WsConfig; +use libp2p_swarm::{Multiaddr, Transport}; use libp2p_tcp_transport::TcpConfig; +use libp2p_websocket::WsConfig; use tokio_core::reactor::Core; let core = Core::new().unwrap(); let ws_config = WsConfig::new(TcpConfig::new(core.handle())); -// let _ = ws_config.dial(Multiaddr::new("/ip4/40.41.42.43/tcp/12345/ws").unwrap()); +let _ = ws_config.dial("/ip4/40.41.42.43/tcp/12345/ws".parse().unwrap()); ``` diff --git a/libp2p-websocket/src/browser.rs b/libp2p-websocket/src/browser.rs index fe327c02..fa95a5ca 100644 --- a/libp2p-websocket/src/browser.rs +++ b/libp2p-websocket/src/browser.rs @@ -49,10 +49,10 @@ impl WsConfig { } impl Transport for WsConfig { - type RawConn = Connec; + type RawConn = WsConn; type Listener = Box>; // TODO: use `!` type ListenerUpgrade = Box>; // TODO: use `!` - type Dial = FutureThen>, Result, fn(Result, oneshot::Canceled>) -> Result>; + type Dial = FutureThen>, Result, fn(Result, oneshot::Canceled>) -> Result>; #[inline] fn listen_on(self, a: Multiaddr) -> Result<(Self::Listener, Multiaddr), (Self, Multiaddr)> { @@ -75,7 +75,7 @@ impl Transport for WsConfig { // Create the JS `WebSocket` object. let websocket = { - let val = js!{ + let val = js! { try { return new WebSocket(@{inner_addr}); } catch(e) { @@ -107,10 +107,10 @@ impl Transport for WsConfig { } }; - // Create a `open` channel that will be used to communicate the `Connec` that represents + // Create a `open` channel that will be used to communicate the `WsConn` that represents // the open dialing websocket. Also create a `open_cb` callback that will be used for the // `open` message of the websocket. - let (open_tx, open_rx) = oneshot::channel::>(); + let (open_tx, open_rx) = oneshot::channel::>(); let open_tx = Arc::new(Mutex::new(Some(open_tx))); let websocket_clone = websocket.clone(); let open_cb = { @@ -124,18 +124,18 @@ impl Transport for WsConfig { // is not supposed to happen. let message_rx = message_rx.take().expect("the websocket can only open once"); - // Send a `Connec` to the future that was returned by `dial`. Ignoring errors that + // Send a `WsConn` to the future that was returned by `dial`. Ignoring errors that // would happen the future has been dropped by the user. let _ = tx - .send(Ok(Connec { + .send(Ok(WsConn { websocket: websocket_clone.clone(), incoming_data: RwStreamSink::new(message_rx.then(|result| { // An `Err` happens here if `message_tx` has been dropped. However // `message_tx` is grabbed by the websocket, which stays alive for as - // long as the `Connec` is alive. + // long as the `WsConn` is alive. match result { Ok(r) => r, - Err(_) => unreachable!("the message channel outlives the Connec") + Err(_) => unreachable!("the message channel outlives the WsConn") } })), })); @@ -155,7 +155,7 @@ impl Transport for WsConfig { "close event on the websocket"))); }; - js!{ + js! { var socket = @{websocket}; var open_cb = @{open_cb}; var message_cb = @{message_cb}; @@ -191,7 +191,7 @@ impl Transport for WsConfig { } } -pub struct Connec { +pub struct WsConn { websocket: Reference, // Stream of messages that goes through a `RwStreamSink` in order to become a `AsyncRead`. incoming_data: RwStreamSink, IoError>>, @@ -200,38 +200,38 @@ pub struct Connec { >>, } -impl Drop for Connec { +impl Drop for WsConn { #[inline] fn drop(&mut self) { // TODO: apparently there's a memory leak related to callbacks? - js!{ @{&self.websocket}.close(); } + js! { @{&self.websocket}.close(); } } } -impl AsyncRead for Connec { +impl AsyncRead for WsConn { } -impl Read for Connec { +impl Read for WsConn { #[inline] fn read(&mut self, buf: &mut [u8]) -> Result { self.incoming_data.read(buf) } } -impl AsyncWrite for Connec { +impl AsyncWrite for WsConn { #[inline] fn shutdown(&mut self) -> Poll<(), IoError> { Ok(Async::Ready(())) } } -impl Write for Connec { +impl Write for WsConn { fn write(&mut self, buf: &[u8]) -> Result { let typed_array = TypedArray::from(buf); // `send` can throw if the websocket isn't open (which can happen if it was closed by the // remote). - let returned = js!{ + let returned = js! { try { @{&self.websocket}.send(@{typed_array.buffer()}); return true; diff --git a/libp2p-websocket/src/lib.rs b/libp2p-websocket/src/lib.rs index 6798035d..175916c6 100644 --- a/libp2p-websocket/src/lib.rs +++ b/libp2p-websocket/src/lib.rs @@ -43,7 +43,7 @@ //! use libp2p_websocket::WsConfig; //! //! let ws_config = WsConfig::new(); -//! // let _ = ws_config.dial(Multiaddr::new("/ip4/40.41.42.43/tcp/12345/ws").unwrap()); +//! // let _ = ws_config.dial("/ip4/40.41.42.43/tcp/12345/ws".parse().unwrap()); //! ``` //! //! # Other operating systems @@ -53,18 +53,23 @@ //! //! This underlying transport must be passed to the `WsConfig::new()` function. //! -//! ```ignore +//! ``` +//! extern crate libp2p_swarm; //! extern crate libp2p_tcp_transport; //! extern crate libp2p_websocket; //! extern crate tokio_core; //! -//! use libp2p_websocket::WsConfig; +//! use libp2p_swarm::{Multiaddr, Transport}; //! use libp2p_tcp_transport::TcpConfig; +//! use libp2p_websocket::WsConfig; //! use tokio_core::reactor::Core; //! +//! # fn main() { //! let core = Core::new().unwrap(); //! let ws_config = WsConfig::new(TcpConfig::new(core.handle())); -//! // let _ = ws_config.dial(Multiaddr::new("/ip4/40.41.42.43/tcp/12345/ws").unwrap()); +//! # return; +//! let _ = ws_config.dial("/ip4/40.41.42.43/tcp/12345/ws".parse().unwrap()); +//! # } //! ``` //!