Finished implementing, now for the testing phase.

This commit is contained in:
Karutoh 2025-01-26 21:43:17 -08:00
parent 7bc4b9977d
commit 981b40d3b1
47 changed files with 2070 additions and 1597 deletions

View File

@ -134,7 +134,7 @@ set(EHS_SOURCES
include/ehs/io/socket/ehc/NetUtils.h
src/io/socket/EHC.cpp include/ehs/io/socket/EHC.h
src/io/socket/ehc/NetFrags.cpp include/ehs/io/socket/ehc/NetFrags.h
src/io/socket/ehc/NetFrag.cpp include/ehs/io/socket/ehc/NetFrag.h
src/io/socket/ehc/NetEnd.cpp include/ehs/io/socket/ehc/NetEnd.h
src/io/socket/ehc/NetSys.cpp include/ehs/io/socket/ehc/NetSys.h
src/io/socket/ehc/NetOp.cpp include/ehs/io/socket/ehc/NetOp.h
@ -187,6 +187,13 @@ set(EHS_SOURCES
include/ehs/io/Directory.h
include/ehs/io/socket/ehc/NetEnc.h
src/io/socket/ehc/NetEnc.cpp
include/ehs/io/socket/ehc/NetChannel.h
src/io/socket/ehc/NetChannel.cpp
include/ehs/io/socket/ehc/NetServerCh.h
include/ehs/io/socket/ehc/NetClientCh.h
src/io/socket/ehc/NetClientCh.cpp
src/io/socket/ehc/NetServerCh.cpp
src/io/socket/ehc/NetUtils.cpp
)
if (IS_OS_WINDOWS)

View File

@ -8,7 +8,7 @@ namespace ehs
class EHS_LIB_IO BaseDNS
{
public:
static Str_8 Resolve(AddrType type, const Str_8 &hostname);
static Str_8 Resolve(IP type, const Str_8 &hostname);
/// Resolves a hostname to an ip address.
/// @param [in] hostname The given hostname to resolve.

View File

@ -12,7 +12,7 @@ namespace ehs
class EHS_LIB_IO BaseTCP
{
protected:
AddrType addrType;
IP ip;
Str_8 localAddr;
UInt_16 localPort;
Str_8 remoteHostName;
@ -35,7 +35,7 @@ namespace ehs
/// Properly initializes the socket.
/// @param [in] type The ip version to initialize the socket with.
BaseTCP(AddrType addrType);
BaseTCP(IP ip);
BaseTCP(BaseTCP&& tcp) noexcept;
@ -104,7 +104,7 @@ namespace ehs
/// Retrieves the sockets ip version.
/// @returns The ip version.
AddrType GetAddressType() const;
IP GetAddressType() const;
/// Retrieves the bound ip address.
/// @returns The ip address.

View File

@ -9,9 +9,7 @@ namespace ehs
class EHS_LIB_IO BaseUDP
{
protected:
AddrType type;
Str_8 address;
UInt_16 port;
Endpoint localEndpoint;
bool bound;
public:
@ -22,7 +20,7 @@ namespace ehs
/// Properly initializes the socket.
/// @param [in] type The ip version to initialize the socket with.
BaseUDP(AddrType type);
BaseUDP(IP version);
BaseUDP(BaseUDP&& udp) noexcept;
@ -40,13 +38,13 @@ namespace ehs
/// @param [in] address The ip address to bind to.
/// @param [in] port The port to bind to.
/// @note Used for servers.
virtual void Bind(AddrType type, const Str_8& address, UInt_16 port) = 0;
virtual void Bind(const Endpoint &endpoint) = 0;
/// Sends data to the endpoint.
/// @param [in] type The ip version of the endpoint.
/// @param [in] address The ip address of the endpoint.
/// @param [in] port The port of the endpoint is bound to.
virtual UInt_64 Send(AddrType type, const Str_8& address, UInt_16 port, const Byte* data, UInt_64 size) = 0;
virtual UInt_64 Send(const Endpoint &endpoint, const Byte *data, UInt_64 size) = 0;
/// Receives data from the endpoint.
/// @param [out] type The ip version of the endpoint.
@ -55,7 +53,7 @@ namespace ehs
/// @param [out] data The incoming data from the endpoint.
/// @param [in] size The max size of the buffer in bytes to store the data.
/// @returns The size of the incoming data in bytes.
virtual UInt_64 Receive(AddrType* type, Str_8* address, UInt_16* port, Byte* data, UInt_64 size) = 0;
virtual UInt_64 Receive(Endpoint *endpoint, Byte *data, UInt_64 size) = 0;
/// Retrieves whether or not this socket is bound to an ip address and port.
/// @returns The result.
@ -73,17 +71,7 @@ namespace ehs
virtual bool IsIPv6Only() const = 0;
/// Retrieves the bound ip version.
/// @returns The result.
AddrType GetLocalAddressType() const;
/// Retrieves the bound ip address.
/// @returns The bound ip address.
Str_8 GetLocalAddress() const;
/// Retrieves the bound port.
/// @returns The bound port.
UInt_16 GetLocalPort() const;
Endpoint GetLocalEndpoint() const;
/// Retrieves whether or not this socket was initialized.
/// @returns The result.

View File

@ -10,7 +10,7 @@ namespace ehs
class EHS_LIB_IO DNS final : public BaseDNS
{
public:
static Str_8 Resolve(AddrType type, const Str_8 &hostname);
static Str_8 Resolve(IP type, const Str_8 &hostname);
static Str_8 Resolve(const Str_8 &hostname);
};

View File

@ -7,7 +7,7 @@ namespace ehs
class EHS_LIB_IO DNS final : public BaseDNS
{
public:
static Str_8 Resolve(AddrType type, const Str_8 &hostname);
static Str_8 Resolve(IP type, const Str_8 &hostname);
static Str_8 Resolve(const Str_8 &hostname);
};

View File

@ -1,78 +1,41 @@
#pragma once
#include "ehs/io/socket/ehc/NetUtils.h"
#include "ehs/io/socket/ehc/NetEnc.h"
#include "ehs/Serializer.h"
#include "ehs/Vector.h"
#include "ehs/Array.h"
#include "Socket.h"
#include "UDP.h"
namespace ehs
{
class NetSys;
class NetServerCh;
class NetClientCh;
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 NetEnc;
friend class NetChannel;
friend class NetServerCh;
friend class NetClientCh;
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<NetEnc *> encryptions;
Array<NetSys *> 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;
Array<NetEnc *> encryptions;
Array<NetServerCh *> servers;
Array<NetClientCh *> clients;
public:
~EHC();
EHC();
EHC(const Version &ver, Str_8 name, UInt_64 maxEndpoints);
EHC(const Version &ver, Str_8 name);
EHC(IP version = IP::V6);
EHC(EHC &&sock) noexcept;
@ -86,143 +49,47 @@ namespace ehs
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, UInt_64 encHashId,
bool ensure, UInt_64 sysHashId, UInt_64 opHashId,
const Serializer<UInt_64> &payload);
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);
void Bind(const Endpoint &endpoint);
void Poll();
bool IsInitialized() const;
AddrType GetLocalAddressType() const;
Str_8 GetLocalAddress() const;
UInt_16 GetLocalPort() const;
Endpoint GetLocalEndpoint() 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 HasEncryption(UInt_64 encHashId) const;
bool HasEncryption(const Str_8& encId) const;
bool AddEncryption(NetEnc *enc);
NetEnc* GetEncryption(UInt_64 encHashId) const;
bool AddServer(NetServerCh *server);
NetEnc* GetEncryption(const Str_8& encId) const;
bool HasSystem(UInt_64 sysHashId) const;
bool HasSystem(const Str_8& sysId) const;
bool AddSystem(NetSys *sys);
NetSys* GetSystem(UInt_64 sysHashId) const;
NetSys* 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);
bool AddClient(NetClientCh *channel);
private:
void GenerateToken(Char_8 in[64]);
bool HasEncryption(UInt_64 encId) const;
void UpdateQueue(UInt_64 active);
bool HasEncryption(const Str_8& encName) const;
void UpdateQueue();
NetEnc* GetEncryption(UInt_64 encId) const;
bool RemoveEndpoint(EndDisp disposition, const Char_8 token[64]);
NetEnc* GetEncryption(const Str_8& encName) const;
bool RemoveEndpoint(const Str_8& address, UInt_16 port);
bool HasServer(UInt_64 serverId) const;
bool RemoveEndpoint(const NetEnd* end);
bool HasServer(const Str_8& serverName) const;
void PollEndpoints();
NetServerCh *GetServer(UInt_64 serverId) const;
NetServerCh *GetServer(const Str_8& serverName) const;
bool HasClient(UInt_64 clientId) const;
bool HasClient(const Str_8& clientName) const;
NetClientCh *GetClient(UInt_64 clientId) const;
NetClientCh *GetClient(const Str_8& clientName) const;
};
}

View File

@ -23,7 +23,7 @@ namespace ehs
SSL();
SSL(const AddrType type);
SSL(const IP type);
SSL(TCP&& tcp) noexcept;

View File

@ -1,7 +1,10 @@
#pragma once
#include "ehs/Str.h"
#ifndef EHS_IPV4_HEADER
#define EHS_IPV4_HEADER 60
#include <string>
#endif
#ifndef EHS_IPV6_HEADER
@ -22,10 +25,10 @@
namespace ehs
{
enum class AddrType
enum class IP
{
IPV6,
IPV4
V4,
V6
};
enum class ContentType
@ -41,6 +44,13 @@ namespace ehs
NONE
};
struct Endpoint
{
IP version = IP::V6;
Str_8 address;
UInt_16 port = 0;
};
#if defined(EHS_OS_WINDOWS)
typedef UInt_64 Socket;
#define EHS_INVALID_SOCKET EHS_UINT_64_MAX

View File

@ -22,7 +22,7 @@ namespace ehs
/// Default members initialization.
TCP();
TCP(AddrType addrType);
TCP(IP IP);
TCP(TCP&& tcp) noexcept;

View File

@ -22,7 +22,7 @@ namespace ehs
/// Default members initialization.
TCP();
TCP(AddrType addrType);
TCP(IP IP);
TCP(TCP&& tcp) noexcept;

View File

@ -19,7 +19,7 @@ namespace ehs
UDP();
/// Default members initialization.
UDP(AddrType type);
UDP(IP version);
UDP(UDP&& udp) noexcept;
@ -41,7 +41,7 @@ namespace ehs
/// @param [in] address The local IPv4 or IPv6 address to bind to. Resolves domain names. The given address can be empty, "127.0.0.1", or "localhost" to automatically find the appropriate device.
/// @param [in] port The port to bind to.
/// @note Requires the port given to be forwarded if this is called.
void Bind(AddrType type, const Str_8& address, UInt_16 port) override;
void Bind(const Endpoint &endpoint) override;
/// Sends data using a C-style byte array.
/// @param [in] addr The remote Ipv4 or Ipv6 address to send to. Resolves domain names. The given address can be empty, "127.0.0.1", or "localhost" to automatically find the appropriate device.
@ -49,7 +49,7 @@ namespace ehs
/// @param [in] data The C-style byte array to send.
/// @param [in] size The size of the C-style byte array.
/// @note The size of data to be sent cannot exceed "UDP::maxPayloadIpv4" or "UDP::maxPayloadIpv6".
UInt_64 Send(AddrType type, const Str_8& address, UInt_16 port, const Byte* data, UInt_64 size) override;
UInt_64 Send(const Endpoint &endpoint, const Byte* data, UInt_64 size) override;
/// Receives data using the packet helper class.
/// @param [out] addr The Ipv4 or Ipv6 address of the sender.
@ -58,7 +58,7 @@ namespace ehs
/// @param [in] size The size of the pre-allocated C-style byte array.
/// @returns The size of the data received.
/// @warning The provided C-style byte array must be freed when finished using.
UInt_64 Receive(AddrType* type, Str_8* address, UInt_16* port, Byte* data, UInt_64 size) override;
UInt_64 Receive(Endpoint *endpoint, Byte* data, UInt_64 size) override;
/// Sets whether or not receiving data blocks the next task.
/// @param [in] blocking Whether or not to block.

View File

@ -19,7 +19,7 @@ namespace ehs
UDP();
/// Default members initialization.
UDP(const AddrType addrType);
UDP(const IP IP);
UDP(UDP&& udp) noexcept;
@ -41,7 +41,7 @@ namespace ehs
/// @param [in] address The local IPv4 or IPv6 address to bind to. Resolves domain names. The given address can be empty, "127.0.0.1", or "localhost" to automatically find the appropriate device.
/// @param [in] port The port to bind to.
/// @note Requires the port given to be forwarded if this is called.
void Bind(AddrType type, const Str_8& address, UInt_16 port) override;
void Bind(IP type, const Str_8& address, UInt_16 port) override;
/// Sends data using a C-style byte array.
/// @param [in] addr The remote Ipv4 or Ipv6 address to send to. Resolves domain names. The given address can be empty, "127.0.0.1", or "localhost" to automatically find the appropriate device.
@ -49,7 +49,7 @@ namespace ehs
/// @param [in] data The C-style byte array to send.
/// @param [in] size The size of the C-style byte array.
/// @note The size of data to be sent cannot exceed "UDP::maxPayloadIpv4" or "UDP::maxPayloadIpv6".
UInt_64 Send(AddrType type, const Str_8& addr, UInt_16 port, const Byte* data, UInt_64 size) override;
UInt_64 Send(IP type, const Str_8& addr, UInt_16 port, const Byte* data, UInt_64 size) override;
/// Receives data using the packet helper class.
/// @param [out] addr The Ipv4 or Ipv6 address of the sender.
@ -58,7 +58,7 @@ namespace ehs
/// @param [in] size The size of the pre-allocated C-style byte array.
/// @returns The size of the data received.
/// @warning The provided C-style byte array must be freed when finished using.
UInt_64 Receive(AddrType* type, Str_8* addr, UInt_16* port, Byte* data, UInt_64 size) override;
UInt_64 Receive(IP* type, Str_8* addr, UInt_16* port, Byte* data, UInt_64 size) override;
/// Sets whether or not receiving data blocks the next task.
/// @param [in] blocking Whether or not to block.

