diff --git a/src/terrama2/core/network/TcpManager.cpp b/src/terrama2/core/network/TcpManager.cpp index 9eb553be5..6edd7f7d6 100644 --- a/src/terrama2/core/network/TcpManager.cpp +++ b/src/terrama2/core/network/TcpManager.cpp @@ -179,9 +179,11 @@ void terrama2::core::TcpManager::readReadySlot(QTcpSocket* tcpSocket) noexcept return; } - //Raii block - RaiiBlock block(blockSize_); - Q_UNUSED(block) + // clear socket after message processing + RaiiSocket raiiSocket(tcpSocket); Q_UNUSED(raiiSocket); + + // clear block after message processing + RaiiBlock block(blockSize_); Q_UNUSED(block) int sigInt = -1; in >> sigInt; @@ -396,3 +398,13 @@ void terrama2::core::TcpManager::sendSignalSlot(QTcpSocket* tcpSocket, TcpSignal if(written == -1 || !tcpSocket->waitForBytesWritten(30000)) TERRAMA2_LOG_WARNING() << QObject::tr("Unable to write to server."); } + +terrama2::core::RaiiSocket::~RaiiSocket() +{ + auto bytes = tcpSocket_->readAll(); + if(bytes.size()) + { + QString errMsg(bytes); + TERRAMA2_LOG_ERROR() << QObject::tr("Garbage in socket message:\n%1.").arg(errMsg); + } +} diff --git a/src/terrama2/core/network/TcpManager.hpp b/src/terrama2/core/network/TcpManager.hpp index a276586d6..6e924d15e 100644 --- a/src/terrama2/core/network/TcpManager.hpp +++ b/src/terrama2/core/network/TcpManager.hpp @@ -40,6 +40,23 @@ namespace terrama2 { namespace core { + /** + * @brief RAII class for clearing the socket after reading data + * + * This class ensures no data is left in the socket for further reading. + * + */ + class RaiiSocket + { + public: + RaiiSocket(QTcpSocket* tcpSocket) + : tcpSocket_(tcpSocket) {} + //! Clear the socket end log warning message if there was data to be read. + ~RaiiSocket(); + private: + QTcpSocket* tcpSocket_; + }; + class ServiceManager; class DataManager; diff --git a/webapp/core/Service.js b/webapp/core/Service.js index 50485149b..0436d98ed 100644 --- a/webapp/core/Service.js +++ b/webapp/core/Service.js @@ -126,7 +126,7 @@ var Service = module.exports = function(serviceInstance) { // append and check if the complete message has arrived tempBuffer = _createBufferFrom(tempBuffer, byteArray); const messageSizeReceived = tempBuffer.readUInt32BE(0); - if(tempBuffer.length !== (messageSizeReceived + 4)) { + if(tempBuffer.length < (messageSizeReceived + 4)) { // if we don't have the complete message // wait for the rest return;