Add BEAMMP_{WINDOWS,LINUX,APPLE} preprocessor defines instead of platform specific ones

This commit is contained in:
Lion Kortlepel 2021-11-27 02:11:22 +01:00
parent 9d2d4bb221
commit fd7bea0f36
No known key found for this signature in database
GPG Key ID: 4322FF2B4C71259B
7 changed files with 51 additions and 37 deletions

View File

@ -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)

View File

@ -13,10 +13,10 @@
class TServer;
#ifdef WIN32
#ifdef BEAMMP_WINDOWS
// for socklen_t
#include <WS2tcpip.h>
#endif // WIN32
#endif // WINDOWS
struct TConnection final {
SOCKET Socket;

View File

@ -1,8 +1,10 @@
#pragma once
#include "Environment.h"
// ======================= UNIX ========================
#ifdef __unix
#ifdef BEAMMP_LINUX
#include <arpa/inet.h>
#include <sys/socket.h>
#include <termios.h>
@ -19,7 +21,9 @@ inline void CloseSocketProper(int TheSocket) {
}
#endif // unix
#ifdef __APPLE__
// ======================= APPLE ========================
#ifdef BEAMMP_APPLE
#include <arpa/inet.h>
#include <sys/socket.h>
#include <termios.h>
@ -36,9 +40,9 @@ inline void CloseSocketProper(int TheSocket) {
}
#endif // unix
// ======================= WIN32 =======================
// ======================= WINDOWS =======================
#ifdef WIN32
#ifdef BEAMMP_WINDOWS
#include <conio.h>
#include <winsock2.h>
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

17
include/Environment.h Normal file
View File

@ -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

View File

@ -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
}

View File

@ -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 <csignal>
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 <windows.h>
// 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 <windows.h>
#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
}

View File

@ -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<const char*>(&optval);
#else
#elif defined(BEAMMP_LINUX) || defined(BEAMMP_APPLE)
void* optval_ptr = reinterpret_cast<void*>(&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)