mirror of
https://github.com/HifiExperiments/overte.git
synced 2025-08-18 13:01:18 +02:00
change readPacketType and readPacketTypeVersion
This commit is contained in:
parent
b983f662ee
commit
4fe6102a7c
2 changed files with 39 additions and 39 deletions
|
@ -33,7 +33,7 @@ std::unique_ptr<Packet> Packet::create(PacketType::Value type, qint64 size) {
|
|||
// Invalid size, return null pointer
|
||||
return std::unique_ptr<Packet>();
|
||||
}
|
||||
|
||||
|
||||
// allocate memory
|
||||
return std::unique_ptr<Packet>(new Packet(type, size));
|
||||
}
|
||||
|
@ -59,10 +59,10 @@ Packet::Packet(PacketType::Value type, qint64 size) :
|
|||
_capacity(size) {
|
||||
// Sanity check
|
||||
Q_ASSERT(size <= maxPayloadSize(type));
|
||||
|
||||
|
||||
// copy packet type and version in header
|
||||
writePacketTypeAndVersion(type);
|
||||
|
||||
|
||||
// Set control bit and sequence number to 0 if necessary
|
||||
if (SEQUENCE_NUMBERED_PACKETS.contains(type)) {
|
||||
writeSequenceNumber(0);
|
||||
|
@ -77,12 +77,12 @@ Packet& Packet::operator=(const Packet& other) {
|
|||
_packetSize = other._packetSize;
|
||||
_packet = std::unique_ptr<char>(new char(_packetSize));
|
||||
memcpy(_packet.get(), other._packet.get(), _packetSize);
|
||||
|
||||
|
||||
_payloadStart = _packet.get() + (other._payloadStart - other._packet.get());
|
||||
_capacity = other._capacity;
|
||||
|
||||
|
||||
_sizeUsed = other._sizeUsed;
|
||||
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
@ -93,12 +93,12 @@ Packet::Packet(Packet&& other) {
|
|||
Packet& Packet::operator=(Packet&& other) {
|
||||
_packetSize = other._packetSize;
|
||||
_packet = std::move(other._packet);
|
||||
|
||||
|
||||
_payloadStart = other._payloadStart;
|
||||
_capacity = other._capacity;
|
||||
|
||||
|
||||
_sizeUsed = other._sizeUsed;
|
||||
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
@ -106,16 +106,16 @@ void Packet::setPacketType(PacketType::Value type) {
|
|||
auto currentHeaderSize = totalHeadersSize();
|
||||
_type = type;
|
||||
writePacketTypeAndVersion(_type);
|
||||
|
||||
|
||||
// Setting new packet type with a different header size not currently supported
|
||||
Q_ASSERT(currentHeaderSize == totalHeadersSize());
|
||||
}
|
||||
|
||||
PacketType::Value Packet::readPacketType() const {
|
||||
PacketType::Value Packet::readType() const {
|
||||
return (PacketType::Value)arithmeticCodingValueFromBuffer(_packet.get());
|
||||
}
|
||||
|
||||
PacketVersion Packet::readPacketTypeVersion() const {
|
||||
PacketVersion Packet::readVersion() const {
|
||||
return *reinterpret_cast<PacketVersion*>(_packet.get() + numBytesForArithmeticCodedPacketType(_type));
|
||||
}
|
||||
|
||||
|
@ -142,7 +142,7 @@ bool Packet::readIsControlPacket() const {
|
|||
void Packet::writePacketTypeAndVersion(PacketType::Value type) {
|
||||
// Pack the packet type
|
||||
auto offset = packArithmeticallyCodedValue(type, _packet.get());
|
||||
|
||||
|
||||
// Pack the packet version
|
||||
auto version = versionForPacketType(type);
|
||||
memcpy(_packet.get() + offset, &version, sizeof(version));
|
||||
|
@ -169,16 +169,16 @@ qint64 Packet::writeData(const char* data, qint64 maxSize) {
|
|||
// make sure we have the space required to write this block
|
||||
if (maxSize <= bytesAvailable()) {
|
||||
qint64 currentPos = pos();
|
||||
|
||||
|
||||
// good to go - write the data
|
||||
memcpy(_payloadStart + currentPos, data, maxSize);
|
||||
|
||||
|
||||
// seek to the new position based on where our write just finished
|
||||
seek(currentPos + maxSize);
|
||||
|
||||
|
||||
// keep track of _sizeUsed so we can just write the actual data when packet is about to be sent
|
||||
_sizeUsed = std::max(pos() + 1, _sizeUsed);
|
||||
|
||||
|
||||
// return the number of bytes written
|
||||
return maxSize;
|
||||
} else {
|
||||
|
@ -190,16 +190,16 @@ qint64 Packet::writeData(const char* data, qint64 maxSize) {
|
|||
qint64 Packet::readData(char* dest, qint64 maxSize) {
|
||||
// we're either reading what is left from the current position or what was asked to be read
|
||||
qint64 numBytesToRead = std::min(bytesAvailable(), maxSize);
|
||||
|
||||
|
||||
if (numBytesToRead > 0) {
|
||||
int currentPosition = pos();
|
||||
|
||||
|
||||
// read out the data
|
||||
memcpy(dest, _payloadStart + currentPosition, numBytesToRead);
|
||||
|
||||
|
||||
// seek to the end of the read
|
||||
seek(currentPosition + numBytesToRead);
|
||||
}
|
||||
|
||||
|
||||
return numBytesToRead;
|
||||
}
|
||||
|
|
|
@ -21,47 +21,47 @@
|
|||
class Packet : public QIODevice {
|
||||
public:
|
||||
using SequenceNumber = uint16_t;
|
||||
|
||||
|
||||
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 maxPayloadSize(PacketType::Value type);
|
||||
|
||||
|
||||
virtual qint64 totalHeadersSize() const; // Cumulated size of all the headers
|
||||
virtual qint64 localHeaderSize() const; // Current level's header size
|
||||
|
||||
|
||||
// Payload direct access to the payload, use responsibly!
|
||||
char* getPayload() { 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(); }
|
||||
|
||||
|
||||
PacketType::Value getPacketType() const { return _type; }
|
||||
void setPacketType(PacketType::Value type);
|
||||
|
||||
|
||||
qint64 getSizeWithHeader() const { return localHeaderSize() + getSizeUsed(); }
|
||||
qint64 getSizeUsed() const { return _sizeUsed; }
|
||||
void setSizeUsed(qint64 sizeUsed) { _sizeUsed = sizeUsed; }
|
||||
|
||||
// Header readers
|
||||
PacketType::Value readPacketType() const;
|
||||
PacketVersion readPacketTypeVersion() const;
|
||||
PacketType::Value readType() const;
|
||||
PacketVersion readVersion() const;
|
||||
SequenceNumber readSequenceNumber() const;
|
||||
bool readIsControlPacket() const;
|
||||
|
||||
|
||||
// QIODevice virtual functions
|
||||
// WARNING: Those methods all refer to the payload ONLY and NOT the entire packet
|
||||
virtual bool isSequential() const { return false; }
|
||||
virtual bool reset() { setSizeUsed(0); return QIODevice::reset(); }
|
||||
virtual qint64 size() const { return _capacity; }
|
||||
|
||||
|
||||
template<typename T> qint64 read(T* data);
|
||||
template<typename T> qint64 write(const T& data);
|
||||
|
||||
|
||||
protected:
|
||||
Packet(PacketType::Value type, int64_t size);
|
||||
Packet(const Packet& other);
|
||||
|
@ -72,20 +72,20 @@ protected:
|
|||
// QIODevice virtual functions
|
||||
virtual qint64 writeData(const char* data, qint64 maxSize);
|
||||
virtual qint64 readData(char* data, qint64 maxSize);
|
||||
|
||||
|
||||
// Header writers
|
||||
void writePacketTypeAndVersion(PacketType::Value type);
|
||||
void writeSequenceNumber(SequenceNumber seqNum);
|
||||
|
||||
|
||||
PacketType::Value _type; // Packet type
|
||||
|
||||
|
||||
qint64 _packetSize = 0; // Total size of the allocated memory
|
||||
std::unique_ptr<char> _packet; // Allocated memory
|
||||
|
||||
|
||||
char* _payloadStart = nullptr; // Start of the payload
|
||||
qint64 _capacity = 0; // Total capacity of the payload
|
||||
|
||||
|
||||
qint64 _sizeUsed = 0; // How much of the payload is actually used
|
||||
};
|
||||
|
||||
#endif // hifi_Packet_h
|
||||
#endif // hifi_Packet_h
|
||||
|
|
Loading…
Reference in a new issue