mirror of
https://github.com/overte-org/overte.git
synced 2025-04-20 18:23:54 +02:00
Make PacketList class NLPacketList inherits from
This commit is contained in:
parent
44c5d4a2bf
commit
eb56999077
4 changed files with 178 additions and 141 deletions
|
@ -11,100 +11,12 @@
|
|||
|
||||
#include "NLPacketList.h"
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
NLPacketList::NLPacketList(PacketType::Value packetType) :
|
||||
_packetType(packetType)
|
||||
{
|
||||
#include "NLPacket.h"
|
||||
|
||||
NLPacketList::NLPacketList(PacketType::Value packetType) : PacketList(packetType) {
|
||||
}
|
||||
|
||||
std::unique_ptr<NLPacket> NLPacketList::createPacketWithExtendedHeader() {
|
||||
// use the static create method to create a new packet
|
||||
auto packet = NLPacket::create(_packetType);
|
||||
|
||||
// add the extended header to the front of the packet
|
||||
if (packet->write(_extendedHeader) == -1) {
|
||||
qDebug() << "Could not write extendedHeader in PacketList::createPacketWithExtendedHeader"
|
||||
<< "- make sure that _extendedHeader is not larger than the payload capacity.";
|
||||
}
|
||||
|
||||
return packet;
|
||||
}
|
||||
|
||||
qint64 NLPacketList::writeData(const char* data, qint64 maxSize) {
|
||||
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->bytesAvailable()) {
|
||||
// it fits, just write it to the current packet
|
||||
_currentPacket->write(data, maxSize);
|
||||
|
||||
// return the number of bytes written
|
||||
return maxSize;
|
||||
} else {
|
||||
// it does not fit - this may need to be in the next packet
|
||||
|
||||
if (!_isOrdered) {
|
||||
auto newPacket = createPacketWithExtendedHeader();
|
||||
|
||||
if (_segmentStartIndex >= 0) {
|
||||
// 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
|
||||
|
||||
// check now to see if this is an unsupported write
|
||||
int numBytesToEnd = _currentPacket->bytesAvailable();
|
||||
|
||||
if ((newPacket->size() - numBytesToEnd) < maxSize) {
|
||||
// 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);
|
||||
}
|
||||
|
||||
// copy from currentPacket where the segment started to the beginning of the newPacket
|
||||
newPacket->write(_currentPacket->getPayload() + _segmentStartIndex, numBytesToEnd);
|
||||
|
||||
// the current segment now starts at the beginning of the new packet
|
||||
_segmentStartIndex = 0;
|
||||
|
||||
// shrink the current payload to the actual size of the packet
|
||||
_currentPacket->setSizeUsed(_segmentStartIndex);
|
||||
}
|
||||
|
||||
// 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->bytesAvailable();
|
||||
_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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void NLPacketList::closeCurrentPacket() {
|
||||
// move the current packet to our list of packets
|
||||
_packets.push_back(std::move(_currentPacket));
|
||||
std::unique_ptr<Packet> NLPacketList::createPacket() {
|
||||
return std::move(NLPacket::create(getType()));
|
||||
}
|
||||
|
||||
|
|
|
@ -12,61 +12,17 @@
|
|||
#ifndef hifi_NLPacketList_h
|
||||
#define hifi_NLPacketList_h
|
||||
|
||||
#include <QtCore/QIODevice>
|
||||
#include "udt/PacketList.h"
|
||||
|
||||
#include "PacketHeaders.h"
|
||||
|
||||
#include "NLPacket.h"
|
||||
|
||||
class NLPacketList : public QIODevice {
|
||||
Q_OBJECT
|
||||
class NLPacketList : public PacketList {
|
||||
public:
|
||||
NLPacketList(PacketType::Value packetType);
|
||||
|
||||
virtual bool isSequential() const { return true; }
|
||||
|
||||
void startSegment() { _segmentStartIndex = _currentPacket->pos(); }
|
||||
void endSegment() { _segmentStartIndex = -1; }
|
||||
|
||||
int getNumPackets() const { return _packets.size() + (_currentPacket ? 1 : 0); }
|
||||
|
||||
void closeCurrentPacket();
|
||||
|
||||
void setExtendedHeader(const QByteArray& extendedHeader) { _extendedHeader = extendedHeader; }
|
||||
|
||||
template<typename T> qint64 readPrimitive(T* data);
|
||||
template<typename T> qint64 writePrimitive(const T& data);
|
||||
protected:
|
||||
virtual qint64 writeData(const char* data, qint64 maxSize);
|
||||
virtual qint64 readData(char* data, qint64 maxSize) { return 0; }
|
||||
|
||||
private:
|
||||
friend class LimitedNodeList;
|
||||
|
||||
private:
|
||||
NLPacketList(const NLPacketList& other) = delete;
|
||||
NLPacketList& operator=(const NLPacketList& other) = delete;
|
||||
|
||||
std::unique_ptr<NLPacket> createPacketWithExtendedHeader();
|
||||
|
||||
PacketType::Value _packetType;
|
||||
bool _isOrdered = false;
|
||||
|
||||
std::unique_ptr<NLPacket> _currentPacket;
|
||||
std::list<std::unique_ptr<NLPacket>> _packets;
|
||||
|
||||
int _segmentStartIndex = -1;
|
||||
|
||||
QByteArray _extendedHeader;
|
||||
|
||||
virtual std::unique_ptr<Packet> createPacket();
|
||||
};
|
||||
|
||||
template <typename T> qint64 NLPacketList::readPrimitive(T* data) {
|
||||
return QIODevice::read(reinterpret_cast<char*>(data), sizeof(T));
|
||||
}
|
||||
|
||||
template <typename T> qint64 NLPacketList::writePrimitive(const T& data) {
|
||||
static_assert(!std::is_pointer<T>::value, "T must not be a pointer");
|
||||
return QIODevice::write(reinterpret_cast<const char*>(&data), sizeof(T));
|
||||
}
|
||||
|
||||
|
||||
#endif // hifi_PacketList_h
|
||||
|
|
|
@ -10,3 +10,114 @@
|
|||
//
|
||||
|
||||
#include "PacketList.h"
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
#include "Packet.h"
|
||||
|
||||
PacketList::PacketList(PacketType::Value packetType) : _packetType(packetType) {
|
||||
}
|
||||
|
||||
void PacketList::startSegment() {
|
||||
_segmentStartIndex = _currentPacket->pos();
|
||||
}
|
||||
|
||||
void PacketList::endSegment() {
|
||||
_segmentStartIndex = -1;
|
||||
}
|
||||
|
||||
std::unique_ptr<Packet> PacketList::createPacket() {
|
||||
// use the static create method to create a new packet
|
||||
return std::move(Packet::create(getType()));
|
||||
}
|
||||
|
||||
std::unique_ptr<Packet> PacketList::createPacketWithExtendedHeader() {
|
||||
// use the static create method to create a new packet
|
||||
auto packet = createPacket();
|
||||
|
||||
if (!_extendedHeader.isEmpty()) {
|
||||
// add the extended header to the front of the packet
|
||||
if (packet->write(_extendedHeader) == -1) {
|
||||
qDebug() << "Could not write extendedHeader in PacketList::createPacketWithExtendedHeader"
|
||||
<< "- make sure that _extendedHeader is not larger than the payload capacity.";
|
||||
}
|
||||
}
|
||||
|
||||
return std::move(packet);
|
||||
}
|
||||
|
||||
qint64 PacketList::writeData(const char* data, qint64 maxSize) {
|
||||
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->bytesAvailable()) {
|
||||
// it fits, just write it to the current packet
|
||||
_currentPacket->write(data, maxSize);
|
||||
|
||||
// return the number of bytes written
|
||||
return maxSize;
|
||||
} else {
|
||||
// it does not fit - this may need to be in the next packet
|
||||
|
||||
if (!_isOrdered) {
|
||||
auto newPacket = createPacketWithExtendedHeader();
|
||||
|
||||
if (_segmentStartIndex >= 0) {
|
||||
// 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
|
||||
|
||||
// check now to see if this is an unsupported write
|
||||
int numBytesToEnd = _currentPacket->bytesAvailable();
|
||||
|
||||
if ((newPacket->size() - numBytesToEnd) < maxSize) {
|
||||
// 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);
|
||||
}
|
||||
|
||||
// copy from currentPacket where the segment started to the beginning of the newPacket
|
||||
newPacket->write(_currentPacket->getPayload() + _segmentStartIndex, numBytesToEnd);
|
||||
|
||||
// the current segment now starts at the beginning of the new packet
|
||||
_segmentStartIndex = 0;
|
||||
|
||||
// shrink the current payload to the actual size of the packet
|
||||
_currentPacket->setSizeUsed(_segmentStartIndex);
|
||||
}
|
||||
|
||||
// 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->bytesAvailable();
|
||||
_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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void PacketList::closeCurrentPacket() {
|
||||
// move the current packet to our list of packets
|
||||
_packets.push_back(std::move(_currentPacket));
|
||||
}
|
||||
|
|
|
@ -12,4 +12,62 @@
|
|||
#ifndef hifi_PacketList_h
|
||||
#define hifi_PacketList_h
|
||||
|
||||
#include <QtCore/QIODevice>
|
||||
|
||||
#include "PacketHeaders.h"
|
||||
|
||||
class Packet;
|
||||
|
||||
class PacketList : public QIODevice {
|
||||
Q_OBJECT
|
||||
public:
|
||||
PacketList(PacketType::Value packetType);
|
||||
|
||||
virtual bool isSequential() const { return true; }
|
||||
|
||||
void startSegment();
|
||||
void endSegment();
|
||||
|
||||
PacketType::Value getType() const { return _packetType; }
|
||||
int getNumPackets() const { return _packets.size() + (_currentPacket ? 1 : 0); }
|
||||
|
||||
void closeCurrentPacket();
|
||||
|
||||
void setExtendedHeader(const QByteArray& extendedHeader) { _extendedHeader = extendedHeader; }
|
||||
|
||||
template<typename T> qint64 readPrimitive(T* data);
|
||||
template<typename T> qint64 writePrimitive(const T& data);
|
||||
protected:
|
||||
virtual qint64 writeData(const char* data, qint64 maxSize);
|
||||
virtual qint64 readData(char* data, qint64 maxSize) { return 0; }
|
||||
|
||||
private:
|
||||
friend class LimitedNodeList;
|
||||
|
||||
PacketList(const PacketList& other) = delete;
|
||||
PacketList& operator=(const PacketList& other) = delete;
|
||||
|
||||
virtual std::unique_ptr<Packet> createPacket();
|
||||
std::unique_ptr<Packet> createPacketWithExtendedHeader();
|
||||
|
||||
PacketType::Value _packetType;
|
||||
bool _isOrdered = false;
|
||||
|
||||
std::unique_ptr<Packet> _currentPacket;
|
||||
std::list<std::unique_ptr<Packet>> _packets;
|
||||
|
||||
int _segmentStartIndex = -1;
|
||||
|
||||
QByteArray _extendedHeader;
|
||||
};
|
||||
|
||||
template <typename T> qint64 PacketList::readPrimitive(T* data) {
|
||||
return QIODevice::read(reinterpret_cast<char*>(data), sizeof(T));
|
||||
}
|
||||
|
||||
template <typename T> qint64 PacketList::writePrimitive(const T& data) {
|
||||
static_assert(!std::is_pointer<T>::value, "T must not be a pointer");
|
||||
return QIODevice::write(reinterpret_cast<const char*>(&data), sizeof(T));
|
||||
}
|
||||
|
||||
#endif // hifi_PacketList_h
|
Loading…
Reference in a new issue