diff --git a/src/io/mod.rs b/src/io/mod.rs index 694174892..4c9ac0bf9 100644 --- a/src/io/mod.rs +++ b/src/io/mod.rs @@ -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}; diff --git a/src/io/udp_frame.rs b/src/io/udp_frame.rs index d09d0a8fa..99cc90a5a 100644 --- a/src/io/udp_frame.rs +++ b/src/io/udp_frame.rs @@ -241,3 +241,22 @@ impl Sink for FramedUdpWrite { } } +/// Default implementation of a DGram "parser" +/// This receives and produces a tuple of ('SocketAddr', `Vec`) +pub struct VecDGramCodec; + +impl CodecUdp for VecDGramCodec { + type In = (SocketAddr, Vec); + type Out = (SocketAddr, Vec); + + fn decode(&mut self, addr : &SocketAddr, buf: &[u8]) -> Result { + Ok((*addr, buf.into())) + } + + fn encode(&mut self, item: &Self::Out, into: &mut Vec) -> SocketAddr { + into.extend_from_slice(item.1.as_slice()); + into.push('\n' as u8); + item.0 + } +} +