Fix concerns

This commit is contained in:
Pierre Krieger 2018-01-10 18:24:58 +01:00
parent c60fb982d2
commit 2783c5713e
No known key found for this signature in database
GPG Key ID: A03CE3AD81F08F7C
4 changed files with 32 additions and 26 deletions

View File

@ -109,7 +109,6 @@ impl Transport for TcpConfig {
let future = future::result(listener).map(|listener| { let future = future::result(listener).map(|listener| {
// Pull out a stream of sockets for incoming connections // Pull out a stream of sockets for incoming connections
listener.incoming().map(|(sock, addr)| { listener.incoming().map(|(sock, addr)| {
println!("incoming tcp stream");
let addr = addr.to_multiaddr() let addr = addr.to_multiaddr()
.expect("generating a multiaddr from a socket addr never fails"); .expect("generating a multiaddr from a socket addr never fails");
(Ok(sock).into_future(), addr) (Ok(sock).into_future(), addr)

View File

@ -18,7 +18,7 @@ which uses `ws` on top of TCP/IP will automatically use the `XMLHttpRequest` Jav
use libp2p_websocket::WsConfig; use libp2p_websocket::WsConfig;
let ws_config = WsConfig::new(); 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 # 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. This underlying transport must be passed to the `WsConfig::new()` function.
```rust ```rust
extern crate libp2p_swarm;
extern crate libp2p_tcp_transport; extern crate libp2p_tcp_transport;
extern crate libp2p_websocket; extern crate libp2p_websocket;
extern crate tokio_core; extern crate tokio_core;
use libp2p_websocket::WsConfig; use libp2p_swarm::{Multiaddr, Transport};
use libp2p_tcp_transport::TcpConfig; use libp2p_tcp_transport::TcpConfig;
use libp2p_websocket::WsConfig;
use tokio_core::reactor::Core; use tokio_core::reactor::Core;
let core = Core::new().unwrap(); let core = Core::new().unwrap();
let ws_config = WsConfig::new(TcpConfig::new(core.handle())); 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());
``` ```

View File

@ -49,10 +49,10 @@ impl WsConfig {
} }
impl Transport for WsConfig { impl Transport for WsConfig {
type RawConn = Connec; type RawConn = WsConn;
type Listener = Box<Stream<Item = (Self::ListenerUpgrade, Multiaddr), Error = IoError>>; // TODO: use `!` type Listener = Box<Stream<Item = (Self::ListenerUpgrade, Multiaddr), Error = IoError>>; // TODO: use `!`
type ListenerUpgrade = Box<Future<Item = Self::RawConn, Error = IoError>>; // TODO: use `!` type ListenerUpgrade = Box<Future<Item = Self::RawConn, Error = IoError>>; // TODO: use `!`
type Dial = FutureThen<oneshot::Receiver<Result<Connec, IoError>>, Result<Connec, IoError>, fn(Result<Result<Connec, IoError>, oneshot::Canceled>) -> Result<Connec, IoError>>; type Dial = FutureThen<oneshot::Receiver<Result<WsConn, IoError>>, Result<WsConn, IoError>, fn(Result<Result<WsConn, IoError>, oneshot::Canceled>) -> Result<WsConn, IoError>>;
#[inline] #[inline]
fn listen_on(self, a: Multiaddr) -> Result<(Self::Listener, Multiaddr), (Self, Multiaddr)> { 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. // Create the JS `WebSocket` object.
let websocket = { let websocket = {
let val = js!{ let val = js! {
try { try {
return new WebSocket(@{inner_addr}); return new WebSocket(@{inner_addr});
} catch(e) { } 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 // the open dialing websocket. Also create a `open_cb` callback that will be used for the
// `open` message of the websocket. // `open` message of the websocket.
let (open_tx, open_rx) = oneshot::channel::<Result<Connec, IoError>>(); let (open_tx, open_rx) = oneshot::channel::<Result<WsConn, IoError>>();
let open_tx = Arc::new(Mutex::new(Some(open_tx))); let open_tx = Arc::new(Mutex::new(Some(open_tx)));
let websocket_clone = websocket.clone(); let websocket_clone = websocket.clone();
let open_cb = { let open_cb = {
@ -124,18 +124,18 @@ impl Transport for WsConfig {
// is not supposed to happen. // is not supposed to happen.
let message_rx = message_rx.take().expect("the websocket can only open once"); 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. // would happen the future has been dropped by the user.
let _ = tx let _ = tx
.send(Ok(Connec { .send(Ok(WsConn {
websocket: websocket_clone.clone(), websocket: websocket_clone.clone(),
incoming_data: RwStreamSink::new(message_rx.then(|result| { incoming_data: RwStreamSink::new(message_rx.then(|result| {
// An `Err` happens here if `message_tx` has been dropped. However // An `Err` happens here if `message_tx` has been dropped. However
// `message_tx` is grabbed by the websocket, which stays alive for as // `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 { match result {
Ok(r) => r, 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"))); "close event on the websocket")));
}; };
js!{ js! {
var socket = @{websocket}; var socket = @{websocket};
var open_cb = @{open_cb}; var open_cb = @{open_cb};
var message_cb = @{message_cb}; var message_cb = @{message_cb};
@ -191,7 +191,7 @@ impl Transport for WsConfig {
} }
} }
pub struct Connec { pub struct WsConn {
websocket: Reference, websocket: Reference,
// Stream of messages that goes through a `RwStreamSink` in order to become a `AsyncRead`. // Stream of messages that goes through a `RwStreamSink` in order to become a `AsyncRead`.
incoming_data: RwStreamSink<StreamThen<mpsc::UnboundedReceiver<Result<Vec<u8>, IoError>>, incoming_data: RwStreamSink<StreamThen<mpsc::UnboundedReceiver<Result<Vec<u8>, IoError>>,
@ -200,38 +200,38 @@ pub struct Connec {
>>, >>,
} }
impl Drop for Connec { impl Drop for WsConn {
#[inline] #[inline]
fn drop(&mut self) { fn drop(&mut self) {
// TODO: apparently there's a memory leak related to callbacks? // 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] #[inline]
fn read(&mut self, buf: &mut [u8]) -> Result<usize, IoError> { fn read(&mut self, buf: &mut [u8]) -> Result<usize, IoError> {
self.incoming_data.read(buf) self.incoming_data.read(buf)
} }
} }
impl AsyncWrite for Connec { impl AsyncWrite for WsConn {
#[inline] #[inline]
fn shutdown(&mut self) -> Poll<(), IoError> { fn shutdown(&mut self) -> Poll<(), IoError> {
Ok(Async::Ready(())) Ok(Async::Ready(()))
} }
} }
impl Write for Connec { impl Write for WsConn {
fn write(&mut self, buf: &[u8]) -> Result<usize, IoError> { fn write(&mut self, buf: &[u8]) -> Result<usize, IoError> {
let typed_array = TypedArray::from(buf); let typed_array = TypedArray::from(buf);
// `send` can throw if the websocket isn't open (which can happen if it was closed by the // `send` can throw if the websocket isn't open (which can happen if it was closed by the
// remote). // remote).
let returned = js!{ let returned = js! {
try { try {
@{&self.websocket}.send(@{typed_array.buffer()}); @{&self.websocket}.send(@{typed_array.buffer()});
return true; return true;

View File

@ -43,7 +43,7 @@
//! use libp2p_websocket::WsConfig; //! use libp2p_websocket::WsConfig;
//! //!
//! let ws_config = WsConfig::new(); //! 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 //! # Other operating systems
@ -53,18 +53,23 @@
//! //!
//! This underlying transport must be passed to the `WsConfig::new()` function. //! This underlying transport must be passed to the `WsConfig::new()` function.
//! //!
//! ```ignore //! ```
//! extern crate libp2p_swarm;
//! extern crate libp2p_tcp_transport; //! extern crate libp2p_tcp_transport;
//! extern crate libp2p_websocket; //! extern crate libp2p_websocket;
//! extern crate tokio_core; //! extern crate tokio_core;
//! //!
//! use libp2p_websocket::WsConfig; //! use libp2p_swarm::{Multiaddr, Transport};
//! use libp2p_tcp_transport::TcpConfig; //! use libp2p_tcp_transport::TcpConfig;
//! use libp2p_websocket::WsConfig;
//! use tokio_core::reactor::Core; //! use tokio_core::reactor::Core;
//! //!
//! # fn main() {
//! let core = Core::new().unwrap(); //! let core = Core::new().unwrap();
//! let ws_config = WsConfig::new(TcpConfig::new(core.handle())); //! 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());
//! # }
//! ``` //! ```
//! //!