Skip to content

Commit

Permalink
Merge pull request #1543 from TerraMA2/b4.0
Browse files Browse the repository at this point in the history
update 4.0.4
  • Loading branch information
janosimas authored Apr 18, 2018
2 parents b5ab11c + d4d0107 commit fda6592
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
18 changes: 15 additions & 3 deletions src/terrama2/core/network/TcpManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
17 changes: 17 additions & 0 deletions src/terrama2/core/network/TcpManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion webapp/core/Service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit fda6592

Please sign in to comment.