|
6 | 6 |
|
7 | 7 | #include "demo1/message/Ack.h" |
8 | 8 | #include "comms/units.h" |
| 9 | +#include "comms/process.h" |
9 | 10 |
|
10 | 11 | namespace demo1 |
11 | 12 | { |
@@ -349,39 +350,10 @@ void Session::terminateSession() |
349 | 350 |
|
350 | 351 | void Session::processInput() |
351 | 352 | { |
352 | | - std::size_t consumed = 0U; |
353 | | - while (consumed < m_inputBuf.size()) { |
354 | | - // Smart pointer to the message object. |
355 | | - Frame::MsgPtr msgPtr; |
356 | | - |
357 | | - // Get the iterator for reading |
358 | | - auto begIter = comms::readIteratorFor<InputMsg>(&m_inputBuf[0] + consumed); |
359 | | - auto iter = begIter; |
360 | | - |
361 | | - // Do the read |
362 | | - auto es = m_frame.read(msgPtr, iter, m_inputBuf.size() - consumed); |
363 | | - if (es == comms::ErrorStatus::NotEnoughData) { |
364 | | - break; // Not enough data in the buffer, stop processing |
365 | | - } |
366 | | - |
367 | | - if (es == comms::ErrorStatus::ProtocolError) { |
368 | | - // Something is not right with the data, remove one character and try again |
369 | | - std::cerr << "WARNING: Corrupted buffer" << std::endl; |
370 | | - ++consumed; |
371 | | - continue; |
372 | | - } |
373 | | - |
374 | | - if (es == comms::ErrorStatus::Success) { |
375 | | - assert(msgPtr); // If read is successful, msgPtr is expected to hold a valid pointer |
376 | | - std::cout << "INFO: New message: " << msgPtr->name() << std::endl; |
377 | | - msgPtr->dispatch(*this); // Call appropriate handle() function |
378 | | - } |
379 | | - |
380 | | - // The iterator for reading has been advanced, update the difference |
381 | | - consumed += std::distance(begIter, iter); |
| 353 | + if (!m_inputBuf.empty()) { |
| 354 | + auto consumed = comms::processAllWithDispatch(&m_inputBuf[0], m_inputBuf.size(), m_frame, *this); |
| 355 | + m_inputBuf.erase(m_inputBuf.begin(), m_inputBuf.begin() + consumed); |
382 | 356 | } |
383 | | - |
384 | | - m_inputBuf.erase(m_inputBuf.begin(), m_inputBuf.begin() + consumed); |
385 | 357 | } |
386 | 358 |
|
387 | 359 | void Session::sendAck(demo1::MsgId id) |
|
0 commit comments