mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-08-25 00:00:18 +02:00
Remove the Reactor::run method (#58)
This commit removes the `Reactor::run` method which has previously been used to execute futures and turn the reactor at the same time. The tests/examples made heavy usage of this method but they have now all temporarily moved to `wait()` until the futures dependency is upgraded. In the meantime this'll allow us to further trim down the `Reactor` APIs to their final state.
This commit is contained in:
committed by
Carl Lerche
parent
32f2750c2d
commit
a577bfc033
+15
-26
@@ -20,18 +20,17 @@ extern crate tokio;
|
||||
extern crate tokio_io;
|
||||
|
||||
use std::env;
|
||||
use std::net::{self, SocketAddr};
|
||||
use std::net::SocketAddr;
|
||||
use std::thread;
|
||||
|
||||
use futures::Future;
|
||||
use futures::prelude::*;
|
||||
use futures::future::Executor;
|
||||
use futures::stream::Stream;
|
||||
use futures::sync::mpsc;
|
||||
use futures_cpupool::CpuPool;
|
||||
use tokio_io::AsyncRead;
|
||||
use tokio_io::io::copy;
|
||||
use tokio::net::TcpStream;
|
||||
use tokio::reactor::Reactor;
|
||||
use tokio::net::{TcpStream, TcpListener};
|
||||
use tokio::reactor::Handle;
|
||||
|
||||
fn main() {
|
||||
// First argument, the address to bind
|
||||
@@ -42,9 +41,8 @@ fn main() {
|
||||
let num_threads = env::args().nth(2).and_then(|s| s.parse().ok())
|
||||
.unwrap_or(num_cpus::get());
|
||||
|
||||
// Use `std::net` to bind the requested port, we'll use this on the main
|
||||
// thread below
|
||||
let listener = net::TcpListener::bind(&addr).expect("failed to bind");
|
||||
let handle = Handle::default();
|
||||
let listener = TcpListener::bind(&addr, &handle).expect("failed to bind");
|
||||
println!("Listening on: {}", addr);
|
||||
|
||||
// Spin up our worker threads, creating a channel routing to each worker
|
||||
@@ -56,31 +54,22 @@ fn main() {
|
||||
thread::spawn(|| worker(rx));
|
||||
}
|
||||
|
||||
// Infinitely accept sockets from our `std::net::TcpListener`, as this'll do
|
||||
// blocking I/O. Each socket is then shipped round-robin to a particular
|
||||
// thread which will associate the socket with the corresponding event loop
|
||||
// and process the connection.
|
||||
// Infinitely accept sockets from our `TcpListener`. Each socket is then
|
||||
// shipped round-robin to a particular thread which will associate the
|
||||
// socket with the corresponding event loop and process the connection.
|
||||
let mut next = 0;
|
||||
for socket in listener.incoming() {
|
||||
let socket = socket.expect("failed to accept");
|
||||
let srv = listener.incoming().for_each(|(socket, _)| {
|
||||
channels[next].unbounded_send(socket).expect("worker thread died");
|
||||
next = (next + 1) % channels.len();
|
||||
}
|
||||
Ok(())
|
||||
});
|
||||
srv.wait().unwrap();
|
||||
}
|
||||
|
||||
fn worker(rx: mpsc::UnboundedReceiver<net::TcpStream>) {
|
||||
let mut core = Reactor::new().unwrap();
|
||||
let handle = core.handle();
|
||||
|
||||
fn worker(rx: mpsc::UnboundedReceiver<TcpStream>) {
|
||||
let pool = CpuPool::new(1);
|
||||
|
||||
let done = rx.for_each(move |socket| {
|
||||
// First up when we receive a socket we associate it with our event loop
|
||||
// using the `TcpStream::from_stream` API. After that the socket is not
|
||||
// a `tokio::net::TcpStream` meaning it's in nonblocking mode and
|
||||
// ready to be used with Tokio
|
||||
let socket = TcpStream::from_std(socket, &handle)
|
||||
.expect("failed to associate TCP stream");
|
||||
let addr = socket.peer_addr().expect("failed to get remote address");
|
||||
|
||||
// Like the single-threaded `echo` example we split the socket halves
|
||||
@@ -101,5 +90,5 @@ fn worker(rx: mpsc::UnboundedReceiver<net::TcpStream>) {
|
||||
|
||||
Ok(())
|
||||
});
|
||||
core.run(done).unwrap();
|
||||
done.wait().unwrap();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user