resolve conflicts on merge with Atlante45/protocol

This commit is contained in:
Stephen Birarda 2015-07-07 16:12:07 -07:00
commit 6d502405ef
5 changed files with 59 additions and 45 deletions

View file

@ -38,6 +38,7 @@
#include "Node.h" #include "Node.h"
#include "NLPacket.h" #include "NLPacket.h"
#include "PacketHeaders.h" #include "PacketHeaders.h"
#include "PacketList.h"
#include "UUIDHasher.h" #include "UUIDHasher.h"
const int MAX_PACKET_SIZE = 1450; const int MAX_PACKET_SIZE = 1450;
@ -67,6 +68,7 @@ Q_DECLARE_METATYPE(SharedNodePointer)
using namespace tbb; using namespace tbb;
typedef std::pair<QUuid, SharedNodePointer> UUIDNodePair; typedef std::pair<QUuid, SharedNodePointer> UUIDNodePair;
typedef concurrent_unordered_map<QUuid, SharedNodePointer, UUIDHasher> NodeHash; typedef concurrent_unordered_map<QUuid, SharedNodePointer, UUIDHasher> NodeHash;
using NLPacketList = PacketList<NLPacket>;
typedef quint8 PingType_t; typedef quint8 PingType_t;
namespace PingType { namespace PingType {
@ -147,8 +149,8 @@ public:
qint64 sendUnreliablePacket(std::unique_ptr<NLPacket>& packet, const HifiSockAddr& sockAddr) {}; qint64 sendUnreliablePacket(std::unique_ptr<NLPacket>& packet, const HifiSockAddr& sockAddr) {};
qint64 sendPacket(std::unique_ptr<NLPacket> packet, const SharedNodePointer& destinationNode) {}; qint64 sendPacket(std::unique_ptr<NLPacket> packet, const SharedNodePointer& destinationNode) {};
qint64 sendPacket(std::unique_ptr<NLPacket> packet, const HifiSockAddr& sockAddr) {}; qint64 sendPacket(std::unique_ptr<NLPacket> packet, const HifiSockAddr& sockAddr) {};
qint64 sendPacketList(PacketList& packetList, const SharedNodePointer& destinationNode) {}; qint64 sendPacketList(NLPacketList& packetList, const SharedNodePointer& destinationNode) {};
qint64 sendPacketList(PacketList& packetList, const HifiSockAddr& sockAddr) {}; qint64 sendPacketList(NLPacketList& packetList, const HifiSockAddr& sockAddr) {};
void (*linkedDataCreateCallback)(Node *); void (*linkedDataCreateCallback)(Node *);

View file

@ -19,7 +19,7 @@
/// Storage of not-yet processed inbound, or not yet sent outbound generic UDP network packet /// Storage of not-yet processed inbound, or not yet sent outbound generic UDP network packet
class NetworkPacket { class NetworkPacket {
public: public:
NetworkPacket() { } NetworkPacket();
NetworkPacket(const NetworkPacket& packet); // copy constructor NetworkPacket(const NetworkPacket& packet); // copy constructor
NetworkPacket& operator= (const NetworkPacket& other); // copy assignment NetworkPacket& operator= (const NetworkPacket& other); // copy assignment

View file

@ -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();
} }
@ -56,11 +62,11 @@ Packet::Packet(PacketType::Value type, qint64 size) :
Q_ASSERT(size <= maxPayloadSize(type)); Q_ASSERT(size <= maxPayloadSize(type));
// copy packet type and version in header // copy packet type and version in header
setPacketTypeAndVersion(type); writePacketTypeAndVersion(type);
// Set control bit and sequence number to 0 if necessary // Set control bit and sequence number to 0 if necessary
if (SEQUENCE_NUMBERED_PACKETS.contains(type)) { if (SEQUENCE_NUMBERED_PACKETS.contains(type)) {
setSequenceNumber(0); writeSequenceNumber(0);
} }
} }
@ -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;
@ -99,15 +103,24 @@ Packet& Packet::operator=(Packet&& other) {
return *this; return *this;
} }
PacketType::Value Packet::getPacketType() const { 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 {
return (PacketType::Value)arithmeticCodingValueFromBuffer(_packet.get()); return (PacketType::Value)arithmeticCodingValueFromBuffer(_packet.get());
} }
PacketVersion Packet::getPacketTypeVersion() const { PacketVersion Packet::readPacketTypeVersion() const {
return *reinterpret_cast<PacketVersion*>(_packet.get() + numBytesForArithmeticCodedPacketType(_type)); return *reinterpret_cast<PacketVersion*>(_packet.get() + numBytesForArithmeticCodedPacketType(_type));
} }
Packet::SequenceNumber Packet::getSequenceNumber() const { Packet::SequenceNumber Packet::readSequenceNumber() const {
if (SEQUENCE_NUMBERED_PACKETS.contains(_type)) { if (SEQUENCE_NUMBERED_PACKETS.contains(_type)) {
SequenceNumber seqNum = *reinterpret_cast<SequenceNumber*>(_packet.get() + SequenceNumber seqNum = *reinterpret_cast<SequenceNumber*>(_packet.get() +
numBytesForArithmeticCodedPacketType(_type) + numBytesForArithmeticCodedPacketType(_type) +
@ -117,7 +130,7 @@ Packet::SequenceNumber Packet::getSequenceNumber() const {
return -1; return -1;
} }
bool Packet::isControlPacket() const { bool Packet::readIsControlPacket() const {
if (SEQUENCE_NUMBERED_PACKETS.contains(_type)) { if (SEQUENCE_NUMBERED_PACKETS.contains(_type)) {
SequenceNumber seqNum = *reinterpret_cast<SequenceNumber*>(_packet.get() + SequenceNumber seqNum = *reinterpret_cast<SequenceNumber*>(_packet.get() +
numBytesForArithmeticCodedPacketType(_type) + numBytesForArithmeticCodedPacketType(_type) +
@ -127,16 +140,16 @@ bool Packet::isControlPacket() const {
return false; return false;
} }
void Packet::setPacketTypeAndVersion(PacketType::Value type) { void Packet::writePacketTypeAndVersion(PacketType::Value type) {
// Pack the packet type // Pack the packet 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));
} }
void Packet::setSequenceNumber(SequenceNumber seqNum) { void Packet::writeSequenceNumber(SequenceNumber seqNum) {
// Here we are overriding the control bit to 0. // Here we are overriding the control bit to 0.
// But that is not an issue since we should only ever set the seqNum // But that is not an issue since we should only ever set the seqNum
// for data packets going out // for data packets going out
@ -144,14 +157,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

View file

@ -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,56 +32,59 @@ 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(); }
PacketType::Value getPacketType() const { return _type; }
void setPacketType(PacketType::Value type);
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; }
// Header readers // Header readers
PacketType::Value getPacketType() const; PacketType::Value readPacketType() const;
PacketVersion getPacketTypeVersion() const; PacketVersion readPacketTypeVersion() const;
SequenceNumber getSequenceNumber() const; SequenceNumber readSequenceNumber() const;
bool isControlPacket() const; bool readIsControlPacket() const;
// 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);
virtual qint64 readData(char* data, qint64 maxSize); virtual qint64 readData(char* data, qint64 maxSize);
// Header writers // Header writers
void setPacketTypeAndVersion(PacketType::Value type); void writePacketTypeAndVersion(PacketType::Value type);
void setSequenceNumber(SequenceNumber seqNum); void writeSequenceNumber(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

View file

@ -12,7 +12,9 @@
#ifndef hifi_PacketList_h #ifndef hifi_PacketList_h
#define hifi_PacketList_h #define hifi_PacketList_h
#pragma once #include <QIODevice>
#include "PacketHeaders.h"
template <class T> class PacketList : public QIODevice { template <class T> class PacketList : public QIODevice {
public: public:
@ -20,7 +22,7 @@ public:
virtual bool isSequential() const { return true; } virtual bool isSequential() const { return true; }
void startSegment() { _segmentStartIndex = currentPacket->payload().pos(); } void startSegment() { _segmentStartIndex = _currentPacket->payload().pos(); }
void endSegment() { _segmentStartIndex = -1; } void endSegment() { _segmentStartIndex = -1; }
void closeCurrentPacket(); void closeCurrentPacket();
@ -28,7 +30,7 @@ public:
void setExtendedHeader(const QByteArray& extendedHeader) { _extendedHeader = extendedHeader; } void setExtendedHeader(const QByteArray& extendedHeader) { _extendedHeader = extendedHeader; }
protected: protected:
qint64 writeData(const char* data, qint64 maxSize); qint64 writeData(const char* data, qint64 maxSize);
qint64 readData(const char* data, qint64 maxSize) { return 0 }; qint64 readData(const char* data, qint64 maxSize) { return 0; };
private: private:
std::unique_ptr<NLPacket> createPacketWithExtendedHeader(); std::unique_ptr<NLPacket> createPacketWithExtendedHeader();
@ -40,7 +42,7 @@ private:
int _segmentStartIndex = -1; int _segmentStartIndex = -1;
QByteArray _extendedHeader = extendedHeader; QByteArray _extendedHeader;
}; };
#endif // hifi_PacketList_h #endif // hifi_PacketList_h