mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2026-02-16 02:30:54 +00:00
Add BEAMMP_{WINDOWS,LINUX,APPLE} preprocessor defines instead of platform specific ones
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user