Moved the Event Horizon Communications to this project.
This commit is contained in:
216
include/ehs/io/socket/EHC.h
Normal file
216
include/ehs/io/socket/EHC.h
Normal file
@@ -0,0 +1,216 @@
|
||||
#pragma once
|
||||
|
||||
#include "ehs/io/socket/ehc/Utils.h"
|
||||
|
||||
#include "ehs/Serializer.h"
|
||||
#include "ehs/Vector.h"
|
||||
#include "ehs/Array.h"
|
||||
#include "Socket.h"
|
||||
#include "UDP.h"
|
||||
|
||||
namespace ehs
|
||||
{
|
||||
class NetSystem;
|
||||
class NetEnd;
|
||||
class EHC;
|
||||
|
||||
typedef bool (*ConnectCb)(EHC *, NetEnd **, Serializer<UInt_64>);
|
||||
typedef void (*ConnectedCb)(EHC *, NetEnd *);
|
||||
typedef void (*ActiveCb)(EHC *, NetEnd *);
|
||||
typedef void (*DisconnectCb)(EHC *, NetEnd *, Serializer<UInt_64>);
|
||||
typedef void (*DisconnectedCb)(EHC *, NetEnd *);
|
||||
typedef void (*TimeoutCb)(EHC *, NetEnd *);
|
||||
|
||||
class EHC
|
||||
{
|
||||
private:
|
||||
friend class NetEnd;
|
||||
|
||||
static const Version version;
|
||||
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;
|
||||
|
||||
UDP udp;
|
||||
Version appVer;
|
||||
EndDisp disposition;
|
||||
UInt_64 hashName;
|
||||
Str_8 name;
|
||||
bool dropPackets;
|
||||
Byte* buffer;
|
||||
UInt_32 bufferSize;
|
||||
Array<NetSystem *> systems;
|
||||
UInt_32 maxEndpoints;
|
||||
UInt_64 lastTSC;
|
||||
float delta;
|
||||
float maxTimeout;
|
||||
float resendRate;
|
||||
ConnectCb connectCb;
|
||||
ConnectedCb connectedCb;
|
||||
ActiveCb activeCb;
|
||||
DisconnectCb disconnectCb;
|
||||
DisconnectedCb disconnectedCb;
|
||||
TimeoutCb timeoutCb;
|
||||
|
||||
protected:
|
||||
Vector<NetEnd*> endpoints;
|
||||
|
||||
public:
|
||||
~EHC();
|
||||
|
||||
EHC();
|
||||
|
||||
EHC(const Version &ver, Str_8 name, UInt_64 maxEndpoints);
|
||||
|
||||
EHC(const Version &ver, Str_8 name);
|
||||
|
||||
EHC(EHC &&sock) noexcept;
|
||||
|
||||
EHC(const EHC &sock);
|
||||
|
||||
EHC &operator=(EHC&& sock) noexcept;
|
||||
|
||||
EHC &operator=(const EHC &sock);
|
||||
|
||||
void Initialize();
|
||||
|
||||
void Release();
|
||||
|
||||
void Bind(AddrType newType, const Str_8& newAddress, UInt_16 newPort);
|
||||
|
||||
void Connect(AddrType rType, const Str_8& rAddress, UInt_16 rPort, Serializer<UInt_64> data);
|
||||
|
||||
bool Disconnect(EndDisp endDisp, const Char_8 token[64], const Str_8& msg);
|
||||
|
||||
void Broadcast(EndDisp endDisp, Status endStatus, bool deltaLocked, bool encrypted,
|
||||
bool ensure, UInt_64 sysHashId, UInt_64 opHashId,
|
||||
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 Poll();
|
||||
|
||||
bool IsInitialized() const;
|
||||
|
||||
AddrType GetLocalAddressType() const;
|
||||
|
||||
Str_8 GetLocalAddress() const;
|
||||
|
||||
UInt_16 GetLocalPort() const;
|
||||
|
||||
bool IsBound() const;
|
||||
|
||||
static Version GetVersion();
|
||||
|
||||
Version GetAppVersion() const;
|
||||
|
||||
EndDisp GetDisposition() const;
|
||||
|
||||
UInt_64 GetHashName() const;
|
||||
|
||||
Str_8 GetName() const;
|
||||
|
||||
void EnableDropPackets(bool enable);
|
||||
|
||||
bool IsDropPacketsEnabled() const;
|
||||
|
||||
bool HasSystem(UInt_64 sysHashId) const;
|
||||
|
||||
bool HasSystem(const Str_8& sysId) const;
|
||||
|
||||
bool AddSystem(NetSystem *sys);
|
||||
|
||||
NetSystem* GetSystem(UInt_64 sysHashId) const;
|
||||
|
||||
NetSystem* GetSystem(const Str_8& sysId) const;
|
||||
|
||||
bool HasEndpoint(EndDisp endDisp, Status endStatus, const Char_8 token[64]) const;
|
||||
|
||||
bool HasEndpoint(EndDisp endDisp, Status endStatus, UInt_64 hashId) const;
|
||||
|
||||
bool HasEndpoint(EndDisp endDisp, Status endStatus, const Str_8 &id) const;
|
||||
|
||||
bool HasEndpoint(EndDisp endDisp, const Char_8 token[64]) const;
|
||||
|
||||
bool HasEndpoint(EndDisp endDisp, UInt_64 hashId) const;
|
||||
|
||||
bool HasEndpoint(EndDisp endDisp, 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 Str_8& rAddress, UInt_16 rPort) const;
|
||||
|
||||
NetEnd *GetEndpoint(EndDisp endDisp, Status endStatus, const Char_8 token[64]) const;
|
||||
|
||||
NetEnd *GetEndpoint(EndDisp endDisp, Status endStatus, UInt_64 hashId) const;
|
||||
|
||||
NetEnd *GetEndpoint(EndDisp endDisp, Status endStatus, const Str_8 &id) const;
|
||||
|
||||
NetEnd *GetEndpoint(EndDisp endDisp, const Char_8 token[64]) const;
|
||||
|
||||
NetEnd *GetEndpoint(EndDisp endDisp, UInt_64 hashId) const;
|
||||
|
||||
NetEnd *GetEndpoint(EndDisp endDisp, const Str_8 &id) const;
|
||||
|
||||
NetEnd *GetEndpoint(const Str_8& rAddress, UInt_16 rPort) const;
|
||||
|
||||
Array<NetEnd *> GetEndpoints(EndDisp endDisp, Status endStatus);
|
||||
|
||||
Array<NetEnd *> GetEndpoints(EndDisp endDisp);
|
||||
|
||||
UInt_64 GetEndpointsCount(EndDisp endDisp, Status endStatus);
|
||||
|
||||
UInt_64 GetEndpointsCount(EndDisp endDisp);
|
||||
|
||||
UInt_64 GetMaxEndpoints() const;
|
||||
|
||||
void SetMaxTimeout(float seconds);
|
||||
|
||||
float GetMaxTimeout() const;
|
||||
|
||||
void SetResendRate(float seconds);
|
||||
|
||||
float GetResendRate() const;
|
||||
|
||||
void SetConnectCb(ConnectCb cb);
|
||||
|
||||
void SetConnectedCb(ConnectedCb cb);
|
||||
|
||||
void SetActiveCb(ActiveCb cb);
|
||||
|
||||
void SetDisconnectCb(DisconnectCb cb);
|
||||
|
||||
void SetDisconnectedCb(DisconnectedCb cb);
|
||||
|
||||
void SetTimeoutCb(TimeoutCb cb);
|
||||
|
||||
private:
|
||||
void GenerateToken(Char_8 in[64]);
|
||||
|
||||
void UpdateQueue(UInt_64 active);
|
||||
|
||||
void UpdateQueue();
|
||||
|
||||
bool RemoveEndpoint(EndDisp disposition, const Char_8 token[64]);
|
||||
|
||||
bool RemoveEndpoint(const Str_8& address, UInt_16 port);
|
||||
|
||||
bool RemoveEndpoint(const NetEnd* end);
|
||||
|
||||
void PollEndpoints();
|
||||
};
|
||||
}
|
43
include/ehs/io/socket/ehc/Fragments.h
Normal file
43
include/ehs/io/socket/ehc/Fragments.h
Normal file
@@ -0,0 +1,43 @@
|
||||
#pragma once
|
||||
|
||||
#include "Utils.h"
|
||||
|
||||
#include <ehs/Serializer.h>
|
||||
|
||||
namespace ehs
|
||||
{
|
||||
class Fragments
|
||||
{
|
||||
private:
|
||||
Header header;
|
||||
Serializer<UInt_64>* data;
|
||||
UInt_64 size;
|
||||
|
||||
public:
|
||||
~Fragments();
|
||||
|
||||
Fragments();
|
||||
|
||||
Fragments(const Header &header, const Serializer<UInt_64> &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<UInt_64> *() const;
|
||||
|
||||
Header GetHeader() const;
|
||||
|
||||
UInt_64 Size() const;
|
||||
|
||||
bool IsComplete() const;
|
||||
|
||||
Packet Combine() const;
|
||||
};
|
||||
}
|
134
include/ehs/io/socket/ehc/NetEnd.h
Normal file
134
include/ehs/io/socket/ehc/NetEnd.h
Normal file
@@ -0,0 +1,134 @@
|
||||
#pragma once
|
||||
|
||||
#include "Utils.h"
|
||||
#include "Fragments.h"
|
||||
|
||||
#include <ehs/Str.h>
|
||||
#include <ehs/Vector.h>
|
||||
#include <ehs/Serializer.h>
|
||||
#include <ehs/io/socket/Socket.h>
|
||||
|
||||
namespace ehs
|
||||
{
|
||||
class EHC;
|
||||
|
||||
class NetEnd
|
||||
{
|
||||
private:
|
||||
friend class EHC;
|
||||
|
||||
EHC* owner;
|
||||
EndDisp disposition;
|
||||
UInt_64 hashName;
|
||||
Str_8 name;
|
||||
Status status;
|
||||
Architecture arch;
|
||||
Char_8 token[64];
|
||||
UInt_64 nextSendId;
|
||||
Vector<Insurance> sent;
|
||||
UInt_64 nextRecvId;
|
||||
Vector<Fragments> received;
|
||||
AddrType type;
|
||||
Str_8 address;
|
||||
UInt_16 port;
|
||||
float deltaDuration;
|
||||
float deltaRate;
|
||||
float timeout;
|
||||
float lastPing;
|
||||
float oldLatency;
|
||||
float latency;
|
||||
UInt_64 queueSlot;
|
||||
|
||||
public:
|
||||
NetEnd();
|
||||
|
||||
NetEnd(EndDisp disposition, Str_8 id, Architecture arch, AddrType type, Str_8 address, UInt_16 port);
|
||||
|
||||
NetEnd(AddrType type, Str_8 address, UInt_16 port);
|
||||
|
||||
NetEnd(NetEnd &&end) noexcept;
|
||||
|
||||
NetEnd(const NetEnd &end);
|
||||
|
||||
NetEnd &operator=(NetEnd &&end) noexcept;
|
||||
|
||||
NetEnd &operator=(const NetEnd &end);
|
||||
|
||||
EndDisp GetDisposition() const;
|
||||
|
||||
UInt_64 GetHashName() const;
|
||||
|
||||
Str_8 GetName() const;
|
||||
|
||||
Status GetStatus() const;
|
||||
|
||||
Architecture GetArchitecture() const;
|
||||
|
||||
UInt_64 GetNextSendId() const;
|
||||
|
||||
/// 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] 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<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.
|
||||
/// @param [in] encrypted Whether or not to encrypt this data before sending to the remote endpoint.
|
||||
/// @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<UInt_64>& payload);
|
||||
|
||||
UInt_64 GetNextRecvId() const;
|
||||
|
||||
Str_8 GetAddress() const;
|
||||
|
||||
UInt_16 GetPort() const;
|
||||
|
||||
void SetDeltaRate(float newDeltaRate);
|
||||
|
||||
float GetDeltaRate() const;
|
||||
|
||||
float GetTimeout() const;
|
||||
|
||||
float GetLastPing() const;
|
||||
|
||||
void SendLatency();
|
||||
|
||||
float GetLatency() const;
|
||||
|
||||
UInt_64 GetQueueSlot() const;
|
||||
|
||||
private:
|
||||
void Poll(float delta);
|
||||
|
||||
void SetStatus(Status newStatus);
|
||||
|
||||
void RemoveInsurance(UInt_64 msgId, UInt_64 fragment);
|
||||
|
||||
void AddReceived(const Header& header, const Serializer<>& payload);
|
||||
|
||||
Vector<Fragments>* GetReceived();
|
||||
|
||||
void Ping(float delta);
|
||||
|
||||
void Pong(float delta);
|
||||
|
||||
void SetLatency(float newLatency);
|
||||
|
||||
void SetQueueSlot(UInt_64 slot);
|
||||
|
||||
Fragments FragmentData(const Header& header, const Serializer<>& data);
|
||||
|
||||
void Send(const Header& header, const Serializer<>& payload);
|
||||
|
||||
bool SortingNeeded() const;
|
||||
|
||||
void SortReceived();
|
||||
};
|
||||
}
|
42
include/ehs/io/socket/ehc/NetOp.h
Normal file
42
include/ehs/io/socket/ehc/NetOp.h
Normal file
@@ -0,0 +1,42 @@
|
||||
#pragma once
|
||||
|
||||
#include <ehs/Str.h>
|
||||
#include <ehs/Serializer.h>
|
||||
|
||||
namespace ehs
|
||||
{
|
||||
class EHC;
|
||||
class NetSystem;
|
||||
class NetEnd;
|
||||
|
||||
class NetOp
|
||||
{
|
||||
private:
|
||||
friend class NetSystem;
|
||||
|
||||
UInt_64 hashId;
|
||||
Str_8 id;
|
||||
|
||||
public:
|
||||
virtual ~NetOp() = default;
|
||||
|
||||
NetOp();
|
||||
|
||||
NetOp(Str_8 id);
|
||||
|
||||
NetOp(NetOp &&op) noexcept;
|
||||
|
||||
NetOp(const NetOp &op);
|
||||
|
||||
NetOp &operator=(NetOp &&op) noexcept;
|
||||
|
||||
NetOp &operator=(const NetOp &op);
|
||||
|
||||
Str_8 GetId() const;
|
||||
|
||||
UInt_64 GetHashId() const;
|
||||
|
||||
private:
|
||||
virtual void Process(EHC *ehc, NetEnd *endpoint, NetSystem *sys, Serializer<UInt_64> &payload);
|
||||
};
|
||||
}
|
48
include/ehs/io/socket/ehc/NetSystem.h
Normal file
48
include/ehs/io/socket/ehc/NetSystem.h
Normal file
@@ -0,0 +1,48 @@
|
||||
#pragma once
|
||||
|
||||
#include <ehs/Str.h>
|
||||
#include <ehs/Array.h>
|
||||
#include <ehs/Serializer.h>
|
||||
|
||||
namespace ehs
|
||||
{
|
||||
class EHC;
|
||||
class NetEnd;
|
||||
class NetOp;
|
||||
|
||||
class NetSystem
|
||||
{
|
||||
private:
|
||||
friend class EHC;
|
||||
|
||||
UInt_64 hashId;
|
||||
Str_8 id;
|
||||
Array<NetOp*> ops;
|
||||
|
||||
public:
|
||||
virtual ~NetSystem();
|
||||
|
||||
NetSystem();
|
||||
|
||||
NetSystem(Str_8 id);
|
||||
|
||||
NetSystem(NetSystem &&sys) noexcept;
|
||||
|
||||
NetSystem(const NetSystem &sys);
|
||||
|
||||
NetSystem &operator=(NetSystem &&sys) noexcept;
|
||||
|
||||
NetSystem &operator=(const NetSystem &sys);
|
||||
|
||||
Str_8 GetId() const;
|
||||
|
||||
UInt_64 GetHashId() const;
|
||||
|
||||
bool HasOperation(UInt_64 hashId) const;
|
||||
|
||||
bool AddOperation(NetOp *op);
|
||||
|
||||
private:
|
||||
void Execute(EHC *ehc, NetEnd *endpoint, UInt_64 hashId, Serializer<UInt_64> &payload);
|
||||
};
|
||||
}
|
54
include/ehs/io/socket/ehc/Utils.h
Normal file
54
include/ehs/io/socket/ehc/Utils.h
Normal file
@@ -0,0 +1,54 @@
|
||||
#pragma once
|
||||
|
||||
#include <ehs/Serializer.h>
|
||||
|
||||
namespace ehs
|
||||
{
|
||||
enum class EndDisp : UInt_8
|
||||
{
|
||||
ENDPOINT,
|
||||
SERVICE
|
||||
};
|
||||
|
||||
enum class Status : UInt_8
|
||||
{
|
||||
ACTIVE,
|
||||
PENDING,
|
||||
IN_LOCAL_QUEUE,
|
||||
IN_REMOTE_QUEUE,
|
||||
};
|
||||
|
||||
struct Header
|
||||
{
|
||||
bool encrypted = true;
|
||||
UInt_64 id = 0;
|
||||
UInt_64 fragments = 0;
|
||||
UInt_64 fragment = 0;
|
||||
bool ensure = false;
|
||||
EndDisp disposition = EndDisp::ENDPOINT;
|
||||
Char_8 token[64] = {};
|
||||
UInt_64 system = 0;
|
||||
UInt_64 op = 0;
|
||||
};
|
||||
|
||||
struct Packet
|
||||
{
|
||||
Header header;
|
||||
Serializer<UInt_64> payload;
|
||||
};
|
||||
|
||||
struct Insurance
|
||||
{
|
||||
Header header;
|
||||
Serializer<UInt_64> payload;
|
||||
float lastResend;
|
||||
};
|
||||
}
|
||||
|
||||
#ifndef COMMS_IPV4_PAYLOAD
|
||||
#define COMMS_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))
|
||||
#endif
|
Reference in New Issue
Block a user