diff --git a/libraries/networking/src/NLPacket.cpp b/libraries/networking/src/NLPacket.cpp new file mode 100644 index 0000000000..df91b4fdbe --- /dev/null +++ b/libraries/networking/src/NLPacket.cpp @@ -0,0 +1,48 @@ +// +// NLPacket.cpp +// libraries/networking/src +// +// Created by Clement on 7/6/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 "NLPacket.h" + +int64_t NLPacket::headerSize(PacketType::Value type) { + int64_t size = ((NON_SOURCED_PACKETS.contains(type)) ? 0 : NUM_BYTES_RFC4122_UUID) + + ((NON_VERIFIED_PACKETS.contains(type)) ? 0 : NUM_BYTES_RFC4122_UUID); + return size; +} + +int64_t NLPacket::maxPayloadSize(PacketType::Value type) { + return Packet::maxPayloadSize(type) - headerSize(type); +} + +std::unique_ptr NLPacket::create(PacketType::Value type, int64_t size) { + if (size > maxPayloadSize(type)) { + return std::unique_ptr(); + } + + return std::unique_ptr(new NLPacket(type, size)); +} + +NLPacket::NLPacket(PacketType::Value type, int64_t size) : Packet(type, headerSize(type) + size) { +} + +void NLPacket::setSourceUuid(QUuid sourceUuid) { + auto type = getPacketType(); + Q_ASSERT(!NON_SOURCED_PACKETS.contains(type)); + auto offset = Packet::headerSize(type) + NLPacket::headerSize(type); + memcpy(_packet.get() + offset, sourceUuid.toRfc4122().constData(), NUM_BYTES_RFC4122_UUID); +} + +void NLPacket::setConnectionUuid(QUuid connectionUuid) { + auto type = getPacketType(); + Q_ASSERT(!NON_VERIFIED_PACKETS.contains(type)); + auto offset = Packet::headerSize(type) + NLPacket::headerSize(type) + + ((NON_SOURCED_PACKETS.contains(type)) ? 0 : NUM_BYTES_RFC4122_UUID); + memcpy(_packet.get() + offset, connectionUuid.toRfc4122().constData(), NUM_BYTES_RFC4122_UUID); +} \ No newline at end of file diff --git a/libraries/networking/src/NLPacket.h b/libraries/networking/src/NLPacket.h new file mode 100644 index 0000000000..b1ceee9e83 --- /dev/null +++ b/libraries/networking/src/NLPacket.h @@ -0,0 +1,32 @@ +// +// NLPacket.h +// libraries/networking/src +// +// Created by Clement on 7/6/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_NLPacket_h +#define hifi_NLPacket_h + +#include "Packet.h" + +class NLPacket : public Packet { +public: + static int64_t headerSize(PacketType::Value type); + static int64_t maxPayloadSize(PacketType::Value type); + + static std::unique_ptr create(PacketType::Value type, int64_t size = -1); + +protected: + NLPacket(PacketType::Value type, int64_t size); + + void setSourceUuid(QUuid sourceUuid); + void setConnectionUuid(QUuid connectionUuid); + +}; + +#endif // hifi_NLPacket_h \ No newline at end of file