summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--examples/echo-udp.rs73
-rw-r--r--examples/echo.rs14
2 files changed, 50 insertions, 37 deletions
diff --git a/examples/echo-udp.rs b/examples/echo-udp.rs
index 83aa995a..f95fbd3e 100644
--- a/examples/echo-udp.rs
+++ b/examples/echo-udp.rs
@@ -1,4 +1,18 @@
//! An UDP echo server that just sends back everything that it receives.
+//!
+//! If you're on unix you can test this out by in one terminal executing:
+//!
+//! ```sh
+//! $ cargo run --example echo-udp
+//! ```
+//!
+//! and in another terminal you can run:
+//!
+//! ```sh
+//! $ nc -u localhost 8080
+//! ```
+//!
+//! Each line you type in to the `nc` terminal should be echo'd back to you!
extern crate futures;
#[macro_use]
@@ -11,21 +25,10 @@ use futures::{Future, Poll};
use tokio_core::net::UdpSocket;
use tokio_core::reactor::Core;
-/// UDP echo server
struct Server {
- socket : UdpSocket,
- buf : Vec<u8>,
- to_send : Option<(usize, SocketAddr)>,
-}
-
-impl Server {
- fn new(s : UdpSocket) -> Self {
- Server {
- socket: s,
- to_send: None,
- buf: vec![0u8; 1600],
- }
- }
+ socket: UdpSocket,
+ buf: Vec<u8>,
+ to_send: Option<(usize, SocketAddr)>,
}
impl Future for Server {
@@ -34,21 +37,18 @@ impl Future for Server {
fn poll(&mut self) -> Poll<(), io::Error> {
loop {
- if let Some((size, peer)) = self.to_send.take() {
- match self.socket.send_to(&self.buf[..size], &peer) {
- Err(e) => {
- self.to_send = Some((size, peer));
- return try_nb!(Err(e));
- },
- Ok(_) => {
- println!("Echoed {} bytes", size);
- }
- }
+ // First we check to see if there's a message we need to echo back.
+ // If so then we try to send it back to the original source, waiting
+ // until we're writable and able to do so.
+ if let Some((size, peer)) = self.to_send {
+ let amt = try_nb!(self.socket.send_to(&self.buf[..size], &peer));
+ println!("Echoed {}/{} bytes to {}", amt, size, peer);
+ self.to_send = None;
}
- self.to_send = Some(
- try_nb!(self.socket.recv_from(&mut self.buf))
- );
+ // If we're here then `to_send` is `None`, so we take a look for the
+ // next message we're going to echo back.
+ self.to_send = Some(try_nb!(self.socket.recv_from(&mut self.buf)));
}
}
}
@@ -57,19 +57,18 @@ fn main() {
let addr = env::args().nth(1).unwrap_or("127.0.0.1:8080".to_string());
let addr = addr.parse::<SocketAddr>().unwrap();
- // Create the event loop that will drive this server
+ // Create the event loop that will drive this server, and also bind the
+ // socket we'll be listening to.
let mut l = Core::new().unwrap();
let handle = l.handle();
-
- // Create and bind an UDP socket
let socket = UdpSocket::bind(&addr, &handle).unwrap();
-
- // Inform that we have are listening
println!("Listening on: {}", addr);
- // Create a server future
- let server = Server::new(socket);
-
- // Start event loop with the initial future: UDP echo server
- l.run(server).unwrap();
+ // Next we'll create a future to spawn (the one we defined above) and then
+ // we'll run the event loop by running the future.
+ l.run(Server {
+ socket: socket,
+ buf: vec![0; 1024],
+ to_send: None,
+ }).unwrap();
}
diff --git a/examples/echo.rs b/examples/echo.rs
index 38eb9044..d46cbd96 100644
--- a/examples/echo.rs
+++ b/examples/echo.rs
@@ -1,4 +1,18 @@
//! An echo server that just writes back everything that's written to it.
+//!
+//! If you're on unix you can test this out by in one terminal executing:
+//!
+//! ```sh
+//! $ cargo run --example echo
+//! ```
+//!
+//! and in another terminal you can run:
+//!
+//! ```sh
+//! $ nc localhost 8080
+//! ```
+//!
+//! Each line you type in to the `nc` terminal should be echo'd back to you!
extern crate env_logger;
extern crate futures;