mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-08-22 00:00:11 +02:00
added Default Codec for Udp
This commit is contained in:
+1
-1
@@ -44,7 +44,7 @@ mod window;
|
||||
mod write_all;
|
||||
pub use self::copy::{copy, Copy};
|
||||
pub use self::frame::{EasyBuf, EasyBufMut, FramedRead, FramedWrite, Framed, Codec};
|
||||
pub use self::udp_frame::{FramedUdp, framed_udp, FramedUdpRead, FramedUdpWrite, CodecUdp};
|
||||
pub use self::udp_frame::{FramedUdp, framed_udp, FramedUdpRead, FramedUdpWrite, CodecUdp, VecDGramCodec};
|
||||
pub use self::flush::{flush, Flush};
|
||||
pub use self::read_exact::{read_exact, ReadExact};
|
||||
pub use self::read_to_end::{read_to_end, ReadToEnd};
|
||||
|
||||
@@ -241,3 +241,22 @@ impl<C : CodecUdp> Sink for FramedUdpWrite<C> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Default implementation of a DGram "parser"
|
||||
/// This receives and produces a tuple of ('SocketAddr', `Vec`<u8>)
|
||||
pub struct VecDGramCodec;
|
||||
|
||||
impl CodecUdp for VecDGramCodec {
|
||||
type In = (SocketAddr, Vec<u8>);
|
||||
type Out = (SocketAddr, Vec<u8>);
|
||||
|
||||
fn decode(&mut self, addr : &SocketAddr, buf: &[u8]) -> Result<Self::In, io::Error> {
|
||||
Ok((*addr, buf.into()))
|
||||
}
|
||||
|
||||
fn encode(&mut self, item: &Self::Out, into: &mut Vec<u8>) -> SocketAddr {
|
||||
into.extend_from_slice(item.1.as_slice());
|
||||
into.push('\n' as u8);
|
||||
item.0
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user