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 e437bf2 commit b3a9cae
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
{
// 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{};
}
DWORD bytesReceived = 0;
// 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();

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

0 comments on commit b3a9cae

Please sign in to comment.