mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-08-24 00:00:11 +02:00
Remove Handle argument from I/O constructors (#61)
This commit removes the `Handle` argument from the following constructors * `TcpListener::bind` * `TcpStream::connect` * `UdpSocket::bind` The `Handle` argument remains on the various `*_std` constructors as they're more low-level, but this otherwise is intended to set forth a precedent of by default not taking `Handle` arguments and instead relying on the global `Handle::default` return value when necesary.
This commit is contained in:
committed by
Carl Lerche
parent
849771ecfa
commit
4ef772b2db
+1
-3
@@ -33,7 +33,6 @@ use futures::future::Executor;
|
||||
use futures::stream::{self, Stream};
|
||||
use futures_cpupool::CpuPool;
|
||||
use tokio::net::TcpListener;
|
||||
use tokio::reactor::Handle;
|
||||
use tokio_io::io;
|
||||
use tokio_io::AsyncRead;
|
||||
|
||||
@@ -42,8 +41,7 @@ fn main() {
|
||||
let addr = addr.parse().unwrap();
|
||||
|
||||
// Create the TCP listener we'll accept connections on.
|
||||
let handle = Handle::default();
|
||||
let socket = TcpListener::bind(&addr, &handle).unwrap();
|
||||
let socket = TcpListener::bind(&addr).unwrap();
|
||||
println!("Listening on: {}", addr);
|
||||
|
||||
// This is currently a multi threaded server.
|
||||
|
||||
@@ -32,7 +32,6 @@ use futures::{Future, Stream, Poll};
|
||||
use futures::future::Executor;
|
||||
use futures_cpupool::CpuPool;
|
||||
use tokio::net::{TcpListener, TcpStream};
|
||||
use tokio::reactor::Handle;
|
||||
use tokio_io::{AsyncRead, AsyncWrite};
|
||||
use flate2::write::GzEncoder;
|
||||
|
||||
@@ -41,8 +40,7 @@ fn main() {
|
||||
// reactor.
|
||||
let addr = env::args().nth(1).unwrap_or("127.0.0.1:8080".to_string());
|
||||
let addr = addr.parse::<SocketAddr>().unwrap();
|
||||
let handle = Handle::default();
|
||||
let socket = TcpListener::bind(&addr, &handle).unwrap();
|
||||
let socket = TcpListener::bind(&addr).unwrap();
|
||||
println!("Listening on: {}", addr);
|
||||
|
||||
// This is where we're going to offload our computationally heavy work
|
||||
|
||||
+4
-11
@@ -28,7 +28,6 @@ use std::thread;
|
||||
use futures::sync::mpsc;
|
||||
use futures::{Sink, Future, Stream};
|
||||
use futures_cpupool::CpuPool;
|
||||
use tokio::reactor::Handle;
|
||||
|
||||
fn main() {
|
||||
// Determine if we're going to run in TCP or UDP mode
|
||||
@@ -47,8 +46,6 @@ fn main() {
|
||||
});
|
||||
let addr = addr.parse::<SocketAddr>().unwrap();
|
||||
|
||||
let handle = Handle::default();
|
||||
|
||||
let pool = CpuPool::new(1);
|
||||
|
||||
// Right now Tokio doesn't support a handle to stdin running on the event
|
||||
@@ -63,9 +60,9 @@ fn main() {
|
||||
// our UDP connection to get a stream of bytes we're going to emit to
|
||||
// stdout.
|
||||
let stdout = if tcp {
|
||||
tcp::connect(&addr, &handle, &pool, Box::new(stdin_rx))
|
||||
tcp::connect(&addr, &pool, Box::new(stdin_rx))
|
||||
} else {
|
||||
udp::connect(&addr, &handle, &pool, Box::new(stdin_rx))
|
||||
udp::connect(&addr, &pool, Box::new(stdin_rx))
|
||||
};
|
||||
|
||||
// And now with our stream of bytes to write to stdout, we execute that in
|
||||
@@ -88,17 +85,15 @@ mod tcp {
|
||||
use futures::future::Executor;
|
||||
use futures_cpupool::CpuPool;
|
||||
use tokio::net::TcpStream;
|
||||
use tokio::reactor::Handle;
|
||||
use tokio_io::AsyncRead;
|
||||
use tokio_io::codec::{Encoder, Decoder};
|
||||
|
||||
pub fn connect(addr: &SocketAddr,
|
||||
handle: &Handle,
|
||||
pool: &CpuPool,
|
||||
stdin: Box<Stream<Item = Vec<u8>, Error = io::Error> + Send>)
|
||||
-> Box<Stream<Item = BytesMut, Error = io::Error>>
|
||||
{
|
||||
let tcp = TcpStream::connect(addr, handle);
|
||||
let tcp = TcpStream::connect(addr);
|
||||
let pool = pool.clone();
|
||||
|
||||
// After the TCP connection has been established, we set up our client
|
||||
@@ -175,10 +170,8 @@ mod udp {
|
||||
use futures::future::Executor;
|
||||
use futures_cpupool::CpuPool;
|
||||
use tokio::net::{UdpCodec, UdpSocket};
|
||||
use tokio::reactor::Handle;
|
||||
|
||||
pub fn connect(&addr: &SocketAddr,
|
||||
handle: &Handle,
|
||||
pool: &CpuPool,
|
||||
stdin: Box<Stream<Item = Vec<u8>, Error = io::Error> + Send>)
|
||||
-> Box<Stream<Item = BytesMut, Error = io::Error>>
|
||||
@@ -190,7 +183,7 @@ mod udp {
|
||||
} else {
|
||||
"[::]:0".parse().unwrap()
|
||||
};
|
||||
let udp = UdpSocket::bind(&addr_to_bind, handle)
|
||||
let udp = UdpSocket::bind(&addr_to_bind)
|
||||
.expect("failed to bind socket");
|
||||
|
||||
// Like above with TCP we use an instance of `UdpCodec` to transform
|
||||
|
||||
@@ -30,7 +30,6 @@ use futures_cpupool::CpuPool;
|
||||
use tokio_io::AsyncRead;
|
||||
use tokio_io::io::copy;
|
||||
use tokio::net::{TcpStream, TcpListener};
|
||||
use tokio::reactor::Handle;
|
||||
|
||||
fn main() {
|
||||
// First argument, the address to bind
|
||||
@@ -41,8 +40,7 @@ fn main() {
|
||||
let num_threads = env::args().nth(2).and_then(|s| s.parse().ok())
|
||||
.unwrap_or(num_cpus::get());
|
||||
|
||||
let handle = Handle::default();
|
||||
let listener = TcpListener::bind(&addr, &handle).expect("failed to bind");
|
||||
let listener = TcpListener::bind(&addr).expect("failed to bind");
|
||||
println!("Listening on: {}", addr);
|
||||
|
||||
// Spin up our worker threads, creating a channel routing to each worker
|
||||
|
||||
@@ -20,7 +20,6 @@ use std::net::SocketAddr;
|
||||
|
||||
use futures::{Future, Poll};
|
||||
use tokio::net::UdpSocket;
|
||||
use tokio::reactor::Handle;
|
||||
|
||||
struct Server {
|
||||
socket: UdpSocket,
|
||||
@@ -54,8 +53,7 @@ fn main() {
|
||||
let addr = env::args().nth(1).unwrap_or("127.0.0.1:8080".to_string());
|
||||
let addr = addr.parse::<SocketAddr>().unwrap();
|
||||
|
||||
let handle = Handle::default();
|
||||
let socket = UdpSocket::bind(&addr, &handle).unwrap();
|
||||
let socket = UdpSocket::bind(&addr).unwrap();
|
||||
println!("Listening on: {}", socket.local_addr().unwrap());
|
||||
|
||||
// Next we'll create a future to spawn (the one we defined above) and then
|
||||
|
||||
+1
-4
@@ -32,7 +32,6 @@ use futures_cpupool::CpuPool;
|
||||
use tokio_io::AsyncRead;
|
||||
use tokio_io::io::copy;
|
||||
use tokio::net::TcpListener;
|
||||
use tokio::reactor::Handle;
|
||||
|
||||
fn main() {
|
||||
// Allow passing an address to listen on as the first argument of this
|
||||
@@ -41,14 +40,12 @@ fn main() {
|
||||
let addr = env::args().nth(1).unwrap_or("127.0.0.1:8080".to_string());
|
||||
let addr = addr.parse::<SocketAddr>().unwrap();
|
||||
|
||||
let handle = Handle::default();
|
||||
|
||||
// Next up we create a TCP listener which will listen for incoming
|
||||
// connections. This TCP listener is bound to the address we determined
|
||||
// above and must be associated with an event loop, so we pass in a handle
|
||||
// to our event loop. After the socket's created we inform that we're ready
|
||||
// to go and start accepting connections.
|
||||
let socket = TcpListener::bind(&addr, &handle).unwrap();
|
||||
let socket = TcpListener::bind(&addr).unwrap();
|
||||
println!("Listening on: {}", addr);
|
||||
|
||||
// A CpuPool allows futures to be executed concurrently.
|
||||
|
||||
+1
-3
@@ -21,15 +21,13 @@ use std::net::SocketAddr;
|
||||
|
||||
use futures::prelude::*;
|
||||
use tokio::net::TcpListener;
|
||||
use tokio::reactor::Handle;
|
||||
|
||||
fn main() {
|
||||
env_logger::init().unwrap();
|
||||
let addr = env::args().nth(1).unwrap_or("127.0.0.1:8080".to_string());
|
||||
let addr = addr.parse::<SocketAddr>().unwrap();
|
||||
|
||||
let handle = Handle::default();
|
||||
let listener = TcpListener::bind(&addr, &handle).unwrap();
|
||||
let listener = TcpListener::bind(&addr).unwrap();
|
||||
|
||||
let addr = listener.local_addr().unwrap();
|
||||
println!("Listening for connections on {}", addr);
|
||||
|
||||
+2
-5
@@ -31,7 +31,6 @@ use futures::{Future, Poll};
|
||||
use futures::future::Executor;
|
||||
use futures_cpupool::CpuPool;
|
||||
use tokio::net::{TcpListener, TcpStream};
|
||||
use tokio::reactor::Handle;
|
||||
use tokio_io::{AsyncRead, AsyncWrite};
|
||||
use tokio_io::io::{copy, shutdown};
|
||||
|
||||
@@ -42,17 +41,15 @@ fn main() {
|
||||
let server_addr = env::args().nth(2).unwrap_or("127.0.0.1:8080".to_string());
|
||||
let server_addr = server_addr.parse::<SocketAddr>().unwrap();
|
||||
|
||||
let handle = Handle::default();
|
||||
|
||||
let pool = CpuPool::new(1);
|
||||
|
||||
// Create a TCP listener which will listen for incoming connections.
|
||||
let socket = TcpListener::bind(&listen_addr, &handle).unwrap();
|
||||
let socket = TcpListener::bind(&listen_addr).unwrap();
|
||||
println!("Listening on: {}", listen_addr);
|
||||
println!("Proxying to: {}", server_addr);
|
||||
|
||||
let done = socket.incoming().for_each(move |(client, client_addr)| {
|
||||
let server = TcpStream::connect(&server_addr, &handle);
|
||||
let server = TcpStream::connect(&server_addr);
|
||||
let amounts = server.and_then(move |server| {
|
||||
// Create separate read/write handles for the TCP clients that we're
|
||||
// proxying data between. Note that typically you'd use
|
||||
|
||||
+1
-3
@@ -31,7 +31,6 @@ use futures::stream::{self, Stream};
|
||||
use futures_cpupool::CpuPool;
|
||||
use tokio_io::IoFuture;
|
||||
use tokio::net::{TcpListener, TcpStream};
|
||||
use tokio::reactor::Handle;
|
||||
|
||||
fn main() {
|
||||
env_logger::init().unwrap();
|
||||
@@ -40,8 +39,7 @@ fn main() {
|
||||
|
||||
let pool = CpuPool::new(1);
|
||||
|
||||
let handle = Handle::default();
|
||||
let socket = TcpListener::bind(&addr, &handle).unwrap();
|
||||
let socket = TcpListener::bind(&addr).unwrap();
|
||||
println!("Listening on: {}", addr);
|
||||
let server = socket.incoming().for_each(|(socket, addr)| {
|
||||
println!("got a socket: {}", addr);
|
||||
|
||||
+1
-3
@@ -54,7 +54,6 @@ use futures::prelude::*;
|
||||
use futures::future::Executor;
|
||||
use futures_cpupool::CpuPool;
|
||||
use tokio::net::TcpListener;
|
||||
use tokio::reactor::Handle;
|
||||
use tokio_io::AsyncRead;
|
||||
use tokio_io::io::{lines, write_all};
|
||||
|
||||
@@ -84,8 +83,7 @@ fn main() {
|
||||
// and set up our TCP listener to accept connections.
|
||||
let addr = env::args().nth(1).unwrap_or("127.0.0.1:8080".to_string());
|
||||
let addr = addr.parse::<SocketAddr>().unwrap();
|
||||
let handle = Handle::default();
|
||||
let listener = TcpListener::bind(&addr, &handle).expect("failed to bind");
|
||||
let listener = TcpListener::bind(&addr).expect("failed to bind");
|
||||
println!("Listening on: {}", addr);
|
||||
|
||||
// Create a CpuPool to execute tasks
|
||||
|
||||
@@ -18,7 +18,6 @@ use futures::{Future, Stream, Sink};
|
||||
use futures::future::Executor;
|
||||
use futures_cpupool::CpuPool;
|
||||
use tokio::net::{UdpSocket, UdpCodec};
|
||||
use tokio::reactor::Handle;
|
||||
|
||||
pub struct LineCodec;
|
||||
|
||||
@@ -39,15 +38,13 @@ impl UdpCodec for LineCodec {
|
||||
fn main() {
|
||||
drop(env_logger::init());
|
||||
|
||||
let handle = Handle::default();
|
||||
|
||||
let pool = CpuPool::new(1);
|
||||
|
||||
let addr: SocketAddr = "127.0.0.1:0".parse().unwrap();
|
||||
|
||||
// Bind both our sockets and then figure out what ports we got.
|
||||
let a = UdpSocket::bind(&addr, &handle).unwrap();
|
||||
let b = UdpSocket::bind(&addr, &handle).unwrap();
|
||||
let a = UdpSocket::bind(&addr).unwrap();
|
||||
let b = UdpSocket::bind(&addr).unwrap();
|
||||
let b_addr = b.local_addr().unwrap();
|
||||
|
||||
// We're parsing each socket with the `LineCodec` defined above, and then we
|
||||
|
||||
Reference in New Issue
Block a user