From 62f8a662a0a139f3bbac0a61f2c6fc975bb31792 Mon Sep 17 00:00:00 2001 From: Arron Nelson Date: Wed, 29 Jan 2025 16:21:11 -0800 Subject: [PATCH] Backup. --- include/ehs/io/socket/UDP_W32.h | 6 ++-- include/ehs/io/socket/ehc/NetOp.h | 2 +- src/io/socket/TCP_W32.cpp | 18 ++++++------ src/io/socket/UDP_W32.cpp | 49 ++++++++++++++++--------------- src/io/socket/ehc/NetChannel.cpp | 10 +++++++ src/io/socket/ehc/NetOp.cpp | 2 +- src/io/socket/ehc/NetSys.cpp | 3 +- 7 files changed, 52 insertions(+), 38 deletions(-) diff --git a/include/ehs/io/socket/UDP_W32.h b/include/ehs/io/socket/UDP_W32.h index 92447d8..fe491bc 100644 --- a/include/ehs/io/socket/UDP_W32.h +++ b/include/ehs/io/socket/UDP_W32.h @@ -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(IP 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(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. /// @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(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. /// @param [in] blocking Whether or not to block. diff --git a/include/ehs/io/socket/ehc/NetOp.h b/include/ehs/io/socket/ehc/NetOp.h index 99d5281..6302c34 100644 --- a/include/ehs/io/socket/ehc/NetOp.h +++ b/include/ehs/io/socket/ehc/NetOp.h @@ -37,6 +37,6 @@ namespace ehs UInt_64 GetHashId() const; private: - virtual void Process(NetChannel *channel, NetEnd *endpoint, NetSys *sys, Serializer &payload); + virtual void Execute(NetChannel *channel, NetEnd *issuer, NetSys *sys, Serializer &payload); }; } \ No newline at end of file diff --git a/src/io/socket/TCP_W32.cpp b/src/io/socket/TCP_W32.cpp index 09e2349..e2b2344 100644 --- a/src/io/socket/TCP_W32.cpp +++ b/src/io/socket/TCP_W32.cpp @@ -90,9 +90,9 @@ namespace ehs return; } - if (IP == IP::V6) + if (ip == IP::V6) 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); else return; @@ -142,9 +142,9 @@ namespace ehs if (!IsValid() || bound || connection) return; - if (IP == IP::V6) + if (ip == IP::V6) Bind_v6(address, port); - else if (IP == IP::V4) + 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->IP = IP; + client->ip = ip; client->localAddr = localAddr; client->localPort = localPort; client->connection = true; @@ -244,7 +244,7 @@ namespace ehs remoteHostName = address; remotePort = port; - if (IP == IP::V6) + if (ip == IP::V6) { if (IsIPv6Only()) remoteAddr = DNS::Resolve(IP::V6, address); @@ -253,7 +253,7 @@ namespace ehs Connect_v6(remoteAddr, port); } - else if (IP == IP::V4) + else if (ip == IP::V4) { remoteAddr = DNS::Resolve(IP::V4, address); @@ -359,7 +359,7 @@ namespace ehs 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."); return; @@ -383,7 +383,7 @@ namespace ehs bool TCP::IsIPv6Only() const { - if (IP != IP::V6) + if (ip != IP::V6) return false; if (!IsValid()) diff --git a/src/io/socket/UDP_W32.cpp b/src/io/socket/UDP_W32.cpp index dd2c966..09765f6 100644 --- a/src/io/socket/UDP_W32.cpp +++ b/src/io/socket/UDP_W32.cpp @@ -110,33 +110,32 @@ namespace ehs 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) return; - if (type == IP::V6) - Bind_v6(address, port); - else if (type == IP::V4) - 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 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) - return Send_v6(address, port, data, size); - else if (type == IP::V4) - 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(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()) { @@ -174,9 +173,11 @@ namespace ehs return received; } - *type = IP::V6; - *address = tmpAddr; - *port = ntohs(remote.sin6_port); + *endpoint = { + IP::V6, + tmpAddr, + ntohs(remote.sin6_port) + }; } else if (addrLen == sizeof(sockaddr_in)) { @@ -191,9 +192,11 @@ namespace ehs return received; } - *type = IP::V4; - *address = tmpAddr; - *port = ntohs(((sockaddr_in*)&remote)->sin_port); + *endpoint = { + IP::V4, + tmpAddr, + ntohs(((sockaddr_in*)&remote)->sin_port) + }; } return received; @@ -225,7 +228,7 @@ namespace ehs 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."); return; @@ -249,7 +252,7 @@ namespace ehs bool UDP::IsIPv6Only() const { - if (type != IP::V6) + if (localEndpoint.version != IP::V6) return false; if (!IsValid()) @@ -362,7 +365,7 @@ namespace ehs Int_32 code = inet_pton(AF_INET6, addr, &result.sin6_addr); 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; } else if (code == -1) @@ -403,7 +406,7 @@ namespace ehs int code = inet_pton(AF_INET, addr, &result.sin_addr); 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; } else if (code == -1) diff --git a/src/io/socket/ehc/NetChannel.cpp b/src/io/socket/ehc/NetChannel.cpp index 15f3b5b..7a4e0f5 100644 --- a/src/io/socket/ehc/NetChannel.cpp +++ b/src/io/socket/ehc/NetChannel.cpp @@ -7,6 +7,16 @@ namespace ehs { 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() { diff --git a/src/io/socket/ehc/NetOp.cpp b/src/io/socket/ehc/NetOp.cpp index 7488dd1..2551084 100644 --- a/src/io/socket/ehc/NetOp.cpp +++ b/src/io/socket/ehc/NetOp.cpp @@ -59,7 +59,7 @@ namespace ehs return hashId; } - void NetOp::Process(NetChannel *channel, NetEnd *endpoint, NetSys *sys, Serializer &payload) + void NetOp::Execute(NetChannel *channel, NetEnd *endpoint, NetSys *sys, Serializer &payload) { } } \ No newline at end of file diff --git a/src/io/socket/ehc/NetSys.cpp b/src/io/socket/ehc/NetSys.cpp index 4feb0c9..0879982 100644 --- a/src/io/socket/ehc/NetSys.cpp +++ b/src/io/socket/ehc/NetSys.cpp @@ -2,6 +2,7 @@ #include "ehs/io/socket/EHC.h" #include "ehs/io/socket/ehc/NetEnd.h" #include "ehs/io/socket/ehc/NetOp.h" +#include "ehs/io/socket/ehc/NetChannel.h" namespace ehs { @@ -95,7 +96,7 @@ namespace ehs { if (ops[i]->GetHashId() == hashId) { - ops[i]->Process(channel, endpoint, this, payload); + ops[i]->Execute(channel, endpoint, this, payload); return; } }