EHS/src/io/socket/ehc/Fragments.cpp

122 lines
2.3 KiB
C++

#include "ehs/io/socket/ehc/Fragments.h"
namespace ehs
{
Fragments::~Fragments()
{
delete[] data;
}
Fragments::Fragments()
: data(nullptr), size(0)
{
}
Fragments::Fragments(const Header &header, const Serializer<UInt_64> &payload)
: header(header), data(new Serializer<UInt_64>[header.fragments]), size(header.fragments)
{
this->header.fragment = 0;
data[header.fragment] = payload;
}
Fragments::Fragments(const Header &header, const UInt_64 size)
: header(header), data(new Serializer<UInt_64>[size]), size(size)
{
this->header.fragments = size;
this->header.fragment = 0;
}
Fragments::Fragments(Fragments &&frags) noexcept
: header(frags.header), data(frags.data), size(frags.size)
{
frags.header = {};
frags.data = nullptr;
frags.size = 0;
}
Fragments::Fragments(const Fragments &frags)
: header(frags.header), data(new Serializer<UInt_64>[frags.size]), size(frags.size)
{
for (UInt_64 i = 0; i < size; ++i)
data[i] = frags.data[i];
}
Fragments &Fragments::operator=(Fragments &&frags) noexcept
{
if (this == &frags)
return *this;
header = frags.header;
delete[] data;
data = frags.data;
size = frags.size;
frags.header = {};
frags.data = nullptr;
frags.size = 0;
return *this;
}
Fragments &Fragments::operator=(const Fragments &frags)
{
if (this == &frags)
return *this;
header = frags.header;
delete[] data;
data = new Serializer<UInt_64>[frags.size];
for (UInt_64 i = 0; i < frags.size; ++i)
data[i] = frags.data[i];
size = frags.size;
return *this;
}
Fragments::operator Serializer<UInt_64> *() const
{
return data;
}
Header Fragments::GetHeader() const
{
return header;
}
UInt_64 Fragments::Size() const
{
return size;
}
bool Fragments::IsComplete() const
{
for (UInt_64 i = 0; i < size; ++i)
if (!data[i].Size())
return false;
return true;
}
Packet Fragments::Combine() const
{
UInt_64 rSize = 0;
for (UInt_64 i = 0; i < size; ++i)
rSize += data[i].Size();
Packet result =
{
header,
{Endianness::LE, rSize}
};
result.header.fragments = 0;
for (UInt_64 i = 0; i < size; ++i)
result.payload.WriteSer(data[i]);
result.payload.SetOffset(0);
return result;
}
}