Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 61 additions & 15 deletions libraries/messages/AxonMessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Networking::SerializedAxonMessage::SerializedAxonMessage(SerializedAxonMessage &
}

Networking::SerializedAxonMessage::~SerializedAxonMessage() {
if (bytes && owning)
if (bytes)
delete[] bytes;
}

Expand Down Expand Up @@ -120,24 +120,74 @@ Networking::AxonMessage::AxonMessage(const AxonMessage& message) :
memcpy(this->message, message.getMessage(), this->size);
}

Networking::AxonMessage::AxonMessage(AxonMessage &message, size64_t size, uint8_t partID, uint8_t flags, uint64_t uniqueID, size64_t offset) :
Networking::AxonMessage::AxonMessage(AxonMessage &&other) noexcept :
size(std::exchange(other.size, 0)),
message(std::exchange(other.message, nullptr)),
partID(std::exchange(other.partID, 0)),
flags(std::exchange(other.flags, UNDEFINED)),
uniqueID(std::exchange(other.uniqueID, 0))
{}

Networking::AxonMessage::AxonMessage(const AxonMessage &message, size64_t size, uint8_t partID, uint8_t flags, uint64_t uniqueID, size64_t offset) :
size(size),
message(message.message),
partID(partID),
flags(flags),
uniqueID(uniqueID),
offset(offset)
uniqueID(uniqueID)
{
message.owning = false;
if (!message.message || size == 0)
return;

this->message = new char[size];
memcpy(this->message, message.message + offset, size);
}

Networking::AxonMessage::~AxonMessage()
{
if (this->message && owning)
delete[] static_cast<char*>( message );
if (this->message)
delete[] message;
message = nullptr;
}

Networking::AxonMessage & Networking::AxonMessage::operator=(const AxonMessage &other) {
if (&other == this) {
return *this;
}

if (this->message) {
delete[] this->message;
}

size = other.size;
partID = other.partID;
flags = other.flags;
uniqueID = other.uniqueID;

if (other.message && other.size > 0) {
message = new char[size];
memcpy(message, other.message, size);
}

return *this;
}

Networking::AxonMessage & Networking::AxonMessage::operator=(AxonMessage &&other) noexcept {
if (&other == this) {
return *this;
}

if (this->message) {
delete[] message;
}

size = std::exchange(other.size, 0);
message = std::exchange(other.message, nullptr);
partID = std::exchange(other.partID, 0);
flags = std::exchange(other.flags, UNDEFINED);
uniqueID = std::exchange(other.uniqueID, 0);

return *this;
}

Networking::SerializedAxonMessage Networking::AxonMessage::getSerialized() const noexcept {
return SerializedAxonMessage(*this);
}
Expand Down Expand Up @@ -165,22 +215,18 @@ Networking::AxonMessage::UniqueAxonMessagePtr Networking::AxonMessage::split(con
return std::make_unique<AxonMessage>(*this, left, partID + 1, flags ^ PARTIAL, uniqueID, size);
}

void Networking::AxonMessage::append(const Networking::AxonMessage &other) {
void Networking::AxonMessage::append(const AxonMessage &other) {
if (!other.getMessage() || other.size == 0 || other.partID != partID + 1)
return;

char* tempBuffer = new char[other.size + size];

// assert((uintptr_t) &size < (uintptr_t) tempBuffer || (uintptr_t) &size > (uintptr_t) tempBuffer + other.size + size);

if (message) {
memcpy(tempBuffer, getMessage(), size);
if (owning)
delete[] static_cast<char*>(message);
delete[] message;
message = nullptr;
}
memcpy((static_cast<char*>(tempBuffer) + size), other.getMessage(), other.size);

memcpy(tempBuffer + size, other.getMessage(), other.size);
message = tempBuffer;
size += other.size;
partID = other.partID;
Expand Down
15 changes: 7 additions & 8 deletions libraries/messages/AxonMessage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ namespace Networking
void move(SerializedAxonMessage&) noexcept;
private:
size64_t size = 0;
bool owning = true; // todo: `uint16_t _refcnt;` or shared_ptr around bytes array
const char* bytes = nullptr;

friend class AxonMessage;
Expand Down Expand Up @@ -103,15 +102,17 @@ namespace Networking
*/
AxonMessage(const AxonMessage &message, uint8_t additionalFlags);

/**
* Copy constructor
*/
AxonMessage(const AxonMessage &);
AxonMessage(AxonMessage&, size64_t, uint8_t, uint8_t, uint64_t, size64_t);
AxonMessage(AxonMessage &&) noexcept;

AxonMessage(const AxonMessage&, size64_t, uint8_t, uint8_t, uint64_t, size64_t);
~AxonMessage();

AxonMessage & operator= (const AxonMessage &);
AxonMessage & operator= (AxonMessage &&) noexcept;

WGETTER(SerializedAxonMessage getSerialized());
WGETTER(void* getMessage()) { return (message) ? message + offset : message; }
WGETTER(void* getMessage()) { return message; }
WGETTER(size64_t getSize()) { return size; }
WGETTER(uint16_t ID()) { return uniqueID; }
WGETTER(uint8_t getFlags()) { return flags; }
Expand Down Expand Up @@ -139,10 +140,8 @@ namespace Networking
private:
size64_t size = 0;
char * message = nullptr;
bool owning = true;
uint8_t partID = 0;
uint8_t flags = UNDEFINED;
size64_t offset = 0;
uint16_t uniqueID = generateUniqueID();
};
}
Expand Down
Loading