Skip to content

Commit 336bb25

Browse files
Merge pull request snapview#38 from Eroc33/master
Port to tokio reform
2 parents 6ac55ac + e201263 commit 336bb25

File tree

6 files changed

+34
-46
lines changed

6 files changed

+34
-46
lines changed

Cargo.toml

+4-5
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@ version = "0.5.1"
1212

1313
[features]
1414
default = ["connect", "tls"]
15-
connect = ["tokio-dns-unofficial", "tokio-core", "stream"]
15+
connect = ["tokio-dns-unofficial", "tokio", "stream"]
1616
tls = ["tokio-tls", "native-tls", "stream", "tungstenite/tls"]
1717
stream = ["bytes"]
1818

1919
[dependencies]
2020
futures = "0.1.17"
21-
tokio-io = "0.1.2"
2221

2322
[dependencies.tungstenite]
2423
version = "0.5.3"
@@ -34,11 +33,11 @@ version = "0.1.5"
3433

3534
[dependencies.tokio-dns-unofficial]
3635
optional = true
37-
version = "0.1.1"
36+
version = "0.3.0"
3837

39-
[dependencies.tokio-core]
38+
[dependencies.tokio]
4039
optional = true
41-
version = "0.1.9"
40+
version = "0.1.6"
4241

4342
[dependencies.tokio-tls]
4443
optional = true

examples/client.rs

+4-9
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
//! You can use this example together with the `server` example.
1212
1313
extern crate futures;
14-
extern crate tokio_core;
14+
extern crate tokio;
1515
extern crate tokio_tungstenite;
1616
extern crate tungstenite;
1717
extern crate url;
@@ -22,7 +22,6 @@ use std::thread;
2222

2323
use futures::sync::mpsc;
2424
use futures::{Future, Sink, Stream};
25-
use tokio_core::reactor::Core;
2625
use tungstenite::protocol::Message;
2726

