Finished implementing, now for the testing phase.

This commit is contained in:
2025-01-26 21:43:17 -08:00
parent 7bc4b9977d
commit 981b40d3b1
47 changed files with 2070 additions and 1597 deletions

View File

@@ -0,0 +1,94 @@
#pragma once
#include "ehs/Str.h"
#include "ehs/Array.h"
#include "ehs/io/socket/Socket.h"
#include "NetUtils.h"
namespace ehs
{
class EHC;
class NetEnd;
class NetSys;
class NetChannel
{
protected:
static const UInt_64 internalSys;
static const UInt_64 connectOp;
static const UInt_64 connectedOp;
static const UInt_64 rejectedOp;
static const UInt_64 disconnectOp;
static const UInt_64 disconnectedOp;
static const UInt_64 statusUpdateOp;
static const UInt_64 pingOp;
static const UInt_64 pongOp;
static const UInt_64 latencyOp;
static const UInt_64 receivedOp;
private:
friend class EHC;
EHC *owner;
UInt_64 id;
Str_8 name;
Version version;
float maxTimeout;
float resendRate;
bool dropPackets;
Array<NetSys *> systems;
public:
virtual ~NetChannel();
NetChannel();
NetChannel(Str_8 name, const Version &version);
NetChannel(NetChannel &&channel) noexcept;
NetChannel(const NetChannel &channel);
NetChannel &operator=(NetChannel &&channel) noexcept;
NetChannel &operator=(const NetChannel &channel);
EHC *GetOwner() const;
UInt_64 GetId() const;
Str_8 GetName() const;
Version GetVersion() const;
void SetMaxTimeout(float seconds);
float GetMaxTimeout() const;
void SetResendRate(float seconds);
float GetResendRate() const;
void EnableDropPackets(bool enable);
bool IsDropPacketsEnabled() const;
bool AddSystem(NetSys *sys);
bool IsValid() const;
private:
virtual void Process(const float &delta, const Endpoint &endpoint, const Header &header, Serializer<UInt_64> &payload);
virtual void Poll(const float &delta);
protected:
bool HasSystem(UInt_64 sysHashId) const;
bool HasSystem(const Str_8& sysId) const;
NetSys* GetSystem(UInt_64 sysHashId) const;
NetSys* GetSystem(const Str_8& sysId) const;
};
}

View File

@@ -0,0 +1,81 @@
#pragma once
#include "NetChannel.h"
#include "ehs/io/socket/Socket.h"
#include "ehs/io/socket/ehc/NetEnc.h"
#include "ehs/io/socket/ehc/NetFrag.h"
namespace ehs
{
class NetClientCh : public NetChannel
{
private:
friend class EHC;
Endpoint remoteEndpoint;
Char_8 token[64];
NetStatus status;
UInt_64 queueSlot;
float deltaDuration;
float deltaRate;
float lastPing;
float latency;
float timeout;
UInt_64 nextSendId;
Vector<Insurance> sent;
UInt_64 nextRecvId;
Vector<NetFrag> received;
public:
~NetClientCh();
NetClientCh();
NetClientCh(Str_8 name, const Version &version, Endpoint remoteEndpoint);
NetClientCh(NetClientCh &&client) noexcept;
NetClientCh(const NetClientCh &client);
NetClientCh &operator=(NetClientCh &&client) noexcept;
NetClientCh &operator=(const NetClientCh &client);
virtual void OnConnected(Serializer<UInt_64> data);
virtual void OnActive(Serializer<UInt_64> data);
virtual void OnQueueUpdate(Serializer<UInt_64> data);
virtual void OnDisconnected(Serializer<UInt_64> data);
virtual void OnRejected(Serializer<UInt_64> data);
virtual void OnTimeout(Serializer<UInt_64> data);
void Connect(const Endpoint &endpoint, const Serializer<UInt_64>& payload);
bool Disconnect(const Serializer<UInt_64>& payload);
void Send(bool deltaLocked, UInt_64 encId, bool ensure, UInt_64 sysId, UInt_64 opId, const Serializer<UInt_64>& payload);
void Send(bool deltaLocked, const Str_8 &encName, bool ensure, const Str_8& sysName, const Str_8& opName, const Serializer<UInt_64>& payload);
NetStatus GetStatus() const;
private:
void Process(const float &delta, const Endpoint &endpoint, const Header &header, Serializer<UInt_64> &payload) override;
void Poll(const float &delta) override;
NetFrag FragmentData(IP version, const Header& header, const Serializer<>& data);
void Send(NetEnc *enc, const Header& header, const Serializer<>& payload);
void Pong(float delta);
void RemoveInsurance(UInt_64 msgId, UInt_64 fragment);
void AddReceived(const Header& header, const Serializer<>& payload);
};
}

