fix segmented write logic in PacketList

This commit is contained in:
Stephen Birarda 2015-10-08 15:29:42 -07:00
parent 26c40adecb
commit dc4f987a2e

View file

@ -176,26 +176,42 @@ qint64 PacketList::writeData(const char* data, qint64 maxSize) {
// 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
int numBytesToEnd = _currentPacket->bytesAvailableForWrite();
if ((newPacket->size() - numBytesToEnd) < sizeRemaining) {
int segmentSize = _currentPacket->pos() - _segmentStartIndex;
if (segmentSize + sizeRemaining > newPacket->getPayloadCapacity()) {
// 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"
<< "larger than the payload size.";
Q_ASSERT(false);
// we won't be writing this new data to the packet go back before the current segment
// and return -1 to indicate error
_currentPacket->seek(_segmentStartIndex);
_currentPacket->setPayloadSize(_segmentStartIndex);
return -1;
} else {
// 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;
// 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);
}
if (sizeRemaining > newPacket->getPayloadCapacity()) {
// this is an unsupported case - attempting to write a block of data larger
// than the capacity of a new packet in an unordered PacketList
qDebug() << "Error in PacketList::writeData - attempted to write data to an unordered packet that is"
<< "larger than the payload size.";
Q_ASSERT(false);
// return -1 to indicate error
return -1;
}
// move the current packet to our list of packets