EHS/include/io/socket/Comms.h
2023-12-18 02:13:20 -08:00

196 lines
4.9 KiB
C++

#pragma once
#include "EHS.h"
#include "Log.h"
#include "BaseObj.h"
#include "Serializer.h"
#include "Vector.h"
#include "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 ver;
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;
Socket hdl;
AddrType type;
Str_8 address;
UInt_16 port;
bool bound;
Version appVer;
EndDisp disposition;
bool dropPackets;
Str_8 id;
UInt_32 hashId;
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, const EndDisp disposition, const Str_8& id, const UInt_64 maxEndpoints);
Comms(const Comms& sock);
Comms& operator=(const Comms& sock);
void Initialize();
void UnInitialize();
void Bind(const Str_8& newAddress, const UInt_16 newPort);
void Connect(const Str_8& address, const UInt_16 port);
bool Disconnect(const EndDisp disposition, const UInt_64 hashId, const Str_8& msg);
bool Disconnect(const EndDisp disposition, const Str_8& id, const Str_8& msg);
void Broadcast(const EndDisp disposition, const Status status, const bool deltaLocked, const bool encrypted,
const bool ensure, const UInt_64 sysHashId, const UInt_64 opHashId,
const Serializer<>& payload);
void Broadcast(const EndDisp disposition, const Status status, const bool deltaLocked, const bool encrypted,
const bool ensure, const Str_8& sysId, const Str_8& opId,
const Serializer<>& payload);
void Poll();
bool IsInitialized() const;
void SetAddressType(const AddrType newType);
AddrType GetAddressType() const;
Str_8 GetAddress() const;
UInt_16 GetPort() const;
bool IsBound() const;
Version GetVersion() const;
Version GetAppVersion() const;
EndDisp GetDisposition() const;
void EnableDropPackets(const bool enable);
bool IsDropPacketsEnabled() const;
Str_8 GetId() const;
UInt_64 GetHashId() const;
bool HasSystem(const UInt_64 hashId) const;
bool HasSystem(const Str_8& id) const;
bool AddSystem(CommsSystem* sys);
CommsSystem* GetSystem(const UInt_64 hashId);
CommsSystem* GetSystem(const Str_8& id);
bool HasEndpoint(const EndDisp disposition, const Status status, const UInt_64 hashId) const;
bool HasEndpoint(const EndDisp disposition, const Status status, const Str_8& id) const;
bool HasEndpoint(const EndDisp disposition, const UInt_64 hashId) const;
bool HasEndpoint(const EndDisp disposition, const Str_8& id) const;
bool HasEndpoint(const Str_8& address, const UInt_16 port) const;
Endpoint* GetEndpoint(const EndDisp disposition, const Status status, const UInt_64 hashId);
Endpoint* GetEndpoint(const EndDisp disposition, const Status status, const Str_8& id);
Endpoint* GetEndpoint(const EndDisp disposition, const UInt_64 hashId);
Endpoint* GetEndpoint(const EndDisp disposition, const Str_8& id);
Endpoint* GetEndpoint(const Str_8& address, const UInt_16 port);
Array<Endpoint*> GetEndpoints(const EndDisp disposition, const Status status);
Array<Endpoint*> GetEndpoints(const EndDisp disposition);
UInt_64 GetEndpointsCount(const EndDisp disposition, const Status status);
UInt_64 GetEndpointsCount(const EndDisp disposition);
UInt_64 GetMaxEndpoints() const;
void SetBlocking(const bool blocking);
bool IsBlocking() const;
void SetMaxTimeout(const float seconds);
float GetMaxTimeout() const;
void SetResendRate(const 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(const EndDisp disposition, const UInt_64 hashId);
bool RemoveEndpoint(const Str_8& address, const UInt_16 port);
bool RemoveEndpoint(const Endpoint* const end);
void PollEndpoints(Vector<Endpoint*>& endpoints);
void Bind_v6(const Str_8& address, const UInt_16 port);
void Bind_v4(const Str_8& address, const UInt_16 port);
UInt_16 Receive(Str_8* address, UInt_16* port, Byte* const data, const UInt_16 size);
};
}