Skip to content

Commit

Permalink
Read Payload after reading header in one call.
Browse files Browse the repository at this point in the history
Increases performance, as it requires only minimum 1 call to read frame.
Before, at least 2 calls were required.
  • Loading branch information
gabryelreyes committed Sep 27, 2023
1 parent fe2e718 commit 425055d
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions src/SerialMuxProtServer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -400,14 +400,16 @@ class SerialMuxProtServer
*/
void processRxData()
{
uint8_t expectedBytes = 0;
uint8_t dlc = 0;
uint8_t expectedBytes = 0;
uint8_t dlc = 0;
bool expectingHeader = false;

/* Determine how many bytes to read. */
if (HEADER_LEN > m_receivedBytes)
{
/* Header must be read. */
expectedBytes = (HEADER_LEN - m_receivedBytes);
expectedBytes = (HEADER_LEN - m_receivedBytes);
expectingHeader = true;
}
else
{
Expand All @@ -431,6 +433,27 @@ class SerialMuxProtServer
m_receivedBytes += m_stream.readBytes(&m_receiveFrame.raw[m_receivedBytes], expectedBytes);
}

if ((HEADER_LEN == m_receivedBytes) && (true == expectingHeader))
{
/* Header has been read. Get DLC of Rx Channel using Header. */
dlc = m_receiveFrame.fields.header.headerFields.m_dlc;

/* DLC = 0 means that the channel does not exist. */
if ((0U != dlc) && (MAX_RX_ATTEMPTS >= m_rxAttempts))
{
expectedBytes = (dlc - (m_receivedBytes - HEADER_LEN));
m_rxAttempts++;
}

if (0U != expectedBytes)
{
if (expectedBytes <= m_stream.available())
{
m_receivedBytes += m_stream.readBytes(&m_receiveFrame.raw[m_receivedBytes], expectedBytes);
}
}
}

/* Frame has been received. */
if ((0U != dlc) && ((HEADER_LEN + dlc) == m_receivedBytes))
{
Expand Down

0 comments on commit 425055d

Please sign in to comment.