summaryrefslogtreecommitdiffstats
path: root/examples/udp-codec.rs
diff options
context:
space:
mode:
authorRoman <humbug@deeptown.org>2018-02-07 01:41:31 +0400
committerCarl Lerche <me@carllerche.com>2018-02-06 13:41:31 -0800
commitad8338e4da63f659acce89284381d08a2474f85b (patch)
treee98a11f5aed7663c88956eb9635747389aae144e /examples/udp-codec.rs
parent73b763f69fe517fdbbb0360bd9c0a50db8f8f62c (diff)
Remove UdpCodec (#109)
`UdpFramed` is updated to use the `Encoder` and `Decoder` traits from `tokio-io`.
Diffstat (limited to 'examples/udp-codec.rs')
-rw-r--r--examples/udp-codec.rs40
1 files changed, 12 insertions, 28 deletions
diff --git a/examples/udp-codec.rs b/examples/udp-codec.rs
index c874ebd7..a2429e49 100644
--- a/examples/udp-codec.rs
+++ b/examples/udp-codec.rs
@@ -1,40 +1,24 @@
-//! This is a basic example of leveraging `UdpCodec` to create a simple UDP
+//! This is a basic example of leveraging `BytesCodec` to create a simple UDP
//! client and server which speak a custom protocol.
//!
-//! Here we're using the a custom codec to convert a UDP socket to a stream of
+//! Here we're using the codec from tokio-io to convert a UDP socket to a stream of
//! client messages. These messages are then processed and returned back as a
//! new message with a new destination. Overall, we then use this to construct a
//! "ping pong" pair where two sockets are sending messages back and forth.
extern crate tokio;
+extern crate tokio_io;
extern crate env_logger;
extern crate futures;
extern crate futures_cpupool;
-use std::io;
use std::net::SocketAddr;
use futures::{Future, Stream, Sink};
use futures::future::Executor;
use futures_cpupool::CpuPool;
-use tokio::net::{UdpSocket, UdpCodec};
-
-pub struct LineCodec;
-
-impl UdpCodec for LineCodec {
- type In = (SocketAddr, Vec<u8>);
- type Out = (SocketAddr, Vec<u8>);
- type Error = io::Error;
-
- fn decode(&mut self, addr: &SocketAddr, buf: &[u8]) -> io::Result<Self::In> {
- Ok((*addr, buf.to_vec()))
- }
-
- fn encode(&mut self, (addr, buf): Self::Out, into: &mut Vec<u8>) -> io::Result<SocketAddr> {
- into.extend(buf);
- Ok(addr)
- }
-}
+use tokio::net::UdpSocket;
+use tokio_io::codec::BytesCodec;
fn main() {
drop(env_logger::init());
@@ -50,27 +34,27 @@ fn main() {
// We're parsing each socket with the `LineCodec` defined above, and then we
// `split` each codec into the sink/stream halves.
- let (a_sink, a_stream) = a.framed(LineCodec).split();
- let (b_sink, b_stream) = b.framed(LineCodec).split();
+ let (a_sink, a_stream) = a.framed(BytesCodec::new()).split();
+ let (b_sink, b_stream) = b.framed(BytesCodec::new()).split();
// Start off by sending a ping from a to b, afterwards we just print out
// what they send us and continually send pings
// let pings = stream::iter((0..5).map(Ok));
- let a = a_sink.send((b_addr, b"PING".to_vec())).and_then(|a_sink| {
+ let a = a_sink.send(("PING".into(), b_addr)).and_then(|a_sink| {
let mut i = 0;
- let a_stream = a_stream.take(4).map(move |(addr, msg)| {
+ let a_stream = a_stream.take(4).map(move |(msg, addr)| {
i += 1;
println!("[a] recv: {}", String::from_utf8_lossy(&msg));
- (addr, format!("PING {}", i).into_bytes())
+ (format!("PING {}", i).into(), addr)
});
a_sink.send_all(a_stream)
});
// The second client we have will receive the pings from `a` and then send
// back pongs.
- let b_stream = b_stream.map(|(addr, msg)| {
+ let b_stream = b_stream.map(|(msg, addr)| {
println!("[b] recv: {}", String::from_utf8_lossy(&msg));
- (addr, b"PONG".to_vec())
+ ("PONG".into(), addr)
});
let b = b_sink.send_all(b_stream);