pub fn decode(&mut self, data: &mut BytesMut) -> Option<Message> {
        loop {
            match &self.state {
                FrameDecoderState::NoLength => {
                    if data.len() < 4 {
                        data.reserve(4);
                        return None;
                    } else {
                        let size = data.get_u32();
                        self.state = FrameDecoderState::Length(size);
                    }
                }
                FrameDecoderState::Length(size) => {
                    let size: usize = *size as usize;
                    if data.len() < size {
                        data.reserve(size);
                        return None;
                    } else {
                        let frame = data.split_to(size);
                        self.state = FrameDecoderState::NoLength;
                        let frame = frame.clone();
                        let mut deserializer = Deserializer::from_slice(&frame);
                        let value: Result<Message, _> =
                            serde::de::Deserialize::deserialize(&mut deserializer);

                        if value.is_err() {
                            continue;
                        }
                        let value = value.unwrap();
                        return Some(value);
                    }
                }
            }
        }
    }
/// Tries to extract one message from buffer given. The buffer is
/// consumed as far as possible and resized to adequate size
/// if more data is needed. Returns one frame if it can be parsed
/// from given buffer.
/// This is to be called repeatedly, interleaved with filling the
/// buffer with more data as needed.
Tries to extract one message from buffer given. The buffer is
consumed as far as possible and resized to adequate size
if more data is needed. Returns one frame if it can be parsed
from given buffer.
This is to be called repeatedly, interleaved with filling the
buffer with more data as needed.
Tries to extract one message from buffer given . The buffer is consumed as far as possible and resized to adequate size if more data is needed . Returns one frame if it can be parsed from given buffer . This is to be called repeatedly interleaved with filling the buffer with more data as needed .
pub fn decode(&mut self, data: &mut BytesMut) -> Option<Message> {
        loop {
            match &self.state {
                FrameDecoderState::NoLength => {
                    if data.len() < 4 {
                        data.reserve(4);
                        return None;
                    } else {
                        let size = data.get_u32();
                        self.state = FrameDecoderState::Length(size);
                    }
                }
                FrameDecoderState::Length(size) => {
                    let size: usize = *size as usize;
                    if data.len() < size {
                        data.reserve(size);
                        return None;
                    } else {
                        let frame = data.split_to(size);
                        self.state = FrameDecoderState::NoLength;
                        let frame = frame.clone();
                        let mut deserializer = Deserializer::from_slice(&frame);
                        let value: Result<Message, _> =
                            serde::de::Deserialize::deserialize(&mut deserializer);
                        if value.is_err() {
                            continue;
                        }
                        let value = value.unwrap();
                        return Some(value);
                    }
                }
            }
        }
    }
pub fn decode ( & mut self , data : & mut BytesMut ) -> Option < Message > { loop { match & self . state { FrameDecoderState :: NoLength => { if data . len ( ) < 4 { data . reserve ( 4 ) ; return None ; } else { let size = data . get_u32 ( ) ; self . state = FrameDecoderState :: Length ( size ) ; } } FrameDecoderState :: Length ( size ) => { let size : usize = * size as usize ; if data . len ( ) < size { data . reserve ( size ) ; return None ; } else { let frame = data . split_to ( size ) ; self . state = FrameDecoderState :: NoLength ; let frame = frame . clone ( ) ; let mut deserializer = Deserializer :: from_slice ( & frame ) ; let value : Result < Message , _ > = serde :: de :: Deserialize :: deserialize ( & mut deserializer ) ; if value . is_err ( ) { continue ; } let value = value . unwrap ( ) ; return Some ( value ) ; } } } } }
Tries to extract one message from buffer given.
Tries to extract one message from buffer given .
&mut self
data
&mut BytesMut
&mut self
data
&mut BytesMut