From 6d876e831f93e4ea5c862b7e0423d9020a7362e9 Mon Sep 17 00:00:00 2001 From: dgelessus Date: Sat, 19 Aug 2023 00:49:41 +0200 Subject: [PATCH 1/3] Include Winsock header only where it's actually used pnNetCommon is also the only library that actually links with Winsock, so including the header elsewhere doesn't make much sense. --- Sources/Plasma/CoreLib/hsWindows.h | 5 ++--- Sources/Plasma/NucleusLib/pnNetCommon/pnNetCommon.cpp | 8 +++++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/Sources/Plasma/CoreLib/hsWindows.h b/Sources/Plasma/CoreLib/hsWindows.h index 013b58c609..b8f126aab9 100644 --- a/Sources/Plasma/CoreLib/hsWindows.h +++ b/Sources/Plasma/CoreLib/hsWindows.h @@ -49,8 +49,8 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com /** \file hsWindows.h * \brief Pulls in Windows core headers * - * This file pulls in the core Windows headers and Winsock2. It is separate from - * HeadSpin.h to improve build times and to facillitate adding precompiled headers. + * This file pulls in the core Windows headers. It is separate from HeadSpin.h + * to improve build times and to facilitate adding precompiled headers. * You should avoid including this header from other headers! */ @@ -75,7 +75,6 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com # define WIN32_LEAN_AND_MEAN # include -# include // Pulls in WinSock 2 for us // This needs to be after #include , since it also includes windows.h # ifdef USE_VLD diff --git a/Sources/Plasma/NucleusLib/pnNetCommon/pnNetCommon.cpp b/Sources/Plasma/NucleusLib/pnNetCommon/pnNetCommon.cpp index 8207527a81..607528ace0 100644 --- a/Sources/Plasma/NucleusLib/pnNetCommon/pnNetCommon.cpp +++ b/Sources/Plasma/NucleusLib/pnNetCommon/pnNetCommon.cpp @@ -41,14 +41,16 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com *==LICENSE==*/ #include #include "pnNetCommon.h" -#include "hsWindows.h" -#if HS_BUILD_FOR_UNIX +#if defined(HS_BUILD_FOR_UNIX) # include # include # include # include -#elif !defined(HS_BUILD_FOR_WIN32) +#elif defined(HS_BUILD_FOR_WIN32) +#include "hsWindows.h" +#include +#else #error "Not implemented for this platform" #endif From afc2dc63fae98073d5c4bd5ca274d1c4a9ea42b9 Mon Sep 17 00:00:00 2001 From: dgelessus Date: Sat, 19 Aug 2023 01:02:19 +0200 Subject: [PATCH 2/3] Don't expect specific value for AF_INET from system headers Although AF_INET is always 2 on every relevant system, it's better not to assume this. Here, we use AF_INET only as a marker value in a serialization format and never pass it to any system functions, so it's more important that the value is consistent between systems than that it matches the system constant. --- Sources/Plasma/NucleusLib/pnNetCommon/plNetAddress.cpp | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/Sources/Plasma/NucleusLib/pnNetCommon/plNetAddress.cpp b/Sources/Plasma/NucleusLib/pnNetCommon/plNetAddress.cpp index f3151f2135..1a8912809f 100644 --- a/Sources/Plasma/NucleusLib/pnNetCommon/plNetAddress.cpp +++ b/Sources/Plasma/NucleusLib/pnNetCommon/plNetAddress.cpp @@ -46,10 +46,6 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com #include "plNetAddress.h" #include "pnNetCommon.h" -#ifndef AF_INET -# define AF_INET 2 -#endif - ST::string plNetAddress::GetHostString() const { return pnNetCommon::GetTextAddr(fHost); @@ -105,5 +101,5 @@ void plNetAddress::Write(hsStream * s) s->WriteLE16(fPort); // Family is always AF_INET - s->WriteLE16((uint16_t)AF_INET); + s->WriteLE16(static_cast(2)); } From 7b1bee4daa4d0b8c7246adfd92fc3bbc22d5b7b2 Mon Sep 17 00:00:00 2001 From: dgelessus Date: Sat, 19 Aug 2023 14:53:28 +0200 Subject: [PATCH 3/3] Add an OS-independent enum for plNetAddress families Only has kInet (AF_INET, IPv4) for now. This will be relevant if IPv6 support is ever added here - unlike AF_INET, which is 2 on all relevant OSes, AF_INET6 has a different value on Windows, macOS, and Linux. --- Sources/Plasma/NucleusLib/pnNetCommon/plNetAddress.cpp | 5 ++--- Sources/Plasma/NucleusLib/pnNetCommon/plNetAddress.h | 6 ++++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/Sources/Plasma/NucleusLib/pnNetCommon/plNetAddress.cpp b/Sources/Plasma/NucleusLib/pnNetCommon/plNetAddress.cpp index 1a8912809f..e1e020cdcb 100644 --- a/Sources/Plasma/NucleusLib/pnNetCommon/plNetAddress.cpp +++ b/Sources/Plasma/NucleusLib/pnNetCommon/plNetAddress.cpp @@ -91,7 +91,7 @@ void plNetAddress::Read(hsStream * s) fHost = s->ReadLE32(); fPort = s->ReadLE16(); - // Family is always AF_INET + // Family is always kInet (void) s->ReadLE16(); } @@ -100,6 +100,5 @@ void plNetAddress::Write(hsStream * s) s->WriteLE32(fHost); s->WriteLE16(fPort); - // Family is always AF_INET - s->WriteLE16(static_cast(2)); + s->WriteLE16(static_cast(Family::kInet)); } diff --git a/Sources/Plasma/NucleusLib/pnNetCommon/plNetAddress.h b/Sources/Plasma/NucleusLib/pnNetCommon/plNetAddress.h index 11d34a24bb..ab78a2214b 100644 --- a/Sources/Plasma/NucleusLib/pnNetCommon/plNetAddress.h +++ b/Sources/Plasma/NucleusLib/pnNetCommon/plNetAddress.h @@ -61,6 +61,12 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com */ class plNetAddress { + // This is used in the Read/Write format and so must not use the potentially OS-dependent AF_* constants. + enum class Family : uint16_t + { + kInet = 2, // matches AF_INET from Winsock + }; + uint32_t fHost; uint16_t fPort;