added Default Codec for Udp

This commit is contained in:
Rick Richardson 2016-11-22 08:47:57 -08:00
parent 2cb600bd19
commit 1a6753df1f
2 changed files with 20 additions and 1 deletions

View File

@ -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};

View File

@ -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
}
}