Skip to content

Commit

Permalink
windows/serialInterface: Implemented wait-based back-off on the ReadF…
Browse files Browse the repository at this point in the history
…ile() call to help mitigate CPU impacts and implement proper timeouts
  • Loading branch information
dragonmux committed Jan 16, 2024
1 parent ac93dfc commit 643993a
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/windows/serialInterface.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,12 @@ void serialInterface_t::writePacket(const std::string_view &packet) const
// NOLINTNEXTLINE(readability-convert-member-functions-to-static)
void serialInterface_t::refillBuffer() const

Check warning on line 282 in src/windows/serialInterface.cxx

View check run for this annotation

Codecov / codecov/patch

src/windows/serialInterface.cxx#L282

Added line #L282 was not covered by tests
{
// Try to wait for up to 100ms for data to become available
if (WaitForSingleObject(device, 100) != WAIT_OBJECT_0)
{
console.error("Waiting for data from device failed ("sv, GetLastError(), ")"sv);
throw bmpCommsError_t{};

Check warning on line 288 in src/windows/serialInterface.cxx

View check run for this annotation

Codecov / codecov/patch

src/windows/serialInterface.cxx#L287-L288

Added lines #L287 - L288 were not covered by tests
}
DWORD bytesReceived = 0;

Check warning on line 290 in src/windows/serialInterface.cxx

View check run for this annotation

Codecov / codecov/patch

src/windows/serialInterface.cxx#L290

Added line #L290 was not covered by tests
// Try to fill the read buffer, and if that fails, bail
if (!ReadFile(device, readBuffer.data(), static_cast<DWORD>(readBuffer.size()), &bytesReceived, nullptr))
Expand All @@ -302,7 +308,7 @@ std::string serialInterface_t::readPacket() const
while (length < packet.size())
{
// Check if we need more data or should use what's in the buffer already
while (readBufferOffset == readBufferFullness)
if (readBufferOffset == readBufferFullness)
refillBuffer();

Check warning on line 312 in src/windows/serialInterface.cxx

View check run for this annotation

Codecov / codecov/patch

src/windows/serialInterface.cxx#L312

Added line #L312 was not covered by tests

const auto *const bufferBegin{readBuffer.data() + readBufferOffset};
Expand Down

0 comments on commit 643993a

Please sign in to comment.