Skip to content

Commit

Permalink
Resolved network issue
Browse files Browse the repository at this point in the history
We used to store a temporary part of network result of every ApiQuery
in a string and on every ReadData event we converted that QByteArray
into a string and appended it to this temporary string.

This was wrong as the QByteArray didn't contain whole Unicode text but
only a part of it that might be just a half of a symbol or just something
invalid that couldn't be parsed by QString.

That resulted in randomly corrupt symbols in some pages, that in case of
software rollback were written back to wikipedia.

Now we store the parts of QByteArray to a temporary QByteArray which
converted to QString in one shot later. It uses a bit more operating
memory and less CPU, but it works.
  • Loading branch information
benapetr committed Sep 3, 2015
1 parent a250d4d commit 2d5b5b2
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
10 changes: 6 additions & 4 deletions huggle/apiquery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,10 @@ void ApiQuery::Finished()
if (this->reply == nullptr)
throw new Huggle::NullPointerException("loc ApiQuery::reply", BOOST_CURRENT_FUNCTION);
ApiQueryResult *result = (ApiQueryResult*)this->Result;
result->Data += QString(this->reply->readAll());
this->temp += this->reply->readAll();
result->Data = QString(this->temp);
// remove the temporary data so that we save the ram
this->temp.clear();
// now we need to check if request was successful or not
if (this->reply->error())
{
Expand Down Expand Up @@ -242,6 +245,7 @@ void ApiQuery::Process()
}
this->StartTime = QDateTime::currentDateTime();
this->ThrowOnValidResult();
this->temp.clear();
if (!this->URL.size())
this->ConstructUrl();
this->Status = StatusProcessing;
Expand Down Expand Up @@ -305,11 +309,9 @@ void ApiQuery::ReadData()
// don't even try to do anything if query was killed
if (this->Status == StatusKilled)
return;
if (this->Result == nullptr)
throw new Huggle::NullPointerException("loc ApiQuery::Result", BOOST_CURRENT_FUNCTION);
if (this->reply == nullptr)
throw new Huggle::NullPointerException("loc ApiQuery::reply", BOOST_CURRENT_FUNCTION);
this->Result->Data += QString(this->reply->readAll());
this->temp += this->reply->readAll();
}

void ApiQuery::SetAction(const Action action)
Expand Down
1 change: 1 addition & 0 deletions huggle/apiquery.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ namespace Huggle
//! This is only needed when you are using rollback
void FinishRollback();
QString ActionPart;
QByteArray temp;
//! Reply from qnet
QNetworkReply *reply = nullptr;
};
Expand Down
8 changes: 4 additions & 4 deletions huggle/webserverquery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,9 @@ void WebserverQuery::ReadData()
{ // don't even try to do anything if query was killed
if (this->Status == StatusKilled)
return;
if (this->Result == nullptr)
throw new Huggle::NullPointerException("loc WebserverQuery::Result", BOOST_CURRENT_FUNCTION);
if (this->reply == nullptr)
throw new Huggle::NullPointerException("loc WebserverQuery::reply", BOOST_CURRENT_FUNCTION);
this->Result->Data += QString(this->reply->readAll());
this->temp += this->reply->readAll();
}

void WebserverQuery::Finished()
Expand All @@ -98,7 +96,9 @@ void WebserverQuery::Finished()
throw new Huggle::NullPointerException("loc WebserverQuery::Result", BOOST_CURRENT_FUNCTION);
if (this->reply == nullptr)
throw new Huggle::NullPointerException("loc WebserverQuery::reply", BOOST_CURRENT_FUNCTION);
this->Result->Data += QString(this->reply->readAll());
this->temp += this->reply->readAll();
this->Result->Data = QString(this->temp);
this->temp.clear();
// now we need to check if request was successful or not
if (this->reply->error())
{
Expand Down
1 change: 1 addition & 0 deletions huggle/webserverquery.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ namespace Huggle
void Kill();
private:
QNetworkReply *reply = nullptr;
QByteArray temp;
private slots:
void ReadData();
void Finished();
Expand Down

0 comments on commit 2d5b5b2

Please sign in to comment.