Update PacketList::writeData() to be non-recursive

This commit is contained in:
Ryan Huffman 2015-08-25 14:58:56 -07:00
parent 0352782e46
commit c1b9613a30

View file

@ -98,76 +98,84 @@ QByteArray PacketList::getMessage() {
} }
qint64 PacketList::writeData(const char* data, qint64 maxSize) { qint64 PacketList::writeData(const char* data, qint64 maxSize) {
if (!_currentPacket) { auto sizeRemaining = maxSize;
// 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 while (sizeRemaining > 0) {
if (maxSize <= _currentPacket->bytesAvailableForWrite()) { if (!_currentPacket) {
// it fits, just write it to the current packet // we don't have a current packet, time to set one up
_currentPacket->write(data, maxSize); _currentPacket = createPacketWithExtendedHeader();
}
// return the number of bytes written // check if this block of data can fit into the currentPacket
return maxSize; if (sizeRemaining <= _currentPacket->bytesAvailableForWrite()) {
} else { // it fits, just write it to the current packet
// it does not fit - this may need to be in the next packet _currentPacket->write(data, sizeRemaining);
if (!_isOrdered) { sizeRemaining = 0;
auto newPacket = createPacketWithExtendedHeader(); } else {
// it does not fit - this may need to be in the next packet
if (_segmentStartIndex >= 0) { if (!_isOrdered) {
// We in the process of writing a segment for an unordered PacketList. auto newPacket = createPacketWithExtendedHeader();
// We need to try and pull the first part of the segment out to our new packet
// check now to see if this is an unsupported write if (_segmentStartIndex >= 0) {
int numBytesToEnd = _currentPacket->bytesAvailableForWrite(); // We in the process of writing a segment for an unordered PacketList.
// We need to try and pull the first part of the segment out to our new packet
if ((newPacket->size() - numBytesToEnd) < maxSize) { // check now to see if this is an unsupported write
// this is an unsupported case - the segment is bigger than the size of an individual packet int numBytesToEnd = _currentPacket->bytesAvailableForWrite();
// 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" if ((newPacket->size() - numBytesToEnd) < sizeRemaining) {
<< "larger than the payload size."; // this is an unsupported case - the segment is bigger than the size of an individual packet
Q_ASSERT(false); // 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"
<< "larger than the payload size.";
Q_ASSERT(false);
}
int segmentSize = _currentPacket->pos() - _segmentStartIndex;
// copy from currentPacket where the segment started to the beginning of the newPacket
newPacket->write(_currentPacket->getPayload() + _segmentStartIndex, segmentSize);
// the current segment now starts at the beginning of the new packet
_segmentStartIndex = _extendedHeader.size();
// shrink the current payload to the actual size of the packet
_currentPacket->setPayloadSize(_segmentStartIndex);
} }
int segmentSize = _currentPacket->pos() - _segmentStartIndex; // move the current packet to our list of packets
_packets.push_back(std::move(_currentPacket));
// copy from currentPacket where the segment started to the beginning of the newPacket // write the data to the newPacket
newPacket->write(_currentPacket->getPayload() + _segmentStartIndex, segmentSize); newPacket->write(data, sizeRemaining);
// the current segment now starts at the beginning of the new packet // swap our current packet with the new packet
_segmentStartIndex = _extendedHeader.size(); _currentPacket.swap(newPacket);
// shrink the current payload to the actual size of the packet // We've written all of the data, so set sizeRemaining to 0
_currentPacket->setPayloadSize(_segmentStartIndex); 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
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));
} }
// move the current packet to our list of packets
_packets.push_back(std::move(_currentPacket));
// write the data to the newPacket
newPacket->write(data, maxSize);
// swap our current packet with the new packet
_currentPacket.swap(newPacket);
// return the number of bytes written to the new packet
return maxSize;
} 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
int numBytesToEnd = _currentPacket->bytesAvailableForWrite();
_currentPacket->write(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) { void PacketList::closeCurrentPacket(bool shouldSendEmpty) {