This commit is contained in:
Arron Nelson 2025-01-29 16:21:11 -08:00
parent 39bbcd0d56
commit 62f8a662a0
7 changed files with 52 additions and 38 deletions

View File

@ -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] 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. /// @param [in] port The port to bind to.
/// @note Requires the port given to be forwarded if this is called. /// @note Requires the port given to be forwarded if this is called.
void Bind(IP type, const Str_8& address, UInt_16 port) override; void Bind(const Endpoint &endpoint) override;
/// Sends data using a C-style byte array. /// 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. /// @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] data The C-style byte array to send.
/// @param [in] size The size of the C-style byte array. /// @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". /// @note The size of data to be sent cannot exceed "UDP::maxPayloadIpv4" or "UDP::maxPayloadIpv6".
UInt_64 Send(IP type, const Str_8& addr, 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. /// Receives data using the packet helper class.
/// @param [out] addr The Ipv4 or Ipv6 address of the sender. /// @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. /// @param [in] size The size of the pre-allocated C-style byte array.
/// @returns The size of the data received. /// @returns The size of the data received.
/// @warning The provided C-style byte array must be freed when finished using. /// @warning The provided C-style byte array must be freed when finished using.
UInt_64 Receive(IP* type, Str_8* addr, 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. /// Sets whether or not receiving data blocks the next task.
/// @param [in] blocking Whether or not to block. /// @param [in] blocking Whether or not to block.

View File

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

View File

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

View File

@ -110,33 +110,32 @@ namespace ehs
bound = false; bound = false;
} }
void UDP::Bind(const IP type, const Str_8& address, const UInt_16 port) void UDP::Bind(const Endpoint &endpoint)
{ {
if (!IsValid() || bound) if (!IsValid() || bound)
return; return;
if (type == IP::V6) if (endpoint.version == IP::V6)
Bind_v6(address, port); Bind_v6(endpoint.address, endpoint.port);
else if (type == IP::V4) else if (endpoint.version == IP::V4)
Bind_v4(address, port); Bind_v4(endpoint.address, endpoint.port);
this->address = address; localEndpoint = endpoint;
this->port = port;
bound = true; bound = true;
} }
UInt_64 UDP::Send(const IP 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 == IP::V6) if (endpoint.version == IP::V6)
return Send_v6(address, port, data, size); return Send_v6(endpoint.address, endpoint.port, data, size);
else if (type == IP::V4) else if (endpoint.version == IP::V4)
return Send_v4(address, port, data, size); return Send_v4(endpoint.address, endpoint.port, data, size);
return 0; return 0;
} }
UInt_64 UDP::Receive(IP* 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()) if (!IsValid())
{ {
@ -174,9 +173,11 @@ namespace ehs
return received; return received;
} }
*type = IP::V6; *endpoint = {
*address = tmpAddr; IP::V6,
*port = ntohs(remote.sin6_port); tmpAddr,
ntohs(remote.sin6_port)
};
} }
else if (addrLen == sizeof(sockaddr_in)) else if (addrLen == sizeof(sockaddr_in))
{ {
@ -191,9 +192,11 @@ namespace ehs
return received; return received;
} }
*type = IP::V4; *endpoint = {
*address = tmpAddr; IP::V4,
*port = ntohs(((sockaddr_in*)&remote)->sin_port); tmpAddr,
ntohs(((sockaddr_in*)&remote)->sin_port)
};
} }
return received; return received;
@ -225,7 +228,7 @@ namespace ehs
void UDP::SetIPv6Only(const bool value) void UDP::SetIPv6Only(const bool value)
{ {
if (type != IP::V6) if (localEndpoint.version != IP::V6)
{ {
EHS_LOG_INT(LogType::WARN, 0, "Cannot set IPv6 only mode while socket is not using IPv6."); EHS_LOG_INT(LogType::WARN, 0, "Cannot set IPv6 only mode while socket is not using IPv6.");
return; return;
@ -249,7 +252,7 @@ namespace ehs
bool UDP::IsIPv6Only() const bool UDP::IsIPv6Only() const
{ {
if (type != IP::V6) if (localEndpoint.version != IP::V6)
return false; return false;
if (!IsValid()) if (!IsValid())
@ -362,7 +365,7 @@ namespace ehs
Int_32 code = inet_pton(AF_INET6, addr, &result.sin6_addr); Int_32 code = inet_pton(AF_INET6, addr, &result.sin6_addr);
if (!code) if (!code)
{ {
EHS_LOG_INT(LogType::ERR, 1, "The given address, \"" + address + "\" is not valid."); EHS_LOG_INT(LogType::ERR, 1, "The given address, \"" + addr + "\" is not valid.");
return 0; return 0;
} }
else if (code == -1) else if (code == -1)
@ -403,7 +406,7 @@ namespace ehs
int code = inet_pton(AF_INET, addr, &result.sin_addr); int code = inet_pton(AF_INET, addr, &result.sin_addr);
if (!code) if (!code)
{ {
EHS_LOG_INT(LogType::ERR, 1, "The given address, \"" + address + "\" is not valid."); EHS_LOG_INT(LogType::ERR, 1, "The given address, \"" + addr + "\" is not valid.");
return 0; return 0;
} }
else if (code == -1) else if (code == -1)

View File

@ -7,6 +7,16 @@
namespace ehs namespace ehs
{ {
const UInt_64 NetChannel::internalSys = Str_8::Hash_64("Internal"); const UInt_64 NetChannel::internalSys = Str_8::Hash_64("Internal");
const UInt_64 NetChannel::connectOp = Str_8::Hash_64("Connect");
const UInt_64 NetChannel::connectedOp = Str_8::Hash_64("Connected");
const UInt_64 NetChannel::rejectedOp = Str_8::Hash_64("Rejected");
const UInt_64 NetChannel::disconnectOp = Str_8::Hash_64("Disconnect");
const UInt_64 NetChannel::disconnectedOp = Str_8::Hash_64("Disconnected");
const UInt_64 NetChannel::statusUpdateOp = Str_8::Hash_64("StatusUpdate");
const UInt_64 NetChannel::pingOp = Str_8::Hash_64("Ping");
const UInt_64 NetChannel::pongOp = Str_8::Hash_64("Pong");
const UInt_64 NetChannel::latencyOp = Str_8::Hash_64("Latency");
const UInt_64 NetChannel::receivedOp = Str_8::Hash_64("Received");
NetChannel::~NetChannel() NetChannel::~NetChannel()
{ {

View File

@ -59,7 +59,7 @@ namespace ehs
return hashId; return hashId;
} }
void NetOp::Process(NetChannel *channel, NetEnd *endpoint, NetSys *sys, Serializer<UInt_64> &payload) void NetOp::Execute(NetChannel *channel, NetEnd *endpoint, NetSys *sys, Serializer<UInt_64> &payload)
{ {
} }
} }

View File

@ -2,6 +2,7 @@
#include "ehs/io/socket/EHC.h" #include "ehs/io/socket/EHC.h"
#include "ehs/io/socket/ehc/NetEnd.h" #include "ehs/io/socket/ehc/NetEnd.h"
#include "ehs/io/socket/ehc/NetOp.h" #include "ehs/io/socket/ehc/NetOp.h"
#include "ehs/io/socket/ehc/NetChannel.h"
namespace ehs namespace ehs
{ {
@ -95,7 +96,7 @@ namespace ehs
{ {
if (ops[i]->GetHashId() == hashId) if (ops[i]->GetHashId() == hashId)
{ {
ops[i]->Process(channel, endpoint, this, payload); ops[i]->Execute(channel, endpoint, this, payload);
return; return;
} }
} }