Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/udp-codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ 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()))
Expand Down
19 changes: 16 additions & 3 deletions src/net/udp/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@ pub trait UdpCodec {
/// The type of frames to be encoded.
type Out;

/// The type of unrecoverable frame decoding errors.
///
/// If an individual message is ill-formed but can be ignored without
/// interfering with the processing of future messages, it may be more
/// useful to report the failure as an `Item`.
///
/// `From<io::Error>` is required in the interest of making `Error` suitable
/// for returning directly from a `Framed`.alloc
///
/// Note that implementors of this trait can simply indicate `type Error =
/// io::Error` to use I/O errors as this type.
type Error: From<io::Error>;

/// Attempts to decode a frame from the provided buffer of bytes.
///
/// This method is called by `UdpFramed` on a single datagram which has been
Expand All @@ -37,7 +50,7 @@ pub trait UdpCodec {
/// Finally, if the bytes in the buffer are malformed then an error is
/// returned indicating why. This informs `Framed` that the stream is now
/// corrupt and should be terminated.
fn decode(&mut self, src: &SocketAddr, buf: &[u8]) -> io::Result<Self::In>;
fn decode(&mut self, src: &SocketAddr, buf: &[u8]) -> Result<Self::In,Self::Error>;

/// Encodes a frame into the buffer provided.
///
Expand Down Expand Up @@ -65,9 +78,9 @@ pub struct UdpFramed<C> {

impl<C: UdpCodec> Stream for UdpFramed<C> {
type Item = C::In;
type Error = io::Error;
type Error = C::Error;

fn poll(&mut self) -> Poll<Option<C::In>, io::Error> {
fn poll(&mut self) -> Poll<Option<C::In>, C::Error> {
let (n, addr) = try_nb!(self.socket.recv_from(&mut self.rd));
trace!("received {} bytes, decoding", n);
let frame = try!(self.codec.decode(&addr, &self.rd[..n]));
Expand Down