mirror of
https://github.com/overte-org/overte.git
synced 2025-07-16 02:56:37 +02:00
Update PacketList::writeData() to be non-recursive
This commit is contained in:
parent
0352782e46
commit
c1b9613a30
1 changed files with 78 additions and 70 deletions
|
@ -98,18 +98,20 @@ QByteArray PacketList::getMessage() {
|
|||
}
|
||||
|
||||
qint64 PacketList::writeData(const char* data, qint64 maxSize) {
|
||||
auto sizeRemaining = maxSize;
|
||||
|
||||
while (sizeRemaining > 0) {
|
||||
if (!_currentPacket) {
|
||||
// we don't have a current packet, time to set one up
|
||||
_currentPacket = createPacketWithExtendedHeader();
|
||||
}
|
||||
|
||||
// check if this block of data can fit into the currentPacket
|
||||
if (maxSize <= _currentPacket->bytesAvailableForWrite()) {
|
||||
if (sizeRemaining <= _currentPacket->bytesAvailableForWrite()) {
|
||||
// it fits, just write it to the current packet
|
||||
_currentPacket->write(data, maxSize);
|
||||
_currentPacket->write(data, sizeRemaining);
|
||||
|
||||
// return the number of bytes written
|
||||
return maxSize;
|
||||
sizeRemaining = 0;
|
||||
} else {
|
||||
// it does not fit - this may need to be in the next packet
|
||||
|
||||
|
@ -123,7 +125,7 @@ qint64 PacketList::writeData(const char* data, qint64 maxSize) {
|
|||
// check now to see if this is an unsupported write
|
||||
int numBytesToEnd = _currentPacket->bytesAvailableForWrite();
|
||||
|
||||
if ((newPacket->size() - numBytesToEnd) < maxSize) {
|
||||
if ((newPacket->size() - numBytesToEnd) < sizeRemaining) {
|
||||
// this is an unsupported case - the segment is bigger than the size of an individual packet
|
||||
// but the PacketList is not going to be sent ordered
|
||||
qDebug() << "Error in PacketList::writeData - attempted to write a segment to an unordered packet that is"
|
||||
|
@ -147,13 +149,13 @@ qint64 PacketList::writeData(const char* data, qint64 maxSize) {
|
|||
_packets.push_back(std::move(_currentPacket));
|
||||
|
||||
// write the data to the newPacket
|
||||
newPacket->write(data, maxSize);
|
||||
newPacket->write(data, sizeRemaining);
|
||||
|
||||
// swap our current packet with the new packet
|
||||
_currentPacket.swap(newPacket);
|
||||
|
||||
// return the number of bytes written to the new packet
|
||||
return maxSize;
|
||||
// We've written all of the data, so set sizeRemaining to 0
|
||||
sizeRemaining = 0;
|
||||
} else {
|
||||
// we're an ordered PacketList - let's fit what we can into the current packet and then put the leftover
|
||||
// into a new packet
|
||||
|
@ -161,13 +163,19 @@ qint64 PacketList::writeData(const char* data, qint64 maxSize) {
|
|||
int numBytesToEnd = _currentPacket->bytesAvailableForWrite();
|
||||
_currentPacket->write(data, numBytesToEnd);
|
||||
|
||||
// Remove number of bytes written from sizeRemaining
|
||||
sizeRemaining -= numBytesToEnd;
|
||||
|
||||
// Move the data pointer forward
|
||||
data += numBytesToEnd;
|
||||
|
||||
// move the current packet to our list of packets
|
||||
_packets.push_back(std::move(_currentPacket));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// recursively call our writeData method for the remaining data to write to a new packet
|
||||
return numBytesToEnd + writeData(data + numBytesToEnd, maxSize - numBytesToEnd);
|
||||
}
|
||||
}
|
||||
return maxSize;
|
||||
}
|
||||
|
||||
void PacketList::closeCurrentPacket(bool shouldSendEmpty) {
|
||||
|
|
Loading…
Reference in a new issue