View File

@ -0,0 +1,94 @@
#pragma once
#include "ehs/Str.h"
#include "ehs/Array.h"
#include "ehs/io/socket/Socket.h"
#include "NetUtils.h"
namespace ehs
{
class EHC;
class NetEnd;
class NetSys;
class NetChannel
{
protected:
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;
private:
friend class EHC;
EHC *owner;
UInt_64 id;
Str_8 name;
Version version;
float maxTimeout;
float resendRate;
bool dropPackets;
Array<NetSys *> systems;
public:
virtual ~NetChannel();
NetChannel();
NetChannel(Str_8 name, const Version &version);
NetChannel(NetChannel &&channel) noexcept;
NetChannel(const NetChannel &channel);
NetChannel &operator=(NetChannel &&channel) noexcept;
NetChannel &operator=(const NetChannel &channel);
EHC *GetOwner() const;
UInt_64 GetId() const;
Str_8 GetName() const;
Version GetVersion() const;
void SetMaxTimeout(float seconds);
float GetMaxTimeout() const;
void SetResendRate(float seconds);
float GetResendRate() const;
void EnableDropPackets(bool enable);
bool IsDropPacketsEnabled() const;
bool AddSystem(NetSys *sys);
bool IsValid() const;
private:
virtual void Process(const float &delta, const Endpoint &endpoint, const Header &header, Serializer<UInt_64> &payload);
virtual void Poll(const float &delta);
protected:
bool HasSystem(UInt_64 sysHashId) const;
bool HasSystem(const Str_8& sysId) const;
NetSys* GetSystem(UInt_64 sysHashId) const;
NetSys* GetSystem(const Str_8& sysId) const;
};
}

View File

@ -0,0 +1,81 @@
#pragma once
#include "NetChannel.h"
#include "ehs/io/socket/Socket.h"
#include "ehs/io/socket/ehc/NetEnc.h"
#include "ehs/io/socket/ehc/NetFrag.h"
namespace ehs
{
class NetClientCh : public NetChannel
{
private:
friend class EHC;
Endpoint remoteEndpoint;
Char_8 token[64];
NetStatus status;
UInt_64 queueSlot;
float deltaDuration;
float deltaRate;
float lastPing;
float latency;
float timeout;
UInt_64 nextSendId;
Vector<Insurance> sent;
UInt_64 nextRecvId;
Vector<NetFrag> received;
public:
~NetClientCh();
NetClientCh();
NetClientCh(Str_8 name, const Version &version, Endpoint remoteEndpoint);
NetClientCh(NetClientCh &&client) noexcept;
NetClientCh(const NetClientCh &client);
NetClientCh &operator=(NetClientCh &&client) noexcept;
NetClientCh &operator=(const NetClientCh &client);
virtual void OnConnected(Serializer<UInt_64> data);
virtual void OnActive(Serializer<UInt_64> data);
virtual void OnQueueUpdate(Serializer<UInt_64> data);
virtual void OnDisconnected(Serializer<UInt_64> data);
virtual void OnRejected(Serializer<UInt_64> data);
virtual void OnTimeout(Serializer<UInt_64> data);
void Connect(const Endpoint &endpoint, const Serializer<UInt_64>& payload);
bool Disconnect(const Serializer<UInt_64>& payload);
void Send(bool deltaLocked, UInt_64 encId, bool ensure, UInt_64 sysId, UInt_64 opId, const Serializer<UInt_64>& payload);
void Send(bool deltaLocked, const Str_8 &encName, bool ensure, const Str_8& sysName, const Str_8& opName, const Serializer<UInt_64>& payload);
NetStatus GetStatus() const;
private:
void Process(const float &delta, const Endpoint &endpoint, const Header &header, Serializer<UInt_64> &payload) override;
void Poll(const float &delta) override;
NetFrag FragmentData(IP version, const Header& header, const Serializer<>& data);
void Send(NetEnc *enc, const Header& header, const Serializer<>& payload);
void Pong(float delta);
void RemoveInsurance(UInt_64 msgId, UInt_64 fragment);
void AddReceived(const Header& header, const Serializer<>& payload);
};
}

View File

