From fd7bea0f36c363e1efe67b76db37611cbc4235d5 Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Sat, 27 Nov 2021 02:11:22 +0100 Subject: [PATCH] Add BEAMMP_{WINDOWS,LINUX,APPLE} preprocessor defines instead of platform specific ones --- CMakeLists.txt | 3 ++- include/Client.h | 4 ++-- include/Compat.h | 18 ++++++++---------- include/Environment.h | 17 +++++++++++++++++ src/Common.cpp | 10 +++++----- src/SignalHandling.cpp | 14 ++++++-------- src/TNetwork.cpp | 22 +++++++++++----------- 7 files changed, 51 insertions(+), 37 deletions(-) create mode 100644 include/Environment.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 0a50caa..133b274 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -98,7 +98,8 @@ add_executable(BeamMP-Server include/LuaAPI.h src/LuaAPI.cpp include/TScopedTimer.h src/TScopedTimer.cpp include/SignalHandling.h src/SignalHandling.cpp - include/ArgsParser.h src/ArgsParser.cpp) + include/ArgsParser.h src/ArgsParser.cpp + include/Environment.h) target_compile_definitions(BeamMP-Server PRIVATE SECRET_SENTRY_URL="${BEAMMP_SECRET_SENTRY_URL}") include_directories(BeamMP-Server PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include) diff --git a/include/Client.h b/include/Client.h index 0d66e50..93882f2 100644 --- a/include/Client.h +++ b/include/Client.h @@ -13,10 +13,10 @@ class TServer; -#ifdef WIN32 +#ifdef BEAMMP_WINDOWS // for socklen_t #include -#endif // WIN32 +#endif // WINDOWS struct TConnection final { SOCKET Socket; diff --git a/include/Compat.h b/include/Compat.h index 7a366d8..e1e906d 100644 --- a/include/Compat.h +++ b/include/Compat.h @@ -1,8 +1,10 @@ #pragma once +#include "Environment.h" + // ======================= UNIX ======================== -#ifdef __unix +#ifdef BEAMMP_LINUX #include #include #include @@ -19,7 +21,9 @@ inline void CloseSocketProper(int TheSocket) { } #endif // unix -#ifdef __APPLE__ +// ======================= APPLE ======================== + +#ifdef BEAMMP_APPLE #include #include #include @@ -36,9 +40,9 @@ inline void CloseSocketProper(int TheSocket) { } #endif // unix -// ======================= WIN32 ======================= +// ======================= WINDOWS ======================= -#ifdef WIN32 +#ifdef BEAMMP_WINDOWS #include #include inline void CloseSocketProper(SOCKET TheSocket) { @@ -47,9 +51,3 @@ inline void CloseSocketProper(SOCKET TheSocket) { } #endif // WIN32 - -// ======================= OTHER ======================= - -#if !defined(WIN32) && !defined(__unix) && !defined(__APPLE__) -#error "OS not supported" -#endif \ No newline at end of file diff --git a/include/Environment.h b/include/Environment.h new file mode 100644 index 0000000..5c01601 --- /dev/null +++ b/include/Environment.h @@ -0,0 +1,17 @@ +#pragma once + +// one of BEAMMP_{WINDOWS,LINUX,APPLE} will be set at the end of this + +// clang-format off +#if !defined(BEAMMP_WINDOWS) && !defined(BEAMMP_UNIX) && !defined(BEAMMP_APPLE) + #if defined(_WIN32) || defined(__CYGWIN__) + #define BEAMMP_WINDOWS + #elif defined(__linux__) || defined(__linux) || defined(linux) || defined(__unix__) || defined(__unix) || defined(unix) + #define BEAMMP_LINUX + #elif defined(__APPLE__) || defined(__MACH__) + #define BEAMMP_APPLE + #else + #error "This platform is not known. Please define one of the above for your OS." + #endif +#endif +// clang-format on diff --git a/src/Common.cpp b/src/Common.cpp index 59fb366..acf3b21 100644 --- a/src/Common.cpp +++ b/src/Common.cpp @@ -115,11 +115,11 @@ std::string ThreadName(bool DebugModeOverride) { void RegisterThread(const std::string& str) { std::string ThreadId; -#ifdef WIN32 +#ifdef BEAMMP_WINDOWS ThreadId = std::to_string(GetCurrentThreadId()); -#elif __APPLE__ +#elif defined(BEAMMP_APPLE) ThreadId = std::to_string(getpid()); // todo: research if 'getpid()' is a valid, posix compliant alternative to 'gettid()' -#else +#elif defined(BEAMMP_LINUX) ThreadId = std::to_string(gettid()); #endif if (Application::Settings.DebugModeEnabled) { @@ -159,7 +159,7 @@ void LogChatMessage(const std::string& name, int id, const std::string& msg) { } std::string GetPlatformAgnosticErrorString() { -#ifdef WIN32 +#ifdef BEAMMP_WINDOWS // This will provide us with the error code and an error message, all in one. int err; char msgbuf[256]; @@ -180,7 +180,7 @@ std::string GetPlatformAgnosticErrorString() { } else { return std::to_string(GetLastError()); } -#else // posix +#elif defined(BEAMMP_LINUX) || defined(BEAMMP_APPLE) return std::strerror(errno); #endif } diff --git a/src/SignalHandling.cpp b/src/SignalHandling.cpp index 49f50c5..2b5070e 100644 --- a/src/SignalHandling.cpp +++ b/src/SignalHandling.cpp @@ -1,7 +1,7 @@ #include "SignalHandling.h" #include "Common.h" -#if defined(__unix) || defined(__linux) || defined(__APPLE__) +#if defined(BEAMMP_LINUX) || defined(BEAMMP_APPLE) #include static void UnixSignalHandler(int sig) { switch (sig) { @@ -21,9 +21,9 @@ static void UnixSignalHandler(int sig) { break; } } -#endif // __unix +#endif // UNIX -#ifdef WIN32 +#ifdef BEAMMP_WINDOWS #include // return TRUE if handled, FALSE if not BOOL WINAPI Win32CtrlC_Handler(DWORD CtrlType) { @@ -44,21 +44,19 @@ BOOL WINAPI Win32CtrlC_Handler(DWORD CtrlType) { // we dont care for any others like CTRL_LOGOFF_EVENT and CTRL_SHUTDOWN_EVENT return FALSE; } -#endif // WIN32 +#endif // WINDOWS void SetupSignalHandlers() { // signal handlers for unix#include -#if defined(__unix) || defined(__linux) || defined(__APPLE__) +#if defined(BEAMMP_LINUX) || defined(BEAMMP_APPLE) beammp_trace("registering handlers for signals"); signal(SIGPIPE, UnixSignalHandler); signal(SIGTERM, UnixSignalHandler); #ifndef DEBUG signal(SIGINT, UnixSignalHandler); #endif // DEBUG -#elif defined(WIN32) +#elif defined(BEAMMP_WINDOWS) beammp_trace("registering handlers for CTRL_*_EVENTs"); SetConsoleCtrlHandler(Win32CtrlC_Handler, TRUE); -#else -#error "Please implement necessary signals like Ctrl+C handling here" #endif } diff --git a/src/TNetwork.cpp b/src/TNetwork.cpp index 18a710a..c3e9169 100644 --- a/src/TNetwork.cpp +++ b/src/TNetwork.cpp @@ -38,13 +38,13 @@ TNetwork::TNetwork(TServer& Server, TPPSMonitor& PPSMonitor, TResourceManager& R void TNetwork::UDPServerMain() { RegisterThread("UDPServer"); -#ifdef WIN32 +#if defined(BEAMMP_WINDOWS) WSADATA data; if (WSAStartup(514, &data)) { beammp_error(("Can't start Winsock!")); // return; } -#endif // WIN32 +#endif // WINDOWS mUDPSock = socket(AF_INET, SOCK_DGRAM, 0); // Create a server hint structure for the server sockaddr_in serverAddr {}; @@ -99,19 +99,19 @@ void TNetwork::UDPServerMain() { void TNetwork::TCPServerMain() { RegisterThread("TCPServer"); -#ifdef WIN32 +#if defined(BEAMMP_WINDOWS) WSADATA wsaData; if (WSAStartup(514, &wsaData)) { beammp_error("Can't start Winsock!"); return; } -#endif // WIN32 +#endif // WINDOWS TConnection client {}; SOCKET Listener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); int optval = 1; -#ifdef WIN32 +#if defined(BEAMMP_WINDOWS) const char* optval_ptr = reinterpret_cast(&optval); -#else +#elif defined(BEAMMP_LINUX) || defined(BEAMMP_APPLE) void* optval_ptr = reinterpret_cast(&optval); #endif setsockopt(Listener, SOL_SOCKET, SO_REUSEADDR, optval_ptr, sizeof(optval)); @@ -157,10 +157,10 @@ void TNetwork::TCPServerMain() { beammp_debug("all ok, arrived at " + std::string(__func__) + ":" + std::to_string(__LINE__)); CloseSocketProper(client.Socket); -#ifdef WIN32 +#ifdef BEAMMP_WINDOWS CloseSocketProper(client.Socket); WSACleanup(); -#endif // WIN32 +#endif // WINDOWS } #undef GetObject // Fixes Windows @@ -366,11 +366,11 @@ bool TNetwork::TCPSend(TClient& c, const std::string& Data, bool IsSync) { Sent = 0; Size += 4; do { -#ifdef WIN32 +#if defined(BEAMMP_WINDOWS) int32_t Temp = send(c.GetTCPSock(), &Send[Sent], Size - Sent, 0); -#else // WIN32 +#elif defined(BEAMMP_LINUX) || defined(BEAMMP_APPLE) int32_t Temp = send(c.GetTCPSock(), &Send[Sent], Size - Sent, MSG_NOSIGNAL); -#endif // WIN32 +#endif if (Temp == 0) { beammp_debug("send() == 0: " + GetPlatformAgnosticErrorString()); if (c.GetStatus() > -1)