2024-10-06 20:08:22 -07:00
|
|
|
#pragma once
|
|
|
|
|
2024-10-06 22:54:29 -07:00
|
|
|
#include "ehs/io/socket/ehc/NetUtils.h"
|
|
|
|
#include "ehs/io/socket/ehc/NetEnc.h"
|
2024-10-06 20:08:22 -07:00
|
|
|
#include "ehs/Serializer.h"
|
|
|
|
#include "ehs/Vector.h"
|
|
|
|
#include "ehs/Array.h"
|
|
|
|
#include "Socket.h"
|
|
|
|
#include "UDP.h"
|
|
|
|
|
|
|
|
namespace ehs
|
|
|
|
{
|
2024-10-06 22:54:29 -07:00
|
|
|
class NetSys;
|
2024-10-06 20:08:22 -07:00
|
|
|
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:
|
2024-10-06 22:54:29 -07:00
|
|
|
friend class NetEnc;
|
2024-10-06 20:08:22 -07:00
|
|
|
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;
|
2024-10-06 22:54:29 -07:00
|
|
|
Array<NetEnc *> encryptions;
|
|
|
|
Array<NetSys *> systems;
|
2024-10-06 20:08:22 -07:00
|
|
|
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);
|
|
|
|
|
2024-10-06 22:54:29 -07:00
|
|
|
void Broadcast(EndDisp endDisp, Status endStatus, bool deltaLocked, UInt_64 encHashId,
|
2024-10-06 20:08:22 -07:00
|
|
|
bool ensure, UInt_64 sysHashId, UInt_64 opHashId,
|
2024-10-06 22:54:29 -07:00
|
|
|
const Serializer<UInt_64> &payload);
|
2024-10-06 20:08:22 -07:00
|
|
|
|
2024-10-06 22:54:29 -07:00
|
|
|
void Broadcast(EndDisp endDisp, Status endStatus, bool deltaLocked, const Str_8 &encId,
|
|
|
|
bool ensure, const Str_8 &sysId, const Str_8 &opId,
|
|
|
|
const Serializer<UInt_64> &payload);
|
2024-10-06 20:08:22 -07:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2024-10-06 22:54:29 -07:00
|
|
|
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;
|
|
|
|
|
2024-10-06 20:08:22 -07:00
|
|
|
bool HasSystem(UInt_64 sysHashId) const;
|
|
|
|
|
|
|
|
bool HasSystem(const Str_8& sysId) const;
|
|
|
|
|
2024-10-06 22:54:29 -07:00
|
|
|
bool AddSystem(NetSys *sys);
|
2024-10-06 20:08:22 -07:00
|
|
|
|
2024-10-06 22:54:29 -07:00
|
|
|
NetSys* GetSystem(UInt_64 sysHashId) const;
|
2024-10-06 20:08:22 -07:00
|
|
|
|
2024-10-06 22:54:29 -07:00
|
|
|
NetSys* GetSystem(const Str_8& sysId) const;
|
2024-10-06 20:08:22 -07:00
|
|
|
|
|
|
|
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();
|
|
|
|
};
|
|
|
|
}
|