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

184 lines
4.3 KiB
C++

#pragma once
#include "ehs/EHS.h"
#include "ehs/Log.h"
#include "ehs/BaseObj.h"
#include "ehs/Serializer.h"
#include "ehs/Vector.h"
#include "ehs/Array.h"
#include "Socket.h"
#include "UDP.h"
#include "Utils.h"
namespace ehs
{
class CommsSystem;
class Endpoint;
class Comms : public BaseObj
{
private:
friend class Endpoint;
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;
bool dropPackets;
UInt_32 hashId;
Str_8 id;
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);
Comms(const Comms& sock);
Comms& operator=(const Comms& sock);
void Initialize();
void UnInitialize();
void Bind(AddrType newType, const Str_8& newAddress, UInt_16 newPort);
void Connect(AddrType rType, const Str_8& rAddress, UInt_16 rPort);
bool Disconnect(EndDisp endDisp, UInt_64 endHashId, const Str_8& msg);
bool Disconnect(EndDisp endDisp, const Str_8& endId, 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;
void EnableDropPackets(bool enable);
bool IsDropPacketsEnabled() const;
Str_8 GetId() const;
UInt_64 GetHashId() const;
bool HasSystem(UInt_64 sysHashId) const;
bool HasSystem(const Str_8& sysId) const;
bool AddSystem(CommsSystem* sys);
CommsSystem* GetSystem(UInt_64 sysHashId);
CommsSystem* GetSystem(const Str_8& sysId);
bool HasEndpoint(EndDisp endDisp, Status endStatus, UInt_64 endHashId) const;
bool HasEndpoint(EndDisp endDisp, Status endStatus, const Str_8& endId) const;
bool HasEndpoint(EndDisp endDisp, UInt_64 endHashId) const;
bool HasEndpoint(EndDisp endDisp, const Str_8& endId) const;
bool HasEndpoint(const Str_8& rAddress, UInt_16 rPort) const;
Endpoint* GetEndpoint(EndDisp endDisp, Status endStatus, UInt_64 endHashId);
Endpoint* GetEndpoint(EndDisp endDisp, Status endStatus, const Str_8& endId);
Endpoint* GetEndpoint(EndDisp endDisp, UInt_64 endHashId);
Endpoint* GetEndpoint(EndDisp endDisp, const Str_8& endId);
Endpoint* GetEndpoint(const Str_8& rAddress, UInt_16 rPort);
Array<Endpoint*> GetEndpoints(EndDisp endDisp, Status endStatus);
Array<Endpoint*> GetEndpoints(EndDisp endDisp);
UInt_64 GetEndpointsCount(EndDisp endDisp, Status endStatus);
UInt_64 GetEndpointsCount(EndDisp endDisp);
UInt_64 GetMaxEndpoints() const;
void SetBlocking(bool blocking);
bool IsBlocking() const;
void SetMaxTimeout(float seconds);
float GetMaxTimeout() const;
void SetResendRate(float seconds);
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);
bool RemoveEndpoint(const Str_8& address, UInt_16 port);
bool RemoveEndpoint(const Endpoint* end);
void PollEndpoints();
};
}