View File

@@ -1,6 +1,7 @@
#pragma once
#include "ehs/Str.h"
#include "ehs/Version.h"
namespace ehs
{
@@ -11,16 +12,16 @@ namespace ehs
private:
friend class EHC;
EHC *owner;
UInt_64 hashId;
Str_8 id;
UInt_64 id;
Str_8 name;
Version version;
public:
virtual ~NetEnc() = default;
NetEnc();
NetEnc(Str_8 id);
NetEnc(Str_8 name, const Version &version);
NetEnc(NetEnc &&enc) noexcept;
@@ -30,14 +31,14 @@ namespace ehs
NetEnc &operator=(const NetEnc &enc);
EHC *GetOwner() const;
UInt_64 GetId() const;
UInt_64 GetHashId() const;
Str_8 GetName() const;
Str_8 GetId() const;
Version GetVersion() const;
virtual void Encrypt(Byte *data, UInt_64 size) const;
virtual void Decrypt(Byte *data, UInt_64 size) const;
};
}
}

View File

@@ -1,7 +1,7 @@
#pragma once
#include "NetUtils.h"
#include "NetFrags.h"
#include "NetFrag.h"
#include "ehs/Str.h"
#include "ehs/Vector.h"
@@ -10,27 +10,23 @@
namespace ehs
{
class EHC;
class NetServerCh;
class NetEnd
{
private:
friend class EHC;
friend class NetServerCh;
EHC* owner;
EndDisp disposition;
UInt_64 hashName;
NetServerCh* owner;
UInt_64 id;
Str_8 name;
Status status;
Architecture arch;
NetStatus status;
Char_8 token[64];
UInt_64 nextSendId;
Vector<Insurance> sent;
UInt_64 nextRecvId;
Vector<NetFrags> received;
AddrType type;
Str_8 address;
UInt_16 port;
Vector<NetFrag> received;
Endpoint endpoint;
float deltaDuration;
float deltaRate;
float timeout;
@@ -42,9 +38,9 @@ namespace ehs
public:
NetEnd();
NetEnd(EndDisp disposition, Str_8 id, Architecture arch, AddrType type, Str_8 address, UInt_16 port);
NetEnd(Str_8 id, Endpoint endpoint);
NetEnd(AddrType type, Str_8 address, UInt_16 port);
NetEnd(Endpoint endpoint);
NetEnd(NetEnd &&end) noexcept;
@@ -54,15 +50,11 @@ namespace ehs
NetEnd &operator=(const NetEnd &end);
EndDisp GetDisposition() const;
UInt_64 GetHashName() const;
UInt_64 GetId() const;
Str_8 GetName() const;
Status GetStatus() const;
Architecture GetArchitecture() const;
NetStatus GetStatus() const;
UInt_64 GetNextSendId() const;
@@ -73,7 +65,7 @@ namespace ehs
/// @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, UInt_64 encHashId, bool ensure, UInt_64 sys, UInt_64 op, const Serializer<UInt_64>& payload);
void Send(bool deltaLocked, UInt_64 encId, bool ensure, UInt_64 sysId, UInt_64 opId, const Serializer<UInt_64>& 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.
@@ -82,13 +74,11 @@ namespace ehs
/// @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, const Str_8 &encID, bool ensure, const Str_8& sys, const Str_8& op, const Serializer<UInt_64>& payload);
void Send(bool deltaLocked, const Str_8 &encName, bool ensure, const Str_8& sysName, const Str_8& opName, const Serializer<UInt_64>& payload);
UInt_64 GetNextRecvId() const;
Str_8 GetAddress() const;
UInt_16 GetPort() const;
Endpoint GetEndpoint() const;
void SetDeltaRate(float newDeltaRate);
@@ -107,23 +97,21 @@ namespace ehs
private:
void Poll(float delta);
void SetStatus(Status newStatus);
void SetStatus(NetStatus newStatus);
void RemoveInsurance(UInt_64 msgId, UInt_64 fragment);
void AddReceived(const Header& header, const Serializer<>& payload);
Vector<NetFrags>* GetReceived();
Vector<NetFrag>* GetReceived();
void Ping(float delta);
void Pong(float delta);
void SetLatency(float newLatency);
void SetQueueSlot(UInt_64 slot);
NetFrags FragmentData(const Header& header, const Serializer<>& data);
NetFrag FragmentData(const Header& header, const Serializer<>& data);
void Send(const Header& header, const Serializer<>& payload);

View File

@@ -0,0 +1,43 @@
#pragma once
#include "NetUtils.h"
#include <ehs/Serializer.h>
namespace ehs
{
class NetFrag
{
private:
Header header;
Serializer<UInt_64>* data;
UInt_64 size;
public:
~NetFrag();
NetFrag();
NetFrag(const Header &header, const Serializer<UInt_64> &payload);
NetFrag(const Header &header, UInt_64 size);
NetFrag(NetFrag &&frags) noexcept;
NetFrag(const NetFrag &frags);
NetFrag &operator=(NetFrag &&frags) noexcept;
NetFrag &operator=(const NetFrag &frags);
operator Serializer<UInt_64> *() const;
Header GetHeader() const;
UInt_64 Size() const;
bool IsComplete() const;
Packet Combine() const;
};
}

View File

@@ -1,43 +0,0 @@
#pragma once
#include "NetUtils.h"
#include <ehs/Serializer.h>
namespace ehs
{
class NetFrags
{
private:
Header header;
Serializer<UInt_64>* data;
UInt_64 size;
public:
~NetFrags();
NetFrags();
NetFrags(const Header &header, const Serializer<UInt_64> &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<UInt_64> *() const;
Header GetHeader() const;
UInt_64 Size() const;
bool IsComplete() const;
Packet Combine() const;
};
}

View File

@@ -5,7 +5,7 @@
namespace ehs
{
class EHC;
class NetChannel;
class NetSys;
class NetEnd;
@@ -37,6 +37,6 @@ namespace ehs
UInt_64 GetHashId() const;
private:
virtual void Process(EHC *ehc, NetEnd *endpoint, NetSys *sys, Serializer<UInt_64> &payload);
virtual void Process(NetChannel *channel, NetEnd *endpoint, NetSys *sys, Serializer<UInt_64> &payload);
};
}

View File

@@ -0,0 +1,103 @@
#pragma once
#include "NetChannel.h"
namespace ehs
{
class NetEnd;
class NetServerCh : public NetChannel
{
private:
friend class EHC;
Vector<NetEnd *> endpoints;
UInt_64 maxEndpoints;
public:
~NetServerCh() override;
NetServerCh(UInt_64 maxEndpoints = 0);
NetServerCh(NetServerCh &&server) noexcept;
NetServerCh(const NetServerCh &server);
NetServerCh &operator=(NetServerCh &&server) noexcept;
NetServerCh &operator=(const NetServerCh &server);
virtual bool OnEndpointConnect(NetEnd *endpoint, Serializer<UInt_64> payload);
virtual Serializer<UInt_64> OnEndpointAccepted(NetEnd *endpoint);
virtual void OnEndpointDisconnect(NetEnd *endpoint, Serializer<UInt_64> payload);
virtual void OnEndpointTimeout(NetEnd *endpoint);
virtual void OnEndpointActive(NetEnd *endpoint);
virtual Serializer<UInt_64> OnShutdown();
void Broadcast(NetStatus endStatus, bool deltaLocked, UInt_64 encHashId,
bool ensure, UInt_64 sysHashId, UInt_64 opHashId,
const Serializer<UInt_64> &payload);
void Broadcast(NetStatus endStatus, bool deltaLocked, const Str_8 &encId,
bool ensure, const Str_8 &sysId, const Str_8 &opId,
const Serializer<UInt_64> &payload);
bool HasEndpoint(NetStatus endStatus, const Char_8 token[64]) const;
bool HasEndpoint(NetStatus endStatus, UInt_64 hashId) const;
bool HasEndpoint(NetStatus endStatus, const Str_8 &id) const;
bool HasEndpoint(const Char_8 token[64]) const;
bool HasEndpoint(UInt_64 hashId) const;
bool HasEndpoint(const Str_8 &id) const;
bool HasEndpoint(const Endpoint &endpoint) const;
NetEnd *GetEndpoint(NetStatus endStatus, const Char_8 token[64]) const;
NetEnd *GetEndpoint(NetStatus endStatus, UInt_64 hashId) const;
NetEnd *GetEndpoint(NetStatus endStatus, const Str_8 &id) const;
NetEnd *GetEndpoint(const Char_8 token[64]) const;
NetEnd *GetEndpoint(UInt_64 hashId) const;
NetEnd *GetEndpoint(const Str_8 &id) const;
NetEnd *GetEndpoint(const Endpoint &endpoint) const;
Array<NetEnd *> GetEndpoints(NetStatus endStatus);
UInt_64 GetEndpointsCount(NetStatus endStatus);
UInt_64 GetMaxEndpoints() const;
private:
void Process(const float &delta, const Endpoint &endpoint, const Header &header, Serializer<UInt_64> &payload) override;
void GenerateToken(Char_8 in[64]);
void UpdateQueue(UInt_64 active);
void UpdateQueue();
bool RemoveEndpoint(const Char_8 token[64]);
bool RemoveEndpoint(const Endpoint &endpoint);
bool RemoveEndpoint(const NetEnd* end);
void Poll(const float &delta) override;
void Shutdown();
};
}

View File

@@ -6,14 +6,16 @@
namespace ehs
{
class EHC;
class NetChannel;
class NetEnd;
class NetOp;
class NetSys
{
private:
friend class EHC;
friend class NetChannel;
friend class NetServerCh;
friend class NetClientCh;
UInt_64 hashId;
Str_8 id;
@@ -43,6 +45,6 @@ namespace ehs
bool AddOperation(NetOp *op);
private:
void Execute(EHC *ehc, NetEnd *endpoint, UInt_64 hashId, Serializer<UInt_64> &payload);
void Execute(NetChannel *channel, NetEnd *endpoint, UInt_64 hashId, Serializer<UInt_64> &payload);
};
}

View File

@@ -4,33 +4,41 @@
namespace ehs
{
enum class EndDisp : UInt_8
{
ENDPOINT,
SERVICE
};
enum class Status : UInt_8
enum class NetStatus : UInt_8
{
DISCONNECTED,
ACTIVE,
PENDING,
IN_LOCAL_QUEUE,
IN_REMOTE_QUEUE,
QUEUED
};
enum class NetChannelType : UInt_8
{
SERVER,
CLIENT
};
struct Header
{
UInt_64 encHashId = 0;
Version version = {};
UInt_64 encId = 0;
Version encVer = {};
UInt_64 channelId = 0;
NetChannelType channelType = NetChannelType::SERVER;
Version channelVer = {};
UInt_64 id = 0;
UInt_64 fragments = 0;
UInt_64 fragmentCount = 0;
UInt_64 fragment = 0;
bool ensure = false;
EndDisp disposition = EndDisp::ENDPOINT;
Char_8 token[64] = {};
UInt_64 system = 0;
UInt_64 op = 0;
UInt_64 systemId = 0;
UInt_64 opId = 0;
};
void WriteHeader(const Header &header, Serializer<UInt_64> &data);
Header ReadHeader(Serializer<UInt_64> &data);
struct Packet
{
Header header;