2827
use tokio_tungstenite::connect_async;
@@ -35,10 +34,6 @@ fn main() {
3534

3635
let url = url::Url::parse(&connect_addr).unwrap();
3736

38-
// Create the event loop and initiate the connection to the remote server.
39-
let mut core = Core::new().unwrap();
40-
let handle = core.handle();
41-
4237
// Right now Tokio doesn't support a handle to stdin running on the event
4338
// loop, so we farm out that work to a separate thread. This thread will
4439
// read data from stdin and then send it to the event loop over a standard
@@ -63,7 +58,7 @@ fn main() {
6358
// finishes. If we don't have any more data to read or we won't receive any
6459
// more work from the remote then we can exit.
6560
let mut stdout = io::stdout();
66-
let client = connect_async(url, handle.remote().clone()).and_then(|(ws_stream, _)| {
61+
let client = connect_async(url).and_then(move |(ws_stream, _)| {
6762
println!("WebSocket handshake has been successfully completed");
6863

6964
// `sink` is the stream of messages going out.
@@ -73,7 +68,7 @@ fn main() {
7368
// We forward all messages, composed out of the data, entered to
7469
// the stdin, to the `sink`.
7570
let send_stdin = stdin_rx.forward(sink);
76-
let write_stdout = stream.for_each(|message| {
71+
let write_stdout = stream.for_each(move |message| {
7772
stdout.write_all(&message.into_data()).unwrap();
7873
Ok(())
7974
});
@@ -88,7 +83,7 @@ fn main() {
8883
});
8984

9085
// And now that we've got our client, we execute it in the event loop!
91-
core.run(client).unwrap();
86+
tokio::runtime::run(client.map_err(|_e| ()));
9287
}
9388

9489
// Our helper method which will read data from stdin and send it along the

examples/server.rs

+13-16
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,18 @@
1818
//! messages.
1919
2020
extern crate futures;
21-
extern crate tokio_core;
21+
extern crate tokio;
2222
extern crate tokio_tungstenite;
2323
extern crate tungstenite;
2424

25-
use std::cell::RefCell;
2625
use std::collections::HashMap;
2726
use std::env;
2827
use std::io::{Error, ErrorKind};
29-
use std::rc::Rc;
28+
use std::sync::{Arc,Mutex};
3029

3130
use futures::stream::Stream;
3231
use futures::Future;
33-
use tokio_core::net::TcpListener;
34-
use tokio_core::reactor::Core;
32+
use tokio::net::TcpListener;
3533
use tungstenite::protocol::Message;
3634

3735
use tokio_tungstenite::accept_async;
@@ -41,24 +39,23 @@ fn main() {
4139
let addr = addr.parse().unwrap();
4240

4341
// Create the event loop and TCP listener we'll accept connections on.
44-
let mut core = Core::new().unwrap();
45-
let handle = core.handle();
46-
let socket = TcpListener::bind(&addr, &handle).unwrap();
42+
let socket = TcpListener::bind(&addr).unwrap();
4743
println!("Listening on: {}", addr);
4844

4945
// This is a single-threaded server, so we can just use Rc and RefCell to
5046
// store the map of all connections we know about.
51-
let connections = Rc::new(RefCell::new(HashMap::new()));
47+
let connections = Arc::new(Mutex::new(HashMap::new()));
5248

53-
let srv = socket.incoming().for_each(|(stream, addr)| {
49+
let srv = socket.incoming().for_each(move |stream| {
50+
51+
let addr = stream.peer_addr().expect("connected streams should have a peer address");
5452

5553
// We have to clone both of these values, because the `and_then`
5654
// function below constructs a new future, `and_then` requires
5755
// `FnOnce`, so we construct a move closure to move the
5856
// environment inside the future (AndThen future may overlive our
5957
// `for_each` future).
6058
let connections_inner = connections.clone();
61-
let handle_inner = handle.clone();
6259

6360
accept_async(stream).and_then(move |ws_stream| {
6461
println!("New WebSocket connection: {}", addr);
@@ -67,7 +64,7 @@ fn main() {
6764
// send us messages. Then register our address with the stream to send
6865
// data to us.
6966
let (tx, rx) = futures::sync::mpsc::unbounded();
70-
connections_inner.borrow_mut().insert(addr, tx);
67+
connections_inner.lock().unwrap().insert(addr, tx);
7168

7269
// Let's split the WebSocket stream, so we can work with the
7370
// reading and writing halves separately.
@@ -81,7 +78,7 @@ fn main() {
8178

8279
// For each open connection except the sender, send the
8380
// string via the channel.
84-
let mut conns = connections.borrow_mut();
81+
let mut conns = connections.lock().unwrap();
8582
let iter = conns.iter_mut()
8683
.filter(|&(&k, _)| k != addr)
8784
.map(|(_, v)| v);
@@ -105,8 +102,8 @@ fn main() {
105102
let connection = ws_reader.map(|_| ()).map_err(|_| ())
106103
.select(ws_writer.map(|_| ()).map_err(|_| ()));
107104

108-
handle_inner.spawn(connection.then(move |_| {
109-
connections_inner.borrow_mut().remove(&addr);
105+
tokio::spawn(connection.then(move |_| {
106+
connections_inner.lock().unwrap().remove(&addr);
110107
println!("Connection {} closed.", addr);
111108
Ok(())
112109
}));
@@ -119,5 +116,5 @@ fn main() {
119116
});
120117

121118
// Execute server.
122-
core.run(srv).unwrap();
119+
tokio::runtime::run(srv.map_err(|_e| ()));
123120
}

src/connect.rs

+10-13
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
//! Connection helper.
22
33
extern crate tokio_dns;
4-
extern crate tokio_core;
54

65
use std::io::Result as IoResult;
76

8-
use self::tokio_core::net::TcpStream;
9-
use self::tokio_core::reactor::Remote;
10-
use self::tokio_dns::tcp_connect;
7+
use tokio::net::TcpStream;
118

129
use futures::{future, Future};
13-
use tokio_io::{AsyncRead, AsyncWrite};
10+
use tokio::io::{AsyncRead, AsyncWrite};
1411

1512
use tungstenite::Error;
1613
use tungstenite::client::url_mode;
@@ -36,7 +33,7 @@ mod encryption {
3633
use std::io::{Read, Write, Result as IoResult};
3734

3835
use futures::{future, Future};
39-
use tokio_io::{AsyncRead, AsyncWrite};
36+
use tokio::io::{AsyncRead, AsyncWrite};
4037

4138
use tungstenite::Error;
4239
use tungstenite::stream::Mode;
@@ -53,9 +50,9 @@ mod encryption {
5350
}
5451

5552
pub fn wrap_stream<S>(socket: S, domain: String, mode: Mode)
56-
-> Box<Future<Item=AutoStream<S>, Error=Error>>
53+
-> Box<Future<Item=AutoStream<S>, Error=Error> + Send>
5754
where
58-
S: 'static + AsyncRead + AsyncWrite,
55+
S: 'static + AsyncRead + AsyncWrite + Send,
5956
{
6057
match mode {
6158
Mode::Plain => Box::new(future::ok(StreamSwitcher::Plain(socket))),
@@ -106,10 +103,10 @@ fn domain(request: &Request) -> Result<String, Error> {
106103
/// Creates a WebSocket handshake from a request and a stream,
107104
/// upgrading the stream to TLS if required.
108105
pub fn client_async_tls<R, S>(request: R, stream: S)
109-
-> Box<Future<Item=(WebSocketStream<AutoStream<S>>, Response), Error=Error>>
106+
-> Box<Future<Item=(WebSocketStream<AutoStream<S>>, Response), Error=Error> + Send>
110107
where
111108
R: Into<Request<'static>>,
112-
S: 'static + AsyncRead + AsyncWrite + NoDelay,
109+
S: 'static + AsyncRead + AsyncWrite + NoDelay + Send,
113110
{
114111
let request: Request = request.into();
115112

@@ -134,8 +131,8 @@ where
134131
}
135132

136133
/// Connect to a given URL.
137-
pub fn connect_async<R>(request: R, handle: Remote)
138-
-> Box<Future<Item=(WebSocketStream<AutoStream<TcpStream>>, Response), Error=Error>>
134+
pub fn connect_async<R>(request: R)
135+
-> Box<Future<Item=(WebSocketStream<AutoStream<TcpStream>>, Response), Error=Error> + Send>
139136
where
140137
R: Into<Request<'static>>
141138
{
@@ -147,6 +144,6 @@ where
147144
};
148145
let port = request.url.port_or_known_default().expect("Bug: port unknown");
149146

150-
Box::new(tcp_connect((domain.as_str(), port), handle).map_err(|e| e.into())
147+
Box::new(tokio_dns::TcpStream::connect((domain.as_str(), port)).map_err(|e| e.into())
151148
.and_then(move |socket| client_async_tls(request, socket)))
152149
}

src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
unused_import_braces)]
1717

1818
extern crate futures;
19-
extern crate tokio_io;
19+
extern crate tokio;
2020

2121
pub extern crate tungstenite;
2222

@@ -29,7 +29,7 @@ pub mod stream;
2929
use std::io::ErrorKind;
3030

3131
use futures::{Poll, Future, Async, AsyncSink, Stream, Sink, StartSend};
32-
use tokio_io::{AsyncRead, AsyncWrite};
32+
use tokio::io::{AsyncRead, AsyncWrite};
3333

3434
use tungstenite::handshake::client::{ClientHandshake, Response, Request};
3535
use tungstenite::handshake::server::{ServerHandshake, Callback, NoCallback};

src/stream.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::io::{Read, Write, Result as IoResult, Error as IoError};
1010

1111
use self::bytes::{Buf, BufMut};
1212
use futures::Poll;
13-
use tokio_io::{AsyncRead, AsyncWrite};
13+
use tokio::io::{AsyncRead, AsyncWrite};
1414

1515
/// Trait to switch TCP_NODELAY.
1616
pub trait NoDelay {

0 commit comments

Comments
 (0)