From 227188243ec1efc6dd73172acc3f11f855c8312d Mon Sep 17 00:00:00 2001 From: karutoh Date: Thu, 1 Feb 2024 19:56:06 -0800 Subject: [PATCH] Fixed UDP/TCP sockets and documented BaseObj class. --- include/ehs/BaseObj.h | 22 ++++++++++++++++++++++ include/ehs/io/socket/BaseTCP.h | 4 ++-- include/ehs/io/socket/TCP_BSD.h | 20 ++++++++++---------- include/ehs/io/socket/TCP_W32.h | 18 +++++++++--------- src/io/socket/TCP_BSD.cpp | 12 ++++++------ src/io/socket/TCP_W32.cpp | 12 ++++++------ 6 files changed, 55 insertions(+), 33 deletions(-) diff --git a/include/ehs/BaseObj.h b/include/ehs/BaseObj.h index d87ac99..76ce8b5 100644 --- a/include/ehs/BaseObj.h +++ b/include/ehs/BaseObj.h @@ -28,25 +28,47 @@ namespace ehs bool operator!=(const BaseObj& base) const; + /// Retrieves the class hierarchy. + /// @returns The hierarchy array. const Type* GetHierarchy() const; + /// Retrieves the class hierarchy size. + /// @returns The hierarchy size. UInt_64 GetHierarchySize() const; + /// Checks if this class derives from another. + /// @param [in] typeHashId The type hash id to look for. + /// @returns True if found. bool HasType(UInt_64 typeHashId) const; + /// Checks if this class derives from another. + /// @param [in] typeId The type id to look for. + /// @returns True if found. bool HasType(const Char_8* typeId) const; + /// Retrieves the top class' information. + /// @returns The Type object containing the class information. Type GetType() const; + /// Retrieves the top class' string name, size. + /// @returns The name size. UInt_64 GetTypeIdSize() const; + /// Retrieves the top class' string name. + /// @returns The name. const Char_8* GetTypeId() const; + /// Retrieves the top class' hashed name. + /// @returns The hashed name. UInt_64 GetTypeHashId() const; + /// Clones the object onto the heap. + /// @returns The cloned object. virtual BaseObj* Clone() const; protected: + /// Adds the class name to the class hierarchy. + /// @param [in] id The name of the class to add. void AddType(const Char_8* id); }; } \ No newline at end of file diff --git a/include/ehs/io/socket/BaseTCP.h b/include/ehs/io/socket/BaseTCP.h index 49ec5c1..546525d 100644 --- a/include/ehs/io/socket/BaseTCP.h +++ b/include/ehs/io/socket/BaseTCP.h @@ -14,10 +14,10 @@ namespace ehs protected: AddrType addrType; Str_8 localAddr; - unsigned short localPort; + UInt_16 localPort; Str_8 remoteHostName; Str_8 remoteAddr; - unsigned short remotePort; + UInt_16 remotePort; bool connection; bool bound; bool listening; diff --git a/include/ehs/io/socket/TCP_BSD.h b/include/ehs/io/socket/TCP_BSD.h index f25e705..edc9258 100644 --- a/include/ehs/io/socket/TCP_BSD.h +++ b/include/ehs/io/socket/TCP_BSD.h @@ -22,7 +22,7 @@ namespace ehs /// Default members initialization. TCP(); - TCP(const AddrType addrType); + TCP(AddrType addrType); TCP(TCP&& tcp) noexcept; @@ -46,7 +46,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(const Str_8& address, unsigned short port) override; + void Bind(const Str_8& address, UInt_16 port) override; /// Listens for incoming connections. Used for servers or PtP. void Listen() override; @@ -58,23 +58,23 @@ namespace ehs /// Connects to a TCP Socket that listens for incoming connections. Used for clients or PtP. /// @param address The address of the listening TCP socket. Resolves domain names. The given address can be empty, "127.0.0.1", or "localhost" to automatically find the appropriate device. /// @param port The port of the listening TCP socket. - void Connect(const Str_8& address, const unsigned short port) override; + void Connect(const Str_8& address, UInt_16 port) override; /// Sends data in a C-style array with raw functionality. Meaning no internal help outside of native functions besides error checking. /// @param [in] buffer The C-style array to send. /// @param [in] size The size of the given C-style array. /// @returns The size of the data actually sent in bytes. - UInt_64 Send(const Byte* const buffer, const UInt_32 size) override; + UInt_64 Send(const Byte* buffer, UInt_32 size) override; /// Receives data in a C-style array with raw functionality. Meaning no internal help outside of native functions besides error checking. /// @param [out] buffer The C-style array to receive with. /// @param [in] size The size of the given C-style array. /// @returns The size of the data actually received in bytes. - UInt_64 Receive(Byte* const buffer, const UInt_32 size) override; + UInt_64 Receive(Byte* buffer, UInt_32 size) override; /// Sets whether or not receiving data blocks the next task. /// @param [in] blocking Whether or not to block. - void SetBlocking(const bool blocking) override; + void SetBlocking(bool blocking) override; /// Retrieves whether or not this socket will block when receiving data. /// @returns The result. @@ -83,12 +83,12 @@ namespace ehs bool IsValid() const override; private: - void Bind_v6(const Str_8& address, unsigned short port); + void Bind_v6(const Str_8& address, UInt_16 port); - void Bind_v4(const Str_8& address, unsigned short port); + void Bind_v4(const Str_8& address, UInt_16 port); - void Connect_v6(const Str_8& address, unsigned short port); + void Connect_v6(const Str_8& address, UInt_16 port); - void Connect_v4(const Str_8& address, unsigned short port); + void Connect_v4(const Str_8& address, UInt_16 port); }; } \ No newline at end of file diff --git a/include/ehs/io/socket/TCP_W32.h b/include/ehs/io/socket/TCP_W32.h index f25e705..b949158 100644 --- a/include/ehs/io/socket/TCP_W32.h +++ b/include/ehs/io/socket/TCP_W32.h @@ -46,7 +46,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(const Str_8& address, unsigned short port) override; + void Bind(const Str_8& address, UInt_16 port) override; /// Listens for incoming connections. Used for servers or PtP. void Listen() override; @@ -58,23 +58,23 @@ namespace ehs /// Connects to a TCP Socket that listens for incoming connections. Used for clients or PtP. /// @param address The address of the listening TCP socket. Resolves domain names. The given address can be empty, "127.0.0.1", or "localhost" to automatically find the appropriate device. /// @param port The port of the listening TCP socket. - void Connect(const Str_8& address, const unsigned short port) override; + void Connect(const Str_8& address, UInt_16 port) override; /// Sends data in a C-style array with raw functionality. Meaning no internal help outside of native functions besides error checking. /// @param [in] buffer The C-style array to send. /// @param [in] size The size of the given C-style array. /// @returns The size of the data actually sent in bytes. - UInt_64 Send(const Byte* const buffer, const UInt_32 size) override; + UInt_64 Send(const Byte* buffer, UInt_32 size) override; /// Receives data in a C-style array with raw functionality. Meaning no internal help outside of native functions besides error checking. /// @param [out] buffer The C-style array to receive with. /// @param [in] size The size of the given C-style array. /// @returns The size of the data actually received in bytes. - UInt_64 Receive(Byte* const buffer, const UInt_32 size) override; + UInt_64 Receive(Byte* buffer, UInt_32 size) override; /// Sets whether or not receiving data blocks the next task. /// @param [in] blocking Whether or not to block. - void SetBlocking(const bool blocking) override; + void SetBlocking(bool blocking) override; /// Retrieves whether or not this socket will block when receiving data. /// @returns The result. @@ -83,12 +83,12 @@ namespace ehs bool IsValid() const override; private: - void Bind_v6(const Str_8& address, unsigned short port); + void Bind_v6(const Str_8& address, UInt_16 port); - void Bind_v4(const Str_8& address, unsigned short port); + void Bind_v4(const Str_8& address, UInt_16 port); - void Connect_v6(const Str_8& address, unsigned short port); + void Connect_v6(const Str_8& address, UInt_16 port); - void Connect_v4(const Str_8& address, unsigned short port); + void Connect_v4(const Str_8& address, UInt_16 port); }; } \ No newline at end of file diff --git a/src/io/socket/TCP_BSD.cpp b/src/io/socket/TCP_BSD.cpp index 3ec2998..39a8c2a 100644 --- a/src/io/socket/TCP_BSD.cpp +++ b/src/io/socket/TCP_BSD.cpp @@ -114,7 +114,7 @@ namespace ehs hdl = EHS_INVALID_SOCKET; } - void TCP::Bind(const Str_8& address, unsigned short port) + void TCP::Bind(const Str_8& address, UInt_16 port) { if (!IsValid() || bound || connection) return; @@ -207,7 +207,7 @@ namespace ehs return client; } - void TCP::Connect(const Str_8& address, const unsigned short port) + void TCP::Connect(const Str_8& address, const UInt_16 port) { if (connection || !IsValid() || listening) return; @@ -331,7 +331,7 @@ namespace ehs return hdl != EHS_INVALID_SOCKET; } - void TCP::Bind_v6(const Str_8& address, unsigned short port) + void TCP::Bind_v6(const Str_8& address, UInt_16 port) { sockaddr_in6 result = {}; result.sin6_family = AF_INET6; @@ -367,7 +367,7 @@ namespace ehs } } - void TCP::Bind_v4(const Str_8& address, unsigned short port) + void TCP::Bind_v4(const Str_8& address, UInt_16 port) { sockaddr_in result = {}; result.sin_family = AF_INET; @@ -402,7 +402,7 @@ namespace ehs } } - void TCP::Connect_v6(const Str_8& address, unsigned short port) + void TCP::Connect_v6(const Str_8& address, UInt_16 port) { sockaddr_in6 result = {}; result.sin6_family = AF_INET6; @@ -439,7 +439,7 @@ namespace ehs } } - void TCP::Connect_v4(const Str_8& address, unsigned short port) + void TCP::Connect_v4(const Str_8& address, UInt_16 port) { sockaddr_in result = {}; result.sin_family = AF_INET; diff --git a/src/io/socket/TCP_W32.cpp b/src/io/socket/TCP_W32.cpp index 3c09e88..21062e7 100644 --- a/src/io/socket/TCP_W32.cpp +++ b/src/io/socket/TCP_W32.cpp @@ -137,7 +137,7 @@ namespace ehs connected = false; } - void TCP::Bind(const Str_8& address, unsigned short port) + void TCP::Bind(const Str_8& address, UInt_16 port) { if (!IsValid() || bound || connection) return; @@ -236,7 +236,7 @@ namespace ehs return client; } - void TCP::Connect(const Str_8& address, const unsigned short port) + void TCP::Connect(const Str_8& address, const UInt_16 port) { if (connection || !IsValid() || listening) return; @@ -352,7 +352,7 @@ namespace ehs return hdl != EHS_INVALID_SOCKET; } - void TCP::Bind_v6(const Str_8& address, unsigned short port) + void TCP::Bind_v6(const Str_8& address, UInt_16 port) { sockaddr_in6 result = {}; result.sin6_family = AF_INET6; @@ -388,7 +388,7 @@ namespace ehs } } - void TCP::Bind_v4(const Str_8& address, unsigned short port) + void TCP::Bind_v4(const Str_8& address, UInt_16 port) { sockaddr_in result = {}; result.sin_family = AF_INET; @@ -424,7 +424,7 @@ namespace ehs } } - void TCP::Connect_v6(const Str_8& address, unsigned short port) + void TCP::Connect_v6(const Str_8& address, UInt_16 port) { sockaddr_in6 result = {}; result.sin6_family = AF_INET6; @@ -461,7 +461,7 @@ namespace ehs } } - void TCP::Connect_v4(const Str_8& address, unsigned short port) + void TCP::Connect_v4(const Str_8& address, UInt_16 port) { sockaddr_in result = {}; result.sin_family = AF_INET;