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

122 lines
2.2 KiB
C++

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