From 8984c0971bfa6a05994f8cb7a48e9d937a237aa3 Mon Sep 17 00:00:00 2001 From: falkTX Date: Tue, 30 Jun 2015 18:21:03 +0200 Subject: [PATCH] Fix sending big messages via pipe; Cleanup --- source/bridges-ui/CarlaBridgeUI.cpp | 9 ++++-- source/utils/CarlaPipeUtils.cpp | 45 ++++++++++++++++------------- 2 files changed, 31 insertions(+), 23 deletions(-) diff --git a/source/bridges-ui/CarlaBridgeUI.cpp b/source/bridges-ui/CarlaBridgeUI.cpp index 428306235..f96b78d17 100644 --- a/source/bridges-ui/CarlaBridgeUI.cpp +++ b/source/bridges-ui/CarlaBridgeUI.cpp @@ -182,11 +182,11 @@ bool CarlaBridgeUI::msgReceived(const char* const msg) noexcept if (std::strcmp(msg, "atom") == 0) { - uint32_t index, size; + uint32_t index, atomTotalSize; const char* base64atom; CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(index), true); - CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(size), true); + CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(atomTotalSize), true); CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(base64atom), true); std::vector chunk(carla_getChunkFromBase64String(base64atom)); @@ -194,7 +194,10 @@ bool CarlaBridgeUI::msgReceived(const char* const msg) noexcept CARLA_SAFE_ASSERT_RETURN(chunk.size() >= sizeof(LV2_Atom), true); const LV2_Atom* const atom((const LV2_Atom*)chunk.data()); - CARLA_SAFE_ASSERT_RETURN(lv2_atom_total_size(atom) == chunk.size(), true); + const uint32_t atomTotalSizeCheck(lv2_atom_total_size(atom)); + + CARLA_SAFE_ASSERT_RETURN(atomTotalSizeCheck == atomTotalSize, true); + CARLA_SAFE_ASSERT_RETURN(atomTotalSizeCheck == chunk.size(), true); dspAtomReceived(index, atom); return true; diff --git a/source/utils/CarlaPipeUtils.cpp b/source/utils/CarlaPipeUtils.cpp index 43f870c7b..15e94a108 100644 --- a/source/utils/CarlaPipeUtils.cpp +++ b/source/utils/CarlaPipeUtils.cpp @@ -799,12 +799,12 @@ bool CarlaPipeCommon::writeAndFixMessage(const char* const msg) const noexcept if (fixedMsg[size-1] == '\r') { fixedMsg[size-1] = '\n'; - fixedMsg[size] = '\0'; + fixedMsg[size ] = '\0'; fixedMsg[size+1] = '\0'; } else { - fixedMsg[size] = '\n'; + fixedMsg[size ] = '\n'; fixedMsg[size+1] = '\0'; } } @@ -962,7 +962,8 @@ void CarlaPipeCommon::writeLv2AtomMessage(const uint32_t index, const LV2_Atom* char tmpBuf[0xff+1]; tmpBuf[0xff] = '\0'; - CarlaString base64atom(CarlaString::asBase64(atom, lv2_atom_total_size(atom))); + const uint32_t atomTotalSize(lv2_atom_total_size(atom)); + CarlaString base64atom(CarlaString::asBase64(atom, atomTotalSize)); const CarlaMutexLocker cml(pData->writeLock); @@ -972,7 +973,7 @@ void CarlaPipeCommon::writeLv2AtomMessage(const uint32_t index, const LV2_Atom* std::snprintf(tmpBuf, 0xff, "%i\n", index); _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf)); - std::snprintf(tmpBuf, 0xff, "%i\n", atom->size); + std::snprintf(tmpBuf, 0xff, "%i\n", atomTotalSize); _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf)); writeAndFixMessage(base64atom.buffer()); @@ -1016,7 +1017,7 @@ const char* CarlaPipeCommon::_readline() const noexcept pData->tmpStr.clear(); - for (int i=0; i < 0xff; ++i) + for (int i=0; i<0xff; ++i) { try { #ifdef CARLA_OS_WIN @@ -1027,6 +1028,9 @@ const char* CarlaPipeCommon::_readline() const noexcept #endif } CARLA_SAFE_EXCEPTION_BREAK("CarlaPipeCommon::readline() - read"); + //if (ret == 0 || c == '\n') + // break; + if (ret == 1 && c != '\n') { if (c == '\r') @@ -1037,30 +1041,31 @@ const char* CarlaPipeCommon::_readline() const noexcept if (i+1 == 0xff) { i = 0; - ptr = pData->tmpBuf; - pData->tmpStr += pData->tmpBuf; - } - - continue; - } - - if (pData->tmpStr.isNotEmpty() || ptr != pData->tmpBuf || ret == 1) - { - if (ptr != pData->tmpBuf) - { *ptr = '\0'; pData->tmpStr += pData->tmpBuf; + ptr = pData->tmpBuf; } - try { - return pData->tmpStr.dup(); - } CARLA_SAFE_EXCEPTION_RETURN("CarlaPipeCommon::readline() - dup", nullptr); + continue; } break; } - return nullptr; + if (ptr != pData->tmpBuf) + { + *ptr = '\0'; + pData->tmpStr += pData->tmpBuf; + } + else if (pData->tmpStr.isEmpty()) + { + // some error + return nullptr; + } + + try { + return pData->tmpStr.dup(); + } CARLA_SAFE_EXCEPTION_RETURN("CarlaPipeCommon::readline() - dup", nullptr); } const char* CarlaPipeCommon::_readlineblock(const uint32_t timeOutMilliseconds) const noexcept