mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-08-28 00:00:11 +02:00
Remove UdpCodec (#109)
`UdpFramed` is updated to use the `Encoder` and `Decoder` traits from `tokio-io`.
This commit is contained in:
+12
-28
@@ -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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user