mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-08-26 00:00:16 +02:00
Migrate to using tokio-io
Deprecate the existing `io` module in this crate entirely. More details coming soon! Closes #61
This commit is contained in:
+6
-5
@@ -17,8 +17,9 @@
|
||||
//! connected clients they'll all join the same room and see everyone else's
|
||||
//! messages.
|
||||
|
||||
extern crate tokio_core;
|
||||
extern crate futures;
|
||||
extern crate tokio_core;
|
||||
extern crate tokio_io;
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::rc::Rc;
|
||||
@@ -27,12 +28,12 @@ use std::iter;
|
||||
use std::env;
|
||||
use std::io::{Error, ErrorKind, BufReader};
|
||||
|
||||
use futures::Future;
|
||||
use futures::stream::{self, Stream};
|
||||
use tokio_core::net::TcpListener;
|
||||
use tokio_core::reactor::Core;
|
||||
use tokio_core::io::{self, Io};
|
||||
|
||||
use futures::stream::{self, Stream};
|
||||
use futures::Future;
|
||||
use tokio_io::io;
|
||||
use tokio_io::AsyncRead;
|
||||
|
||||
fn main() {
|
||||
let addr = env::args().nth(1).unwrap_or("127.0.0.1:8080".to_string());
|
||||
|
||||
+24
-11
@@ -10,17 +10,21 @@
|
||||
|
||||
extern crate futures;
|
||||
extern crate tokio_core;
|
||||
extern crate tokio_io;
|
||||
extern crate bytes;
|
||||
|
||||
use std::env;
|
||||
use std::io::{self, Read, Write};
|
||||
use std::net::SocketAddr;
|
||||
use std::thread;
|
||||
|
||||
use futures::{Sink, Future, Stream};
|
||||
use bytes::{BufMut, BytesMut};
|
||||
use futures::sync::mpsc;
|
||||
use tokio_core::reactor::Core;
|
||||
use tokio_core::io::{Io, EasyBuf, Codec};
|
||||
use futures::{Sink, Future, Stream};
|
||||
use tokio_core::net::TcpStream;
|
||||
use tokio_core::reactor::Core;
|
||||
use tokio_io::AsyncRead;
|
||||
use tokio_io::codec::{Encoder, Decoder};
|
||||
|
||||
fn main() {
|
||||
// Parse what address we're going to connect to
|
||||
@@ -63,7 +67,7 @@ fn main() {
|
||||
let (sink, stream) = stream.framed(Bytes).split();
|
||||
let send_stdin = stdin_rx.forward(sink);
|
||||
let write_stdout = stream.for_each(move |buf| {
|
||||
stdout.write_all(buf.as_slice())
|
||||
stdout.write_all(&buf)
|
||||
});
|
||||
|
||||
send_stdin.map(|_| ())
|
||||
@@ -83,21 +87,30 @@ fn main() {
|
||||
/// data into the output location without looking at it.
|
||||
struct Bytes;
|
||||
|
||||
impl Codec for Bytes {
|
||||
type In = EasyBuf;
|
||||
type Out = Vec<u8>;
|
||||
impl Decoder for Bytes {
|
||||
type Item = BytesMut;
|
||||
type Error = io::Error;
|
||||
|
||||
fn decode(&mut self, buf: &mut EasyBuf) -> io::Result<Option<EasyBuf>> {
|
||||
fn decode(&mut self, buf: &mut BytesMut) -> io::Result<Option<BytesMut>> {
|
||||
if buf.len() > 0 {
|
||||
let len = buf.len();
|
||||
Ok(Some(buf.drain_to(len)))
|
||||
Ok(Some(buf.split_to(len)))
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
fn encode(&mut self, data: Vec<u8>, buf: &mut Vec<u8>) -> io::Result<()> {
|
||||
buf.extend(data);
|
||||
fn decode_eof(&mut self, buf: &mut BytesMut) -> io::Result<Option<BytesMut>> {
|
||||
self.decode(buf)
|
||||
}
|
||||
}
|
||||
|
||||
impl Encoder for Bytes {
|
||||
type Item = Vec<u8>;
|
||||
type Error = io::Error;
|
||||
|
||||
fn encode(&mut self, data: Vec<u8>, buf: &mut BytesMut) -> io::Result<()> {
|
||||
buf.put(&data[..]);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
+4
-2
@@ -19,13 +19,15 @@
|
||||
|
||||
extern crate futures;
|
||||
extern crate tokio_core;
|
||||
extern crate tokio_io;
|
||||
|
||||
use std::env;
|
||||
use std::net::SocketAddr;
|
||||
|
||||
use futures::Future;
|
||||
use futures::stream::Stream;
|
||||
use tokio_core::io::{copy, Io};
|
||||
use tokio_io::AsyncRead;
|
||||
use tokio_io::io::copy;
|
||||
use tokio_core::net::TcpListener;
|
||||
use tokio_core::reactor::Core;
|
||||
|
||||
@@ -93,7 +95,7 @@ fn main() {
|
||||
// information.
|
||||
let msg = amt.then(move |result| {
|
||||
match result {
|
||||
Ok(amt) => println!("wrote {} bytes to {}", amt, addr),
|
||||
Ok((amt, _, _)) => println!("wrote {} bytes to {}", amt, addr),
|
||||
Err(e) => println!("error on {}: {}", addr, e),
|
||||
}
|
||||
|
||||
|
||||
+3
-2
@@ -11,9 +11,10 @@
|
||||
//!
|
||||
//! You should see `Hello!` printed out and then the `nc` program will exit.
|
||||
|
||||
extern crate env_logger;
|
||||
extern crate futures;
|
||||
extern crate tokio_core;
|
||||
extern crate env_logger;
|
||||
extern crate tokio_io;
|
||||
|
||||
use std::env;
|
||||
use std::net::SocketAddr;
|
||||
@@ -35,7 +36,7 @@ fn main() {
|
||||
|
||||
let clients = listener.incoming();
|
||||
let welcomes = clients.and_then(|(socket, _peer_addr)| {
|
||||
tokio_core::io::write_all(socket, b"Hello!\n")
|
||||
tokio_io::io::write_all(socket, b"Hello!\n")
|
||||
});
|
||||
let server = welcomes.for_each(|(_socket, _welcome)| {
|
||||
Ok(())
|
||||
|
||||
+3
-2
@@ -18,6 +18,7 @@
|
||||
extern crate env_logger;
|
||||
extern crate futures;
|
||||
extern crate tokio_core;
|
||||
extern crate tokio_io;
|
||||
|
||||
use std::env;
|
||||
use std::iter;
|
||||
@@ -25,7 +26,7 @@ use std::net::SocketAddr;
|
||||
|
||||
use futures::Future;
|
||||
use futures::stream::{self, Stream};
|
||||
use tokio_core::io::IoFuture;
|
||||
use tokio_io::IoFuture;
|
||||
use tokio_core::net::{TcpListener, TcpStream};
|
||||
use tokio_core::reactor::Core;
|
||||
|
||||
@@ -51,6 +52,6 @@ fn write(socket: TcpStream) -> IoFuture<()> {
|
||||
static BUF: &'static [u8] = &[0; 64 * 1024];
|
||||
let iter = iter::repeat(()).map(|()| Ok(()));
|
||||
stream::iter(iter).fold(socket, |socket, ()| {
|
||||
tokio_core::io::write_all(socket, BUF).map(|(socket, _)| socket)
|
||||
tokio_io::io::write_all(socket, BUF).map(|(socket, _)| socket)
|
||||
}).map(|_| ()).boxed()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user