1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
use byteorder::ByteOrder;

pub enum Decoded<T> {
    Msg(T),
    FrameNotYetComplete,
    Error(crate::Error),
}

#[cfg(feature = "std")]
use log::trace;

/// A struct for decoding bytes.
///
/// This is similar to `Decoder` but uses `std` to allocate a buffer.
///
/// This is not part of the `MiniTxRx` struct itself because we do not want to
/// require access to resources when decoding bytes.
#[cfg(feature = "std")]
pub struct StdDecoder {
    buf: Vec<u8>,
    state: FramedReaderState,
}

#[cfg(feature = "std")]
impl StdDecoder {
    pub fn new(sz: usize) -> Self {
        Self {
            buf: vec![0; sz],
            state: FramedReaderState::Empty,
        }
    }

    pub fn consume<T>(&mut self, byte: u8) -> Decoded<T>
    where
        for<'de> T: serde::de::Deserialize<'de>,
    {
        let (new_state, decoded) = consume_inner(&mut self.state, &mut self.buf, byte);
        self.state = new_state;
        decoded
    }
}

/// A struct for decoding bytes.
///
/// This is not part of the `MiniTxRx` struct itself because we do not want to
/// require access to resources when decoding bytes.
pub struct Decoder<'a> {
    buf: &'a mut [u8],
    state: FramedReaderState,
}

impl<'a> Decoder<'a> {
    pub fn new(buf: &'a mut [u8]) -> Self {
        Self {
            buf,
            state: FramedReaderState::Empty,
        }
    }

    pub fn consume<T>(&mut self, byte: u8) -> Decoded<T>
    where
        for<'de> T: serde::de::Deserialize<'de>,
    {
        let (new_state, decoded) = consume_inner(&mut self.state, &mut self.buf, byte);
        self.state = new_state;
        decoded
    }
}

#[inline]
fn consume_inner<T>(
    self_state: &mut FramedReaderState,
    self_buf: &mut [u8],
    byte: u8,
) -> (FramedReaderState, Decoded<T>)
where
    for<'de> T: serde::de::Deserialize<'de>,
{
    let (new_state, result) = match self_state {
        FramedReaderState::Empty => (FramedReaderState::ReadingHeader(byte), Ok(None)),
        FramedReaderState::ReadingHeader(byte0) => {
            let buf: [u8; 2] = [*byte0, byte];
            let len = ::byteorder::LittleEndian::read_u16(&buf);
            if (len as usize) > self_buf.len() {
                (FramedReaderState::Error, Err(crate::Error::TooLong))
            } else {
                if len == 0 {
                    let result: &[u8] = b""; // 0 length slice of u8
                    (FramedReaderState::Empty, Ok(Some(result)))
                } else {
                    #[cfg(feature = "std")]
                    trace!("starting new message with length {}", len);
                    let rms = ReadingMessageState { len: len, idx: 0 };
                    (FramedReaderState::ReadingMessage(rms), Ok(None))
                }
            }
        }
        FramedReaderState::ReadingMessage(ref rms) => {
            let (msg_len, mut idx) = (rms.len, rms.idx);
            self_buf[idx as usize] = byte;
            idx += 1;
            if idx < msg_len {
                #[cfg(feature = "std")]
                trace!("got byte in message with length {}", msg_len);
                let rms = ReadingMessageState { len: msg_len, idx };
                (FramedReaderState::ReadingMessage(rms), Ok(None))
            } else if idx == msg_len {
                #[cfg(feature = "std")]
                trace!("completed message with length {}", msg_len);
                let result = &self_buf[0..(idx as usize)];
                (FramedReaderState::Empty, Ok(Some(result)))
            } else {
                // Frame langer than expected.
                // Theoretically it is impossible to get here, so we panic.
                panic!("frame larger than expected");
            }
        }
        FramedReaderState::Error => (FramedReaderState::Error, Err(crate::Error::PreviousError)),
    };
    let decoded = match result {
        Ok(Some(buf)) => match ssmarshal::deserialize(buf) {
            Ok((msg, _nbytes)) => Decoded::Msg(msg),
            Err(e) => Decoded::Error(e.into()),
        },
        Ok(None) => Decoded::FrameNotYetComplete,
        Err(e) => Decoded::Error(e),
    };
    (new_state, decoded)
}

struct ReadingMessageState {
    len: u16, // the length when full
    idx: u16, // the current length
}

enum FramedReaderState {
    Empty,
    ReadingHeader(u8),
    ReadingMessage(ReadingMessageState),
    Error,
}