diff --git a/CMakeLists.txt b/CMakeLists.txt index 9f556d5..ebd860c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -132,11 +132,11 @@ set(EHS_SOURCES include/ehs/io/socket/TCP.h src/io/socket/SSL.cpp include/ehs/io/socket/SSL.h - include/ehs/io/socket/ehc/Utils.h + include/ehs/io/socket/ehc/NetUtils.h src/io/socket/EHC.cpp include/ehs/io/socket/EHC.h - src/io/socket/ehc/Fragments.cpp include/ehs/io/socket/ehc/Fragments.h + src/io/socket/ehc/NetFrags.cpp include/ehs/io/socket/ehc/NetFrags.h src/io/socket/ehc/NetEnd.cpp include/ehs/io/socket/ehc/NetEnd.h - src/io/socket/ehc/NetSystem.cpp include/ehs/io/socket/ehc/NetSystem.h + src/io/socket/ehc/NetSys.cpp include/ehs/io/socket/ehc/NetSys.h src/io/socket/ehc/NetOp.cpp include/ehs/io/socket/ehc/NetOp.h src/io/socket/rest/Twitch.cpp include/ehs/io/socket/rest/Twitch.h @@ -185,6 +185,8 @@ set(EHS_SOURCES include/ehs/io/BaseDirectory.h src/io/BaseDirectory.cpp include/ehs/io/Directory.h + include/ehs/io/socket/ehc/NetEnc.h + src/io/socket/ehc/NetEnc.cpp ) if (IS_OS_WINDOWS) diff --git a/include/ehs/io/socket/EHC.h b/include/ehs/io/socket/EHC.h index 3a81050..fbd5fb7 100644 --- a/include/ehs/io/socket/EHC.h +++ b/include/ehs/io/socket/EHC.h @@ -1,7 +1,7 @@ #pragma once -#include "ehs/io/socket/ehc/Utils.h" - +#include "ehs/io/socket/ehc/NetUtils.h" +#include "ehs/io/socket/ehc/NetEnc.h" #include "ehs/Serializer.h" #include "ehs/Vector.h" #include "ehs/Array.h" @@ -10,7 +10,7 @@ namespace ehs { - class NetSystem; + class NetSys; class NetEnd; class EHC; @@ -24,6 +24,7 @@ namespace ehs class EHC { private: + friend class NetEnc; friend class NetEnd; static const Version version; @@ -47,7 +48,8 @@ namespace ehs bool dropPackets; Byte* buffer; UInt_32 bufferSize; - Array systems; + Array encryptions; + Array systems; UInt_32 maxEndpoints; UInt_64 lastTSC; float delta; @@ -90,13 +92,13 @@ namespace ehs bool Disconnect(EndDisp endDisp, const Char_8 token[64], const Str_8& msg); - void Broadcast(EndDisp endDisp, Status endStatus, bool deltaLocked, bool encrypted, + void Broadcast(EndDisp endDisp, Status endStatus, bool deltaLocked, UInt_64 encHashId, bool ensure, UInt_64 sysHashId, UInt_64 opHashId, - const Serializer<>& payload); + const Serializer &payload); - void Broadcast(EndDisp endDisp, Status endStatus, bool deltaLocked, bool encrypted, - bool ensure, const Str_8& sysId, const Str_8& opId, - const Serializer<>& payload); + void Broadcast(EndDisp endDisp, Status endStatus, bool deltaLocked, const Str_8 &encId, + bool ensure, const Str_8 &sysId, const Str_8 &opId, + const Serializer &payload); void Poll(); @@ -124,15 +126,25 @@ namespace ehs bool IsDropPacketsEnabled() const; + bool HasEncryption(UInt_64 encHashId) const; + + bool HasEncryption(const Str_8& encId) const; + + bool AddEncryption(NetEnc *enc); + + NetEnc* GetEncryption(UInt_64 encHashId) const; + + NetEnc* GetEncryption(const Str_8& encId) const; + bool HasSystem(UInt_64 sysHashId) const; bool HasSystem(const Str_8& sysId) const; - bool AddSystem(NetSystem *sys); + bool AddSystem(NetSys *sys); - NetSystem* GetSystem(UInt_64 sysHashId) const; + NetSys* GetSystem(UInt_64 sysHashId) const; - NetSystem* GetSystem(const Str_8& sysId) const; + NetSys* GetSystem(const Str_8& sysId) const; bool HasEndpoint(EndDisp endDisp, Status endStatus, const Char_8 token[64]) const; diff --git a/include/ehs/io/socket/ehc/Fragments.h b/include/ehs/io/socket/ehc/Fragments.h deleted file mode 100644 index 6182e60..0000000 --- a/include/ehs/io/socket/ehc/Fragments.h +++ /dev/null @@ -1,43 +0,0 @@ -#pragma once - -#include "Utils.h" - -#include - -namespace ehs -{ - class Fragments - { - private: - Header header; - Serializer* data; - UInt_64 size; - - public: - ~Fragments(); - - Fragments(); - - Fragments(const Header &header, const Serializer &payload); - - Fragments(const Header &header, UInt_64 size); - - Fragments(Fragments &&frags) noexcept; - - Fragments(const Fragments &frags); - - Fragments &operator=(Fragments &&frags) noexcept; - - Fragments &operator=(const Fragments &frags); - - operator Serializer *() const; - - Header GetHeader() const; - - UInt_64 Size() const; - - bool IsComplete() const; - - Packet Combine() const; - }; -} diff --git a/include/ehs/io/socket/ehc/NetEnc.h b/include/ehs/io/socket/ehc/NetEnc.h new file mode 100644 index 0000000..769cb34 --- /dev/null +++ b/include/ehs/io/socket/ehc/NetEnc.h @@ -0,0 +1,43 @@ +#pragma once + +#include "ehs/Str.h" + +namespace ehs +{ + class EHC; + + class NetEnc + { + private: + friend class EHC; + + EHC *owner; + UInt_64 hashId; + Str_8 id; + + public: + virtual ~NetEnc() = default; + + NetEnc(); + + NetEnc(Str_8 id); + + NetEnc(NetEnc &&enc) noexcept; + + NetEnc(const NetEnc &enc); + + NetEnc &operator=(NetEnc &&enc) noexcept; + + NetEnc &operator=(const NetEnc &enc); + + EHC *GetOwner() const; + + UInt_64 GetHashId() const; + + Str_8 GetId() const; + + virtual void Encrypt(Byte *data, UInt_64 size) const; + + virtual void Decrypt(Byte *data, UInt_64 size) const; + }; +} \ No newline at end of file diff --git a/include/ehs/io/socket/ehc/NetEnd.h b/include/ehs/io/socket/ehc/NetEnd.h index 0af1fdd..566675f 100644 --- a/include/ehs/io/socket/ehc/NetEnd.h +++ b/include/ehs/io/socket/ehc/NetEnd.h @@ -1,12 +1,12 @@ #pragma once -#include "Utils.h" -#include "Fragments.h" +#include "NetUtils.h" +#include "NetFrags.h" -#include -#include -#include -#include +#include "ehs/Str.h" +#include "ehs/Vector.h" +#include "ehs/Serializer.h" +#include "ehs/io/socket/Socket.h" namespace ehs { @@ -27,7 +27,7 @@ namespace ehs UInt_64 nextSendId; Vector sent; UInt_64 nextRecvId; - Vector received; + Vector received; AddrType type; Str_8 address; UInt_16 port; @@ -68,21 +68,21 @@ namespace ehs /// Sends data to the remote endpoint. /// @param [in] deltaLocked Whether or not to match the remote endpoint's delta time to prevent overloading the client. This will drop data if delta time does not match. - /// @param [in] encrypted Whether or not to encrypt this data before sending to the remote endpoint. + /// @param [in] encHashId The hash id of the encryption to use. Can be zero for none. /// @param [in] ensure Whether or not to ensure the data was received by the remote endpoint. /// @param [in] sys The system hash id to execute an operation from. /// @param [in] op The operation hash id in the system to execute. /// @param [in] payload Additional parameters and data to send to the remote endpoint. - void Send(bool deltaLocked, bool encrypted, bool ensure, UInt_64 sys, UInt_64 op, const Serializer& payload); + void Send(bool deltaLocked, UInt_64 encHashId, bool ensure, UInt_64 sys, UInt_64 op, const Serializer& payload); /// Sends data to the remote endpoint. /// @param [in] deltaLocked Whether or not to match the remote endpoint's delta time to prevent overloading the client. This will drop data if delta time does not match. - /// @param [in] encrypted Whether or not to encrypt this data before sending to the remote endpoint. + /// @param [in] encId The id of the encryption to use. Can be empty for none. /// @param [in] ensure Whether or not to ensure the data was received by the remote endpoint. /// @param [in] sys The system string id to execute an operation from. /// @param [in] op The operation string id in the system to execute. /// @param [in] payload Additional parameters and data to send to the remote endpoint. - void Send(bool deltaLocked, bool encrypted, bool ensure, const Str_8& sys, const Str_8& op, const Serializer& payload); + void Send(bool deltaLocked, const Str_8 &encID, bool ensure, const Str_8& sys, const Str_8& op, const Serializer& payload); UInt_64 GetNextRecvId() const; @@ -113,7 +113,7 @@ namespace ehs void AddReceived(const Header& header, const Serializer<>& payload); - Vector* GetReceived(); + Vector* GetReceived(); void Ping(float delta); @@ -123,7 +123,7 @@ namespace ehs void SetQueueSlot(UInt_64 slot); - Fragments FragmentData(const Header& header, const Serializer<>& data); + NetFrags FragmentData(const Header& header, const Serializer<>& data); void Send(const Header& header, const Serializer<>& payload); diff --git a/include/ehs/io/socket/ehc/NetFrags.h b/include/ehs/io/socket/ehc/NetFrags.h new file mode 100644 index 0000000..13f3ec4 --- /dev/null +++ b/include/ehs/io/socket/ehc/NetFrags.h @@ -0,0 +1,43 @@ +#pragma once + +#include "NetUtils.h" + +#include + +namespace ehs +{ + class NetFrags + { + private: + Header header; + Serializer* data; + UInt_64 size; + + public: + ~NetFrags(); + + NetFrags(); + + NetFrags(const Header &header, const Serializer &payload); + + NetFrags(const Header &header, UInt_64 size); + + NetFrags(NetFrags &&frags) noexcept; + + NetFrags(const NetFrags &frags); + + NetFrags &operator=(NetFrags &&frags) noexcept; + + NetFrags &operator=(const NetFrags &frags); + + operator Serializer *() const; + + Header GetHeader() const; + + UInt_64 Size() const; + + bool IsComplete() const; + + Packet Combine() const; + }; +} diff --git a/include/ehs/io/socket/ehc/NetOp.h b/include/ehs/io/socket/ehc/NetOp.h index 4a86fdc..6ff478c 100644 --- a/include/ehs/io/socket/ehc/NetOp.h +++ b/include/ehs/io/socket/ehc/NetOp.h @@ -1,18 +1,18 @@ #pragma once -#include -#include +#include "ehs/Str.h" +#include "ehs/Serializer.h" namespace ehs { class EHC; - class NetSystem; + class NetSys; class NetEnd; class NetOp { private: - friend class NetSystem; + friend class NetSys; UInt_64 hashId; Str_8 id; @@ -37,6 +37,6 @@ namespace ehs UInt_64 GetHashId() const; private: - virtual void Process(EHC *ehc, NetEnd *endpoint, NetSystem *sys, Serializer &payload); + virtual void Process(EHC *ehc, NetEnd *endpoint, NetSys *sys, Serializer &payload); }; } \ No newline at end of file diff --git a/include/ehs/io/socket/ehc/NetSystem.h b/include/ehs/io/socket/ehc/NetSys.h similarity index 56% rename from include/ehs/io/socket/ehc/NetSystem.h rename to include/ehs/io/socket/ehc/NetSys.h index cd90cf5..97ef9ed 100644 --- a/include/ehs/io/socket/ehc/NetSystem.h +++ b/include/ehs/io/socket/ehc/NetSys.h @@ -1,8 +1,8 @@ #pragma once -#include -#include -#include +#include "ehs/Str.h" +#include "ehs/Array.h" +#include "ehs/Serializer.h" namespace ehs { @@ -10,7 +10,7 @@ namespace ehs class NetEnd; class NetOp; - class NetSystem + class NetSys { private: friend class EHC; @@ -20,19 +20,19 @@ namespace ehs Array ops; public: - virtual ~NetSystem(); + virtual ~NetSys(); - NetSystem(); + NetSys(); - NetSystem(Str_8 id); + NetSys(Str_8 id); - NetSystem(NetSystem &&sys) noexcept; + NetSys(NetSys &&sys) noexcept; - NetSystem(const NetSystem &sys); + NetSys(const NetSys &sys); - NetSystem &operator=(NetSystem &&sys) noexcept; + NetSys &operator=(NetSys &&sys) noexcept; - NetSystem &operator=(const NetSystem &sys); + NetSys &operator=(const NetSys &sys); Str_8 GetId() const; diff --git a/include/ehs/io/socket/ehc/Utils.h b/include/ehs/io/socket/ehc/NetUtils.h similarity index 70% rename from include/ehs/io/socket/ehc/Utils.h rename to include/ehs/io/socket/ehc/NetUtils.h index ee6d4f0..6f94e5e 100644 --- a/include/ehs/io/socket/ehc/Utils.h +++ b/include/ehs/io/socket/ehc/NetUtils.h @@ -1,6 +1,6 @@ #pragma once -#include +#include "ehs/Serializer.h" namespace ehs { @@ -20,7 +20,7 @@ namespace ehs struct Header { - bool encrypted = true; + UInt_64 encHashId = 0; UInt_64 id = 0; UInt_64 fragments = 0; UInt_64 fragment = 0; @@ -45,10 +45,10 @@ namespace ehs }; } -#ifndef COMMS_IPV4_PAYLOAD - #define COMMS_IPV4_PAYLOAD (EHS_IPV4_UDP_PAYLOAD - (UInt_16)sizeof(Header)) +#ifndef EHC_IPV4_PAYLOAD + #define EHC_IPV4_PAYLOAD (EHS_IPV4_UDP_PAYLOAD - (UInt_16)sizeof(Header)) #endif -#ifndef COMMS_IPV6_PAYLOAD - #define COMMS_IPV6_PAYLOAD (EHS_IPV6_UDP_PAYLOAD - (UInt_16)sizeof(Header)) +#ifndef EHC_IPV6_PAYLOAD + #define EHC_IPV6_PAYLOAD (EHS_IPV6_UDP_PAYLOAD - (UInt_16)sizeof(Header)) #endif \ No newline at end of file diff --git a/src/io/socket/EHC.cpp b/src/io/socket/EHC.cpp index f6ae877..2b7197d 100644 --- a/src/io/socket/EHC.cpp +++ b/src/io/socket/EHC.cpp @@ -1,10 +1,10 @@ #include "ehs/io/socket/EHC.h" -#include "ehs/io/socket/ehc/NetSystem.h" +#include "ehs/io/socket/ehc/NetEnc.h" +#include "ehs/io/socket/ehc/NetSys.h" #include "ehs/io/socket/ehc/NetEnd.h" - #include "ehs/Log.h" #include "ehs/PRNG.h" -#include + namespace ehs { @@ -54,11 +54,15 @@ namespace ehs EHC::EHC(EHC &&sock) noexcept : udp((UDP&&)sock.udp), appVer(sock.appVer), disposition(sock.disposition), hashName(sock.hashName), name((Str_8&&)sock.name), dropPackets(sock.dropPackets), buffer(sock.buffer), bufferSize(sock.bufferSize), - systems((Array &&)sock.systems), maxEndpoints(sock.maxEndpoints), lastTSC(sock.lastTSC), - delta(sock.delta), maxTimeout(sock.maxTimeout), resendRate(sock.resendRate), connectCb(sock.connectCb), - connectedCb(sock.connectedCb), activeCb(sock.activeCb), disconnectCb(sock.disconnectCb), - disconnectedCb(sock.disconnectedCb), timeoutCb(sock.timeoutCb), endpoints((Vector &&)sock.endpoints) + encryptions((Array &&)sock.encryptions), systems((Array &&)sock.systems), + maxEndpoints(sock.maxEndpoints), lastTSC(sock.lastTSC), delta(sock.delta), maxTimeout(sock.maxTimeout), + resendRate(sock.resendRate), connectCb(sock.connectCb), connectedCb(sock.connectedCb), activeCb(sock.activeCb), + disconnectCb(sock.disconnectCb), disconnectedCb(sock.disconnectedCb), timeoutCb(sock.timeoutCb), + endpoints((Vector &&)sock.endpoints) { + for (UInt_64 i = 0; i < encryptions.Size(); ++i) + endpoints[i]->owner = this; + for (UInt_64 i = 0; i < endpoints.Size(); ++i) endpoints[i]->owner = this; @@ -107,7 +111,12 @@ namespace ehs buffer = sock.buffer; bufferSize = sock.bufferSize; - systems = (Array &&)sock.systems; + + encryptions = (Array &&)sock.encryptions; + for (UInt_64 i = 0; i < encryptions.Size(); ++i) + encryptions[i]->owner = this; + + systems = (Array &&)sock.systems; maxEndpoints = sock.maxEndpoints; lastTSC = sock.lastTSC; delta = sock.delta; @@ -161,7 +170,7 @@ namespace ehs buffer = nullptr; bufferSize = 0; - systems = Array(); + systems = Array(); maxEndpoints = sock.maxEndpoints; lastTSC = 0; delta = 0.0f; @@ -273,7 +282,7 @@ namespace ehs return true; } - void EHC::Broadcast(const EndDisp endDisp, const Status endStatus, const bool deltaLocked, const bool encrypted, + void EHC::Broadcast(const EndDisp endDisp, const Status endStatus, const bool deltaLocked, const UInt_64 encHashId, const bool ensure, const UInt_64 sysHashId, const UInt_64 opHashId, const Serializer &payload) { if (!udp.IsValid()) @@ -285,15 +294,15 @@ namespace ehs continue; if (endpoints[i]->GetStatus() == endStatus) - endpoints[i]->Send(deltaLocked, encrypted, ensure, sysHashId, opHashId, payload); + endpoints[i]->Send(deltaLocked, encHashId, ensure, sysHashId, opHashId, payload); } } - void EHC::Broadcast(const EndDisp endDisp, const Status endStatus, const bool deltaLocked, const bool encrypted, + void EHC::Broadcast(const EndDisp endDisp, const Status endStatus, const bool deltaLocked, const Str_8 &encId, const bool ensure, const Str_8 &sysId, const Str_8 &opId, const Serializer &payload) { - Broadcast(endDisp, endStatus, deltaLocked, encrypted, ensure, sysId.Hash_64(), opId.Hash_64(), payload); + Broadcast(endDisp, endStatus, deltaLocked, encId.Hash_64(), ensure, sysId.Hash_64(), opId.Hash_64(), payload); } void EHC::Poll() @@ -315,9 +324,15 @@ namespace ehs { Serializer<> payload(Endianness::LE, buffer, received); - bool encrypted = payload.Read(); - if (encrypted) - Encryption::Encrypt_64(payload.Size() - payload.GetOffset(), &payload[payload.GetOffset()]); + UInt_64 encHashId = payload.Read(); + if (encHashId) + { + NetEnc* enc = GetEncryption(encHashId); + if (!enc) + continue; + + enc->Encrypt(&payload[payload.GetOffset()], payload.Size() - payload.GetOffset()); + } payload.SetOffset(0); @@ -609,6 +624,45 @@ namespace ehs return dropPackets; } + bool EHC::HasEncryption(UInt_64 encHashId) const + { + for (UInt_64 i = 0; i < encryptions.Size(); ++i) + if (encryptions[i]->GetHashId() == encHashId) + return true; + + return false; + } + + bool EHC::HasEncryption(const Str_8& encId) const + { + return HasEncryption(encId.Hash_64()); + } + + bool EHC::AddEncryption(NetEnc* enc) + { + if (HasEncryption(enc->GetHashId())) + return false; + + enc->owner = this; + encryptions.Push(enc); + + return true; + } + + NetEnc* EHC::GetEncryption(UInt_64 encHashId) const + { + for (UInt_64 i = 0; i < encryptions.Size(); ++i) + if (encryptions[i]->GetHashId() == encHashId) + return encryptions[i]; + + return nullptr; + } + + NetEnc* EHC::GetEncryption(const Str_8& encId) const + { + return GetEncryption(encId.Hash_64()); + } + bool EHC::HasSystem(const UInt_64 sysHashId) const { if (internalSys == sysHashId) @@ -626,7 +680,7 @@ namespace ehs return HasSystem(sysId.Hash_64()); } - bool EHC::AddSystem(NetSystem *sys) + bool EHC::AddSystem(NetSys *sys) { if (HasSystem(sys->GetHashId())) return false; @@ -636,7 +690,7 @@ namespace ehs return true; } - NetSystem* EHC::GetSystem(const UInt_64 sysHashId) const + NetSys* EHC::GetSystem(const UInt_64 sysHashId) const { for (UInt_64 i = 0; i < systems.Size(); ++i) if (systems[i]->GetHashId() == sysHashId) @@ -645,7 +699,7 @@ namespace ehs return nullptr; } - NetSystem* EHC::GetSystem(const Str_8& sysId) const + NetSys* EHC::GetSystem(const Str_8& sysId) const { return GetSystem(sysId.Hash_64()); } @@ -1121,7 +1175,7 @@ namespace ehs continue; } - Vector* frags = endpoints[i]->GetReceived(); + Vector* frags = endpoints[i]->GetReceived(); UInt_64 f = 0; while (f < frags->Size()) @@ -1134,7 +1188,7 @@ namespace ehs Packet packet = (*frags)[f].Combine(); - NetSystem* sys = GetSystem(packet.header.system); + NetSys* sys = GetSystem(packet.header.system); if (!sys) { ++f; diff --git a/src/io/socket/ehc/NetEnc.cpp b/src/io/socket/ehc/NetEnc.cpp new file mode 100644 index 0000000..e9b88f1 --- /dev/null +++ b/src/io/socket/ehc/NetEnc.cpp @@ -0,0 +1,76 @@ +#include "ehs/io/socket/ehc/NetEnc.h" + +namespace ehs +{ + NetEnc::NetEnc() + : owner(nullptr), hashId(0) + { + } + + NetEnc::NetEnc(Str_8 id) + : owner(nullptr), hashId(id.Hash_64()), id((Str_8 &&)id) + { + } + + NetEnc::NetEnc(NetEnc&& enc) noexcept + : owner(enc.owner), hashId(enc.hashId), id((Str_8 &&)enc.id) + { + enc.owner = nullptr; + enc.hashId = 0; + } + + NetEnc::NetEnc(const NetEnc& enc) + : owner(nullptr), hashId(enc.hashId), id(enc.id) + { + } + + NetEnc& NetEnc::operator=(NetEnc&& enc) noexcept + { + if (this == &enc) + return *this; + + owner = enc.owner; + hashId = enc.hashId; + id = (Str_8 &&)enc.id; + + enc.owner = nullptr; + enc.hashId = 0; + + return *this; + } + + NetEnc& NetEnc::operator=(const NetEnc& enc) + { + if (this == &enc) + return *this; + + owner = nullptr; + hashId = enc.hashId; + id = enc.id; + + return *this; + } + + EHC* NetEnc::GetOwner() const + { + return owner; + } + + UInt_64 NetEnc::GetHashId() const + { + return hashId; + } + + Str_8 NetEnc::GetId() const + { + return id; + } + + void NetEnc::Encrypt(Byte *data, UInt_64 size) const + { + } + + void NetEnc::Decrypt(Byte *data, UInt_64 size) const + { + } +} \ No newline at end of file diff --git a/src/io/socket/ehc/NetEnd.cpp b/src/io/socket/ehc/NetEnd.cpp index 2ea1166..15e20e5 100644 --- a/src/io/socket/ehc/NetEnd.cpp +++ b/src/io/socket/ehc/NetEnd.cpp @@ -1,8 +1,7 @@ #include "ehs/io/socket/ehc/NetEnd.h" #include "ehs/io/socket/EHC.h" - -#include -#include +#include "ehs/io/socket/ehc/NetEnc.h" +#include "ehs/system/CPU.h" namespace ehs { @@ -33,10 +32,10 @@ namespace ehs NetEnd::NetEnd(NetEnd &&end) noexcept : owner(end.owner), disposition(end.disposition), hashName(end.hashName), name((Str_8&&)end.name), status(end.status), arch(end.arch), token{}, - nextSendId(end.nextSendId), sent((Vector&&)end.sent), nextRecvId(end.nextRecvId), - received((Vector&&)end.received), type(end.type), address((Str_8&&)end.address), port(end.port), - deltaDuration(end.deltaDuration), deltaRate(end.deltaRate), timeout(end.timeout), lastPing(end.lastPing), - oldLatency(end.oldLatency), latency(end.latency), queueSlot(end.queueSlot) + nextSendId(end.nextSendId), sent((Vector&&)end.sent), nextRecvId(end.nextRecvId), + received((Vector&&)end.received), type(end.type), address((Str_8&&)end.address), port(end.port), + 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.disposition = EndDisp::ENDPOINT; @@ -80,7 +79,7 @@ namespace ehs nextSendId = end.nextSendId; sent = (Vector&&)end.sent; nextRecvId = end.nextRecvId; - received = (Vector&&)end.received; + received = (Vector&&)end.received; type = end.type; address = (Str_8&&)end.address; port = end.port; @@ -173,14 +172,14 @@ namespace ehs return nextSendId; } - void NetEnd::Send(const bool deltaLocked, const bool encrypted, const bool ensure, const UInt_64 sys, - const UInt_64 op, const Serializer<>& payload) + void NetEnd::Send(const bool deltaLocked, const UInt_64 encHashId, const bool ensure, const UInt_64 sys, + const UInt_64 op, const Serializer &payload) { if (deltaLocked && deltaDuration < deltaRate) return; Header header = { - encrypted, + encHashId, nextSendId++, 1, 0, @@ -193,9 +192,9 @@ namespace ehs Util::Copy(header.token, token, 64); - if ((owner->GetLocalAddressType() == AddrType::IPV6 && payload.Size() > COMMS_IPV6_PAYLOAD) || (owner->GetLocalAddressType() == AddrType::IPV4 && payload.Size() > COMMS_IPV4_PAYLOAD)) + if ((owner->GetLocalAddressType() == AddrType::IPV6 && payload.Size() > EHC_IPV6_PAYLOAD) || (owner->GetLocalAddressType() == AddrType::IPV4 && payload.Size() > EHC_IPV4_PAYLOAD)) { - Fragments frags = FragmentData(header, payload); + NetFrags frags = FragmentData(header, payload); for (UInt_64 i = 0; i < frags.Size(); ++i) { Header newHeader = frags.GetHeader(); @@ -210,10 +209,10 @@ namespace ehs } } - void NetEnd::Send(const bool deltaLocked, const bool encrypted, const bool ensure, const Str_8& sys, - const Str_8& op, const Serializer<>& payload) + void NetEnd::Send(const bool deltaLocked, const Str_8 &encId, const bool ensure, const Str_8& sys, + const Str_8& op, const Serializer<>& payload) { - Send(deltaLocked, encrypted, ensure, sys.Hash_64(), op.Hash_64(), payload); + Send(deltaLocked, encId.Hash_64(), ensure, sys.Hash_64(), op.Hash_64(), payload); } UInt_64 NetEnd::GetNextRecvId() const @@ -278,8 +277,19 @@ namespace ehs result.Write(sent[i].header); result.WriteSer(sent[i].payload); - if (sent[i].header.encrypted) - Encryption::Encrypt_64(result.Size() - sizeof(bool), &result[sizeof(bool)]); + if (sent[i].header.encHashId) + { + NetEnc *enc = owner->GetEncryption(sent[i].header.encHashId); + if (!enc) + { + EHS_LOG_INT(LogType::WARN, 0, "The network encryption with the hash id " + + Str_8::FromNum(sent[i].header.encHashId) + ", does not exist."); + + continue; + } + + enc->Encrypt(&result[sizeof(bool)], result.Size() - sizeof(bool)); + } owner->udp.Send(type, address, port, result, result.Size()); @@ -294,6 +304,8 @@ namespace ehs if (lastPing >= 1.0f) Ping(delta); } + + EHS_LOG_SUCCESS(); } void NetEnd::SetStatus(const Status newStatus) @@ -317,7 +329,7 @@ namespace ehs void NetEnd::AddReceived(const Header& header, const Serializer<>& payload) { - Fragments* frags = nullptr; + NetFrags* frags = nullptr; for (UInt_64 i = 0; i < received.Size(); ++i) { @@ -342,7 +354,7 @@ namespace ehs timeout = 0.0f; } - Vector* NetEnd::GetReceived() + Vector* NetEnd::GetReceived() { return &received; } @@ -395,46 +407,46 @@ namespace ehs queueSlot = slot; } - Fragments NetEnd::FragmentData(const Header& header, const Serializer<>& data) + NetFrags NetEnd::FragmentData(const Header& header, const Serializer<>& data) { - Fragments result; + NetFrags result; if (owner->GetLocalAddressType() == AddrType::IPV6) { - UInt_64 frags = data.Size() / COMMS_IPV6_PAYLOAD; - if (data.Size() % COMMS_IPV6_PAYLOAD) + UInt_64 frags = data.Size() / EHC_IPV6_PAYLOAD; + if (data.Size() % EHC_IPV6_PAYLOAD) ++frags; - result = Fragments(header, frags); + result = NetFrags(header, frags); - UInt_64 size = COMMS_IPV6_PAYLOAD; + UInt_64 size = EHC_IPV6_PAYLOAD; for (UInt_64 i = 0; i < result.Size(); ++i) { - size = COMMS_IPV6_PAYLOAD; + size = EHC_IPV6_PAYLOAD; if (i == result.Size() - 1) - size = data.Size() % COMMS_IPV6_PAYLOAD; + size = data.Size() % EHC_IPV6_PAYLOAD; - result[i] = {data.GetEndianness(), &data[i * COMMS_IPV6_PAYLOAD], size}; + result[i] = {data.GetEndianness(), &data[i * EHC_IPV6_PAYLOAD], size}; } } else if (owner->GetLocalAddressType() == AddrType::IPV4) { - UInt_64 frags = data.Size() / COMMS_IPV4_PAYLOAD; - if (data.Size() % COMMS_IPV4_PAYLOAD) + UInt_64 frags = data.Size() / EHC_IPV4_PAYLOAD; + if (data.Size() % EHC_IPV4_PAYLOAD) ++frags; - result = Fragments(header, frags); + result = NetFrags(header, frags); - UInt_64 size = COMMS_IPV4_PAYLOAD; + UInt_64 size = EHC_IPV4_PAYLOAD; for (UInt_64 i = 0; i < result.Size(); ++i) { - size = COMMS_IPV4_PAYLOAD; + size = EHC_IPV4_PAYLOAD; if (i == result.Size() - 1) - size = data.Size() % COMMS_IPV4_PAYLOAD; + size = data.Size() % EHC_IPV4_PAYLOAD; - result[i] = {data.GetEndianness(), &data[i * COMMS_IPV4_PAYLOAD], size}; + result[i] = {data.GetEndianness(), &data[i * EHC_IPV4_PAYLOAD], size}; } } @@ -447,8 +459,19 @@ namespace ehs result.Write(header); result.WriteSer(payload); - if (header.encrypted) - Encryption::Encrypt_64(result.Size() - sizeof(bool), &result[sizeof(bool)]); + if (header.encHashId) + { + NetEnc *enc = owner->GetEncryption(header.encHashId); + if (!enc) + { + EHS_LOG_INT(LogType::WARN, 0, "The network encryption with the hash id " + + Str_8::FromNum(header.encHashId) + ", does not exist."); + + return; + } + + enc->Encrypt(&result[sizeof(bool)], result.Size() - sizeof(bool)); + } if (header.ensure) sent.Push({header, payload}); @@ -476,7 +499,7 @@ namespace ehs if (!SortingNeeded()) return; - Vector sorted(0, received.Stride()); + Vector sorted(0, received.Stride()); for (UInt_64 a = 0; a < received.Size(); ++a) { diff --git a/src/io/socket/ehc/Fragments.cpp b/src/io/socket/ehc/NetFrags.cpp similarity index 72% rename from src/io/socket/ehc/Fragments.cpp rename to src/io/socket/ehc/NetFrags.cpp index 4c2f8f4..f0db0ae 100644 --- a/src/io/socket/ehc/Fragments.cpp +++ b/src/io/socket/ehc/NetFrags.cpp @@ -1,32 +1,32 @@ -#include "ehs/io/socket/ehc/Fragments.h" +#include "ehs/io/socket/ehc/NetFrags.h" namespace ehs { - Fragments::~Fragments() + NetFrags::~NetFrags() { delete[] data; } - Fragments::Fragments() + NetFrags::NetFrags() : data(nullptr), size(0) { } - Fragments::Fragments(const Header &header, const Serializer &payload) + NetFrags::NetFrags(const Header &header, const Serializer &payload) : header(header), data(new Serializer[header.fragments]), size(header.fragments) { this->header.fragment = 0; data[header.fragment] = payload; } - Fragments::Fragments(const Header &header, const UInt_64 size) + NetFrags::NetFrags(const Header &header, const UInt_64 size) : header(header), data(new Serializer[size]), size(size) { this->header.fragments = size; this->header.fragment = 0; } - Fragments::Fragments(Fragments &&frags) noexcept + NetFrags::NetFrags(NetFrags &&frags) noexcept : header(frags.header), data(frags.data), size(frags.size) { frags.header = {}; @@ -34,14 +34,14 @@ namespace ehs frags.size = 0; } - Fragments::Fragments(const Fragments &frags) + NetFrags::NetFrags(const NetFrags &frags) : header(frags.header), data(new Serializer[frags.size]), size(frags.size) { for (UInt_64 i = 0; i < size; ++i) data[i] = frags.data[i]; } - Fragments &Fragments::operator=(Fragments &&frags) noexcept + NetFrags &NetFrags::operator=(NetFrags &&frags) noexcept { if (this == &frags) return *this; @@ -60,7 +60,7 @@ namespace ehs return *this; } - Fragments &Fragments::operator=(const Fragments &frags) + NetFrags &NetFrags::operator=(const NetFrags &frags) { if (this == &frags) return *this; @@ -75,22 +75,22 @@ namespace ehs return *this; } - Fragments::operator Serializer *() const + NetFrags::operator Serializer *() const { return data; } - Header Fragments::GetHeader() const + Header NetFrags::GetHeader() const { return header; } - UInt_64 Fragments::Size() const + UInt_64 NetFrags::Size() const { return size; } - bool Fragments::IsComplete() const + bool NetFrags::IsComplete() const { for (UInt_64 i = 0; i < size; ++i) if (!data[i].Size()) @@ -99,7 +99,7 @@ namespace ehs return true; } - Packet Fragments::Combine() const + Packet NetFrags::Combine() const { UInt_64 rSize = 0; for (UInt_64 i = 0; i < size; ++i) diff --git a/src/io/socket/ehc/NetOp.cpp b/src/io/socket/ehc/NetOp.cpp index 82f51af..40fe058 100644 --- a/src/io/socket/ehc/NetOp.cpp +++ b/src/io/socket/ehc/NetOp.cpp @@ -1,7 +1,7 @@ #include "ehs/io/socket/ehc/NetOp.h" #include "ehs/io/socket/EHC.h" #include "ehs/io/socket/ehc/NetEnd.h" -#include "ehs/io/socket/ehc/NetSystem.h" +#include "ehs/io/socket/ehc/NetSys.h" namespace ehs { @@ -60,7 +60,7 @@ namespace ehs return hashId; } - void NetOp::Process(EHC *ehc, NetEnd *endpoint, NetSystem *sys, Serializer &payload) + void NetOp::Process(EHC *ehc, NetEnd *endpoint, NetSys *sys, Serializer &payload) { } } \ No newline at end of file diff --git a/src/io/socket/ehc/NetSystem.cpp b/src/io/socket/ehc/NetSys.cpp similarity index 66% rename from src/io/socket/ehc/NetSystem.cpp rename to src/io/socket/ehc/NetSys.cpp index 520933a..f660e01 100644 --- a/src/io/socket/ehc/NetSystem.cpp +++ b/src/io/socket/ehc/NetSys.cpp @@ -1,11 +1,11 @@ -#include "ehs/io/socket/ehc/NetSystem.h" +#include "ehs/io/socket/ehc/NetSys.h" #include "ehs/io/socket/EHC.h" #include "ehs/io/socket/ehc/NetEnd.h" #include "ehs/io/socket/ehc/NetOp.h" namespace ehs { - NetSystem::~NetSystem() + NetSys::~NetSys() { for (UInt_64 i = 0; i < ops.Size(); ++i) delete ops[i]; @@ -13,28 +13,28 @@ namespace ehs ops.Clear(); } - NetSystem::NetSystem() + NetSys::NetSys() : hashId(0) { } - NetSystem::NetSystem(Str_8 id) + NetSys::NetSys(Str_8 id) : hashId(id.Hash_64()), id((Str_8&&)id) { } - NetSystem::NetSystem(NetSystem&& sys) noexcept + NetSys::NetSys(NetSys&& sys) noexcept : hashId(sys.hashId), id((Str_8&&)sys.id), ops((Array&&)sys.ops) { sys.hashId = 0; } - NetSystem::NetSystem(const NetSystem& sys) + NetSys::NetSys(const NetSys& sys) : hashId(sys.hashId), id(sys.id) { } - NetSystem& NetSystem::operator=(NetSystem&& sys) noexcept + NetSys& NetSys::operator=(NetSys&& sys) noexcept { if (this == &sys) return *this; @@ -48,7 +48,7 @@ namespace ehs return *this; } - NetSystem& NetSystem::operator=(const NetSystem& sys) + NetSys& NetSys::operator=(const NetSys& sys) { if (this == &sys) return *this; @@ -60,17 +60,17 @@ namespace ehs return *this; } - Str_8 NetSystem::GetId() const + Str_8 NetSys::GetId() const { return id; } - UInt_64 NetSystem::GetHashId() const + UInt_64 NetSys::GetHashId() const { return hashId; } - bool NetSystem::HasOperation(const UInt_64 hashId) const + bool NetSys::HasOperation(const UInt_64 hashId) const { for (UInt_64 i = 0; i < ops.Size(); ++i) if (ops[i]->GetHashId() == hashId) @@ -79,7 +79,7 @@ namespace ehs return false; } - bool NetSystem::AddOperation(NetOp* op) + bool NetSys::AddOperation(NetOp* op) { if (HasOperation(op->GetHashId())) return false; @@ -89,7 +89,7 @@ namespace ehs return true; } - void NetSystem::Execute(EHC *ehc, NetEnd *endpoint, const UInt_64 hashId, Serializer &payload) + void NetSys::Execute(EHC *ehc, NetEnd *endpoint, const UInt_64 hashId, Serializer &payload) { for (UInt_64 i = 0; i < ops.Size(); ++i) {