EHS/include/ehs/io/socket/Comms.h

184 lines
4.3 KiB
C
Raw Normal View History

2023-12-17 03:29:08 -08:00
#pragma once
2024-01-14 09:38:57 -08:00
#include "ehs/EHS.h"
#include "ehs/Log.h"
#include "ehs/BaseObj.h"
#include "ehs/Serializer.h"
#include "ehs/Vector.h"
#include "ehs/Array.h"
2023-12-17 03:29:08 -08:00
#include "Socket.h"
#include "UDP.h"
#include "Utils.h"
2023-12-17 15:56:13 -08:00
namespace ehs
2023-12-17 03:29:08 -08:00
{
class CommsSystem;
class Endpoint;
class Comms : public BaseObj
{
private:
2023-12-18 02:13:20 -08:00
friend class Endpoint;
static const Version version;
2023-12-17 03:29:08 -08:00
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;
2023-12-17 03:29:08 -08:00
Version appVer;
EndDisp disposition;
bool dropPackets;
UInt_32 hashId;
Str_8 id;
2023-12-17 03:29:08 -08:00
Byte* buffer;
UInt_32 bufferSize;
Array<CommsSystem*> systems;
Vector<Endpoint*> endpoints;
UInt_32 maxEndpoints;
UInt_64 lastTSC;
float delta;
float maxTimeout;
float resendRate;
bool (*connectedCb)(Comms*, Endpoint*);
void (*activeCb)(Comms*, Endpoint*);
void (*disconnectedCb)(Comms*, Endpoint*);
public:
~Comms() override;
Comms();
Comms(const Version& ver, EndDisp disposition, const Str_8& id, UInt_64 maxEndpoints);
2023-12-17 03:29:08 -08:00
Comms(const Comms& sock);
Comms& operator=(const Comms& sock);
void Initialize();
void UnInitialize();
void Bind(AddrType newType, const Str_8& newAddress, UInt_16 newPort);
2023-12-17 03:29:08 -08:00
void Connect(AddrType rType, const Str_8& rAddress, UInt_16 rPort);
2023-12-17 03:29:08 -08:00
bool Disconnect(EndDisp endDisp, UInt_64 endHashId, const Str_8& msg);
2023-12-17 03:29:08 -08:00
bool Disconnect(EndDisp endDisp, const Str_8& endId, const Str_8& msg);
2023-12-17 03:29:08 -08:00
void Broadcast(EndDisp endDisp, Status endStatus, bool deltaLocked, bool encrypted,
bool ensure, UInt_64 sysHashId, UInt_64 opHashId,
2023-12-17 03:29:08 -08:00
const Serializer<>& payload);
void Broadcast(EndDisp endDisp, Status endStatus, bool deltaLocked, bool encrypted,
bool ensure, const Str_8& sysId, const Str_8& opId,
2023-12-17 03:29:08 -08:00
const Serializer<>& payload);
void Poll();
bool IsInitialized() const;
AddrType GetLocalAddressType() const;
2023-12-17 03:29:08 -08:00
Str_8 GetLocalAddress() const;
2023-12-17 03:29:08 -08:00
UInt_16 GetLocalPort() const;
2023-12-17 03:29:08 -08:00
bool IsBound() const;
static Version GetVersion();
2023-12-17 03:29:08 -08:00
Version GetAppVersion() const;
EndDisp GetDisposition() const;
void EnableDropPackets(bool enable);
2023-12-17 03:29:08 -08:00
bool IsDropPacketsEnabled() const;
Str_8 GetId() const;
UInt_64 GetHashId() const;
bool HasSystem(UInt_64 sysHashId) const;
2023-12-17 03:29:08 -08:00
bool HasSystem(const Str_8& sysId) const;
2023-12-17 03:29:08 -08:00
bool AddSystem(CommsSystem* sys);
CommsSystem* GetSystem(UInt_64 sysHashId);
2023-12-17 03:29:08 -08:00
CommsSystem* GetSystem(const Str_8& sysId);
2023-12-17 03:29:08 -08:00
bool HasEndpoint(EndDisp endDisp, Status endStatus, UInt_64 endHashId) const;
2023-12-17 03:29:08 -08:00
bool HasEndpoint(EndDisp endDisp, Status endStatus, const Str_8& endId) const;
2023-12-17 03:29:08 -08:00
bool HasEndpoint(EndDisp endDisp, UInt_64 endHashId) const;
2023-12-17 03:29:08 -08:00
bool HasEndpoint(EndDisp endDisp, const Str_8& endId) const;
2023-12-17 03:29:08 -08:00
bool HasEndpoint(const Str_8& rAddress, UInt_16 rPort) const;
2023-12-17 03:29:08 -08:00
Endpoint* GetEndpoint(EndDisp endDisp, Status endStatus, UInt_64 endHashId);
2023-12-17 03:29:08 -08:00
Endpoint* GetEndpoint(EndDisp endDisp, Status endStatus, const Str_8& endId);
2023-12-17 03:29:08 -08:00
Endpoint* GetEndpoint(EndDisp endDisp, UInt_64 endHashId);
2023-12-17 03:29:08 -08:00
Endpoint* GetEndpoint(EndDisp endDisp, const Str_8& endId);
2023-12-17 03:29:08 -08:00
Endpoint* GetEndpoint(const Str_8& rAddress, UInt_16 rPort);
2023-12-17 03:29:08 -08:00
Array<Endpoint*> GetEndpoints(EndDisp endDisp, Status endStatus);
2023-12-17 03:29:08 -08:00
Array<Endpoint*> GetEndpoints(EndDisp endDisp);
2023-12-17 03:29:08 -08:00
UInt_64 GetEndpointsCount(EndDisp endDisp, Status endStatus);
2023-12-17 03:29:08 -08:00
UInt_64 GetEndpointsCount(EndDisp endDisp);
2023-12-17 03:29:08 -08:00
UInt_64 GetMaxEndpoints() const;
void SetBlocking(bool blocking);
2023-12-17 03:29:08 -08:00
bool IsBlocking() const;
void SetMaxTimeout(float seconds);
2023-12-17 03:29:08 -08:00
float GetMaxTimeout() const;
void SetResendRate(float seconds);
2023-12-17 03:29:08 -08:00
float GetResendRate() const;
void SetConnectedCb(bool (*newCb)(Comms*, Endpoint*));
void SetActiveCb(void (*newCb)(Comms*, Endpoint*));
void SetDisconnectedCb(void (*newCb)(Comms*, Endpoint*));
private:
void UpdateQueue(UInt_64 active);
void UpdateQueue();
bool RemoveEndpoint(EndDisp disposition, UInt_64 hashId);
2023-12-17 03:29:08 -08:00
bool RemoveEndpoint(const Str_8& address, UInt_16 port);
2023-12-17 03:29:08 -08:00
bool RemoveEndpoint(const Endpoint* end);
2023-12-17 03:29:08 -08:00
void PollEndpoints();
2023-12-17 03:29:08 -08:00
};
}