mirror of
https://github.com/HifiExperiments/overte.git
synced 2025-08-09 05:48:26 +02:00
Added Packet::createCopy and getData
This commit is contained in:
parent
422bef4e18
commit
11c58ebc9e
2 changed files with 22 additions and 24 deletions
|
@ -38,6 +38,12 @@ std::unique_ptr<Packet> Packet::create(PacketType::Value type, qint64 size) {
|
||||||
return std::unique_ptr<Packet>(new Packet(type, size));
|
return std::unique_ptr<Packet>(new Packet(type, size));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
std::unique_ptr<Packet> Packet::createCopy(const std::unique_ptr<Packet>& other) {
|
||||||
|
Q_ASSERT(other);
|
||||||
|
return std::unique_ptr<Packet>(new Packet(*other));
|
||||||
|
}
|
||||||
|
|
||||||
qint64 Packet::totalHeadersSize() const {
|
qint64 Packet::totalHeadersSize() const {
|
||||||
return localHeaderSize();
|
return localHeaderSize();
|
||||||
}
|
}
|
||||||
|
@ -74,7 +80,6 @@ Packet& Packet::operator=(const Packet& other) {
|
||||||
memcpy(_packet.get(), other._packet.get(), _packetSize);
|
memcpy(_packet.get(), other._packet.get(), _packetSize);
|
||||||
|
|
||||||
_payloadStart = _packet.get() + (other._payloadStart - other._packet.get());
|
_payloadStart = _packet.get() + (other._payloadStart - other._packet.get());
|
||||||
_position = other._position;
|
|
||||||
_capacity = other._capacity;
|
_capacity = other._capacity;
|
||||||
|
|
||||||
_sizeUsed = other._sizeUsed;
|
_sizeUsed = other._sizeUsed;
|
||||||
|
@ -91,7 +96,6 @@ Packet& Packet::operator=(Packet&& other) {
|
||||||
_packet = std::move(other._packet);
|
_packet = std::move(other._packet);
|
||||||
|
|
||||||
_payloadStart = other._payloadStart;
|
_payloadStart = other._payloadStart;
|
||||||
_position = other._position;
|
|
||||||
_capacity = other._capacity;
|
_capacity = other._capacity;
|
||||||
|
|
||||||
_sizeUsed = other._sizeUsed;
|
_sizeUsed = other._sizeUsed;
|
||||||
|
@ -132,7 +136,7 @@ void Packet::setPacketTypeAndVersion(PacketType::Value type) {
|
||||||
auto offset = packArithmeticallyCodedValue(type, _packet.get());
|
auto offset = packArithmeticallyCodedValue(type, _packet.get());
|
||||||
|
|
||||||
// Pack the packet version
|
// Pack the packet version
|
||||||
auto version { versionForPacketType(type) };
|
auto version = versionForPacketType(type);
|
||||||
memcpy(_packet.get() + offset, &version, sizeof(version));
|
memcpy(_packet.get() + offset, &version, sizeof(version));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -144,14 +148,6 @@ void Packet::setSequenceNumber(SequenceNumber seqNum) {
|
||||||
&seqNum, sizeof(seqNum));
|
&seqNum, sizeof(seqNum));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Packet::seek(qint64 pos) {
|
|
||||||
bool valid = (pos >= 0) && (pos < size());
|
|
||||||
if (valid) {
|
|
||||||
_position = pos;
|
|
||||||
}
|
|
||||||
return valid;
|
|
||||||
}
|
|
||||||
|
|
||||||
static const qint64 PACKET_WRITE_ERROR = -1;
|
static const qint64 PACKET_WRITE_ERROR = -1;
|
||||||
qint64 Packet::writeData(const char* data, qint64 maxSize) {
|
qint64 Packet::writeData(const char* data, qint64 maxSize) {
|
||||||
// make sure we have the space required to write this block
|
// make sure we have the space required to write this block
|
||||||
|
|
|
@ -23,6 +23,8 @@ public:
|
||||||
using SequenceNumber = uint16_t;
|
using SequenceNumber = uint16_t;
|
||||||
|
|
||||||
static std::unique_ptr<Packet> create(PacketType::Value type, int64_t size = -1);
|
static std::unique_ptr<Packet> create(PacketType::Value type, int64_t size = -1);
|
||||||
|
// Provided for convenience, try to limit use
|
||||||
|
static std::unique_ptr<Packet> createCopy(const std::unique_ptr<Packet>& other);
|
||||||
|
|
||||||
static qint64 localHeaderSize(PacketType::Value type);
|
static qint64 localHeaderSize(PacketType::Value type);
|
||||||
static qint64 maxPayloadSize(PacketType::Value type);
|
static qint64 maxPayloadSize(PacketType::Value type);
|
||||||
|
@ -30,10 +32,14 @@ public:
|
||||||
virtual qint64 totalHeadersSize() const; // Cumulated size of all the headers
|
virtual qint64 totalHeadersSize() const; // Cumulated size of all the headers
|
||||||
virtual qint64 localHeaderSize() const; // Current level's header size
|
virtual qint64 localHeaderSize() const; // Current level's header size
|
||||||
|
|
||||||
// Payload direct access, use responsibly!
|
// Payload direct access to the payload, use responsibly!
|
||||||
char* getPayload() { return _payloadStart; }
|
char* getPayload() { return _payloadStart; }
|
||||||
const char* getPayload() const { return _payloadStart; }
|
const char* getPayload() const { return _payloadStart; }
|
||||||
|
|
||||||
|
// Return direct access to the entire packet, use responsibly!
|
||||||
|
char* getData() { return _packet.get(); }
|
||||||
|
const char* getData() const { return _packet.get(); }
|
||||||
|
|
||||||
qint64 getSizeWithHeader() const { return localHeaderSize() + getSizeUsed(); }
|
qint64 getSizeWithHeader() const { return localHeaderSize() + getSizeUsed(); }
|
||||||
qint64 getSizeUsed() const { return _sizeUsed; }
|
qint64 getSizeUsed() const { return _sizeUsed; }
|
||||||
void setSizeUsed(qint64 sizeUsed) { _sizeUsed = sizeUsed; }
|
void setSizeUsed(qint64 sizeUsed) { _sizeUsed = sizeUsed; }
|
||||||
|
@ -46,21 +52,12 @@ public:
|
||||||
|
|
||||||
// QIODevice virtual functions
|
// QIODevice virtual functions
|
||||||
// WARNING: Those methods all refer to the payload ONLY and NOT the entire packet
|
// WARNING: Those methods all refer to the payload ONLY and NOT the entire packet
|
||||||
virtual bool atEnd() const { return _position == _capacity; }
|
|
||||||
virtual qint64 bytesAvailable() const { return size() - pos(); }
|
|
||||||
virtual bool canReadLine() const { return false; }
|
|
||||||
virtual bool isSequential() const { return false; }
|
virtual bool isSequential() const { return false; }
|
||||||
virtual qint64 pos() const { return _position; }
|
virtual bool reset() { setSizeUsed(0); return QIODevice::reset(); }
|
||||||
virtual bool reset() { return seek(0); }
|
|
||||||
virtual bool seek(qint64 pos);
|
|
||||||
virtual qint64 size() const { return _capacity; }
|
virtual qint64 size() const { return _capacity; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Packet(PacketType::Value type, int64_t size);
|
Packet(PacketType::Value type, int64_t size);
|
||||||
Packet(const Packet& other);
|
|
||||||
Packet& operator=(const Packet& other);
|
|
||||||
Packet(Packet&& other);
|
|
||||||
Packet& operator=(Packet&& other);
|
|
||||||
|
|
||||||
// QIODevice virtual functions
|
// QIODevice virtual functions
|
||||||
virtual qint64 writeData(const char* data, qint64 maxSize);
|
virtual qint64 writeData(const char* data, qint64 maxSize);
|
||||||
|
@ -70,16 +67,21 @@ protected:
|
||||||
void setPacketTypeAndVersion(PacketType::Value type);
|
void setPacketTypeAndVersion(PacketType::Value type);
|
||||||
void setSequenceNumber(SequenceNumber seqNum);
|
void setSequenceNumber(SequenceNumber seqNum);
|
||||||
|
|
||||||
PacketType::Value _type;
|
PacketType::Value _type; // Packet type
|
||||||
|
|
||||||
qint64 _packetSize = 0; // Total size of the allocated memory
|
qint64 _packetSize = 0; // Total size of the allocated memory
|
||||||
std::unique_ptr<char> _packet; // Allocated memory
|
std::unique_ptr<char> _packet; // Allocated memory
|
||||||
|
|
||||||
char* _payloadStart = nullptr; // Start of the payload
|
char* _payloadStart = nullptr; // Start of the payload
|
||||||
qint64 _position = 0; // Current position in the payload
|
|
||||||
qint64 _capacity = 0; // Total capacity of the payload
|
qint64 _capacity = 0; // Total capacity of the payload
|
||||||
|
|
||||||
qint64 _sizeUsed = 0; // How much of the payload is actually used
|
qint64 _sizeUsed = 0; // How much of the payload is actually used
|
||||||
|
|
||||||
|
private:
|
||||||
|
Packet(const Packet& other);
|
||||||
|
Packet& operator=(const Packet& other);
|
||||||
|
Packet(Packet&& other);
|
||||||
|
Packet& operator=(Packet&& other);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // hifi_Packet_h
|
#endif // hifi_Packet_h
|
Loading…
Reference in a new issue