diff --git a/source/utils/CarlaPipeUtils.cpp b/source/utils/CarlaPipeUtils.cpp index eb78662bb..8e9ea0091 100644 --- a/source/utils/CarlaPipeUtils.cpp +++ b/source/utils/CarlaPipeUtils.cpp @@ -508,7 +508,7 @@ bool CarlaPipeCommon::readNextLineAsBool(bool& value) noexcept { CARLA_SAFE_ASSERT_RETURN(pData->isReading, false); - if (const char* const msg = readline()) + if (const char* const msg = readlineblock()) { value = (std::strcmp(msg, "true") == 0); delete[] msg; @@ -522,7 +522,7 @@ bool CarlaPipeCommon::readNextLineAsInt(int32_t& value) noexcept { CARLA_SAFE_ASSERT_RETURN(pData->isReading, false); - if (const char* const msg = readline()) + if (const char* const msg = readlineblock()) { value = std::atoi(msg); delete[] msg; @@ -536,7 +536,7 @@ bool CarlaPipeCommon::readNextLineAsUInt(uint32_t& value) noexcept { CARLA_SAFE_ASSERT_RETURN(pData->isReading, false); - if (const char* const msg = readline()) + if (const char* const msg = readlineblock()) { int32_t tmp = std::atoi(msg); delete[] msg; @@ -555,7 +555,7 @@ bool CarlaPipeCommon::readNextLineAsLong(int64_t& value) noexcept { CARLA_SAFE_ASSERT_RETURN(pData->isReading, false); - if (const char* const msg = readline()) + if (const char* const msg = readlineblock()) { value = std::atol(msg); delete[] msg; @@ -569,7 +569,7 @@ bool CarlaPipeCommon::readNextLineAsULong(uint64_t& value) noexcept { CARLA_SAFE_ASSERT_RETURN(pData->isReading, false); - if (const char* const msg = readline()) + if (const char* const msg = readlineblock()) { int64_t tmp = std::atol(msg); delete[] msg; @@ -588,7 +588,7 @@ bool CarlaPipeCommon::readNextLineAsFloat(float& value) noexcept { CARLA_SAFE_ASSERT_RETURN(pData->isReading, false); - if (const char* const msg = readline()) + if (const char* const msg = readlineblock()) { value = static_cast(std::atof(msg)); delete[] msg; @@ -602,7 +602,7 @@ bool CarlaPipeCommon::readNextLineAsDouble(double& value) noexcept { CARLA_SAFE_ASSERT_RETURN(pData->isReading, false); - if (const char* const msg = readline()) + if (const char* const msg = readlineblock()) { value = std::atof(msg); delete[] msg; @@ -616,7 +616,7 @@ bool CarlaPipeCommon::readNextLineAsString(const char*& value) noexcept { CARLA_SAFE_ASSERT_RETURN(pData->isReading, false); - if (const char* const msg = readline()) + if (const char* const msg = readlineblock()) { value = msg; return true; @@ -692,6 +692,8 @@ bool CarlaPipeCommon::writeAndFixMsg(const char* const msg) noexcept // internal const char* CarlaPipeCommon::readline() noexcept { + CARLA_SAFE_ASSERT_RETURN(pData->pipeRecv != INVALID_PIPE_VALUE, nullptr); + char c; char* ptr = pData->tmpBuf; ssize_t ret; @@ -744,6 +746,25 @@ const char* CarlaPipeCommon::readline() noexcept return nullptr; } +const char* CarlaPipeCommon::readlineblock(const uint32_t timeOutMilliseconds) noexcept +{ + const uint32_t timeoutEnd(juce::Time::getMillisecondCounter() + timeOutMilliseconds); + + for (;;) + { + if (const char* const msg = readline()) + return msg; + + if (juce::Time::getMillisecondCounter() >= timeoutEnd) + break; + + carla_msleep(5); + } + + carla_stderr("readlineblock timed out"); + return nullptr; +} + bool CarlaPipeCommon::writeMsgBuffer(const char* const msg, const std::size_t size) const noexcept { // TESTING remove later (replace with trylock scope) diff --git a/source/utils/CarlaPipeUtils.hpp b/source/utils/CarlaPipeUtils.hpp index a43d9936b..1a69c1e02 100644 --- a/source/utils/CarlaPipeUtils.hpp +++ b/source/utils/CarlaPipeUtils.hpp @@ -82,7 +82,7 @@ private: // internal const char* readline() noexcept; - // internal + const char* readlineblock(const uint32_t timeOutMilliseconds = 50) noexcept; bool writeMsgBuffer(const char* const msg, const std::size_t size) const noexcept; CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPipeCommon)