[][src]Trait tokio_core::net::UdpCodec

pub trait UdpCodec {
    type In;
    type Out;
    fn decode(&mut self, src: &SocketAddr, buf: &[u8]) -> Result<Self::In>;
fn encode(&mut self, msg: Self::Out, buf: &mut Vec<u8>) -> SocketAddr; }

Encoding of frames via buffers.

This trait is used when constructing an instance of UdpFramed and provides the In and Out types which are decoded and encoded from the socket, respectively.

Because UDP is a connectionless protocol, the decode method receives the address where data came from and the encode method is also responsible for determining the remote host to which the datagram should be sent

The trait itself is implemented on a type that can track state for decoding or encoding, which is particularly useful for streaming parsers. In many cases, though, this type will simply be a unit struct (e.g. struct HttpCodec).

Associated Types

type In

The type of decoded frames.

type Out

The type of frames to be encoded.

Loading content...

Required methods

fn decode(&mut self, src: &SocketAddr, buf: &[u8]) -> Result<Self::In>

Attempts to decode a frame from the provided buffer of bytes.

This method is called by UdpFramed on a single datagram which has been read from a socket. The buf argument contains the data that was received from the remote address, and src is the address the data came from. Note that typically this method should require the entire contents of buf to be valid or otherwise return an error with trailing data.

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 encode(&mut self, msg: Self::Out, buf: &mut Vec<u8>) -> SocketAddr

Encodes a frame into the buffer provided.

This method will encode msg into the byte buffer provided by buf. The buf provided is an internal buffer of the Framed instance and will be written out when possible.

The encode method also determines the destination to which the buffer should be directed, which will be returned as a SocketAddr.

Loading content...

Implementors

Loading content...