@ -1,6 +1,7 @@
#pragma once
#include "ehs/Str.h"
#include "ehs/Version.h"
namespace ehs
{
@ -11,16 +12,16 @@ namespace ehs
private:
friend class EHC;
EHC *owner;
UInt_64 hashId;
Str_8 id;
UInt_64 id;
Str_8 name;
Version version;
public:
virtual ~NetEnc() = default;
NetEnc();
NetEnc(Str_8 id);
NetEnc(Str_8 name, const Version &version);
NetEnc(NetEnc &&enc) noexcept;
@ -30,11 +31,11 @@ namespace ehs
NetEnc &operator=(const NetEnc &enc);
EHC *GetOwner() const;
UInt_64 GetId() const;
UInt_64 GetHashId() const;
Str_8 GetName() const;
Str_8 GetId() const;
Version GetVersion() const;
virtual void Encrypt(Byte *data, UInt_64 size) const;

View File

@ -1,7 +1,7 @@
#pragma once
#include "NetUtils.h"
#include "NetFrags.h"
#include "NetFrag.h"
#include "ehs/Str.h"
#include "ehs/Vector.h"
@ -10,27 +10,23 @@
namespace ehs
{
class EHC;
class NetServerCh;
class NetEnd
{
private:
friend class EHC;
friend class NetServerCh;
EHC* owner;
EndDisp disposition;
UInt_64 hashName;
NetServerCh* owner;
UInt_64 id;
Str_8 name;
Status status;
Architecture arch;
NetStatus status;
Char_8 token[64];
UInt_64 nextSendId;
Vector<Insurance> sent;
UInt_64 nextRecvId;
Vector<NetFrags> received;
AddrType type;
Str_8 address;
UInt_16 port;
Vector<NetFrag> received;
Endpoint endpoint;
float deltaDuration;
float deltaRate;
float timeout;
@ -42,9 +38,9 @@ namespace ehs
public:
NetEnd();
NetEnd(EndDisp disposition, Str_8 id, Architecture arch, AddrType type, Str_8 address, UInt_16 port);
NetEnd(Str_8 id, Endpoint endpoint);
NetEnd(AddrType type, Str_8 address, UInt_16 port);
NetEnd(Endpoint endpoint);
NetEnd(NetEnd &&end) noexcept;
@ -54,15 +50,11 @@ namespace ehs
NetEnd &operator=(const NetEnd &end);
EndDisp GetDisposition() const;
UInt_64 GetHashName() const;
UInt_64 GetId() const;
Str_8 GetName() const;
Status GetStatus() const;
Architecture GetArchitecture() const;
NetStatus GetStatus() const;
UInt_64 GetNextSendId() const;
@ -73,7 +65,7 @@ namespace ehs
/// @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, UInt_64 encHashId, bool ensure, UInt_64 sys, UInt_64 op, const Serializer<UInt_64>& payload);
void Send(bool deltaLocked, UInt_64 encId, bool ensure, UInt_64 sysId, UInt_64 opId, 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.
@ -82,13 +74,11 @@ namespace ehs
/// @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, const Str_8 &encID, bool ensure, const Str_8& sys, const Str_8& op, const Serializer<UInt_64>& payload);
void Send(bool deltaLocked, const Str_8 &encName, bool ensure, const Str_8& sysName, const Str_8& opName, const Serializer<UInt_64>& payload);
UInt_64 GetNextRecvId() const;
Str_8 GetAddress() const;
UInt_16 GetPort() const;
Endpoint GetEndpoint() const;
void SetDeltaRate(float newDeltaRate);
@ -107,23 +97,21 @@ namespace ehs
private:
void Poll(float delta);
void SetStatus(Status newStatus);
void SetStatus(NetStatus newStatus);
void RemoveInsurance(UInt_64 msgId, UInt_64 fragment);
void AddReceived(const Header& header, const Serializer<>& payload);
Vector<NetFrags>* GetReceived();
Vector<NetFrag>* GetReceived();
void Ping(float delta);
void Pong(float delta);
void SetLatency(float newLatency);
void SetQueueSlot(UInt_64 slot);
NetFrags FragmentData(const Header& header, const Serializer<>& data);
NetFrag FragmentData(const Header& header, const Serializer<>& data);
void Send(const Header& header, const Serializer<>& payload);

View File

@ -0,0 +1,43 @@
#pragma once
#include "NetUtils.h"
#include <ehs/Serializer.h>
namespace ehs
{
class NetFrag
{
private:
Header header;
Serializer<UInt_64>* data;
UInt_64 size;
public:
~NetFrag();
NetFrag();
NetFrag(const Header &header, const Serializer<UInt_64> &payload);
NetFrag(const Header &header, UInt_64 size);
NetFrag(NetFrag &&frags) noexcept;
NetFrag(const NetFrag &frags);
NetFrag &operator=(NetFrag &&frags) noexcept;
NetFrag &operator=(const NetFrag &frags);
operator Serializer<UInt_64> *() const;
Header GetHeader() const;
UInt_64 Size() const;
bool IsComplete() const;
Packet Combine() const;
};
}

View File

@ -1,43 +0,0 @@
#pragma once
#include "NetUtils.h"
#include <ehs/Serializer.h>
namespace ehs
{
class NetFrags
{
private:
Header header;
Serializer<UInt_64>* data;
UInt_64 size;
public:
~NetFrags();
NetFrags();
NetFrags(const Header &header, const Serializer<UInt_64> &payload);
NetFrags(const Header &header, UInt_64 size);
NetFrags(NetFrags &&frags) noexcept;
NetFrags(const NetFrags &frags);
NetFrags &operator=(NetFrags &&frags) noexcept;
NetFrags &operator=(const NetFrags &frags);
operator Serializer<UInt_64> *() const;
Header GetHeader() const;
UInt_64 Size() const;
bool IsComplete() const;
Packet Combine() const;
};
}

View File

@ -5,7 +5,7 @@
namespace ehs
{
class EHC;
class NetChannel;
class NetSys;
class NetEnd;
@ -37,6 +37,6 @@ namespace ehs
UInt_64 GetHashId() const;
private:
virtual void Process(EHC *ehc, NetEnd *endpoint, NetSys *sys, Serializer<UInt_64> &payload);
virtual void Process(NetChannel *channel, NetEnd *endpoint, NetSys *sys, Serializer<UInt_64> &payload);
};
}

View File

@ -0,0 +1,103 @@
#pragma once
#include "NetChannel.h"
namespace ehs
{
class NetEnd;
class NetServerCh : public NetChannel
{
private:
friend class EHC;
Vector<NetEnd *> endpoints;
UInt_64 maxEndpoints;
public:
~NetServerCh() override;
NetServerCh(UInt_64 maxEndpoints = 0);
NetServerCh(NetServerCh &&server) noexcept;
NetServerCh(const NetServerCh &server);
NetServerCh &operator=(NetServerCh &&server) noexcept;
NetServerCh &operator=(const NetServerCh &server);
virtual bool OnEndpointConnect(NetEnd *endpoint, Serializer<UInt_64> payload);
virtual Serializer<UInt_64> OnEndpointAccepted(NetEnd *endpoint);
virtual void OnEndpointDisconnect(NetEnd *endpoint, Serializer<UInt_64> payload);
virtual void OnEndpointTimeout(NetEnd *endpoint);
virtual void OnEndpointActive(NetEnd *endpoint);
virtual Serializer<UInt_64> OnShutdown();
void Broadcast(NetStatus endStatus, bool deltaLocked, UInt_64 encHashId,
bool ensure, UInt_64 sysHashId, UInt_64 opHashId,
const Serializer<UInt_64> &payload);
void Broadcast(NetStatus endStatus, bool deltaLocked, const Str_8 &encId,
bool ensure, const Str_8 &sysId, const Str_8 &opId,
const Serializer<UInt_64> &payload);
bool HasEndpoint(NetStatus endStatus, const Char_8 token[64]) const;
bool HasEndpoint(NetStatus endStatus, UInt_64 hashId) const;
bool HasEndpoint(NetStatus endStatus, 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 Endpoint &endpoint) const;
NetEnd *GetEndpoint(NetStatus endStatus, const Char_8 token[64]) const;
NetEnd *GetEndpoint(NetStatus endStatus, UInt_64 hashId) const;
NetEnd *GetEndpoint(NetStatus endStatus, const Str_8 &id) const;
NetEnd *GetEndpoint(const Char_8 token[64]) const;
NetEnd *GetEndpoint(UInt_64 hashId) const;
NetEnd *GetEndpoint(const Str_8 &id) const;
NetEnd *GetEndpoint(const Endpoint &endpoint) const;
Array<NetEnd *> GetEndpoints(NetStatus endStatus);
UInt_64 GetEndpointsCount(NetStatus endStatus);
UInt_64 GetMaxEndpoints() const;
private:
void Process(const float &delta, const Endpoint &endpoint, const Header &header, Serializer<UInt_64> &payload) override;
void GenerateToken(Char_8 in[64]);
void UpdateQueue(UInt_64 active);
void UpdateQueue();
bool RemoveEndpoint(const Char_8 token[64]);
bool RemoveEndpoint(const Endpoint &endpoint);
bool RemoveEndpoint(const NetEnd* end);
void Poll(const float &delta) override;
void Shutdown();
};
}

View File

@ -6,14 +6,16 @@
namespace ehs
{
class EHC;
class NetChannel;
class NetEnd;
class NetOp;
class NetSys
{
private:
friend class EHC;
friend class NetChannel;
friend class NetServerCh;
friend class NetClientCh;
UInt_64 hashId;
Str_8 id;
@ -43,6 +45,6 @@ namespace ehs
bool AddOperation(NetOp *op);
private:
void Execute(EHC *ehc, NetEnd *endpoint, UInt_64 hashId, Serializer<UInt_64> &payload);
void Execute(NetChannel *channel, NetEnd *endpoint, UInt_64 hashId, Serializer<UInt_64> &payload);
};
}

View File

@ -4,33 +4,41 @@
namespace ehs
{
enum class EndDisp : UInt_8
{
ENDPOINT,
SERVICE
};
enum class Status : UInt_8
enum class NetStatus : UInt_8
{
DISCONNECTED,
ACTIVE,
PENDING,
IN_LOCAL_QUEUE,
IN_REMOTE_QUEUE,
QUEUED
};
enum class NetChannelType : UInt_8
{
SERVER,
CLIENT
};
struct Header
{
UInt_64 encHashId = 0;
Version version = {};
UInt_64 encId = 0;
Version encVer = {};
UInt_64 channelId = 0;
NetChannelType channelType = NetChannelType::SERVER;
Version channelVer = {};
UInt_64 id = 0;
UInt_64 fragments = 0;
UInt_64 fragmentCount = 0;
UInt_64 fragment = 0;
bool ensure = false;
EndDisp disposition = EndDisp::ENDPOINT;
Char_8 token[64] = {};
UInt_64 system = 0;
UInt_64 op = 0;
UInt_64 systemId = 0;
UInt_64 opId = 0;
};
void WriteHeader(const Header &header, Serializer<UInt_64> &data);
Header ReadHeader(Serializer<UInt_64> &data);
struct Packet
{
Header header;

View File

@ -4,7 +4,7 @@
namespace ehs
{
Str_8 BaseDNS::Resolve(const AddrType type, const Str_8 &hostname)
Str_8 BaseDNS::Resolve(const IP type, const Str_8 &hostname)
{
EHS_LOG_INT(LogType::ERR, 0, "Unsupported Operating System.");
return {};

View File

@ -4,26 +4,26 @@
namespace ehs
{
BaseTCP::BaseTCP()
: addrType(AddrType::IPV6), localPort(0), remotePort(0), connection(false), bound(false), listening(false),
: ip(IP::V6), localPort(0), remotePort(0), connection(false), bound(false), listening(false),
connected(false)
{
}
BaseTCP::BaseTCP(const AddrType addrType)
: addrType(addrType), localPort(0), remotePort(0), connection(false), bound(false), listening(false),
BaseTCP::BaseTCP(const IP ip)
: ip(ip), localPort(0), remotePort(0), connection(false), bound(false), listening(false),
connected(false)
{
}
BaseTCP::BaseTCP(BaseTCP&& tcp) noexcept
: addrType(tcp.addrType), localAddr(std::move(tcp.localAddr)), localPort(tcp.localPort),
: ip(tcp.ip), localAddr(std::move(tcp.localAddr)), localPort(tcp.localPort),
remoteHostName(std::move(tcp.remoteHostName)), remoteAddr(std::move(tcp.remoteAddr)), remotePort(tcp.remotePort),
connection(tcp.connection), bound(tcp.bound), listening(tcp.listening), connected(tcp.connected)
{
}
BaseTCP::BaseTCP(const BaseTCP& tcp)
: addrType(tcp.addrType), localPort(0), remotePort(0), connection(false), bound(false), listening(false),
: ip(tcp.ip), localPort(0), remotePort(0), connection(false), bound(false), listening(false),
connected(false)
{
}
@ -33,7 +33,7 @@ namespace ehs
if (this == &tcp)
return *this;
addrType = tcp.addrType;
ip = tcp.ip;
localAddr = std::move(tcp.localAddr);
localPort = tcp.localPort;
remoteHostName = std::move(tcp.remoteHostName);
@ -44,7 +44,7 @@ namespace ehs
listening = tcp.listening;
connected = tcp.connected;
tcp.addrType = AddrType::IPV6;
tcp.ip = IP::V6;
tcp.localPort = 0;
tcp.remotePort = 0;
tcp.connection = false;
@ -60,7 +60,7 @@ namespace ehs
if (this == &tcp)
return *this;
addrType = tcp.addrType;
ip = tcp.ip;
localAddr = Str_8();
localPort = 0;
remoteHostName = Str_8();
@ -188,9 +188,9 @@ namespace ehs
return request;
}
AddrType BaseTCP::GetAddressType() const
IP BaseTCP::GetAddressType() const
{
return addrType;
return ip;
}
Str_8 BaseTCP::GetLocalAddress() const

View File

@ -3,25 +3,23 @@
namespace ehs
{
BaseUDP::BaseUDP()
: type(AddrType::IPV6), port(0), bound(false)
: bound(false)
{
}
BaseUDP::BaseUDP(const AddrType type)
: type(type), port(0), bound(false)
BaseUDP::BaseUDP(const IP version)
: localEndpoint{version, "", 0}, bound(false)
{
}
BaseUDP::BaseUDP(BaseUDP&& udp) noexcept
: type(udp.type), address(std::move(udp.address)), port(udp.port), bound(true)
: localEndpoint((Endpoint &&)udp.localEndpoint), bound(true)
{
udp.type = AddrType::IPV6;
udp.port = 0;
udp.bound = false;
}
BaseUDP::BaseUDP(const BaseUDP& udp)
: type(udp.type), address(udp.address), port(udp.port), bound(false)
: localEndpoint(udp.localEndpoint), bound(false)
{
}
@ -30,13 +28,9 @@ namespace ehs
if (this == &udp)
return *this;
type = udp.type;
address = std::move(udp.address);
port = udp.port;
localEndpoint = (Endpoint &&)udp.localEndpoint;
bound = udp.bound;
udp.type = AddrType::IPV6;
udp.port = 0;
udp.bound = false;
return *this;
@ -47,9 +41,7 @@ namespace ehs
if (this == &udp)
return *this;
type = udp.type;
address = udp.address;
port = udp.port;
localEndpoint = udp.localEndpoint;
bound = false;
return *this;
@ -60,18 +52,8 @@ namespace ehs
return bound;
}
AddrType BaseUDP::GetLocalAddressType() const
Endpoint BaseUDP::GetLocalEndpoint() const
{
return type;
}
Str_8 BaseUDP::GetLocalAddress() const
{
return address;
}
UInt_16 BaseUDP::GetLocalPort() const
{
return port;
return localEndpoint;
}
}

View File

@ -7,14 +7,14 @@
namespace ehs
{
Str_8 DNS::Resolve(const AddrType type, const Str_8 &hostname)
Str_8 DNS::Resolve(const IP type, const Str_8 &hostname)
{
addrinfo* result = nullptr;
addrinfo req = {};
if (type == AddrType::IPV6)
if (type == IP::V6)
req.ai_family = AF_INET6;
else if (type == AddrType::IPV4)
else if (type == IP::V4)
req.ai_family = AF_INET;
Int_32 code = getaddrinfo(hostname, nullptr, &req, &result);

View File

@ -6,7 +6,7 @@
namespace ehs
{
Str_8 DNS::Resolve(const AddrType type, const Str_8 &hostname)
Str_8 DNS::Resolve(const IP type, const Str_8 &hostname)
{
WSADATA data = {};
@ -20,9 +20,9 @@ namespace ehs
addrinfo* result = nullptr;
addrinfo req = {};
if (type == AddrType::IPV6)
if (type == IP::V6)
req.ai_family = AF_INET6;
else if (type == AddrType::IPV4)
else if (type == IP::V4)
req.ai_family = AF_INET;
Int_32 code = getaddrinfo(hostname, nullptr, &req, &result);

File diff suppressed because it is too large Load Diff

View File

@ -29,7 +29,7 @@ namespace ehs
{
}
SSL::SSL(const AddrType type)
SSL::SSL(const IP type)
: TCP(type), ctx(nullptr), sslHdl(nullptr)
{
SSL::Initialize();

View File

@ -38,7 +38,7 @@ namespace ehc
}
Socket::Socket()
: hdl(EHS_INVALID_SOCKET), type(AddrType::IPV4), port(0), bound(false), appVer(0, 0, 0),
: hdl(EHS_INVALID_SOCKET), type(IP::V4), port(0), bound(false), appVer(0, 0, 0),
disposition(Disposition::UNKNOWN), dropPackets(false), hashId(0), buffer(nullptr), bufferSize(0),
maxEndpoints(0), lastTSC(0), delta(0.0f), maxTimeout(5.0f), resendRate(0.5f), connectedCb(nullptr),
activeCb(nullptr), disconnectedCb(nullptr)
@ -47,7 +47,7 @@ namespace ehc
}
Socket::Socket(const Version& ver, const Disposition disposition, const Str_8& id, const UInt_64 maxEndpoints)
: hdl(EHS_INVALID_SOCKET), type(AddrType::IPV4), port(0), bound(false), appVer(ver), disposition(disposition),
: hdl(EHS_INVALID_SOCKET), type(IP::V4), port(0), bound(false), appVer(ver), disposition(disposition),
dropPackets(false), id(id), hashId(id.Hash_32()), buffer(nullptr), bufferSize(0),
maxEndpoints(maxEndpoints), lastTSC(CPU::GetTSC()), delta(0.0f), maxTimeout(5.0f), resendRate(0.5f),
connectedCb(nullptr), activeCb(nullptr), disconnectedCb(nullptr)
@ -113,9 +113,9 @@ namespace ehc
}
#endif
if (type == AddrType::IPV6)
if (type == IP::V6)
hdl = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
else if (type == AddrType::IPV4)
else if (type == IP::V4)
hdl = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
else
return;
@ -140,12 +140,12 @@ namespace ehc
return;
}
if (type == AddrType::IPV4)
if (type == IP::V4)
{
buffer = new Byte[EHS_IPV4_UDP_PAYLOAD];
bufferSize = EHS_IPV4_UDP_PAYLOAD;
}
else if (type == AddrType::IPV6)
else if (type == IP::V6)
{
buffer = new Byte[EHS_IPV6_UDP_PAYLOAD];
bufferSize = EHS_IPV6_UDP_PAYLOAD;
@ -204,9 +204,9 @@ namespace ehc
if (hdl == EHS_INVALID_SOCKET || bound)
return;
if (type == AddrType::IPV6)
if (type == IP::V6)
Bind_v6(newAddress, newPort);
else if (type == AddrType::IPV4)
else if (type == IP::V4)
Bind_v4(newAddress, newPort);
address = newAddress;
@ -580,7 +580,7 @@ namespace ehc
return hdl != EHS_INVALID_SOCKET;
}
void Socket::SetAddressType(const AddrType newType)
void Socket::SetAddressType(const IP newType)
{
if (hdl != EHS_INVALID_SOCKET)
return;
@ -588,7 +588,7 @@ namespace ehc
type = newType;
}
AddrType Socket::GetAddressType() const
IP Socket::GetAddressType() const
{
return type;
}
@ -1219,14 +1219,14 @@ namespace ehc
return 0;
}
if (type == AddrType::IPV4 && size > EHS_IPV4_UDP_PAYLOAD)
if (type == IP::V4 && size > EHS_IPV4_UDP_PAYLOAD)
{
EHS_LOG_INT(LogType::ERR, 1, "Attempted to receive with a buffer size of, \"" + Str_8::FromNum(size)
+ "\", that exceeds, \"" + Str_8::FromNum(EHS_IPV4_UDP_PAYLOAD) + ".");
}
sockaddr_in6 remote = {};
UInt_32 addrLen = type == AddrType::IPV6 ? sizeof(sockaddr_in6) : sizeof(sockaddr_in);
UInt_32 addrLen = type == IP::V6 ? sizeof(sockaddr_in6) : sizeof(sockaddr_in);
SInt_64 received = 0;
#if defined(EHS_OS_WINDOWS)

View File

@ -30,8 +30,8 @@ namespace ehs
{
}
TCP::TCP(const AddrType addrType)
: BaseTCP(addrType), hdl(EHS_INVALID_SOCKET)
TCP::TCP(const IP IP)
: BaseTCP(IP), hdl(EHS_INVALID_SOCKET)
{
TCP::Initialize();
}
@ -78,9 +78,9 @@ namespace ehs
if (IsValid())
return;
if (addrType == AddrType::IPV6)
if (ip == IP::V6)
hdl = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
else if (addrType == AddrType::IPV4)
else if (ip == IP::V4)
hdl = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
else
return;
@ -119,9 +119,9 @@ namespace ehs
if (!IsValid() || bound || connection)
return;
if (addrType == AddrType::IPV6)
if (ip == IP::V6)
Bind_v6(address, port);
else if (addrType == AddrType::IPV4)
else if (ip == IP::V4)
Bind_v4(address, port);
this->localAddr = address;
@ -155,7 +155,7 @@ namespace ehs
UInt_32 addrLen = sizeof(sockaddr_in6);
TCP* client = new TCP();
client->addrType = addrType;
client->ip = ip;
client->localAddr = localAddr;
client->localPort = localPort;
client->connection = true;
@ -215,18 +215,18 @@ namespace ehs
remoteHostName = address;
remotePort = port;
if (addrType == AddrType::IPV6)
if (ip == IP::V6)
{
if (IsIPv6Only())
remoteAddr = DNS::Resolve(AddrType::IPV6, address);
remoteAddr = DNS::Resolve(IP::V6, address);
else
remoteAddr = DNS::Resolve(address);
Connect_v6(remoteAddr, port);
}
else if (addrType == AddrType::IPV4)
else if (ip == IP::V4)
{
remoteAddr = DNS::Resolve(AddrType::IPV4, address);
remoteAddr = DNS::Resolve(IP::V4, address);
Connect_v4(remoteAddr, port);
}
@ -338,7 +338,7 @@ namespace ehs
void TCP::SetIPv6Only(const bool value)
{
if (addrType != AddrType::IPV6)
if (ip != IP::V6)
{
EHS_LOG_INT(LogType::WARN, 0, "Cannot set IPv6 only mode while socket is not using IPv6.");
return;
@ -362,7 +362,7 @@ namespace ehs
bool TCP::IsIPv6Only() const
{
if (addrType != AddrType::IPV6)
if (ip != IP::V6)
return false;
if (!IsValid())

View File

@ -33,8 +33,8 @@ namespace ehs
{
}
TCP::TCP(const AddrType addrType)
: BaseTCP(addrType), hdl(EHS_INVALID_SOCKET)
TCP::TCP(const IP IP)
: BaseTCP(IP), hdl(EHS_INVALID_SOCKET)
{
TCP::Initialize();
}
@ -90,9 +90,9 @@ namespace ehs
return;
}
if (addrType == AddrType::IPV6)
if (IP == IP::V6)
hdl = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
else if (addrType == AddrType::IPV4)
else if (IP == IP::V4)
hdl = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
else
return;
@ -142,9 +142,9 @@ namespace ehs
if (!IsValid() || bound || connection)
return;
if (addrType == AddrType::IPV6)
if (IP == IP::V6)
Bind_v6(address, port);
else if (addrType == AddrType::IPV4)
else if (IP == IP::V4)
Bind_v4(address, port);
this->localAddr = address;
@ -178,7 +178,7 @@ namespace ehs
UInt_32 addrLen = sizeof(sockaddr_in6);
TCP* client = new TCP();
client->addrType = addrType;
client->IP = IP;
client->localAddr = localAddr;
client->localPort = localPort;
client->connection = true;
@ -244,18 +244,18 @@ namespace ehs
remoteHostName = address;
remotePort = port;
if (addrType == AddrType::IPV6)
if (IP == IP::V6)
{
if (IsIPv6Only())
remoteAddr = DNS::Resolve(AddrType::IPV6, address);
remoteAddr = DNS::Resolve(IP::V6, address);
else
remoteAddr = DNS::Resolve(address);
Connect_v6(remoteAddr, port);
}
else if (addrType == AddrType::IPV4)
else if (IP == IP::V4)
{
remoteAddr = DNS::Resolve(AddrType::IPV4, address);
remoteAddr = DNS::Resolve(IP::V4, address);
Connect_v4(remoteAddr, port);
}
@ -359,7 +359,7 @@ namespace ehs
void TCP::SetIPv6Only(const bool value)
{
if (addrType != AddrType::IPV6)
if (IP != IP::V6)
{
EHS_LOG_INT(LogType::WARN, 0, "Cannot set IPv6 only mode while socket is not using IPv6.");
return;
@ -383,7 +383,7 @@ namespace ehs
bool TCP::IsIPv6Only() const
{
if (addrType != AddrType::IPV6)
if (IP != IP::V6)
return false;
if (!IsValid())

View File

@ -25,12 +25,12 @@ namespace ehs
{
}
UDP::UDP(const AddrType addrType)
: BaseUDP(addrType), hdl(EHS_INVALID_SOCKET)
UDP::UDP(const IP version)
: BaseUDP(version), hdl(EHS_INVALID_SOCKET)
{
if (addrType == AddrType::IPV6)
if (version == IP::V6)
hdl = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
else if (addrType == AddrType::IPV4)
else if (version == IP::V4)
hdl = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
else
return;
@ -96,33 +96,32 @@ namespace ehs
bound = false;
}
void UDP::Bind(const AddrType type, const Str_8& address, const UInt_16 port)
void UDP::Bind(const Endpoint &endpoint)
{
if (!IsValid() || bound)
return;
if (type == AddrType::IPV6)
Bind_v6(address, port);
else if (type == AddrType::IPV4)
Bind_v4(address, port);
if (endpoint.version == IP::V6)
Bind_v6(endpoint.address, endpoint.port);
else if (endpoint.version == IP::V4)
Bind_v4(endpoint.address, endpoint.port);
this->address = address;
this->port = port;
localEndpoint = endpoint;
bound = true;
}
UInt_64 UDP::Send(const AddrType type, const Str_8& address, const UInt_16 port, const Byte *const data, const UInt_64 size)
UInt_64 UDP::Send(const Endpoint &endpoint, const Byte *const data, const UInt_64 size)
{
if (type == AddrType::IPV6)
return Send_v6(address, port, data, size);
else if (type == AddrType::IPV4)
return Send_v4(address, port, data, size);
if (endpoint.version == IP::V6)
return Send_v6(endpoint.address, endpoint.port, data, size);
else if (endpoint.version == IP::V4)
return Send_v4(endpoint.address, endpoint.port, data, size);
return 0;
}
UInt_64 UDP::Receive(AddrType* const type, Str_8* const address, UInt_16* const port, Byte* const data, const UInt_64 size)
UInt_64 UDP::Receive(Endpoint *endpoint, Byte* const data, const UInt_64 size)
{
if (!IsValid())
{
@ -161,9 +160,7 @@ namespace ehs
return received;
}
*type = AddrType::IPV6;
*address = tmpAddr;
*port = ntohs(remote.sin6_port);
*endpoint = {IP::V6, tmpAddr, remote.sin6_port};
}
else if (addrLen == sizeof(sockaddr_in))
{
@ -178,9 +175,7 @@ namespace ehs
return received;
}
*type = AddrType::IPV4;
*address = tmpAddr;
*port = ntohs(((sockaddr_in*)&remote)->sin_port);
*endpoint = {IP::V4, tmpAddr, ntohs(((sockaddr_in*)&remote)->sin_port)};
}
return received;
@ -205,7 +200,7 @@ namespace ehs
void UDP::SetIPv6Only(const bool value)
{
if (type != AddrType::IPV6)
if (localEndpoint.version != IP::V6)
{
EHS_LOG_INT(LogType::WARN, 0, "Cannot set IPv6 only mode while socket is not using IPv6.");
return;
@ -229,7 +224,7 @@ namespace ehs
bool UDP::IsIPv6Only() const
{
if (type != AddrType::IPV6)
if (localEndpoint.version != IP::V6)
return false;
if (!IsValid())

View File

@ -24,8 +24,8 @@ namespace ehs
{
}
UDP::UDP(const AddrType addrType)
: BaseUDP(addrType), hdl(EHS_INVALID_SOCKET)
UDP::UDP(const IP IP)
: BaseUDP(IP), hdl(EHS_INVALID_SOCKET)
{
WSADATA data = {};
@ -36,9 +36,9 @@ namespace ehs
return;
}
if (addrType == AddrType::IPV6)
if (IP == IP::V6)
hdl = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
else if (addrType == AddrType::IPV4)
else if (IP == IP::V4)
hdl = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
else
return;
@ -110,14 +110,14 @@ namespace ehs
bound = false;
}
void UDP::Bind(const AddrType type, const Str_8& address, const UInt_16 port)
void UDP::Bind(const IP type, const Str_8& address, const UInt_16 port)
{
if (!IsValid() || bound)
return;
if (type == AddrType::IPV6)
if (type == IP::V6)
Bind_v6(address, port);
else if (type == AddrType::IPV4)
else if (type == IP::V4)
Bind_v4(address, port);
this->address = address;
@ -126,17 +126,17 @@ namespace ehs
bound = true;
}
UInt_64 UDP::Send(const AddrType type, const Str_8& address, const UInt_16 port, const Byte *const data, const UInt_64 size)
UInt_64 UDP::Send(const IP type, const Str_8& address, const UInt_16 port, const Byte *const data, const UInt_64 size)
{
if (type == AddrType::IPV6)
if (type == IP::V6)
return Send_v6(address, port, data, size);
else if (type == AddrType::IPV4)
else if (type == IP::V4)
return Send_v4(address, port, data, size);
return 0;
}
UInt_64 UDP::Receive(AddrType* type, Str_8* const address, UInt_16* const port, Byte* const data, const UInt_64 size)
UInt_64 UDP::Receive(IP* type, Str_8* const address, UInt_16* const port, Byte* const data, const UInt_64 size)
{
if (!IsValid())
{
@ -174,7 +174,7 @@ namespace ehs
return received;
}
*type = AddrType::IPV6;
*type = IP::V6;
*address = tmpAddr;
*port = ntohs(remote.sin6_port);
}
@ -191,7 +191,7 @@ namespace ehs
return received;
}
*type = AddrType::IPV4;
*type = IP::V4;
*address = tmpAddr;
*port = ntohs(((sockaddr_in*)&remote)->sin_port);
}
@ -225,7 +225,7 @@ namespace ehs
void UDP::SetIPv6Only(const bool value)
{
if (type != AddrType::IPV6)
if (type != IP::V6)
{
EHS_LOG_INT(LogType::WARN, 0, "Cannot set IPv6 only mode while socket is not using IPv6.");
return;
@ -249,7 +249,7 @@ namespace ehs
bool UDP::IsIPv6Only() const
{
if (type != AddrType::IPV6)
if (type != IP::V6)
return false;
if (!IsValid())

View File

@ -0,0 +1,183 @@
#include "ehs/io/socket/ehc/NetChannel.h"
#include "ehs/io/socket/ehc/NetEnd.h"
#include "ehs/io/socket/ehc/NetSys.h"
#include "ehs/io/socket/EHC.h"
#include "ehs/PRNG.h"
namespace ehs
{
const UInt_64 NetChannel::internalSys = Str_8::Hash_64("Internal");
NetChannel::~NetChannel()
{
}
NetChannel::NetChannel()
: owner(nullptr), id(0), maxTimeout(5.0f), resendRate(0.5f)
{
}
NetChannel::NetChannel(Str_8 name, const Version &version)
: owner(nullptr), id(name.Hash_64()), name((Str_8 &&)name), version(version), maxTimeout(5.0f),
resendRate(0.5f)
{
}
NetChannel::NetChannel(NetChannel &&channel) noexcept
: owner(channel.owner), id(channel.id), name((Str_8 &&)channel.name), version(channel.version),
maxTimeout(channel.maxTimeout), resendRate(channel.resendRate)
{
channel.owner = nullptr;
channel.id = 0;
channel.version = {};
channel.maxTimeout = 5.0f;
channel.resendRate = 0.5f;
}
NetChannel::NetChannel(const NetChannel &channel)
: owner(nullptr), id(channel.id), name(channel.name), version(channel.version),
maxTimeout(channel.maxTimeout), resendRate(channel.resendRate)
{
}
NetChannel &NetChannel::operator=(NetChannel &&channel) noexcept
{
if (this == &channel)
return *this;
owner = channel.owner;
id = channel.id;
name = (Str_8 &&)channel.name;
version = channel.version;
maxTimeout = channel.maxTimeout;
resendRate = channel.resendRate;
channel.owner = nullptr;
channel.id = 0;
channel.version = {};
channel.maxTimeout = 5.0f;
channel.resendRate = 0.5f;
return *this;
}
NetChannel &NetChannel::operator=(const NetChannel &channel)
{
if (this == &channel)
return *this;
owner = nullptr;
id = channel.id;
name = channel.name;
version = channel.version;
maxTimeout = channel.maxTimeout;
resendRate = channel.resendRate;
return *this;
}
EHC *NetChannel::GetOwner() const
{
return owner;
}
UInt_64 NetChannel::GetId() const
{
return id;
}
Str_8 NetChannel::GetName() const
{
return name;
}
Version NetChannel::GetVersion() const
{
return version;
}
void NetChannel::SetMaxTimeout(const float seconds)
{
maxTimeout = seconds;
}
float NetChannel::GetMaxTimeout() const
{
return maxTimeout;
}
void NetChannel::SetResendRate(const float seconds)
{
resendRate = seconds;
}
float NetChannel::GetResendRate() const
{
return resendRate;
}
void NetChannel::EnableDropPackets(const bool enable)
{
dropPackets = enable;
}
bool NetChannel::IsDropPacketsEnabled() const
{
return dropPackets;
}
bool NetChannel::AddSystem(NetSys *sys)
{
if (sys->GetHashId() == internalSys)
return false;
if (HasSystem(sys->GetHashId()))
return false;
systems.Push(sys);
return true;
}
bool NetChannel::IsValid() const
{
return owner && id;
}
void NetChannel::Process(const float &delta, const Endpoint &endpoint, const Header &header, Serializer<UInt_64> &payload)
{
}
void NetChannel::Poll(const float &delta)
{
}
bool NetChannel::HasSystem(const UInt_64 sysHashId) const
{
for (UInt_64 i = 0; i < systems.Size(); ++i)
if (systems[i]->GetHashId() == sysHashId)
return true;
return false;
}
bool NetChannel::HasSystem(const Str_8& sysId) const
{
return HasSystem(sysId.Hash_64());
}
NetSys* NetChannel::GetSystem(const UInt_64 sysHashId) const
{
for (UInt_64 i = 0; i < systems.Size(); ++i)
if (systems[i]->GetHashId() == sysHashId)
return systems[i];
return nullptr;
}
NetSys* NetChannel::GetSystem(const Str_8& sysId) const
{
return GetSystem(sysId.Hash_64());
}
}

View File

@ -0,0 +1,385 @@
#include "ehs/io/socket/ehc/NetClientCh.h"
#include "ehs/io/socket/EHC.h"
namespace ehs
{
NetClientCh::~NetClientCh()
{
}
NetClientCh::NetClientCh()
: token{}, status(NetStatus::DISCONNECTED), queueSlot(0), deltaDuration(0.0f), deltaRate(0.0f), lastPing(0.0f),
latency(0.0f), timeout(5.0f), nextSendId(0), nextRecvId(0)
{
}
NetClientCh::NetClientCh(Str_8 name, const Version &version, Endpoint remoteEndpoint)
: NetChannel((Str_8 &&)name, version), remoteEndpoint((Endpoint &&)remoteEndpoint), token{},
status(NetStatus::DISCONNECTED), queueSlot(0), deltaDuration(0.0f), deltaRate(0.0f), lastPing(0.0f),
latency(0.0f), timeout(0.0f), nextSendId(0), nextRecvId(0)
{
}
NetClientCh::NetClientCh(NetClientCh &&client) noexcept
: NetChannel((NetChannel &&)client), remoteEndpoint((Endpoint &&)client.remoteEndpoint), token{},
status(client.status), queueSlot(client.queueSlot), deltaDuration(0.0f), deltaRate(0.0f), lastPing(0.0f),
latency(0.0f), timeout(0.0f), nextSendId(0), sent((Vector<Insurance> &&)client.sent), nextRecvId(0),
received((Vector<NetFrag> &&)client.received)
{
Util::Copy(token, client.token, 64);
Util::Zero(client.token, 64);
client.status = NetStatus::DISCONNECTED;
client.queueSlot = 0;
client.deltaDuration = 0.0f;
client.deltaRate = 0.0f;
client.lastPing = 0.0f;
client.latency = 0.0f;
client.timeout = 0.0f;
client.nextSendId = 0;
client.nextRecvId = 0;
}
NetClientCh::NetClientCh(const NetClientCh &client)
: NetChannel(client), remoteEndpoint(client.remoteEndpoint), token{}, status(NetStatus::DISCONNECTED),
queueSlot(0), deltaDuration(0.0f), deltaRate(0.0f), lastPing(0.0f), latency(0.0f), timeout(0.0f), nextSendId(0),
nextRecvId(0)
{
}
NetClientCh & NetClientCh::operator=(NetClientCh &&client) noexcept
{
if (this == &client)
return *this;
NetChannel::operator=((NetChannel &&)client);
remoteEndpoint = (Endpoint &&)client.remoteEndpoint;
Util::Copy(token, client.token, 64);
status = client.status;
queueSlot = client.queueSlot;
deltaDuration = client.deltaDuration;
deltaRate = client.deltaRate;
lastPing = client.lastPing;
latency = client.latency;
timeout = client.timeout;
nextSendId = client.nextSendId;
sent = (Vector<Insurance> &&)client.sent;
nextRecvId = client.nextRecvId;
received = (Vector<NetFrag> &&)client.received;
Util::Zero(client.token, 64);
client.status = NetStatus::DISCONNECTED;
client.queueSlot = 0;
client.deltaDuration = 0.0f;
client.deltaRate = 0.0f;
client.lastPing = 0.0f;
client.latency = 0.0f;
client.timeout = 0.0f;
client.nextSendId = 0;
client.nextRecvId = 0;
return *this;
}
NetClientCh & NetClientCh::operator=(const NetClientCh &client)
{
if (this == &client)
return *this;
remoteEndpoint = client.remoteEndpoint;
Util::Zero(token, 64);
status = NetStatus::DISCONNECTED;
queueSlot = 0;
deltaDuration = 0.0f;
deltaRate = 0.0f;
lastPing = 0.0f;
latency = 0.0f;
timeout = 0.0f;
nextSendId = 0;
sent = {};
nextRecvId = 0;
received = {};
return *this;
}
void NetClientCh::OnConnected(Serializer<UInt_64> data)
{
}
void NetClientCh::OnActive(Serializer<UInt_64> data)
{
}
void NetClientCh::OnQueueUpdate(Serializer<UInt_64> data)
{
}
void NetClientCh::OnDisconnected(Serializer<UInt_64> data)
{
}
void NetClientCh::OnRejected(Serializer<UInt_64> data)
{
}
void NetClientCh::OnTimeout(Serializer<UInt_64> data)
{
}
void NetClientCh::Connect(const Endpoint &endpoint, const Serializer<UInt_64> &payload)
{
if (!GetOwner()->udp.IsValid())
return;
Send(false, true, false, internalSys, connectOp, payload);
status = NetStatus::PENDING;
}
bool NetClientCh::Disconnect(const Serializer<UInt_64> &payload)
{
if (!GetOwner()->udp.IsValid())
return false;
Send(false, true, false, internalSys, disconnectOp, payload);
return true;
}
void NetClientCh::Send(const bool deltaLocked, UInt_64 encId, const bool ensure, UInt_64 sysId, UInt_64 opId,
const Serializer<UInt_64> &payload)
{
if (!IsValid() || (deltaLocked && deltaDuration < deltaRate))
return;
const EHC *owner = GetOwner();
NetEnc *enc = owner->GetEncryption(encId);
if (!enc)
{
EHS_LOG_INT(LogType::WARN, 0, "Encryption with the Id, \"" + Str_8::FromNum(encId) + "\", does not exist.");
return;
}
Header header = {
EHC::GetVersion(),
encId,
enc->GetVersion(),
GetId(),
NetChannelType::SERVER,
GetVersion(),
nextSendId++,
1,
0,
ensure,
"",
sysId,
opId
};
Util::Copy(header.token, token, 64);
const Endpoint localEndpoint = owner->GetLocalEndpoint();
if ((localEndpoint.version == IP::V6 && payload.Size() > EHC_IPV6_PAYLOAD) || (localEndpoint.version == IP::V4 && payload.Size() > EHC_IPV4_PAYLOAD))
{
NetFrag frags = FragmentData(localEndpoint.version, header, payload);
for (UInt_64 i = 0; i < frags.Size(); ++i)
{
Header newHeader = frags.GetHeader();
newHeader.fragment = i;
Send(enc, newHeader, frags[i]);
}
}
else
{
Send(enc, header, payload);
}
}
void NetClientCh::Send(const bool deltaLocked, const Str_8 &encName, const bool ensure, const Str_8 &sysName,
const Str_8 &opName, const Serializer<UInt_64> &payload)
{
Send(deltaLocked, encName.Hash_64(), ensure, sysName.Hash_64(), opName.Hash_64(), payload);
}
NetStatus NetClientCh::GetStatus() const
{
return status;
}
void NetClientCh::Process(const float &delta, const Endpoint &endpoint, const Header &header, Serializer<UInt_64> &payload)
{
if (!Util::Compare(token, header.token, 64))
return;
if (!header.ensure && header.token[0] && header.systemId == internalSys && header.opId == connectedOp)
{
if (status != NetStatus::PENDING)
return;
Util::Copy(token, header.token, 64);
status = payload.Read<NetStatus>();
queueSlot = payload.Read<UInt_64>();
OnConnected({Endianness::LE, &payload[payload.GetOffset()], payload.Size() - payload.GetOffset()});
}
else if (!header.ensure && header.token[0] && header.systemId == internalSys && header.opId == disconnectedOp)
{
OnDisconnected({Endianness::LE, &payload[payload.GetOffset()], payload.Size() - payload.GetOffset()});
}
else if (!header.ensure && header.token[0] && header.systemId == internalSys && header.opId == rejectedOp)
{
OnRejected({Endianness::LE, &payload[payload.GetOffset()], payload.Size() - payload.GetOffset()});
}
else if (!header.ensure && header.token[0] && header.systemId == internalSys && header.opId == statusUpdateOp)
{
const NetStatus newStatus = payload.Read<NetStatus>();
queueSlot = payload.Read<UInt_64>();
if (status == NetStatus::ACTIVE)
OnActive({Endianness::LE, &payload[payload.GetOffset()], payload.Size() - payload.GetOffset()});
else if (status == NetStatus::QUEUED && newStatus == NetStatus::QUEUED)
OnQueueUpdate({Endianness::LE, &payload[payload.GetOffset()], payload.Size() - payload.GetOffset()});
status = newStatus;
}
else if (!header.ensure && header.token[0] && header.systemId == internalSys && header.opId == receivedOp)
{
const UInt_64 msgId = payload.Read<UInt_64>();
const UInt_64 fragment = payload.Read<UInt_64>();
RemoveInsurance(msgId, fragment);
}
else if (!header.ensure && header.token[0] && header.systemId == internalSys && header.opId == pingOp)
{
deltaRate = payload.Read<float>();
Pong(delta);
}
else if (!header.ensure && header.token[0] && header.systemId == internalSys && header.opId == latencyOp)
{
latency = payload.Read<float>();
}
}
void NetClientCh::Poll(const float &delta)
{
}
NetFrag NetClientCh::FragmentData(const IP version, const Header& header, const Serializer<>& data)
{
NetFrag result;
if (version == IP::V6)
{
UInt_64 frags = data.Size() / EHC_IPV6_PAYLOAD;
if (data.Size() % EHC_IPV6_PAYLOAD)
++frags;
result = NetFrag(header, frags);
UInt_64 size = EHC_IPV6_PAYLOAD;
for (UInt_64 i = 0; i < result.Size(); ++i)
{
size = EHC_IPV6_PAYLOAD;
if (i == result.Size() - 1)
size = data.Size() % EHC_IPV6_PAYLOAD;
result[i] = {data.GetEndianness(), &data[i * EHC_IPV6_PAYLOAD], size};
}
}
else if (version == IP::V4)
{
UInt_64 frags = data.Size() / EHC_IPV4_PAYLOAD;
if (data.Size() % EHC_IPV4_PAYLOAD)
++frags;
result = NetFrag(header, frags);
UInt_64 size = EHC_IPV4_PAYLOAD;
for (UInt_64 i = 0; i < result.Size(); ++i)
{
size = EHC_IPV4_PAYLOAD;
if (i == result.Size() - 1)
size = data.Size() % EHC_IPV4_PAYLOAD;
result[i] = {data.GetEndianness(), &data[i * EHC_IPV4_PAYLOAD], size};
}
}
return result;
}
void NetClientCh::Send(NetEnc *enc, const Header& header, const Serializer<UInt_64>& payload)
{
Serializer result(Endianness::LE);
result.Write(header);
result.WriteSer(payload);
enc->Encrypt(&result[sizeof(bool)], result.Size() - sizeof(bool));
if (header.ensure)
sent.Push({header, payload});
GetOwner()->udp.Send(remoteEndpoint, result, result.Size());
}
void NetClientCh::Pong(const float delta)
{
Serializer payload(Endianness::LE);
payload.Write(delta);
Send(false, true, false, internalSys, pongOp, payload);
timeout = 0.0f;
}
void NetClientCh::RemoveInsurance(const UInt_64 msgId, const UInt_64 fragment)
{
for (UInt_64 i = 0; i < sent.Size(); ++i)
{
if (sent[i].header.id == msgId && sent[i].header.fragment == fragment)
{
sent.Remove(i);
break;
}
}
timeout = 0.0f;
}
void NetClientCh::AddReceived(const Header& header, const Serializer<>& payload)
{
NetFrag* frags = nullptr;
for (UInt_64 i = 0; i < received.Size(); ++i)
{
if (received[i].GetHeader().id == header.id)
{
if (received[i][header.fragment].Size())
return;
frags = &received[i];
break;
}
}
if (header.id > nextRecvId)
nextRecvId = header.id + 1;
if (frags)
(*frags)[header.fragment] = payload;
else
received.Push({header, payload});
timeout = 0.0f;
}
}

View File

@ -3,24 +3,24 @@
namespace ehs
{
NetEnc::NetEnc()
: owner(nullptr), hashId(0)
: id(0)
{
}
NetEnc::NetEnc(Str_8 id)
: owner(nullptr), hashId(id.Hash_64()), id((Str_8 &&)id)
NetEnc::NetEnc(Str_8 name, const Version &version)
: id(name.Hash_64()), name((Str_8 &&)name), version(version)
{
}
NetEnc::NetEnc(NetEnc&& enc) noexcept
: owner(enc.owner), hashId(enc.hashId), id((Str_8 &&)enc.id)
: id(enc.id), name((Str_8 &&)enc.name), version(enc.version)
{
enc.owner = nullptr;
enc.hashId = 0;
enc.id = 0;
enc.version = {};
}
NetEnc::NetEnc(const NetEnc& enc)
: owner(nullptr), hashId(enc.hashId), id(enc.id)
: id(enc.id), name(enc.name), version(enc.version)
{
}
@ -29,12 +29,12 @@ namespace ehs
if (this == &enc)
return *this;
owner = enc.owner;
hashId = enc.hashId;
id = (Str_8 &&)enc.id;
id = enc.id;
name = (Str_8 &&)enc.name;
version = enc.version;
enc.owner = nullptr;
enc.hashId = 0;
enc.id = 0;
enc.version = {};
return *this;
}
@ -44,28 +44,28 @@ namespace ehs
if (this == &enc)
return *this;
owner = nullptr;
hashId = enc.hashId;
id = enc.id;
name = enc.name;
version = enc.version;
return *this;
}
EHC* NetEnc::GetOwner() const
{
return owner;
}
UInt_64 NetEnc::GetHashId() const
{
return hashId;
}
Str_8 NetEnc::GetId() const
UInt_64 NetEnc::GetId() const
{
return id;
}
Str_8 NetEnc::GetName() const
{
return name;
}
Version NetEnc::GetVersion() const
{
return version;
}
void NetEnc::Encrypt(Byte *data, UInt_64 size) const
{
}

View File

@ -1,53 +1,46 @@
#include "ehs/io/socket/ehc/NetEnd.h"
#include "ehs/io/socket/EHC.h"
#include "ehs/io/socket/ehc/NetEnc.h"
#include "ehs/io/socket/ehc/NetServerCh.h"
#include "ehs/system/CPU.h"
namespace ehs
{
NetEnd::NetEnd()
: owner(nullptr), disposition(EndDisp::ENDPOINT), hashName(0), status(Status::PENDING),
arch(Architecture::UNKNOWN), token{}, nextSendId(0), nextRecvId(0), type(AddrType::IPV6), port(0),
deltaDuration(0.0f), deltaRate(1.0f / 60.0f), timeout(0.0f), lastPing(0.0f), oldLatency(0.0f), latency(0.0f),
queueSlot(0)
{
}
NetEnd::NetEnd(const EndDisp disposition, Str_8 id, const Architecture arch, const AddrType type,
Str_8 address, const UInt_16 port)
: owner(nullptr), disposition(disposition), hashName(id.Hash_64()), name((Str_8&&)id), status(Status::ACTIVE),
arch(arch), token{}, nextSendId(0), nextRecvId(0), type(type), address((Str_8&&)address), port(port),
deltaDuration(0.0f), deltaRate(1.0f / 60.0f), timeout(0.0f), lastPing(0.0f), oldLatency(0.0f), latency(0.0f),
queueSlot(0)
{
}
NetEnd::NetEnd(const AddrType type, Str_8 address, const UInt_16 port)
: owner(nullptr), disposition(EndDisp::ENDPOINT), hashName(0), status(Status::PENDING),
arch(Architecture::UNKNOWN), token{}, nextSendId(0), nextRecvId(0), type(type), address((Str_8&&)address),
port(port), deltaDuration(0.0f), deltaRate(1.0f / 60.0f), timeout(0.0f), lastPing(0.0f), oldLatency(0.0f),
: owner(nullptr), id(0), status(NetStatus::PENDING), token{}, nextSendId(0), nextRecvId(0),
deltaDuration(0.0f), deltaRate(1.0f / 60.0f), timeout(0.0f), lastPing(0.0f), oldLatency(0.0f),
latency(0.0f), queueSlot(0)
{
}
NetEnd::NetEnd(Str_8 id, Endpoint endpoint)
: owner(nullptr), id(id.Hash_64()), name((Str_8&&)id), status(NetStatus::ACTIVE), token{}, nextSendId(0),
nextRecvId(0), endpoint((Endpoint &&)endpoint), deltaDuration(0.0f), deltaRate(1.0f / 60.0f),
timeout(0.0f), lastPing(0.0f), oldLatency(0.0f), latency(0.0f), queueSlot(0)
{
}
NetEnd::NetEnd(Endpoint endpoint)
: owner(nullptr), id(0), status(NetStatus::PENDING), token{}, nextSendId(0), nextRecvId(0),
endpoint((Endpoint &&)endpoint), deltaDuration(0.0f), deltaRate(1.0f / 60.0f), timeout(0.0f), lastPing(0.0f),
oldLatency(0.0f), latency(0.0f), queueSlot(0)
{
}
NetEnd::NetEnd(NetEnd &&end) noexcept
: owner(end.owner), disposition(end.disposition), hashName(end.hashName), name((Str_8&&)end.name), status(end.status), arch(end.arch), token{},
: owner(end.owner), id(end.id), name((Str_8&&)end.name), status(end.status), token{},
nextSendId(end.nextSendId), sent((Vector<Insurance>&&)end.sent), nextRecvId(end.nextRecvId),
received((Vector<NetFrags>&&)end.received), type(end.type), address((Str_8&&)end.address), port(end.port),
received((Vector<NetFrag>&&)end.received), endpoint((Endpoint &&)end.endpoint),
deltaDuration(end.deltaDuration), deltaRate(end.deltaRate), timeout(end.timeout), lastPing(end.lastPing),
oldLatency(end.oldLatency), latency(end.latency), queueSlot(end.queueSlot)
{
end.owner = nullptr;
end.disposition = EndDisp::ENDPOINT;
end.hashName = 0;
end.status = Status::PENDING;
end.arch = Architecture::UNKNOWN;
end.id = 0;
end.status = NetStatus::PENDING;
Util::Copy(token, end.token, 64);
Util::Zero(end.token, 64);
end.nextSendId = 0;
end.nextRecvId = 0;
end.type = AddrType::IPV6;
end.port = 0;
end.deltaDuration = 0.0f;
end.deltaRate = 1.0f / 60.0f;
end.timeout = 0.0f;
@ -58,9 +51,9 @@ namespace ehs
}
NetEnd::NetEnd(const NetEnd& end)
: owner(nullptr), disposition(EndDisp::ENDPOINT), hashName(end.hashName), name(end.name), status(Status::PENDING), arch(Architecture::UNKNOWN),
token{}, nextSendId(0), nextRecvId(0), type(end.type), port(0), deltaDuration(0.0f), deltaRate(1.0f / 60.0f),
timeout(0.0f), lastPing(0.0f), oldLatency(0.0f), latency(0.0f), queueSlot(0)
: owner(nullptr), id(end.id), name(end.name), status(NetStatus::PENDING), token{}, nextSendId(0),
nextRecvId(0), deltaDuration(0.0f), deltaRate(1.0f / 60.0f), timeout(0.0f), lastPing(0.0f), oldLatency(0.0f),
latency(0.0f), queueSlot(0)
{
}
@ -70,19 +63,15 @@ namespace ehs
return *this;
owner = end.owner;
disposition = end.disposition;
hashName = end.hashName;
id = end.id;
name = (Str_8&&)end.name;
status = end.status;
arch = end.arch;
Util::Copy(token, end.token, 64);
nextSendId = end.nextSendId;
sent = (Vector<Insurance>&&)end.sent;
nextRecvId = end.nextRecvId;
received = (Vector<NetFrags>&&)end.received;
type = end.type;
address = (Str_8&&)end.address;
port = end.port;
received = (Vector<NetFrag>&&)end.received;
endpoint = (Endpoint &&)end.endpoint;
deltaDuration = end.deltaDuration;
deltaRate = end.deltaRate;
timeout = end.timeout;
@ -92,15 +81,11 @@ namespace ehs
queueSlot = end.queueSlot;
end.owner = nullptr;
end.disposition = EndDisp::ENDPOINT;
end.hashName = 0;
end.status = Status::PENDING;
end.arch = Architecture::UNKNOWN;
end.id = 0;
end.status = NetStatus::PENDING;
Util::Zero(end.token, 64);
end.nextSendId = 0;
end.nextRecvId = 0;
end.type = AddrType::IPV6;
end.port = 0;
end.deltaDuration = 0.0f;
end.deltaRate = 1.0f / 60.0f;
end.timeout = 0.0f;
@ -118,19 +103,15 @@ namespace ehs
return *this;
owner = nullptr;
disposition = EndDisp::ENDPOINT;
hashName = end.hashName;
id = end.id;
name = end.name;
status = Status::PENDING;
arch = Architecture::UNKNOWN;
status = NetStatus::PENDING;
Util::Zero(token, 64);
nextSendId = 0;
sent = {};
nextRecvId = 0;
received = {};
type = AddrType::IPV6;
address = {};
port = 0;
endpoint = {};
deltaDuration = 0.0f;
deltaRate = 1.0f / 60.0f;
timeout = 0.0f;
@ -142,14 +123,9 @@ namespace ehs
return *this;
}
EndDisp NetEnd::GetDisposition() const
UInt_64 NetEnd::GetId() const
{
return disposition;
}
UInt_64 NetEnd::GetHashName() const
{
return hashName;
return id;
}
Str_8 NetEnd::GetName() const
@ -157,44 +133,54 @@ namespace ehs
return name;
}
Status NetEnd::GetStatus() const
NetStatus NetEnd::GetStatus() const
{
return status;
}
Architecture NetEnd::GetArchitecture() const
{
return arch;
}
UInt_64 NetEnd::GetNextSendId() const
{
return nextSendId;
}
void NetEnd::Send(const bool deltaLocked, const UInt_64 encHashId, const bool ensure, const UInt_64 sys,
const UInt_64 op, const Serializer<UInt_64> &payload)
void NetEnd::Send(const bool deltaLocked, const UInt_64 encId, const bool ensure, const UInt_64 sysId,
const UInt_64 opId, const Serializer<UInt_64> &payload)
{
if (deltaLocked && deltaDuration < deltaRate)
if (!owner || !owner->GetOwner() || (deltaLocked && deltaDuration < deltaRate))
return;
EHC *ehc = owner->GetOwner();
NetEnc *enc = ehc->GetEncryption(encId);
if (!enc)
{
EHS_LOG_INT(LogType::WARN, 0, "Encryption with the Id, \"" + Str_8::FromNum(encId) + "\", does not exist.");
return;
}
Header header = {
encHashId,
EHC::GetVersion(),
encId,
enc->GetVersion(),
GetId(),
NetChannelType::SERVER,
GetVersion(),
nextSendId++,
1,
0,
ensure,
owner->GetDisposition(),
"",
sys,
op
sysId,
opId
};
Util::Copy(header.token, token, 64);
if ((owner->GetLocalAddressType() == AddrType::IPV6 && payload.Size() > EHC_IPV6_PAYLOAD) || (owner->GetLocalAddressType() == AddrType::IPV4 && payload.Size() > EHC_IPV4_PAYLOAD))
if ((ehc->GetLocalEndpoint().version == IP::V6 && payload.Size() > EHC_IPV6_PAYLOAD) ||
(ehc->GetLocalEndpoint().version == IP::V4 && payload.Size() > EHC_IPV4_PAYLOAD))
{
NetFrags frags = FragmentData(header, payload);
NetFrag frags = FragmentData(header, payload);
for (UInt_64 i = 0; i < frags.Size(); ++i)
{
Header newHeader = frags.GetHeader();
@ -209,10 +195,10 @@ namespace ehs
}
}
void NetEnd::Send(const bool deltaLocked, const Str_8 &encId, const bool ensure, const Str_8& sys,
const Str_8& op, const Serializer<>& payload)
void NetEnd::Send(const bool deltaLocked, const Str_8 &encName, const bool ensure, const Str_8& sysName,
const Str_8& opName, const Serializer<>& payload)
{
Send(deltaLocked, encId.Hash_64(), ensure, sys.Hash_64(), op.Hash_64(), payload);
Send(deltaLocked, encName.Hash_64(), ensure, sysName.Hash_64(), opName.Hash_64(), payload);
}
UInt_64 NetEnd::GetNextRecvId() const
@ -220,14 +206,9 @@ namespace ehs
return nextRecvId;
}
Str_8 NetEnd::GetAddress() const
Endpoint NetEnd::GetEndpoint() const
{
return address;
}
UInt_16 NetEnd::GetPort() const
{
return port;
return endpoint;
}
float NetEnd::GetDeltaRate() const
@ -273,17 +254,21 @@ namespace ehs
sent[i].lastResend += delta;
if (sent[i].lastResend >= owner->GetResendRate())
{
EHC *ehc = owner->GetOwner();
Serializer result(Endianness::LE);
result.Write(sent[i].header);
result.WriteSer(sent[i].payload);
if (sent[i].header.encHashId)
if (sent[i].header.encId)
{
NetEnc *enc = owner->GetEncryption(sent[i].header.encHashId);
NetEnc *enc = ehc->GetEncryption(sent[i].header.encId);
if (!enc)
{
EHS_LOG_INT(LogType::WARN, 0, "The network encryption with the hash id " +
Str_8::FromNum(sent[i].header.encHashId) + ", does not exist.");
Str_8::FromNum(sent[i].header.encId) + ", does not exist.");
continue;
}
@ -291,24 +276,21 @@ namespace ehs
enc->Encrypt(&result[sizeof(bool)], result.Size() - sizeof(bool));
}
owner->udp.Send(type, address, port, result, result.Size());
ehc->udp.Send(endpoint, result, result.Size());
sent[i].lastResend = Math::Mod(sent[i].lastResend, owner->GetResendRate());
}
}
}
if (owner->GetDisposition() == EndDisp::SERVICE)
{
lastPing += delta;
if (lastPing >= 1.0f)
Ping(delta);
}
EHS_LOG_SUCCESS();
}
void NetEnd::SetStatus(const Status newStatus)
void NetEnd::SetStatus(const NetStatus newStatus)
{
status = newStatus;
}
@ -329,7 +311,7 @@ namespace ehs
void NetEnd::AddReceived(const Header& header, const Serializer<>& payload)
{
NetFrags* frags = nullptr;
NetFrag* frags = nullptr;
for (UInt_64 i = 0; i < received.Size(); ++i)
{
@ -354,7 +336,7 @@ namespace ehs
timeout = 0.0f;
}
Vector<NetFrags>* NetEnd::GetReceived()
Vector<NetFrag>* NetEnd::GetReceived()
{
return &received;
}
@ -374,16 +356,6 @@ namespace ehs
latency = 0.0f;
}
void NetEnd::Pong(const float delta)
{
Serializer payload(Endianness::LE);
payload.Write(delta);
Send(false, true, false, "Internal", "Pong", payload);
timeout = 0.0f;
}
void NetEnd::SendLatency()
{
oldLatency = latency * 1000;
@ -407,17 +379,19 @@ namespace ehs
queueSlot = slot;
}
NetFrags NetEnd::FragmentData(const Header& header, const Serializer<>& data)
NetFrag NetEnd::FragmentData(const Header& header, const Serializer<>& data)
{
NetFrags result;
NetFrag result;
if (owner->GetLocalAddressType() == AddrType::IPV6)
EHC *ehc = owner->GetOwner();
if (ehc->GetLocalEndpoint().version == IP::V6)
{
UInt_64 frags = data.Size() / EHC_IPV6_PAYLOAD;
if (data.Size() % EHC_IPV6_PAYLOAD)
++frags;
result = NetFrags(header, frags);
result = NetFrag(header, frags);
UInt_64 size = EHC_IPV6_PAYLOAD;
@ -430,13 +404,13 @@ namespace ehs
result[i] = {data.GetEndianness(), &data[i * EHC_IPV6_PAYLOAD], size};
}
}
else if (owner->GetLocalAddressType() == AddrType::IPV4)
else if (ehc->GetLocalEndpoint().version == IP::V4)
{
UInt_64 frags = data.Size() / EHC_IPV4_PAYLOAD;
if (data.Size() % EHC_IPV4_PAYLOAD)
++frags;
result = NetFrags(header, frags);
result = NetFrag(header, frags);
UInt_64 size = EHC_IPV4_PAYLOAD;
@ -455,17 +429,19 @@ namespace ehs
void NetEnd::Send(const Header& header, const Serializer<UInt_64>& payload)
{
EHC *ehc = owner->GetOwner();
Serializer result(Endianness::LE);
result.Write(header);
result.WriteSer(payload);
if (header.encHashId)
if (header.encId)
{
NetEnc *enc = owner->GetEncryption(header.encHashId);
NetEnc *enc = ehc->GetEncryption(header.encId);
if (!enc)
{
EHS_LOG_INT(LogType::WARN, 0, "The network encryption with the hash id " +
Str_8::FromNum(header.encHashId) + ", does not exist.");
Str_8::FromNum(header.encId) + ", does not exist.");
return;
}
@ -476,7 +452,7 @@ namespace ehs
if (header.ensure)
sent.Push({header, payload});
owner->udp.Send(type, address, port, result, result.Size());
ehc->udp.Send(endpoint, result, result.Size());
}
bool NetEnd::SortingNeeded() const
@ -499,7 +475,7 @@ namespace ehs
if (!SortingNeeded())
return;
Vector<NetFrags> sorted(0, received.Stride());
Vector<NetFrag> sorted(0, received.Stride());
for (UInt_64 a = 0; a < received.Size(); ++a)
{

View File

@ -1,32 +1,32 @@
#include "ehs/io/socket/ehc/NetFrags.h"
#include "ehs/io/socket/ehc/NetFrag.h"
namespace ehs
{
NetFrags::~NetFrags()
NetFrag::~NetFrag()
{
delete[] data;
}
NetFrags::NetFrags()
NetFrag::NetFrag()
: data(nullptr), size(0)
{
}
NetFrags::NetFrags(const Header &header, const Serializer<UInt_64> &payload)
: header(header), data(new Serializer<UInt_64>[header.fragments]), size(header.fragments)
NetFrag::NetFrag(const Header &header, const Serializer<UInt_64> &payload)
: header(header), data(new Serializer<UInt_64>[header.fragmentCount]), size(header.fragmentCount)
{
this->header.fragment = 0;
data[header.fragment] = payload;
}
NetFrags::NetFrags(const Header &header, const UInt_64 size)
NetFrag::NetFrag(const Header &header, const UInt_64 size)
: header(header), data(new Serializer<UInt_64>[size]), size(size)
{
this->header.fragments = size;
this->header.fragmentCount = size;
this->header.fragment = 0;
}
NetFrags::NetFrags(NetFrags &&frags) noexcept
NetFrag::NetFrag(NetFrag &&frags) noexcept
: header(frags.header), data(frags.data), size(frags.size)
{
frags.header = {};
@ -34,14 +34,14 @@ namespace ehs
frags.size = 0;
}
NetFrags::NetFrags(const NetFrags &frags)
NetFrag::NetFrag(const NetFrag &frags)
: header(frags.header), data(new Serializer<UInt_64>[frags.size]), size(frags.size)
{
for (UInt_64 i = 0; i < size; ++i)
data[i] = frags.data[i];
}
NetFrags &NetFrags::operator=(NetFrags &&frags) noexcept
NetFrag &NetFrag::operator=(NetFrag &&frags) noexcept
{
if (this == &frags)
return *this;
@ -60,7 +60,7 @@ namespace ehs
return *this;
}
NetFrags &NetFrags::operator=(const NetFrags &frags)
NetFrag &NetFrag::operator=(const NetFrag &frags)
{
if (this == &frags)
return *this;
@ -75,22 +75,22 @@ namespace ehs
return *this;
}
NetFrags::operator Serializer<UInt_64> *() const
NetFrag::operator Serializer<UInt_64> *() const
{
return data;
}
Header NetFrags::GetHeader() const
Header NetFrag::GetHeader() const
{
return header;
}
UInt_64 NetFrags::Size() const
UInt_64 NetFrag::Size() const
{
return size;
}
bool NetFrags::IsComplete() const
bool NetFrag::IsComplete() const
{
for (UInt_64 i = 0; i < size; ++i)
if (!data[i].Size())
@ -99,7 +99,7 @@ namespace ehs
return true;
}
Packet NetFrags::Combine() const
Packet NetFrag::Combine() const
{
UInt_64 rSize = 0;
for (UInt_64 i = 0; i < size; ++i)
@ -110,7 +110,7 @@ namespace ehs
header,
{Endianness::LE, rSize}
};
result.header.fragments = 0;
result.header.fragmentCount = 0;
for (UInt_64 i = 0; i < size; ++i)
result.payload.WriteSer(data[i]);

View File

@ -1,7 +1,6 @@
#include "ehs/io/socket/ehc/NetOp.h"
#include "ehs/io/socket/EHC.h"
#include "ehs/io/socket/ehc/NetEnd.h"
#include "ehs/io/socket/ehc/NetSys.h"
namespace ehs
{
@ -60,7 +59,7 @@ namespace ehs
return hashId;
}
void NetOp::Process(EHC *ehc, NetEnd *endpoint, NetSys *sys, Serializer<UInt_64> &payload)
void NetOp::Process(NetChannel *channel, NetEnd *endpoint, NetSys *sys, Serializer<UInt_64> &payload)
{
}
}

View File

@ -0,0 +1,588 @@
#include "ehs/io/socket/ehc/NetServerCh.h"
#include "ehs/io/socket/ehc/NetEnd.h"
#include "ehs/io/socket/ehc/NetSys.h"
#include "ehs/io/socket/EHC.h"
#include "ehs/PRNG.h"
namespace ehs
{
NetServerCh::~NetServerCh()
{
Shutdown();
}
NetServerCh::NetServerCh(const UInt_64 maxEndpoints)
: maxEndpoints(maxEndpoints)
{
}
NetServerCh::NetServerCh(NetServerCh &&server) noexcept
: endpoints((Vector<NetEnd *> &&)server.endpoints), maxEndpoints(server.maxEndpoints)
{
for (UInt_64 i = 0; i < endpoints.Size(); i++)
endpoints[i]->owner = this;
}
NetServerCh::NetServerCh(const NetServerCh &server)
: maxEndpoints(server.maxEndpoints)
{
}
NetServerCh &NetServerCh::operator=(NetServerCh &&server) noexcept
{
if (this == &server)
return *this;
Shutdown();
endpoints = (Vector<NetEnd *> &&)server.endpoints;
for (UInt_64 i = 0; i < endpoints.Size(); i++)
endpoints[i]->owner = this;
maxEndpoints = server.maxEndpoints;
server.maxEndpoints = 0;
return *this;
}
NetServerCh &NetServerCh::operator=(const NetServerCh &server)
{
if (this == &server)
return *this;
Shutdown();
endpoints = {};
maxEndpoints = server.maxEndpoints;
return *this;
}
bool NetServerCh::OnEndpointConnect(NetEnd *endpoint, Serializer<UInt_64> payload)
{
return true;
}
Serializer<UInt_64> NetServerCh::OnEndpointAccepted(NetEnd *endpoint)
{
return {};
}
void NetServerCh::OnEndpointDisconnect(NetEnd *endpoint, Serializer<UInt_64> payload)
{
}
void NetServerCh::OnEndpointTimeout(NetEnd *endpoint)
{
}
void NetServerCh::OnEndpointActive(NetEnd *endpoint)
{
}
Serializer<UInt_64> OnShutdown()
{
return {};
}
void NetServerCh::Broadcast(const NetStatus endStatus, const bool deltaLocked, const UInt_64 encHashId,
const bool ensure, const UInt_64 sysHashId, const UInt_64 opHashId, const Serializer<UInt_64> &payload)
{
if (!GetOwner()->udp.IsValid())
return;
for (UInt_64 i = 0; i < endpoints.Size(); ++i)
if (endpoints[i]->GetStatus() == endStatus)
endpoints[i]->Send(deltaLocked, encHashId, ensure, sysHashId, opHashId, payload);
}
void NetServerCh::Broadcast(const NetStatus endStatus, const bool deltaLocked, const Str_8 &encId, const bool ensure,
const Str_8 &sysId, const Str_8 &opId,
const Serializer<UInt_64> &payload)
{
Broadcast(endStatus, deltaLocked, encId.Hash_64(), ensure, sysId.Hash_64(), opId.Hash_64(), payload);
}
bool NetServerCh::HasEndpoint(const NetStatus endStatus, const Char_8 token[64]) const
{
for (UInt_64 i = 0; i < endpoints.Size(); ++i)
{
if (endpoints[i]->GetStatus() != endStatus)
continue;
if (Util::Compare(endpoints[i]->token, token, 64))
return true;
}
return false;
}
bool NetServerCh::HasEndpoint(const NetStatus endStatus, const UInt_64 hashName) const
{
for (UInt_64 i = 0; i < endpoints.Size(); ++i)
{
if (endpoints[i]->GetStatus() != endStatus)
continue;
if (endpoints[i]->GetId() == hashName)
return true;
}
return false;
}
bool NetServerCh::HasEndpoint(const NetStatus endStatus, const Str_8 &id) const
{
return HasEndpoint(endStatus, id.Hash_64());
}
bool NetServerCh::HasEndpoint(const Char_8 token[64]) const
{
for (UInt_64 i = 0; i < endpoints.Size(); ++i)
if (Util::Compare(endpoints[i]->token, token, 64))
return true;
return false;
}
bool NetServerCh::HasEndpoint(const UInt_64 hashName) const
{
for (UInt_64 i = 0; i < endpoints.Size(); ++i)
if (endpoints[i]->GetId() == hashName)
return true;
return false;
}
bool NetServerCh::HasEndpoint(const Str_8 &id) const
{
return HasEndpoint(id.Hash_64());
}
bool NetServerCh::HasEndpoint(const Endpoint &endpoint) const
{
for (UInt_64 i = 0; i < endpoints.Size(); ++i)
if (endpoints[i]->GetEndpoint().address == endpoint.address && endpoints[i]->GetEndpoint().port == endpoint.port)
return true;
return false;
}
NetEnd* NetServerCh::GetEndpoint(const NetStatus endStatus, const Char_8 token[64]) const
{
for (UInt_64 i = 0; i < endpoints.Size(); ++i)
{
if (endpoints[i]->GetStatus() != endStatus)
continue;
if (Util::Compare(endpoints[i]->token, token, 64))
return endpoints[i];
}
return nullptr;
}
NetEnd *NetServerCh::GetEndpoint(const NetStatus endStatus, const UInt_64 hashName) const
{
for (UInt_64 i = 0; i < endpoints.Size(); ++i)
{
if (endpoints[i]->GetStatus() != endStatus)
continue;
if (endpoints[i]->GetId() == hashName)
return endpoints[i];
}
return nullptr;
}
NetEnd *NetServerCh::GetEndpoint(const NetStatus endStatus, const Str_8 &id) const
{
return GetEndpoint(endStatus, id.Hash_64());
}
NetEnd *NetServerCh::GetEndpoint(const Char_8 token[64]) const
{
for (UInt_64 i = 0; i < endpoints.Size(); ++i)
if (Util::Compare(endpoints[i]->token, token, 64))
return endpoints[i];
return nullptr;
}
NetEnd *NetServerCh::GetEndpoint(const UInt_64 hashName) const
{
for (UInt_64 i = 0; i < endpoints.Size(); ++i)
if (endpoints[i]->GetId() == hashName)
return endpoints[i];
return nullptr;
}
NetEnd *NetServerCh::GetEndpoint(const Str_8 &id) const
{
return GetEndpoint(id.Hash_64());
}
NetEnd *NetServerCh::GetEndpoint(const Endpoint &endpoint) const
{
for (UInt_64 i = 0; i < endpoints.Size(); ++i)
if (endpoints[i]->GetEndpoint().address == endpoint.address && endpoints[i]->GetEndpoint().port == endpoint.port)
return endpoints[i];
return nullptr;
}
Array<NetEnd *> NetServerCh::GetEndpoints(const NetStatus endStatus)
{
Array<NetEnd*> result(endpoints.Size());
UInt_64 count = 0;
for (UInt_64 i = 0; i < endpoints.Size(); ++i)
if (endpoints[i]->GetStatus() == endStatus)
result[count++] = endpoints[i];
result.Resize(count);
return result;
}
UInt_64 NetServerCh::GetEndpointsCount(const NetStatus endStatus)
{
UInt_64 count = 0;
for (UInt_64 i = 0; i < endpoints.Size(); ++i)
if (endpoints[i]->GetStatus() == endStatus)
++count;
return count;
}
UInt_64 NetServerCh::GetMaxEndpoints() const
{
return maxEndpoints;
}
void NetServerCh::Process(const float &delta, const Endpoint &endpoint, const Header &header, Serializer<UInt_64> &payload)
{
if (header.channelVer != GetVersion())
return;
if (!header.ensure && !header.token[0] && header.systemId == internalSys && header.opId == connectOp)
{
NetEnd* end = new NetEnd(payload.ReadStr(), endpoint);
end->owner = this;
GenerateToken(end->token);
end->SetStatus(NetStatus::PENDING);
Serializer sPayload(Endianness::LE);
if (!OnEndpointConnect(end, {Endianness::LE, &payload[payload.GetOffset()], payload.Size() - payload.GetOffset()}))
{
sPayload.WriteStr<Char_8, UInt_64>("Connection rejected.");
end->Send(false, true, false, internalSys, rejectedOp, sPayload);
return;
}
endpoints.Push(end);
UInt_64 active = GetEndpointsCount(NetStatus::ACTIVE);
if (maxEndpoints && active >= maxEndpoints)
{
end->SetStatus(NetStatus::QUEUED);
UpdateQueue(active);
sPayload.Write(NetStatus::QUEUED);
sPayload.Write(end->GetQueueSlot());
}
else
{
end->SetStatus(NetStatus::ACTIVE);
OnEndpointActive(end);
sPayload.Write(NetStatus::ACTIVE);
sPayload.Write(0);
}
sPayload.WriteSer(OnEndpointAccepted(end));
end->Send(false, 0, false, internalSys, connectedOp, sPayload);
}
else if (!header.ensure && header.token[0] && header.systemId == internalSys && header.opId == disconnectOp)
{
NetEnd* end = GetEndpoint(header.token);
if (!end)
return;
end->Send(false, 0, false, internalSys, disconnectedOp, {});
OnEndpointDisconnect(end, {Endianness::LE, &payload[payload.GetOffset()], payload.Size() - payload.GetOffset()});
RemoveEndpoint(end->token);
UpdateQueue();
}
else if (!header.ensure && header.token[0] && header.systemId == internalSys && header.opId == pongOp)
{
NetEnd* end = GetEndpoint(header.token);
if (!end)
return;
end->SetDeltaRate(payload.Read<float>());
end->SendLatency();
}
else if (!header.ensure && header.token[0] && header.systemId == internalSys && header.opId == receivedOp)
{
NetEnd* end = GetEndpoint(header.token);
if (!end)
return;
const UInt_64 msgId = payload.Read<UInt_64>();
const UInt_64 fragment = payload.Read<UInt_64>();
end->RemoveInsurance(msgId, fragment);
}
else if (header.token[0])
{
NetEnd* end = GetEndpoint(header.token);
if (!end)
return;
if (IsDropPacketsEnabled() && !header.ensure && header.id < end->GetNextRecvId())
{
EHS_LOG_INT(LogType::INFO, 6, "Old packet intentionally dropped.");
return;
}
if (header.ensure)
{
Serializer sPayload(Endianness::LE);
sPayload.Write(header.id);
sPayload.Write(header.fragment);
end->Send(false, 0, false, internalSys, receivedOp, sPayload);
}
end->AddReceived(
header,
Serializer<>(Endianness::LE, &payload[payload.GetOffset()], payload.Size() - payload.GetOffset())
);
}
else
{
EHS_LOG_INT(LogType::INFO, 7, "Corrupted packet.");
}
}
void NetServerCh::GenerateToken(Char_8 in[64])
{
PRNG_u64 rng(CPU::GetTSC());
for (UInt_64 i = 0; i < 8; ++i)
{
do
((UInt_64*)in)[i] = rng.Generate();
while (!i && ((UInt_64*)in)[i] == 0);
}
if (HasEndpoint(in))
GenerateToken(in);
}
void NetServerCh::UpdateQueue(UInt_64 active)
{
UInt_64 slot = 0;
for (UInt_64 i = 0; i < endpoints.Size(); ++i)
{
if (endpoints[i]->GetStatus() == NetStatus::QUEUED)
{
if (active < maxEndpoints)
{
endpoints[i]->SetStatus(NetStatus::ACTIVE);
endpoints[i]->SetQueueSlot(0);
Serializer payload(Endianness::LE);
payload.Write(NetStatus::ACTIVE);
payload.Write(0);
endpoints[i]->Send(false, true, false, internalSys, statusUpdateOp, payload);
OnEndpointActive(endpoints[i]);
++active;
}
else
{
if (endpoints[i]->GetQueueSlot() != slot)
{
Serializer payload(Endianness::LE);
payload.Write(NetStatus::QUEUED);
payload.Write(slot);
endpoints[i]->Send(false, true, false, internalSys, statusUpdateOp, payload);
endpoints[i]->SetQueueSlot(slot++);
}
else
{
++slot;
}
}
}
}
}
void NetServerCh::UpdateQueue()
{
UpdateQueue(GetEndpointsCount(NetStatus::ACTIVE));
}
bool NetServerCh::RemoveEndpoint(const Char_8 token[64])
{
for (UInt_64 i = 0; i < endpoints.Size(); ++i)
{
if (Util::Compare(endpoints[i]->token, token, 64))
{
delete endpoints[i];
if (i != endpoints.Size() - 1)
endpoints.Swap(i, endpoints.End());
endpoints.Pop();
return true;
}
}
return false;
}
bool NetServerCh::RemoveEndpoint(const Endpoint &endpoint)
{
for (UInt_64 i = 0; i < endpoints.Size(); ++i)
{
if (endpoints[i]->GetEndpoint().address == endpoint.address && endpoints[i]->GetEndpoint().port == endpoint.port)
{
delete endpoints[i];
if (i != endpoints.Size() - 1)
endpoints.Swap(i, endpoints.End());
endpoints.Pop();
return true;
}
}
return false;
}
bool NetServerCh::RemoveEndpoint(const NetEnd* const end)
{
for (UInt_64 i = 0; i < endpoints.Size(); ++i)
{
if (endpoints[i] == end)
{
delete endpoints[i];
if (i != endpoints.Size() - 1)
endpoints.Swap(i, endpoints.End());
endpoints.Pop();
return true;
}
}
return false;
}
void NetServerCh::Poll(const float &delta)
{
UInt_64 i = 0;
while (i < endpoints.Size())
{
endpoints[i]->Poll(delta);
if (endpoints[i]->GetStatus() == NetStatus::PENDING)
{
if (endpoints[i]->GetTimeout() >= GetMaxTimeout())
{
OnEndpointTimeout(endpoints[i]);
delete endpoints[i];
if (i != endpoints.Size() - 1)
endpoints.Swap(i, endpoints.End());
endpoints.Pop();
continue;
}
}
else
{
if (endpoints[i]->GetTimeout() >= GetMaxTimeout())
{
OnEndpointTimeout(endpoints[i]);
delete endpoints[i];
if (i != endpoints.Size() - 1)
endpoints.Swap(i, endpoints.End());
endpoints.Pop();
UpdateQueue();
continue;
}
Vector<NetFrag>* frags = endpoints[i]->GetReceived();
UInt_64 f = 0;
while (f < frags->Size())
{
if (!(*frags)[f].IsComplete())
{
++f;
continue;
}
Packet packet = (*frags)[f].Combine();
NetSys* sys = GetSystem(packet.header.systemId);
if (!sys)
{
++f;
continue;
}
sys->Execute(this, endpoints[i], packet.header.opId, packet.payload);
frags->Swap(f, frags->End());
frags->Pop();
}
}
++i;
}
}
void NetServerCh::Shutdown()
{
Serializer<UInt_64> payload = OnShutdown();
for (UInt_64 i = 0; i < endpoints.Size(); i++)
{
endpoints[i]->Send(false, 0, false, internalSys, disconnectOp, payload);
delete endpoints[i];
}
}
}

View File

@ -89,13 +89,13 @@ namespace ehs
return true;
}
void NetSys::Execute(EHC *ehc, NetEnd *endpoint, const UInt_64 hashId, Serializer<UInt_64> &payload)
void NetSys::Execute(NetChannel *channel, NetEnd *endpoint, const UInt_64 hashId, Serializer<UInt_64> &payload)
{
for (UInt_64 i = 0; i < ops.Size(); ++i)
{
if (ops[i]->GetHashId() == hashId)
{
ops[i]->Process(ehc, endpoint, this, payload);
ops[i]->Process(channel, endpoint, this, payload);
return;
}
}

View File

@ -0,0 +1,44 @@
#include "ehs/io/socket/ehc/NetUtils.h"
namespace ehs
{
void WriteHeader(const Header &header, Serializer<UInt_64> &data)
{
data.WriteVersion(header.version);
data.Write(header.encId);
data.WriteVersion(header.encVer);
data.Write(header.channelId);
data.Write(header.channelType);
data.WriteVersion(header.channelVer);
data.Write(header.id);
data.Write(header.fragmentCount);
data.Write(header.fragment);
data.Write(header.ensure);
data.WriteArray(header.token, 64);
data.Write(header.systemId);
data.Write(header.opId);
}
Header ReadHeader(Serializer<UInt_64> &data)
{
Header header = {};
header.version = data.ReadVersion();
header.encId = data.Read<UInt_64>();
header.encVer = data.ReadVersion();
header.channelId = data.Read<UInt_64>();
header.channelType = data.Read<NetChannelType>();
header.channelVer = data.ReadVersion();
header.id = data.Read<UInt_64>();
header.fragmentCount = data.Read<UInt_64>();
header.fragment = data.Read<UInt_64>();
header.ensure = data.Read<bool>();
UInt_64 tokenSize = sizeof(header.token) / sizeof(Char_8);
data.ReadArray<Char_8, UInt_64>(header.token, &tokenSize);
header.systemId = data.Read<UInt_64>();
header.opId = data.Read<UInt_64>();
return header;
}
}

View File

@ -18,7 +18,7 @@ namespace ehs
}
Spotify::Spotify(Str_8 clientId, Str_8 secret, Str_8 redURI, Array<Str_8> scopes, const bool forceVerify)
: client(AddrType::IPV4), clientId((Str_8 &&)clientId), secret((Str_8 &&)secret), redURI((Str_8 &&)redURI),
: client(IP::V4), clientId((Str_8 &&)clientId), secret((Str_8 &&)secret), redURI((Str_8 &&)redURI),
scopes((Array<Str_8> &&)scopes), forceVerify(forceVerify)
{
}
@ -41,7 +41,7 @@ namespace ehs
"&response_type=code&show_dialog=" + (forceVerify ? "true" : "false") + "&scope=" +
scopesFinal;
TCP server(AddrType::IPV4);
TCP server(IP::V4);
server.Initialize();
server.Bind("", 65534);
server.Listen();
@ -73,7 +73,7 @@ namespace ehs
server.Release();
SSL accounts(AddrType::IPV4);
SSL accounts(IP::V4);
accounts.Initialize();
accounts.Connect("accounts.spotify.com", SSL::HTTPS_Port);
@ -588,7 +588,7 @@ namespace ehs
bool Spotify::ReAuthorize()
{
SSL accounts(AddrType::IPV4);
SSL accounts(IP::V4);
accounts.Initialize();
accounts.Connect("accounts.spotify.com", SSL::HTTPS_Port);

View File

@ -16,7 +16,7 @@ namespace ehs
}
Twitch::Twitch(const Str_8& clientId, const Str_8& secret, const Str_8& redURI, const Array<Str_8>& scopes, const bool forceVerify)
: client(AddrType::IPV4), clientId(clientId), secret(secret), redURI(redURI), scopes(scopes), forceVerify(forceVerify)
: client(IP::V4), clientId(clientId), secret(secret), redURI(redURI), scopes(scopes), forceVerify(forceVerify)
{
}
@ -38,7 +38,7 @@ namespace ehs
"&response_type=code&force_verify=" + (forceVerify ? "true" : "false") + "&scope=" +
scopesFinal;
TCP server(AddrType::IPV4);
TCP server(IP::V4);
server.Bind("", 65535);
server.Listen();

View File

@ -53,7 +53,7 @@ namespace ehs
if (initialized)
return;
client = TCP(ehs::AddrType::IPV4);
client = TCP(ehs::IP::V4);
client.Connect(DNS::Resolve("irc.chat.twitch.tv"), 6667);
client.SetBlocking(false);