511 lines
11 KiB
C++
511 lines
11 KiB
C++
#include "ehs/io/socket/ehc/NetEnd.h"
|
|
#include "ehs/io/socket/EHC.h"
|
|
#include "ehs/io/socket/ehc/NetEnc.h"
|
|
#include "ehs/io/socket/ehc/NetServerCh.h"
|
|
#include "ehs/system/CPU.h"
|
|
|
|
namespace ehs
|
|
{
|
|
NetEnd::NetEnd()
|
|
: owner(nullptr), id(0), status(NetStatus::PENDING), token{}, nextSendId(0), nextRecvId(0),
|
|
deltaDuration(0.0f), deltaRate(1.0f / 60.0f), timeout(0.0f), lastPing(0.0f), oldLatency(0.0f),
|
|
latency(0.0f), queueSlot(0)
|
|
{
|
|
}
|
|
|
|
NetEnd::NetEnd(Str_8 id, Endpoint endpoint)
|
|
: owner(nullptr), id(id.Hash_64()), name((Str_8&&)id), status(NetStatus::ACTIVE), token{}, nextSendId(0),
|
|
nextRecvId(0), endpoint((Endpoint &&)endpoint), deltaDuration(0.0f), deltaRate(1.0f / 60.0f),
|
|
timeout(0.0f), lastPing(0.0f), oldLatency(0.0f), latency(0.0f), queueSlot(0)
|
|
{
|
|
}
|
|
|
|
NetEnd::NetEnd(Endpoint endpoint)
|
|
: owner(nullptr), id(0), status(NetStatus::PENDING), token{}, nextSendId(0), nextRecvId(0),
|
|
endpoint((Endpoint &&)endpoint), deltaDuration(0.0f), deltaRate(1.0f / 60.0f), timeout(0.0f), lastPing(0.0f),
|
|
oldLatency(0.0f), latency(0.0f), queueSlot(0)
|
|
{
|
|
}
|
|
|
|
NetEnd::NetEnd(NetEnd &&end) noexcept
|
|
: owner(end.owner), id(end.id), name((Str_8&&)end.name), status(end.status), token{},
|
|
nextSendId(end.nextSendId), sent((Vector<Insurance>&&)end.sent), nextRecvId(end.nextRecvId),
|
|
received((Vector<NetFrag>&&)end.received), endpoint((Endpoint &&)end.endpoint),
|
|
deltaDuration(end.deltaDuration), deltaRate(end.deltaRate), timeout(end.timeout), lastPing(end.lastPing),
|
|
oldLatency(end.oldLatency), latency(end.latency), queueSlot(end.queueSlot)
|
|
{
|
|
end.owner = nullptr;
|
|
end.id = 0;
|
|
end.status = NetStatus::PENDING;
|
|
Util::Copy(token, end.token, 64);
|
|
Util::Zero(end.token, 64);
|
|
end.nextSendId = 0;
|
|
end.nextRecvId = 0;
|
|
end.deltaDuration = 0.0f;
|
|
end.deltaRate = 1.0f / 60.0f;
|
|
end.timeout = 0.0f;
|
|
end.lastPing = 0.0f;
|
|
end.oldLatency = 0.0f;
|
|
end.latency = 0.0f;
|
|
end.queueSlot = 0;
|
|
}
|
|
|
|
NetEnd::NetEnd(const NetEnd& end)
|
|
: owner(nullptr), id(end.id), name(end.name), status(NetStatus::PENDING), token{}, nextSendId(0),
|
|
nextRecvId(0), deltaDuration(0.0f), deltaRate(1.0f / 60.0f), timeout(0.0f), lastPing(0.0f), oldLatency(0.0f),
|
|
latency(0.0f), queueSlot(0)
|
|
{
|
|
}
|
|
|
|
NetEnd& NetEnd::operator=(NetEnd&& end) noexcept
|
|
{
|
|
if (this == &end)
|
|
return *this;
|
|
|
|
owner = end.owner;
|
|
id = end.id;
|
|
name = (Str_8&&)end.name;
|
|
status = end.status;
|
|
Util::Copy(token, end.token, 64);
|
|
nextSendId = end.nextSendId;
|
|
sent = (Vector<Insurance>&&)end.sent;
|
|
nextRecvId = end.nextRecvId;
|
|
received = (Vector<NetFrag>&&)end.received;
|
|
endpoint = (Endpoint &&)end.endpoint;
|
|
deltaDuration = end.deltaDuration;
|
|
deltaRate = end.deltaRate;
|
|
timeout = end.timeout;
|
|
lastPing = end.lastPing;
|
|
oldLatency = end.oldLatency;
|
|
latency = end.latency;
|
|
queueSlot = end.queueSlot;
|
|
|
|
end.owner = nullptr;
|
|
end.id = 0;
|
|
end.status = NetStatus::PENDING;
|
|
Util::Zero(end.token, 64);
|
|
end.nextSendId = 0;
|
|
end.nextRecvId = 0;
|
|
end.deltaDuration = 0.0f;
|
|
end.deltaRate = 1.0f / 60.0f;
|
|
end.timeout = 0.0f;
|
|
end.lastPing = 0.0f;
|
|
end.oldLatency = 0.0f;
|
|
end.latency = 0.0f;
|
|
end.queueSlot = 0;
|
|
|
|
return *this;
|
|
}
|
|
|
|
NetEnd &NetEnd::operator=(const NetEnd &end)
|
|
{
|
|
if (this == &end)
|
|
return *this;
|
|
|
|
owner = nullptr;
|
|
id = end.id;
|
|
name = end.name;
|
|
status = NetStatus::PENDING;
|
|
Util::Zero(token, 64);
|
|
nextSendId = 0;
|
|
sent = {};
|
|
nextRecvId = 0;
|
|
received = {};
|
|
endpoint = {};
|
|
deltaDuration = 0.0f;
|
|
deltaRate = 1.0f / 60.0f;
|
|
timeout = 0.0f;
|
|
lastPing = 0.0f;
|
|
oldLatency = 0.0f;
|
|
latency = 0.0f;
|
|
queueSlot = 0;
|
|
|
|
return *this;
|
|
}
|
|
|
|
UInt_64 NetEnd::GetId() const
|
|
{
|
|
return id;
|
|
}
|
|
|
|
Str_8 NetEnd::GetName() const
|
|
{
|
|
return name;
|
|
}
|
|
|
|
NetStatus NetEnd::GetStatus() const
|
|
{
|
|
return status;
|
|
}
|
|
|
|
UInt_64 NetEnd::GetNextSendId() const
|
|
{
|
|
return nextSendId;
|
|
}
|
|
|
|
void NetEnd::Send(const bool deltaLocked, const UInt_64 encId, const bool ensure, const UInt_64 sysId,
|
|
const UInt_64 opId, const Serializer<UInt_64> &payload)
|
|
{
|
|
if (!owner || !owner->GetOwner() || (deltaLocked && deltaDuration < deltaRate))
|
|
return;
|
|
|
|
EHC *ehc = owner->GetOwner();
|
|
|
|
NetEnc *enc = ehc->GetEncryption(encId);
|
|
if (!enc)
|
|
{
|
|
EHS_LOG_INT(LogType::WARN, 0, "Encryption with the Id, \"" + Str_8::FromNum(encId) + "\", does not exist.");
|
|
|
|
return;
|
|
}
|
|
|
|
Header header = {
|
|
EHC::GetVersion(),
|
|
encId,
|
|
enc->GetVersion(),
|
|
GetId(),
|
|
NetChannelType::SERVER,
|
|
GetVersion(),
|
|
nextSendId++,
|
|
1,
|
|
0,
|
|
ensure,
|
|
"",
|
|
sysId,
|
|
opId
|
|
};
|
|
|
|
Util::Copy(header.token, token, 64);
|
|
|
|
if ((ehc->GetLocalEndpoint().version == IP::V6 && payload.Size() > EHC_IPV6_PAYLOAD) ||
|
|
(ehc->GetLocalEndpoint().version == IP::V4 && payload.Size() > EHC_IPV4_PAYLOAD))
|
|
{
|
|
NetFrag frags = FragmentData(header, payload);
|
|
for (UInt_64 i = 0; i < frags.Size(); ++i)
|
|
{
|
|
Header newHeader = frags.GetHeader();
|
|
newHeader.fragment = i;
|
|
|
|
Send(newHeader, frags[i]);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Send(header, payload);
|
|
}
|
|
}
|
|
|
|
void NetEnd::Send(const bool deltaLocked, const Str_8 &encName, const bool ensure, const Str_8& sysName,
|
|
const Str_8& opName, const Serializer<>& payload)
|
|
{
|
|
Send(deltaLocked, encName.Hash_64(), ensure, sysName.Hash_64(), opName.Hash_64(), payload);
|
|
}
|
|
|
|
UInt_64 NetEnd::GetNextRecvId() const
|
|
{
|
|
return nextRecvId;
|
|
}
|
|
|
|
Endpoint NetEnd::GetEndpoint() const
|
|
{
|
|
return endpoint;
|
|
}
|
|
|
|
float NetEnd::GetDeltaRate() const
|
|
{
|
|
return deltaRate;
|
|
}
|
|
|
|
float NetEnd::GetTimeout() const
|
|
{
|
|
return timeout;
|
|
}
|
|
|
|
float NetEnd::GetLastPing() const
|
|
{
|
|
return lastPing;
|
|
}
|
|
|
|
float NetEnd::GetLatency() const
|
|
{
|
|
return oldLatency;
|
|
}
|
|
|
|
UInt_64 NetEnd::GetQueueSlot() const
|
|
{
|
|
return queueSlot;
|
|
}
|
|
|
|
void NetEnd::Poll(const float delta)
|
|
{
|
|
SortReceived();
|
|
|
|
if (deltaDuration >= deltaRate)
|
|
deltaDuration = Math::Mod(deltaDuration, deltaRate);
|
|
|
|
deltaDuration += delta;
|
|
timeout += delta;
|
|
latency += delta;
|
|
|
|
if (sent.Size())
|
|
{
|
|
for (UInt_64 i = 0; i < sent.Size(); ++i)
|
|
{
|
|
sent[i].lastResend += delta;
|
|
if (sent[i].lastResend >= owner->GetResendRate())
|
|
{
|
|
EHC *ehc = owner->GetOwner();
|
|
|
|
Serializer result(Endianness::LE);
|
|
result.Write(sent[i].header);
|
|
result.WriteSer(sent[i].payload);
|
|
|
|
if (sent[i].header.encId)
|
|
{
|
|
|
|
|
|
NetEnc *enc = ehc->GetEncryption(sent[i].header.encId);
|
|
if (!enc)
|
|
{
|
|
EHS_LOG_INT(LogType::WARN, 0, "The network encryption with the hash id " +
|
|
Str_8::FromNum(sent[i].header.encId) + ", does not exist.");
|
|
|
|
continue;
|
|
}
|
|
|
|
enc->Encrypt(&result[sizeof(bool)], result.Size() - sizeof(bool));
|
|
}
|
|
|
|
ehc->udp.Send(endpoint, result, result.Size());
|
|
|
|
sent[i].lastResend = Math::Mod(sent[i].lastResend, owner->GetResendRate());
|
|
}
|
|
}
|
|
}
|
|
|
|
lastPing += delta;
|
|
if (lastPing >= 1.0f)
|
|
Ping(delta);
|
|
|
|
EHS_LOG_SUCCESS();
|
|
}
|
|
|
|
void NetEnd::SetStatus(const NetStatus newStatus)
|
|
{
|
|
status = newStatus;
|
|
}
|
|
|
|
void NetEnd::RemoveInsurance(const UInt_64 msgId, const UInt_64 fragment)
|
|
{
|
|
for (UInt_64 i = 0; i < sent.Size(); ++i)
|
|
{
|
|
if (sent[i].header.id == msgId && sent[i].header.fragment == fragment)
|
|
{
|
|
sent.Remove(i);
|
|
break;
|
|
}
|
|
}
|
|
|
|
timeout = 0.0f;
|
|
}
|
|
|
|
void NetEnd::AddReceived(const Header& header, const Serializer<>& payload)
|
|
{
|
|
NetFrag* frags = nullptr;
|
|
|
|
for (UInt_64 i = 0; i < received.Size(); ++i)
|
|
{
|
|
if (received[i].GetHeader().id == header.id)
|
|
{
|
|
if (received[i][header.fragment].Size())
|
|
return;
|
|
|
|
frags = &received[i];
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (header.id > nextRecvId)
|
|
nextRecvId = header.id + 1;
|
|
|
|
if (frags)
|
|
(*frags)[header.fragment] = payload;
|
|
else
|
|
received.Push({header, payload});
|
|
|
|
timeout = 0.0f;
|
|
}
|
|
|
|
Vector<NetFrag>* NetEnd::GetReceived()
|
|
{
|
|
return &received;
|
|
}
|
|
|
|
void NetEnd::SetDeltaRate(const float newDeltaRate)
|
|
{
|
|
deltaRate = newDeltaRate;
|
|
}
|
|
|
|
void NetEnd::Ping(const float delta)
|
|
{
|
|
Serializer payload(Endianness::LE);
|
|
payload.Write(delta);
|
|
|
|
Send(false, true, false, "Internal", "Ping", payload);
|
|
lastPing = 0.0f;
|
|
latency = 0.0f;
|
|
}
|
|
|
|
void NetEnd::SendLatency()
|
|
{
|
|
oldLatency = latency * 1000;
|
|
|
|
Serializer sPayload(Endianness::LE);
|
|
sPayload.Write(oldLatency);
|
|
|
|
Send(false, true, false, "Internal", "Latency", sPayload);
|
|
|
|
latency = 0.0f;
|
|
timeout = 0.0f;
|
|
}
|
|
|
|
void NetEnd::SetLatency(const float newLatency)
|
|
{
|
|
oldLatency = newLatency;
|
|
}
|
|
|
|
void NetEnd::SetQueueSlot(const UInt_64 slot)
|
|
{
|
|
queueSlot = slot;
|
|
}
|
|
|
|
NetFrag NetEnd::FragmentData(const Header& header, const Serializer<>& data)
|
|
{
|
|
NetFrag result;
|
|
|
|
EHC *ehc = owner->GetOwner();
|
|
|
|
if (ehc->GetLocalEndpoint().version == IP::V6)
|
|
{
|
|
UInt_64 frags = data.Size() / EHC_IPV6_PAYLOAD;
|
|
if (data.Size() % EHC_IPV6_PAYLOAD)
|
|
++frags;
|
|
|
|
result = NetFrag(header, frags);
|
|
|
|
UInt_64 size = EHC_IPV6_PAYLOAD;
|
|
|
|
for (UInt_64 i = 0; i < result.Size(); ++i)
|
|
{
|
|
size = EHC_IPV6_PAYLOAD;
|
|
if (i == result.Size() - 1)
|
|
size = data.Size() % EHC_IPV6_PAYLOAD;
|
|
|
|
result[i] = {data.GetEndianness(), &data[i * EHC_IPV6_PAYLOAD], size};
|
|
}
|
|
}
|
|
else if (ehc->GetLocalEndpoint().version == IP::V4)
|
|
{
|
|
UInt_64 frags = data.Size() / EHC_IPV4_PAYLOAD;
|
|
if (data.Size() % EHC_IPV4_PAYLOAD)
|
|
++frags;
|
|
|
|
result = NetFrag(header, frags);
|
|
|
|
UInt_64 size = EHC_IPV4_PAYLOAD;
|
|
|
|
for (UInt_64 i = 0; i < result.Size(); ++i)
|
|
{
|
|
size = EHC_IPV4_PAYLOAD;
|
|
if (i == result.Size() - 1)
|
|
size = data.Size() % EHC_IPV4_PAYLOAD;
|
|
|
|
result[i] = {data.GetEndianness(), &data[i * EHC_IPV4_PAYLOAD], size};
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
void NetEnd::Send(const Header& header, const Serializer<UInt_64>& payload)
|
|
{
|
|
EHC *ehc = owner->GetOwner();
|
|
|
|
Serializer result(Endianness::LE);
|
|
result.Write(header);
|
|
result.WriteSer(payload);
|
|
|
|
if (header.encId)
|
|
{
|
|
NetEnc *enc = ehc->GetEncryption(header.encId);
|
|
if (!enc)
|
|
{
|
|
EHS_LOG_INT(LogType::WARN, 0, "The network encryption with the hash id " +
|
|
Str_8::FromNum(header.encId) + ", does not exist.");
|
|
|
|
return;
|
|
}
|
|
|
|
enc->Encrypt(&result[sizeof(bool)], result.Size() - sizeof(bool));
|
|
}
|
|
|
|
if (header.ensure)
|
|
sent.Push({header, payload});
|
|
|
|
ehc->udp.Send(endpoint, result, result.Size());
|
|
}
|
|
|
|
bool NetEnd::SortingNeeded() const
|
|
{
|
|
UInt_64 lastPacket = 0;
|
|
|
|
for (UInt_64 i = 0; i < received.Size(); ++i)
|
|
{
|
|
if (received[i].GetHeader().id < lastPacket)
|
|
return true;
|
|
else
|
|
lastPacket = received[i].GetHeader().id;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
void NetEnd::SortReceived()
|
|
{
|
|
if (!SortingNeeded())
|
|
return;
|
|
|
|
Vector<NetFrag> sorted(0, received.Stride());
|
|
|
|
for (UInt_64 a = 0; a < received.Size(); ++a)
|
|
{
|
|
if (sorted.Size())
|
|
{
|
|
for (UInt_64 b = sorted.Size(); b; --b)
|
|
{
|
|
if (received[a].GetHeader().id > sorted[b - 1].GetHeader().id)
|
|
{
|
|
if (b == sorted.Size())
|
|
sorted.Push(received[a]);
|
|
else
|
|
sorted.Insert(b, received[a]);
|
|
|
|
break;
|
|
}
|
|
else
|
|
{
|
|
sorted.Insert(b - 1, received[a]);
|
|
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
sorted.Push(received[a]);
|
|
}
|
|
}
|
|
|
|
received = sorted;
|
|
}
|
|
} |