mirror of
https://github.com/overte-org/overte.git
synced 2025-08-10 22:13:12 +02:00
Merge Packet and PacketPayload
This commit is contained in:
parent
a07a24788d
commit
7eddcf383c
5 changed files with 83 additions and 113 deletions
|
@ -41,7 +41,8 @@ std::unique_ptr<Packet> Packet::create(PacketType::Value type, int64_t size) {
|
||||||
Packet::Packet(PacketType::Value type, int64_t size) :
|
Packet::Packet(PacketType::Value type, int64_t size) :
|
||||||
_packetSize(headerSize(type) + size),
|
_packetSize(headerSize(type) + size),
|
||||||
_packet(new char(_packetSize)),
|
_packet(new char(_packetSize)),
|
||||||
_payload(_packet.get() + headerSize(type), size) {
|
_payloadStart(_packet.get() + headerSize(type)),
|
||||||
|
_capacity(size) {
|
||||||
|
|
||||||
// Sanity check
|
// Sanity check
|
||||||
Q_ASSERT(size <= maxPayloadSize(type));
|
Q_ASSERT(size <= maxPayloadSize(type));
|
||||||
|
@ -87,7 +88,7 @@ bool Packet::isControlPacket() const {
|
||||||
|
|
||||||
void Packet::setPacketTypeAndVersion(PacketType::Value type) {
|
void Packet::setPacketTypeAndVersion(PacketType::Value type) {
|
||||||
// Pack the packet type
|
// Pack the packet type
|
||||||
auto offset = packArithmeticallyCodedValue((int)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) };
|
||||||
|
@ -102,3 +103,51 @@ void Packet::setSequenceNumber(SequenceNumber seqNum) {
|
||||||
memcpy(_packet.get() + numBytesForArithmeticCodedPacketType(type) + sizeof(PacketVersion),
|
memcpy(_packet.get() + numBytesForArithmeticCodedPacketType(type) + sizeof(PacketVersion),
|
||||||
&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;
|
||||||
|
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 {
|
||||||
|
// not enough space left for this write - return an error
|
||||||
|
return PACKET_WRITE_ERROR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
|
@ -17,7 +17,7 @@
|
||||||
#include "PacketHeaders.h"
|
#include "PacketHeaders.h"
|
||||||
#include "PacketPayload.h"
|
#include "PacketPayload.h"
|
||||||
|
|
||||||
class Packet {
|
class Packet : public QIODevice {
|
||||||
public:
|
public:
|
||||||
using SequenceNumber = uint16_t;
|
using SequenceNumber = uint16_t;
|
||||||
|
|
||||||
|
@ -26,12 +26,29 @@ public:
|
||||||
|
|
||||||
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);
|
||||||
|
|
||||||
|
|
||||||
|
qint64 getSizeWithHeader() const { return getSizeUsed(); }
|
||||||
|
qint64 getSizeUsed() const { return _sizeUsed; }
|
||||||
|
void setSizeUsed(qint64 sizeUsed) { _sizeUsed = sizeUsed; }
|
||||||
|
|
||||||
|
const char* constData() const { return _payloadStart; }
|
||||||
|
|
||||||
PacketType::Value getPacketType() const;
|
PacketType::Value getPacketType() const;
|
||||||
PacketVersion getPacketTypeVersion() const;
|
PacketVersion getPacketTypeVersion() const;
|
||||||
SequenceNumber getSequenceNumber() const;
|
SequenceNumber getSequenceNumber() const;
|
||||||
bool isControlPacket() const;
|
bool isControlPacket() const;
|
||||||
|
|
||||||
PacketPayload& payload() { return _payload; }
|
// QIODevice virtual functions
|
||||||
|
//
|
||||||
|
// WARNING: Those methods all refer to the paylaod 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 qint64 pos() const { return _position; }
|
||||||
|
virtual bool reset() { return seek(0); }
|
||||||
|
virtual bool seek(qint64 pos);
|
||||||
|
virtual qint64 size() const { return _capacity; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Packet(PacketType::Value type, int64_t size);
|
Packet(PacketType::Value type, int64_t size);
|
||||||
|
@ -39,14 +56,23 @@ protected:
|
||||||
Packet(const Packet&) = delete;
|
Packet(const Packet&) = delete;
|
||||||
Packet& operator=(Packet&&) = delete;
|
Packet& operator=(Packet&&) = delete;
|
||||||
Packet& operator=(const Packet&) = delete;
|
Packet& operator=(const Packet&) = delete;
|
||||||
|
|
||||||
|
// QIODevice virtual functions
|
||||||
|
virtual qint64 writeData(const char* data, qint64 maxSize);
|
||||||
|
virtual qint64 readData(char* data, qint64 maxSize);
|
||||||
|
|
||||||
void setPacketTypeAndVersion(PacketType::Value type);
|
void setPacketTypeAndVersion(PacketType::Value type);
|
||||||
void setSequenceNumber(SequenceNumber seqNum);
|
void setSequenceNumber(SequenceNumber seqNum);
|
||||||
|
|
||||||
int64_t _packetSize;
|
std::unique_ptr<char> _packet; // Allocated memory
|
||||||
std::unique_ptr<char> _packet;
|
int64_t _packetSize = 0; // Total size of the allocated memory
|
||||||
|
|
||||||
|
char* _payloadStart = nullptr; // Start of the payload
|
||||||
|
qint64 _position = 0; // Current position in the payload
|
||||||
|
qint64 _capacity = 0; // Total capacity of the payload
|
||||||
|
|
||||||
|
qint64 _sizeUsed = 0; // How much of the payload is actually used
|
||||||
|
|
||||||
PacketPayload _payload;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // hifi_Packet_h
|
#endif // hifi_Packet_h
|
|
@ -118,6 +118,7 @@ int numBytesForPacketHeader(const QByteArray& packet);
|
||||||
int numBytesForPacketHeader(const char* packet);
|
int numBytesForPacketHeader(const char* packet);
|
||||||
int numBytesForArithmeticCodedPacketType(PacketType::Value packetType);
|
int numBytesForArithmeticCodedPacketType(PacketType::Value packetType);
|
||||||
int numBytesForPacketHeaderGivenPacketType(PacketType::Value packetType);
|
int numBytesForPacketHeaderGivenPacketType(PacketType::Value packetType);
|
||||||
|
int packArithmeticallyCodedValue(int value, char* destination);
|
||||||
|
|
||||||
QUuid uuidFromPacketHeader(const QByteArray& packet);
|
QUuid uuidFromPacketHeader(const QByteArray& packet);
|
||||||
|
|
||||||
|
|
|
@ -1,66 +0,0 @@
|
||||||
//
|
|
||||||
// PacketPayload.cpp
|
|
||||||
// libraries/networking/src
|
|
||||||
//
|
|
||||||
// Created by Stephen Birarda on 07/06/15.
|
|
||||||
// Copyright 2015 High Fidelity, Inc.
|
|
||||||
//
|
|
||||||
// Distributed under the Apache License, Version 2.0.
|
|
||||||
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
|
||||||
//
|
|
||||||
|
|
||||||
#include "PacketPayload.h"
|
|
||||||
|
|
||||||
PacketPayload::PacketPayload(char* data, qint64 capacity) :
|
|
||||||
_data(data)
|
|
||||||
_capacity(capacity)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
const int PACKET_READ_ERROR = -1;
|
|
||||||
|
|
||||||
qint64 PacketPayload::writeData(const char* data, qint64 maxSize) {
|
|
||||||
|
|
||||||
qint64 currentPos = pos();
|
|
||||||
|
|
||||||
// make sure we have the space required to write this block
|
|
||||||
qint64 bytesAvailable = _capacity - currentPos;
|
|
||||||
|
|
||||||
if (bytesAvailable < srcBytes) {
|
|
||||||
// good to go - write the data
|
|
||||||
memcpy(_data + currentPos, src, srcBytes);
|
|
||||||
|
|
||||||
// seek to the new position based on where our write just finished
|
|
||||||
seek(currentPos + srcBytes);
|
|
||||||
|
|
||||||
// keep track of _maxWritten so we can just write the actual data when packet is about to be sent
|
|
||||||
_size = std::max(pos() + 1, _maxWritten);
|
|
||||||
|
|
||||||
// return the number of bytes written
|
|
||||||
return srcBytes;
|
|
||||||
} else {
|
|
||||||
// not enough space left for this write - return an error
|
|
||||||
return PACKET_WRITE_ERROR;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const qint64 PACKET_READ_ERROR = -1;
|
|
||||||
|
|
||||||
qint64 PacketPayload::readData(char* dest, qint64 maxSize) {
|
|
||||||
|
|
||||||
int currentPosition = pos();
|
|
||||||
|
|
||||||
// we're either reading what is left from the current position or what was asked to be read
|
|
||||||
qint64 numBytesToRead = std::min(_maxSize - currentPosition, maxSize);
|
|
||||||
|
|
||||||
if (numBytesToRead > 0) {
|
|
||||||
// read out the data
|
|
||||||
memcpy(dest, _data + currentPosition, numBytesToRead);
|
|
||||||
|
|
||||||
// seek to the end of the read
|
|
||||||
seek(_data + currentPosition + numBytesToRead);
|
|
||||||
}
|
|
||||||
|
|
||||||
return numBytesToRead;
|
|
||||||
}
|
|
|
@ -1,40 +0,0 @@
|
||||||
//
|
|
||||||
// PacketPayload.h
|
|
||||||
// libraries/networking/src
|
|
||||||
//
|
|
||||||
// Created by Stephen Birarda on 07/06/15.
|
|
||||||
// Copyright 2015 High Fidelity, Inc.
|
|
||||||
//
|
|
||||||
// Distributed under the Apache License, Version 2.0.
|
|
||||||
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
|
||||||
//
|
|
||||||
|
|
||||||
#ifndef hifi_PacketPayload_h
|
|
||||||
#define hifi_PacketPayload_h
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include <QtCore/QIODevice>
|
|
||||||
|
|
||||||
class PacketPayload : public QIODevice {
|
|
||||||
public:
|
|
||||||
PacketPayload(char* data, qint64 capacity);
|
|
||||||
|
|
||||||
qint64 getSizeUsed() const { return _sizeUsed; }
|
|
||||||
void setSizeUsed(qint64 sizeUsed) { _sizeUsed = sizeUsed; }
|
|
||||||
|
|
||||||
virtual qint64 size() const { return _capacity; }
|
|
||||||
virtual bool isSequential() const { return false; }
|
|
||||||
|
|
||||||
const char* constData() const { return _data; }
|
|
||||||
protected:
|
|
||||||
qint64 writeData(const char* data, qint64 maxSize);
|
|
||||||
qint64 readData(char* data, qint64 maxSize);
|
|
||||||
|
|
||||||
private:
|
|
||||||
char* _data;
|
|
||||||
qint64 _sizeUsed = 0;
|
|
||||||
qint64 _capacity = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif // hifi_PacketPayload_h
|
|
Loading…
Reference in a new issue