mirror of
https://github.com/overte-org/overte.git
synced 2025-04-21 17:03:58 +02:00
add breaking of marshaled buffer to QStringList
This commit is contained in:
parent
85919972e3
commit
24576574ae
4 changed files with 36 additions and 10 deletions
|
@ -13,6 +13,7 @@
|
|||
#include <QtCore/QJsonObject>
|
||||
#include <QtCore/QVariant>
|
||||
|
||||
#include <JSONBreakableMarshal.h>
|
||||
#include <PacketHeaders.h>
|
||||
|
||||
#include "DomainServerNodeData.h"
|
||||
|
@ -31,16 +32,9 @@ DomainServerNodeData::DomainServerNodeData() :
|
|||
}
|
||||
|
||||
void DomainServerNodeData::parseJSONStatsPacket(const QByteArray& statsPacket) {
|
||||
// push past the packet header
|
||||
QDataStream packetStream(statsPacket);
|
||||
packetStream.skipRawData(numBytesForPacketHeader(statsPacket));
|
||||
QJsonObject packetJson = JSONBreakableMarshal::fromStringBuffer(statsPacket.mid(numBytesForPacketHeader(statsPacket)));
|
||||
|
||||
QVariantMap unpackedVariantMap;
|
||||
|
||||
packetStream >> unpackedVariantMap;
|
||||
|
||||
QJsonObject unpackedStatsJSON = QJsonObject::fromVariantMap(unpackedVariantMap);
|
||||
_statsJSONObject = mergeJSONStatsFromNewObject(unpackedStatsJSON, _statsJSONObject);
|
||||
// _statsJSONObject = mergeJSONStatsFromNewObject(unpackedStatsJSON, _statsJSONObject);
|
||||
}
|
||||
|
||||
QJsonObject DomainServerNodeData::mergeJSONStatsFromNewObject(const QJsonObject& newObject, QJsonObject destinationObject) {
|
||||
|
|
|
@ -96,3 +96,33 @@ QString JSONBreakableMarshal::toString(const QJsonValue& jsonValue, const QStrin
|
|||
|
||||
return QString("%1=%2").arg(keypath, valueAsString);
|
||||
}
|
||||
|
||||
QJsonObject JSONBreakableMarshal::fromStringBuffer(const QByteArray& buffer) {
|
||||
QJsonObject result;
|
||||
|
||||
// this is a packet of strings sep by null terminators - pull out each string and create a stringlist
|
||||
QStringList packetList;
|
||||
int currentIndex = 0;
|
||||
int currentSeparator = buffer.indexOf('\0');
|
||||
|
||||
while (currentIndex < buffer.size() - 1) {
|
||||
packetList << QString::fromUtf8(buffer.mid(currentIndex, currentSeparator));
|
||||
|
||||
if (currentSeparator == -1) {
|
||||
// no more separators to be found, break out of here so we're not looping for nothing
|
||||
break;
|
||||
}
|
||||
|
||||
// bump the currentIndex up to the last found separator
|
||||
currentIndex = currentSeparator + 1;
|
||||
|
||||
// find the index of the next separator, assuming this one wasn't the last one in the packet
|
||||
if (currentSeparator < buffer.size() - 1) {
|
||||
currentSeparator = buffer.indexOf('\0', currentIndex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
return result;
|
||||
}
|
||||
|
|
|
@ -22,6 +22,7 @@ class JSONBreakableMarshal {
|
|||
public:
|
||||
static QStringList toStringList(const QJsonValue& jsonValue, const QString& keypath);
|
||||
static QString toString(const QJsonValue& jsonValue, const QString& keyPath);
|
||||
static QJsonObject fromStringBuffer(const QByteArray& buffer);
|
||||
};
|
||||
|
||||
#endif // hifi_JSONBreakableMarshal_h
|
||||
|
|
|
@ -78,7 +78,8 @@ qint64 NodeList::sendStats(const QJsonObject& statsObject, const HifiSockAddr& d
|
|||
|
||||
// enumerate the resulting strings - pack them and send off packets once we hit MTU size
|
||||
foreach(const QString& statsItem, statsStringList) {
|
||||
QByteArray utf8String = statsItem.toUtf8();
|
||||
QByteArray utf8String = statsItem.toUtf8();
|
||||
utf8String.append('\0');
|
||||
|
||||
if (numBytesWritten + utf8String.size() > MAX_PACKET_SIZE) {
|
||||
// send off the current packet since the next string will make us too big
|
||||
|
|
Loading…
Reference in a new issue