mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2025-07-01 15:26:59 +00:00
Start rewrite of lua, rename all print functions
This commit is contained in:
parent
d082620525
commit
dd4e4c4467
@ -38,7 +38,7 @@ add_executable(BeamMP-Server
|
||||
include/VehicleData.h src/VehicleData.cpp
|
||||
include/TConfig.h src/TConfig.cpp
|
||||
include/TLuaEngine.h src/TLuaEngine.cpp
|
||||
include/TLuaFile.h src/TLuaFile.cpp
|
||||
include/TLuaPlugin.h src/TLuaPlugin.cpp
|
||||
include/TResourceManager.h src/TResourceManager.cpp
|
||||
include/THeartbeatThread.h src/THeartbeatThread.cpp
|
||||
include/Http.h src/Http.cpp
|
||||
@ -49,19 +49,19 @@ target_include_directories(BeamMP-Server PRIVATE "${PROJECT_SOURCE_DIR}/deps/asi
|
||||
target_include_directories(BeamMP-Server PRIVATE "${PROJECT_SOURCE_DIR}/deps/rapidjson/include")
|
||||
target_include_directories(BeamMP-Server PRIVATE "${PROJECT_SOURCE_DIR}/deps/websocketpp")
|
||||
target_include_directories(BeamMP-Server PRIVATE "${PROJECT_SOURCE_DIR}/deps/commandline")
|
||||
target_include_directories(BeamMP-Server PRIVATE "${PROJECT_SOURCE_DIR}/deps/sol2/include")
|
||||
target_include_directories(BeamMP-Server PRIVATE "${PROJECT_SOURCE_DIR}/deps")
|
||||
|
||||
include_directories(BeamMP-Server PUBLIC ${Boost_INCLUDE_DIRS} ${LUA_INCLUDE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/include)
|
||||
|
||||
find_package(Lua REQUIRED)
|
||||
find_package(OpenSSL REQUIRED)
|
||||
|
||||
if (UNIX)
|
||||
target_link_libraries(BeamMP-Server z pthread stdc++fs ${LUA_LIBRARIES} crypto ${OPENSSL_LIBRARIES} commandline sioclient_tls)
|
||||
target_link_libraries(BeamMP-Server z pthread stdc++fs ${SOL2_LIBRARIES} crypto ${OPENSSL_LIBRARIES} commandline sioclient_tls)
|
||||
elseif (WIN32)
|
||||
include(FindLua)
|
||||
find_package(ZLIB REQUIRED)
|
||||
find_package(RapidJSON CONFIG REQUIRED)
|
||||
target_include_directories(BeamMP-Server PRIVATE ${RAPIDJSON_INCLUDE_DIRS} ${Boost_INCLUDE_DIRS})
|
||||
target_link_libraries(BeamMP-Server PRIVATE ws2_32 ZLIB::ZLIB ${LUA_LIBRARIES} ${OPENSSL_LIBRARIES} commandline sioclient_tls)
|
||||
target_link_libraries(BeamMP-Server PRIVATE ws2_32 ZLIB::ZLIB ${SOL2_LIBRARIES} ${OPENSSL_LIBRARIES} commandline sioclient_tls)
|
||||
endif ()
|
||||
|
1
deps/CMakeLists.txt
vendored
1
deps/CMakeLists.txt
vendored
@ -1,2 +1,3 @@
|
||||
add_subdirectory("${PROJECT_SOURCE_DIR}/deps/socket.io-client-cpp")
|
||||
add_subdirectory("${PROJECT_SOURCE_DIR}/deps/commandline")
|
||||
add_subdirectory("${PROJECT_SOURCE_DIR}/deps/sol2")
|
||||
|
@ -122,11 +122,11 @@ void RegisterThread(const std::string str);
|
||||
|
||||
#endif // defined(DEBUG)
|
||||
|
||||
#define warn(x) Application::Console().Write(_this_location + std::string("[WARN] ") + (x))
|
||||
#define info(x) Application::Console().Write(_this_location + std::string("[INFO] ") + (x))
|
||||
#define error(x) Application::Console().Write(_this_location + std::string("[ERROR] ") + (x))
|
||||
#define beammp_warn(x) Application::Console().Write(_this_location + std::string("[WARN] ") + (x))
|
||||
#define beammp_info(x) Application::Console().Write(_this_location + std::string("[INFO] ") + (x))
|
||||
#define beammp_error(x) Application::Console().Write(_this_location + std::string("[ERROR] ") + (x))
|
||||
#define luaprint(x) Application::Console().Write(_this_location + std::string("[LUA] ") + (x))
|
||||
#define debug(x) \
|
||||
#define beammp_debug(x) \
|
||||
do { \
|
||||
if (Application::Settings.DebugModeEnabled) { \
|
||||
Application::Console().Write(_this_location + std::string("[DEBUG] ") + (x)); \
|
||||
|
@ -1,6 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include "TLuaFile.h"
|
||||
#include <atomic>
|
||||
#include <fstream>
|
||||
#include "commandline.h"
|
||||
@ -11,9 +10,9 @@ public:
|
||||
|
||||
void Write(const std::string& str);
|
||||
void WriteRaw(const std::string& str);
|
||||
void InitializeLuaConsole(TLuaEngine& Engine);
|
||||
// BROKEN void InitializeLuaConsole(TLuaEngine& Engine);
|
||||
|
||||
private:
|
||||
std::unique_ptr<TLuaFile> mLuaConsole { nullptr };
|
||||
// BROKEN std::unique_ptr<TLuaFile> mLuaConsole { nullptr };
|
||||
Commandline mCommandline;
|
||||
};
|
||||
|
@ -0,0 +1,32 @@
|
||||
#pragma once
|
||||
|
||||
#include "TNetwork.h"
|
||||
#include "TServer.h"
|
||||
#include <filesystem>
|
||||
#include <sol/sol.hpp>
|
||||
#include <toml11/toml.hpp>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
class TLuaPlugin;
|
||||
|
||||
class TLuaEngine : IThreaded {
|
||||
public:
|
||||
TLuaEngine(TServer& Server, TNetwork& Network);
|
||||
|
||||
void operator()() override;
|
||||
|
||||
private:
|
||||
void CollectPlugins();
|
||||
void InitializePlugin(const fs::path& folder);
|
||||
|
||||
TNetwork& mNetwork;
|
||||
TServer& mServer;
|
||||
sol::state mL;
|
||||
std::atomic_bool mShutdown { false };
|
||||
fs::path mResourceServerPath;
|
||||
std::vector<TLuaPlugin> mLuaPlugins;
|
||||
std::unordered_map<std::string, sol::state> mLuaStates;
|
||||
};
|
@ -13,7 +13,7 @@ void TClient::DeleteCar(int Ident) {
|
||||
if (iter != mVehicleData.end()) {
|
||||
mVehicleData.erase(iter);
|
||||
} else {
|
||||
debug("tried to erase a vehicle that doesn't exist (not an error)");
|
||||
beammp_debug("tried to erase a vehicle that doesn't exist (not an error)");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -17,7 +17,7 @@ void Application::RegisterShutdownHandler(const TShutdownHandler& Handler) {
|
||||
}
|
||||
|
||||
void Application::GracefullyShutdown() {
|
||||
info("please wait while all subsystems are shutting down...");
|
||||
beammp_info("please wait while all subsystems are shutting down...");
|
||||
std::unique_lock Lock(mShutdownHandlersMutex);
|
||||
for (auto& Handler : mShutdownHandlers) {
|
||||
Handler();
|
||||
|
@ -1,7 +1,7 @@
|
||||
#include "Http.h"
|
||||
|
||||
#include "Common.h"
|
||||
#undef error
|
||||
#undef beammp_error
|
||||
|
||||
#include <boost/asio/connect.hpp>
|
||||
#include <boost/asio/ip/tcp.hpp>
|
||||
|
@ -23,12 +23,12 @@ static constexpr std::string_view StrAuthKey = "AuthKey";
|
||||
|
||||
TConfig::TConfig() {
|
||||
if (!fs::exists(ConfigFileName) || !fs::is_regular_file(ConfigFileName)) {
|
||||
info("No config file found! Generating one...");
|
||||
beammp_info("No config file found! Generating one...");
|
||||
CreateConfigFile(ConfigFileName);
|
||||
}
|
||||
if (!mFailed) {
|
||||
if (fs::exists("Server.cfg")) {
|
||||
warn("An old \"Server.cfg\" file still exists. Please note that this is no longer used. Instead, \"" + std::string(ConfigFileName) + "\" is used. You can safely delete the \"Server.cfg\".");
|
||||
beammp_warn("An old \"Server.cfg\" file still exists. Please note that this is no longer used. Instead, \"" + std::string(ConfigFileName) + "\" is used. You can safely delete the \"Server.cfg\".");
|
||||
}
|
||||
ParseFromFile(ConfigFileName);
|
||||
}
|
||||
@ -60,7 +60,7 @@ void TConfig::CreateConfigFile(std::string_view name) {
|
||||
ParseOldFormat();
|
||||
}
|
||||
} catch (const std::exception& e) {
|
||||
error("an error occurred and was ignored during config transfer: " + std::string(e.what()));
|
||||
beammp_error("an error occurred and was ignored during config transfer: " + std::string(e.what()));
|
||||
}
|
||||
|
||||
{ // create file context
|
||||
@ -88,10 +88,10 @@ void TConfig::CreateConfigFile(std::string_view name) {
|
||||
"# IMPORTANT: Fill in the AuthKey with the key you got from `https://beammp.com/k/dashboard` on the left under \"Keys\"\n"
|
||||
<< '\n';
|
||||
ofs << data << '\n';
|
||||
error("There was no \"" + std::string(ConfigFileName) + "\" file (this is normal for the first time running the server), so one was generated for you. It was automatically filled with the settings from your Server.cfg, if you have one. Please open ServerConfig.toml and ensure your AuthKey and other settings are filled in and correct, then restart the server. The old Server.cfg file will no longer be used and causes a warning if it exists from now on.");
|
||||
beammp_error("There was no \"" + std::string(ConfigFileName) + "\" file (this is normal for the first time running the server), so one was generated for you. It was automatically filled with the settings from your Server.cfg, if you have one. Please open ServerConfig.toml and ensure your AuthKey and other settings are filled in and correct, then restart the server. The old Server.cfg file will no longer be used and causes a warning if it exists from now on.");
|
||||
mFailed = true;
|
||||
} else {
|
||||
error("Couldn't create " + std::string(name) + ". Check permissions, try again, and contact support if it continues not to work.");
|
||||
beammp_error("Couldn't create " + std::string(name) + ". Check permissions, try again, and contact support if it continues not to work.");
|
||||
mFailed = true;
|
||||
}
|
||||
}
|
||||
@ -110,30 +110,30 @@ void TConfig::ParseFromFile(std::string_view name) {
|
||||
Application::Settings.Resource = data["General"][StrResourceFolder.data()].as_string();
|
||||
Application::Settings.Key = data["General"][StrAuthKey.data()].as_string();
|
||||
} catch (const std::exception& err) {
|
||||
error("Error parsing config file value: " + std::string(err.what()));
|
||||
beammp_error("Error parsing config file value: " + std::string(err.what()));
|
||||
mFailed = true;
|
||||
return;
|
||||
}
|
||||
PrintDebug();
|
||||
// all good so far, let's check if there's a key
|
||||
if (Application::Settings.Key.empty()) {
|
||||
error("No AuthKey specified in the \"" + std::string(ConfigFileName) + "\" file. Please get an AuthKey, enter it into the config file, and restart this server.");
|
||||
beammp_error("No AuthKey specified in the \"" + std::string(ConfigFileName) + "\" file. Please get an AuthKey, enter it into the config file, and restart this server.");
|
||||
mFailed = true;
|
||||
}
|
||||
}
|
||||
|
||||
void TConfig::PrintDebug() {
|
||||
debug(std::string(StrDebug) + ": " + std::string(Application::Settings.DebugModeEnabled ? "true" : "false"));
|
||||
debug(std::string(StrPrivate) + ": " + std::string(Application::Settings.Private ? "true" : "false"));
|
||||
debug(std::string(StrPort) + ": " + std::to_string(Application::Settings.Port));
|
||||
debug(std::string(StrMaxCars) + ": " + std::to_string(Application::Settings.MaxCars));
|
||||
debug(std::string(StrMaxPlayers) + ": " + std::to_string(Application::Settings.MaxPlayers));
|
||||
debug(std::string(StrMap) + ": \"" + Application::Settings.MapName + "\"");
|
||||
debug(std::string(StrName) + ": \"" + Application::Settings.ServerName + "\"");
|
||||
debug(std::string(StrDescription) + ": \"" + Application::Settings.ServerDesc + "\"");
|
||||
debug(std::string(StrResourceFolder) + ": \"" + Application::Settings.Resource + "\"");
|
||||
beammp_debug(std::string(StrDebug) + ": " + std::string(Application::Settings.DebugModeEnabled ? "true" : "false"));
|
||||
beammp_debug(std::string(StrPrivate) + ": " + std::string(Application::Settings.Private ? "true" : "false"));
|
||||
beammp_debug(std::string(StrPort) + ": " + std::to_string(Application::Settings.Port));
|
||||
beammp_debug(std::string(StrMaxCars) + ": " + std::to_string(Application::Settings.MaxCars));
|
||||
beammp_debug(std::string(StrMaxPlayers) + ": " + std::to_string(Application::Settings.MaxPlayers));
|
||||
beammp_debug(std::string(StrMap) + ": \"" + Application::Settings.MapName + "\"");
|
||||
beammp_debug(std::string(StrName) + ": \"" + Application::Settings.ServerName + "\"");
|
||||
beammp_debug(std::string(StrDescription) + ": \"" + Application::Settings.ServerDesc + "\"");
|
||||
beammp_debug(std::string(StrResourceFolder) + ": \"" + Application::Settings.Resource + "\"");
|
||||
// special!
|
||||
debug("Key Length: " + std::to_string(Application::Settings.Key.length()) + "");
|
||||
beammp_debug("Key Length: " + std::to_string(Application::Settings.Key.length()) + "");
|
||||
}
|
||||
|
||||
void TConfig::ParseOldFormat() {
|
||||
@ -183,7 +183,7 @@ void TConfig::ParseOldFormat() {
|
||||
} else if (Key == "AuthKey") {
|
||||
Application::Settings.Key = Value.substr(1, Value.size() - 3);
|
||||
} else {
|
||||
warn("unknown key in old auth file (ignored): " + Key);
|
||||
beammp_warn("unknown key in old auth file (ignored): " + Key);
|
||||
}
|
||||
Str >> std::ws;
|
||||
}
|
||||
|
@ -45,22 +45,23 @@ TConsole::TConsole() {
|
||||
mCommandline.set_prompt("> ");
|
||||
bool success = mCommandline.enable_write_to_file("Server.log");
|
||||
if (!success) {
|
||||
error("unable to open file for writing: \"Server.log\"");
|
||||
beammp_error("unable to open file for writing: \"Server.log\"");
|
||||
}
|
||||
mCommandline.on_command = [this](Commandline& c) {
|
||||
auto cmd = c.get_command();
|
||||
mCommandline.write("> " + cmd);
|
||||
if (cmd == "exit") {
|
||||
info("gracefully shutting down");
|
||||
beammp_info("gracefully shutting down");
|
||||
Application::GracefullyShutdown();
|
||||
} else if (cmd == "clear" || cmd == "cls") {
|
||||
// TODO: clear screen
|
||||
} else {
|
||||
if (mLuaConsole) {
|
||||
/*if (mLuaConsole) {
|
||||
mLuaConsole->Execute(cmd);
|
||||
} else {
|
||||
error("Lua subsystem not yet initialized, please wait a few seconds and try again");
|
||||
}
|
||||
} BROKEN
|
||||
*/
|
||||
}
|
||||
};
|
||||
}
|
||||
@ -70,9 +71,11 @@ void TConsole::Write(const std::string& str) {
|
||||
mCommandline.write(ToWrite);
|
||||
// TODO write to logfile, too
|
||||
}
|
||||
/* BROKEN
|
||||
void TConsole::InitializeLuaConsole(TLuaEngine& Engine) {
|
||||
mLuaConsole = std::make_unique<TLuaFile>(Engine, true);
|
||||
}
|
||||
*/
|
||||
void TConsole::WriteRaw(const std::string& str) {
|
||||
mCommandline.write(str);
|
||||
}
|
||||
|
@ -26,7 +26,7 @@ void THeartbeatThread::operator()() {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
continue;
|
||||
}
|
||||
debug("heartbeat (after " + std::to_string(std::chrono::duration_cast<std::chrono::seconds>(TimePassed).count()) + "s)");
|
||||
beammp_debug("heartbeat (after " + std::to_string(std::chrono::duration_cast<std::chrono::seconds>(TimePassed).count()) + "s)");
|
||||
|
||||
Last = Body;
|
||||
LastNormalUpdateTime = Now;
|
||||
@ -43,18 +43,18 @@ void THeartbeatThread::operator()() {
|
||||
T = Http::POST(Application::GetBackendHostname(), 443, "/heartbeat", {}, Body, "application/x-www-form-urlencoded");
|
||||
// TODO backup2 + HTTP flag (no TSL)
|
||||
if (T.substr(0, 2) != "20") {
|
||||
warn("Backend system refused server! Server might not show in the public list");
|
||||
debug("server returned \"" + T + "\"");
|
||||
beammp_warn("Backend system refused server! Server might not show in the public list");
|
||||
beammp_debug("server returned \"" + T + "\"");
|
||||
isAuth = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (!isAuth) {
|
||||
if (T == "2000") {
|
||||
info(("Authenticated!"));
|
||||
beammp_info(("Authenticated!"));
|
||||
isAuth = true;
|
||||
} else if (T == "200") {
|
||||
info(("Resumed authenticated session!"));
|
||||
beammp_info(("Resumed authenticated session!"));
|
||||
isAuth = true;
|
||||
}
|
||||
}
|
||||
@ -86,10 +86,10 @@ THeartbeatThread::THeartbeatThread(TResourceManager& ResourceManager, TServer& S
|
||||
, mServer(Server) {
|
||||
Application::RegisterShutdownHandler([&] {
|
||||
if (mThread.joinable()) {
|
||||
debug("shutting down Heartbeat");
|
||||
beammp_debug("shutting down Heartbeat");
|
||||
mShutdown = true;
|
||||
mThread.join();
|
||||
debug("shut down Heartbeat");
|
||||
beammp_debug("shut down Heartbeat");
|
||||
}
|
||||
});
|
||||
Start();
|
||||
|
@ -0,0 +1,44 @@
|
||||
#include "TLuaEngine.h"
|
||||
#include "CustomAssert.h"
|
||||
|
||||
TLuaEngine::TLuaEngine(TServer& Server, TNetwork& Network)
|
||||
: mNetwork(Network)
|
||||
, mServer(Server) {
|
||||
if (!fs::exists(Application::Settings.Resource)) {
|
||||
fs::create_directory(Application::Settings.Resource);
|
||||
}
|
||||
fs::path Path = fs::path(Application::Settings.Resource) / "Server";
|
||||
if (!fs::exists(Path)) {
|
||||
fs::create_directory(Path);
|
||||
}
|
||||
mResourceServerPath = Path;
|
||||
Application::RegisterShutdownHandler([&] {
|
||||
mShutdown = true;
|
||||
if (mThread.joinable()) {
|
||||
mThread.join();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void TLuaEngine::operator()() {
|
||||
RegisterThread("LuaEngine");
|
||||
// lua engine main thread
|
||||
CollectPlugins();
|
||||
}
|
||||
|
||||
void TLuaEngine::CollectPlugins() {
|
||||
for (const auto& dir : fs::directory_iterator(mResourceServerPath)) {
|
||||
auto path = dir.path();
|
||||
path = fs::relative(path);
|
||||
if (!dir.is_directory()) {
|
||||
beammp_error("\"" + dir.path().string() + "\" is not a directory, skipping");
|
||||
} else {
|
||||
beammp_debug("found plugin directory: " + path.string());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void TLuaEngine::InitializePlugin(const fs::path& folder) {
|
||||
Assert(fs::exists(folder));
|
||||
Assert(fs::is_directory(folder));
|
||||
}
|
106
src/TNetwork.cpp
106
src/TNetwork.cpp
@ -10,7 +10,7 @@ TNetwork::TNetwork(TServer& Server, TPPSMonitor& PPSMonitor, TResourceManager& R
|
||||
, mPPSMonitor(PPSMonitor)
|
||||
, mResourceManager(ResourceManager) {
|
||||
Application::RegisterShutdownHandler([&] {
|
||||
debug("Kicking all players due to shutdown");
|
||||
beammp_debug("Kicking all players due to shutdown");
|
||||
Server.ForEachClient([&](std::weak_ptr<TClient> client) -> bool {
|
||||
if (!client.expired()) {
|
||||
ClientKick(*client.lock(), "Server shutdown");
|
||||
@ -20,18 +20,18 @@ TNetwork::TNetwork(TServer& Server, TPPSMonitor& PPSMonitor, TResourceManager& R
|
||||
});
|
||||
Application::RegisterShutdownHandler([&] {
|
||||
if (mUDPThread.joinable()) {
|
||||
debug("shutting down TCPServer");
|
||||
beammp_debug("shutting down TCPServer");
|
||||
mShutdown = true;
|
||||
mUDPThread.detach();
|
||||
debug("shut down TCPServer");
|
||||
beammp_debug("shut down TCPServer");
|
||||
}
|
||||
});
|
||||
Application::RegisterShutdownHandler([&] {
|
||||
if (mTCPThread.joinable()) {
|
||||
debug("shutting down TCPServer");
|
||||
beammp_debug("shutting down TCPServer");
|
||||
mShutdown = true;
|
||||
mTCPThread.detach();
|
||||
debug("shut down TCPServer");
|
||||
beammp_debug("shut down TCPServer");
|
||||
}
|
||||
});
|
||||
mTCPThread = std::thread(&TNetwork::TCPServerMain, this);
|
||||
@ -56,13 +56,13 @@ void TNetwork::UDPServerMain() {
|
||||
|
||||
// Try and bind the socket to the IP and port
|
||||
if (bind(mUDPSock, (sockaddr*)&serverAddr, sizeof(serverAddr)) != 0) {
|
||||
error("bind() failed: " + GetPlatformAgnosticErrorString());
|
||||
beammp_error("bind() failed: " + GetPlatformAgnosticErrorString());
|
||||
std::this_thread::sleep_for(std::chrono::seconds(5));
|
||||
exit(-1);
|
||||
//return;
|
||||
}
|
||||
|
||||
info(("Vehicle data network online on port ") + std::to_string(Application::Settings.Port) + (" with a Max of ")
|
||||
beammp_info(("Vehicle data network online on port ") + std::to_string(Application::Settings.Port) + (" with a Max of ")
|
||||
+ std::to_string(Application::Settings.MaxPlayers) + (" Clients"));
|
||||
while (!mShutdown) {
|
||||
try {
|
||||
@ -94,7 +94,7 @@ void TNetwork::UDPServerMain() {
|
||||
return true;
|
||||
});
|
||||
} catch (const std::exception& e) {
|
||||
error(("fatal: ") + std::string(e.what()));
|
||||
beammp_error(("fatal: ") + std::string(e.what()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -118,40 +118,40 @@ void TNetwork::TCPServerMain() {
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_port = htons(uint16_t(Application::Settings.Port));
|
||||
if (bind(Listener, (sockaddr*)&addr, sizeof(addr)) != 0) {
|
||||
error("bind() failed: " + GetPlatformAgnosticErrorString());
|
||||
beammp_error("bind() failed: " + GetPlatformAgnosticErrorString());
|
||||
std::this_thread::sleep_for(std::chrono::seconds(5));
|
||||
exit(-1);
|
||||
}
|
||||
if (Listener == -1) {
|
||||
error("Invalid listening socket");
|
||||
beammp_error("Invalid listening socket");
|
||||
return;
|
||||
}
|
||||
if (listen(Listener, SOMAXCONN)) {
|
||||
error("listen() failed: " + GetPlatformAgnosticErrorString());
|
||||
beammp_error("listen() failed: " + GetPlatformAgnosticErrorString());
|
||||
// FIXME leak Listener
|
||||
return;
|
||||
}
|
||||
info(("Vehicle event network online"));
|
||||
beammp_info(("Vehicle event network online"));
|
||||
do {
|
||||
try {
|
||||
if (mShutdown) {
|
||||
debug("shutdown during TCP wait for accept loop");
|
||||
beammp_debug("shutdown during TCP wait for accept loop");
|
||||
break;
|
||||
}
|
||||
client.SockAddrLen = sizeof(client.SockAddr);
|
||||
client.Socket = accept(Listener, &client.SockAddr, &client.SockAddrLen);
|
||||
if (client.Socket == -1) {
|
||||
warn(("Got an invalid client socket on connect! Skipping..."));
|
||||
beammp_warn(("Got an invalid client socket on connect! Skipping..."));
|
||||
continue;
|
||||
}
|
||||
std::thread ID(&TNetwork::Identify, this, client);
|
||||
ID.detach(); // TODO: Add to a queue and attempt to join periodically
|
||||
} catch (const std::exception& e) {
|
||||
error(("fatal: ") + std::string(e.what()));
|
||||
beammp_error(("fatal: ") + std::string(e.what()));
|
||||
}
|
||||
} while (client.Socket);
|
||||
|
||||
debug("all ok, arrived at " + std::string(__func__) + ":" + std::to_string(__LINE__));
|
||||
beammp_debug("all ok, arrived at " + std::string(__func__) + ":" + std::to_string(__LINE__));
|
||||
|
||||
CloseSocketProper(client.Socket);
|
||||
#ifdef WIN32
|
||||
@ -208,7 +208,7 @@ void TNetwork::Authentication(const TConnection& ClientConnection) {
|
||||
Client->SetIdentifier("ip", str);
|
||||
|
||||
std::string Rc;
|
||||
info("Identifying new ClientConnection...");
|
||||
beammp_info("Identifying new ClientConnection...");
|
||||
|
||||
Rc = TCPRcv(*Client);
|
||||
|
||||
@ -246,7 +246,7 @@ void TNetwork::Authentication(const TConnection& ClientConnection) {
|
||||
|
||||
if (!AuthResponse.IsObject()) {
|
||||
ClientKick(*Client, "Backend returned invalid auth response format.");
|
||||
error("Backend returned invalid auth response format. This should never happen.");
|
||||
beammp_error("Backend returned invalid auth response format. This should never happen.");
|
||||
return;
|
||||
}
|
||||
|
||||
@ -266,8 +266,8 @@ void TNetwork::Authentication(const TConnection& ClientConnection) {
|
||||
return;
|
||||
}
|
||||
|
||||
debug("Name -> " + Client->GetName() + ", Guest -> " + std::to_string(Client->IsGuest()) + ", Roles -> " + Client->GetRoles());
|
||||
debug("There are " + std::to_string(mServer.ClientCount()) + " known clients");
|
||||
beammp_debug("Name -> " + Client->GetName() + ", Guest -> " + std::to_string(Client->IsGuest()) + ", Roles -> " + Client->GetRoles());
|
||||
beammp_debug("There are " + std::to_string(mServer.ClientCount()) + " known clients");
|
||||
mServer.ForEachClient([&](const std::weak_ptr<TClient>& ClientPtr) -> bool {
|
||||
std::shared_ptr<TClient> Cl;
|
||||
{
|
||||
@ -277,10 +277,10 @@ void TNetwork::Authentication(const TConnection& ClientConnection) {
|
||||
} else
|
||||
return true;
|
||||
}
|
||||
info("Client Iteration: Name -> " + Cl->GetName() + ", Guest -> " + std::to_string(Cl->IsGuest()) + ", Roles -> " + Cl->GetRoles());
|
||||
beammp_info("Client Iteration: Name -> " + Cl->GetName() + ", Guest -> " + std::to_string(Cl->IsGuest()) + ", Roles -> " + Cl->GetRoles());
|
||||
if (Cl->GetName() == Client->GetName() && Cl->IsGuest() == Client->IsGuest()) {
|
||||
info("New ClientConnection matched with current iteration");
|
||||
info("Old ClientConnection (" + Cl->GetName() + ") kicked: Reconnecting");
|
||||
beammp_info("New ClientConnection matched with current iteration");
|
||||
beammp_info("Old ClientConnection (" + Cl->GetName() + ") kicked: Reconnecting");
|
||||
CloseSocketProper(Cl->GetTCPSock());
|
||||
Cl->SetStatus(-2);
|
||||
return false;
|
||||
@ -300,7 +300,7 @@ void TNetwork::Authentication(const TConnection& ClientConnection) {
|
||||
}
|
||||
|
||||
if (mServer.ClientCount() < size_t(Application::Settings.MaxPlayers)) {
|
||||
info("Identification success");
|
||||
beammp_info("Identification success");
|
||||
mServer.InsertClient(Client);
|
||||
TCPClient(Client);
|
||||
} else
|
||||
@ -339,12 +339,12 @@ bool TNetwork::TCPSend(TClient& c, const std::string& Data, bool IsSync) {
|
||||
int32_t Temp = send(c.GetTCPSock(), &Send[Sent], Size - Sent, MSG_NOSIGNAL);
|
||||
#endif //WIN32
|
||||
if (Temp == 0) {
|
||||
debug("send() == 0: " + GetPlatformAgnosticErrorString());
|
||||
beammp_debug("send() == 0: " + GetPlatformAgnosticErrorString());
|
||||
if (c.GetStatus() > -1)
|
||||
c.SetStatus(-1);
|
||||
return false;
|
||||
} else if (Temp < 0) {
|
||||
debug("send() < 0: " + GetPlatformAgnosticErrorString()); //TODO fix it was spamming yet everyone stayed on the server
|
||||
beammp_debug("send() < 0: " + GetPlatformAgnosticErrorString()); //TODO fix it was spamming yet everyone stayed on the server
|
||||
if (c.GetStatus() > -1)
|
||||
c.SetStatus(-1);
|
||||
CloseSocketProper(c.GetTCPSock());
|
||||
@ -358,15 +358,15 @@ bool TNetwork::TCPSend(TClient& c, const std::string& Data, bool IsSync) {
|
||||
|
||||
bool TNetwork::CheckBytes(TClient& c, int32_t BytesRcv) {
|
||||
if (BytesRcv == 0) {
|
||||
debug("(TCP) Connection closing...");
|
||||
beammp_debug("(TCP) Connection closing...");
|
||||
if (c.GetStatus() > -1)
|
||||
c.SetStatus(-1);
|
||||
return false;
|
||||
} else if (BytesRcv < 0) {
|
||||
debug("(TCP) recv() failed: " + GetPlatformAgnosticErrorString());
|
||||
beammp_debug("(TCP) recv() failed: " + GetPlatformAgnosticErrorString());
|
||||
if (c.GetStatus() > -1)
|
||||
c.SetStatus(-1);
|
||||
info(("Closing socket in CheckBytes, BytesRcv < 0"));
|
||||
beammp_info(("Closing socket in CheckBytes, BytesRcv < 0"));
|
||||
CloseSocketProper(c.GetTCPSock());
|
||||
return false;
|
||||
}
|
||||
@ -383,7 +383,7 @@ std::string TNetwork::TCPRcv(TClient& c) {
|
||||
Temp = recv(c.GetTCPSock(), &Data[BytesRcv], 4 - BytesRcv, 0);
|
||||
if (!CheckBytes(c, Temp)) {
|
||||
#ifdef DEBUG
|
||||
error(std::string(__func__) + (": failed on CheckBytes in while(BytesRcv < 4)"));
|
||||
beammp_error(std::string(__func__) + (": failed on CheckBytes in while(BytesRcv < 4)"));
|
||||
#endif // DEBUG
|
||||
return "";
|
||||
}
|
||||
@ -396,7 +396,7 @@ std::string TNetwork::TCPRcv(TClient& c) {
|
||||
#endif // DEBUG
|
||||
if (!CheckBytes(c, BytesRcv)) {
|
||||
#ifdef DEBUG
|
||||
error(std::string(__func__) + (": failed on CheckBytes"));
|
||||
beammp_error(std::string(__func__) + (": failed on CheckBytes"));
|
||||
#endif // DEBUG
|
||||
return "";
|
||||
}
|
||||
@ -404,7 +404,7 @@ std::string TNetwork::TCPRcv(TClient& c) {
|
||||
Data.resize(Header);
|
||||
} else {
|
||||
ClientKick(c, "Header size limit exceeded");
|
||||
warn("Client " + c.GetName() + " (" + std::to_string(c.GetID()) + ") sent header of >100MB - assuming malicious intent and disconnecting the client.");
|
||||
beammp_warn("Client " + c.GetName() + " (" + std::to_string(c.GetID()) + ") sent header of >100MB - assuming malicious intent and disconnecting the client.");
|
||||
return "";
|
||||
}
|
||||
BytesRcv = 0;
|
||||
@ -412,7 +412,7 @@ std::string TNetwork::TCPRcv(TClient& c) {
|
||||
Temp = recv(c.GetTCPSock(), &Data[BytesRcv], Header - BytesRcv, 0);
|
||||
if (!CheckBytes(c, Temp)) {
|
||||
#ifdef DEBUG
|
||||
error(std::string(__func__) + (": failed on CheckBytes in while(BytesRcv < Header)"));
|
||||
beammp_error(std::string(__func__) + (": failed on CheckBytes in while(BytesRcv < Header)"));
|
||||
#endif // DEBUG
|
||||
|
||||
return "";
|
||||
@ -438,7 +438,7 @@ std::string TNetwork::TCPRcv(TClient& c) {
|
||||
}
|
||||
|
||||
void TNetwork::ClientKick(TClient& c, const std::string& R) {
|
||||
info("Client kicked: " + R);
|
||||
beammp_info("Client kicked: " + R);
|
||||
if (!TCPSend(c, "E" + R)) {
|
||||
// TODO handle
|
||||
}
|
||||
@ -454,7 +454,7 @@ void TNetwork::Looper(const std::weak_ptr<TClient>& c) {
|
||||
while (!c.expired()) {
|
||||
auto Client = c.lock();
|
||||
if (Client->GetStatus() < 0) {
|
||||
debug("client status < 0, breaking client loop");
|
||||
beammp_debug("client status < 0, breaking client loop");
|
||||
break;
|
||||
}
|
||||
if (!Client->IsSyncing() && Client->IsSynced() && Client->MissedPacketQueueSize() != 0) {
|
||||
@ -504,13 +504,13 @@ void TNetwork::TCPClient(const std::weak_ptr<TClient>& c) {
|
||||
break;
|
||||
auto Client = c.lock();
|
||||
if (Client->GetStatus() < 0) {
|
||||
debug("client status < 0, breaking client loop");
|
||||
beammp_debug("client status < 0, breaking client loop");
|
||||
break;
|
||||
}
|
||||
|
||||
auto res = TCPRcv(*Client);
|
||||
if (res == "") {
|
||||
debug("TCPRcv error, break client loop");
|
||||
beammp_debug("TCPRcv error, break client loop");
|
||||
break;
|
||||
}
|
||||
TServer::GlobalParser(c, res, mPPSMonitor, *this);
|
||||
@ -522,7 +522,7 @@ void TNetwork::TCPClient(const std::weak_ptr<TClient>& c) {
|
||||
auto Client = c.lock();
|
||||
OnDisconnect(c, Client->GetStatus() == -2);
|
||||
} else {
|
||||
warn("client expired in TCPClient, should never happen");
|
||||
beammp_warn("client expired in TCPClient, should never happen");
|
||||
}
|
||||
}
|
||||
|
||||
@ -545,7 +545,7 @@ void TNetwork::OnDisconnect(const std::weak_ptr<TClient>& ClientPtr, bool kicked
|
||||
Assert(!ClientPtr.expired());
|
||||
auto LockedClientPtr = ClientPtr.lock();
|
||||
TClient& c = *LockedClientPtr;
|
||||
info(c.GetName() + (" Connection Terminated"));
|
||||
beammp_info(c.GetName() + (" Connection Terminated"));
|
||||
std::string Packet;
|
||||
TClient::TSetOfVehicleData VehicleData;
|
||||
{ // Vehicle Data Lock Scope
|
||||
@ -592,16 +592,16 @@ int TNetwork::OpenID() {
|
||||
|
||||
void TNetwork::OnConnect(const std::weak_ptr<TClient>& c) {
|
||||
Assert(!c.expired());
|
||||
info("Client connected");
|
||||
beammp_info("Client connected");
|
||||
auto LockedClient = c.lock();
|
||||
LockedClient->SetID(OpenID());
|
||||
info("Assigned ID " + std::to_string(LockedClient->GetID()) + " to " + LockedClient->GetName());
|
||||
beammp_info("Assigned ID " + std::to_string(LockedClient->GetID()) + " to " + LockedClient->GetName());
|
||||
TriggerLuaEvent("onPlayerConnecting", false, nullptr, std::make_unique<TLuaArg>(TLuaArg { { LockedClient->GetID() } }), false);
|
||||
SyncResources(*LockedClient);
|
||||
if (LockedClient->GetStatus() < 0)
|
||||
return;
|
||||
(void)Respond(*LockedClient, "M" + Application::Settings.MapName, true); //Send the Map on connect
|
||||
info(LockedClient->GetName() + " : Connected");
|
||||
beammp_info(LockedClient->GetName() + " : Connected");
|
||||
TriggerLuaEvent("onPlayerJoining", false, nullptr, std::make_unique<TLuaArg>(TLuaArg { { LockedClient->GetID() } }), false);
|
||||
}
|
||||
|
||||
@ -639,7 +639,7 @@ void TNetwork::Parse(TClient& c, const std::string& Packet) {
|
||||
return;
|
||||
case 'S':
|
||||
if (SubCode == 'R') {
|
||||
debug("Sending Mod Info");
|
||||
beammp_debug("Sending Mod Info");
|
||||
std::string ToSend = mResourceManager.FileList() + mResourceManager.FileSizes();
|
||||
if (ToSend.empty())
|
||||
ToSend = "-";
|
||||
@ -654,13 +654,13 @@ void TNetwork::Parse(TClient& c, const std::string& Packet) {
|
||||
}
|
||||
|
||||
void TNetwork::SendFile(TClient& c, const std::string& UnsafeName) {
|
||||
info(c.GetName() + " requesting : " + UnsafeName.substr(UnsafeName.find_last_of('/')));
|
||||
beammp_info(c.GetName() + " requesting : " + UnsafeName.substr(UnsafeName.find_last_of('/')));
|
||||
|
||||
if (!fs::path(UnsafeName).has_filename()) {
|
||||
if (!TCPSend(c, "CO")) {
|
||||
// TODO: handle
|
||||
}
|
||||
warn("File " + UnsafeName + " is not a file!");
|
||||
beammp_warn("File " + UnsafeName + " is not a file!");
|
||||
return;
|
||||
}
|
||||
auto FileName = fs::path(UnsafeName).filename().string();
|
||||
@ -670,7 +670,7 @@ void TNetwork::SendFile(TClient& c, const std::string& UnsafeName) {
|
||||
if (!TCPSend(c, "CO")) {
|
||||
// TODO: handle
|
||||
}
|
||||
warn("File " + UnsafeName + " could not be accessed!");
|
||||
beammp_warn("File " + UnsafeName + " could not be accessed!");
|
||||
return;
|
||||
}
|
||||
|
||||
@ -686,7 +686,7 @@ void TNetwork::SendFile(TClient& c, const std::string& UnsafeName) {
|
||||
}
|
||||
|
||||
if (c.GetDownSock() < 1) {
|
||||
error("Client doesn't have a download socket!");
|
||||
beammp_error("Client doesn't have a download socket!");
|
||||
if (c.GetStatus() > -1)
|
||||
c.SetStatus(-1);
|
||||
return;
|
||||
@ -723,7 +723,7 @@ void TNetwork::SplitLoad(TClient& c, size_t Sent, size_t Size, bool D, const std
|
||||
TCPSock = c.GetDownSock();
|
||||
else
|
||||
TCPSock = c.GetTCPSock();
|
||||
info("Split load Socket " + std::to_string(TCPSock));
|
||||
beammp_info("Split load Socket " + std::to_string(TCPSock));
|
||||
while (c.GetStatus() > -1 && Sent < Size) {
|
||||
size_t Diff = Size - Sent;
|
||||
if (Diff > Split) {
|
||||
@ -755,7 +755,7 @@ bool TNetwork::TCPSendRaw(TClient& C, SOCKET socket, char* Data, int32_t Size) {
|
||||
do {
|
||||
intmax_t Temp = send(socket, &Data[Sent], int(Size - Sent), 0);
|
||||
if (Temp < 1) {
|
||||
info("Socket Closed! " + std::to_string(socket));
|
||||
beammp_info("Socket Closed! " + std::to_string(socket));
|
||||
CloseSocketProper(socket);
|
||||
return false;
|
||||
}
|
||||
@ -837,7 +837,7 @@ bool TNetwork::SyncClient(const std::weak_ptr<TClient>& c) {
|
||||
return res;
|
||||
}
|
||||
LockedClient->SetIsSynced(true);
|
||||
info(LockedClient->GetName() + (" is now synced!"));
|
||||
beammp_info(LockedClient->GetName() + (" is now synced!"));
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -907,12 +907,12 @@ bool TNetwork::UDPSend(TClient& Client, std::string Data) const {
|
||||
|
||||
sendOk = sendto(mUDPSock, Data.c_str(), len, 0, (sockaddr*)&Addr, int(AddrSize));
|
||||
if (sendOk == -1) {
|
||||
debug("(UDP) sendto() failed: " + GetPlatformAgnosticErrorString());
|
||||
beammp_debug("(UDP) sendto() failed: " + GetPlatformAgnosticErrorString());
|
||||
if (Client.GetStatus() > -1)
|
||||
Client.SetStatus(-1);
|
||||
return false;
|
||||
} else if (sendOk == 0) {
|
||||
debug(("(UDP) sendto() returned 0"));
|
||||
beammp_debug(("(UDP) sendto() returned 0"));
|
||||
if (Client.GetStatus() > -1)
|
||||
Client.SetStatus(-1);
|
||||
return false;
|
||||
@ -930,7 +930,7 @@ std::string TNetwork::UDPRcvFromClient(sockaddr_in& client) const {
|
||||
#endif // WIN32
|
||||
|
||||
if (Rcv == -1) {
|
||||
error("(UDP) Error receiving from client! recvfrom() failed: " + GetPlatformAgnosticErrorString());
|
||||
beammp_error("(UDP) Error receiving from client! recvfrom() failed: " + GetPlatformAgnosticErrorString());
|
||||
return "";
|
||||
}
|
||||
return std::string(Ret.begin(), Ret.begin() + Rcv);
|
||||
|
@ -7,10 +7,10 @@ TPPSMonitor::TPPSMonitor(TServer& Server)
|
||||
Application::SetPPS("-");
|
||||
Application::RegisterShutdownHandler([&] {
|
||||
if (mThread.joinable()) {
|
||||
debug("shutting down PPSMonitor");
|
||||
beammp_debug("shutting down PPSMonitor");
|
||||
mShutdown = true;
|
||||
mThread.join();
|
||||
debug("shut down PPSMonitor");
|
||||
beammp_debug("shut down PPSMonitor");
|
||||
}
|
||||
});
|
||||
Start();
|
||||
@ -21,7 +21,7 @@ void TPPSMonitor::operator()() {
|
||||
// hard spi
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||
}
|
||||
info("PPSMonitor starting");
|
||||
beammp_info("PPSMonitor starting");
|
||||
std::vector<std::shared_ptr<TClient>> TimedOutClients;
|
||||
while (!mShutdown) {
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
@ -45,7 +45,7 @@ void TPPSMonitor::operator()() {
|
||||
}
|
||||
// kick on "no ping"
|
||||
if (c->SecondsSinceLastPing() > (5 * 60)) {
|
||||
debug("client " + std::string("(") + std::to_string(c->GetID()) + ")" + c->GetName() + " timing out: " + std::to_string(c->SecondsSinceLastPing()) + ", pps: " + Application::PPS());
|
||||
beammp_debug("client " + std::string("(") + std::to_string(c->GetID()) + ")" + c->GetName() + " timing out: " + std::to_string(c->SecondsSinceLastPing()) + ", pps: " + Application::PPS());
|
||||
TimedOutClients.push_back(c);
|
||||
}
|
||||
|
||||
|
@ -28,5 +28,5 @@ TResourceManager::TResourceManager() {
|
||||
}
|
||||
|
||||
if (mModsLoaded)
|
||||
info("Loaded " + std::to_string(mModsLoaded) + " Mods");
|
||||
beammp_info("Loaded " + std::to_string(mModsLoaded) + " Mods");
|
||||
}
|
||||
|
@ -14,16 +14,16 @@
|
||||
namespace json = rapidjson;
|
||||
|
||||
TServer::TServer(int argc, char** argv) {
|
||||
info("BeamMP Server v" + Application::ServerVersionString());
|
||||
beammp_info("BeamMP Server v" + Application::ServerVersionString());
|
||||
if (argc > 1) {
|
||||
Application::Settings.CustomIP = argv[1];
|
||||
size_t n = std::count(Application::Settings.CustomIP.begin(), Application::Settings.CustomIP.end(), '.');
|
||||
auto p = Application::Settings.CustomIP.find_first_not_of(".0123456789");
|
||||
if (p != std::string::npos || n != 3 || Application::Settings.CustomIP.substr(0, 3) == "127") {
|
||||
Application::Settings.CustomIP.clear();
|
||||
warn("IP Specified is invalid! Ignoring");
|
||||
beammp_warn("IP Specified is invalid! Ignoring");
|
||||
} else {
|
||||
info("server started with custom IP");
|
||||
beammp_info("server started with custom IP");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -31,7 +31,7 @@ TServer::TServer(int argc, char** argv) {
|
||||
void TServer::RemoveClient(const std::weak_ptr<TClient>& WeakClientPtr) {
|
||||
if (!WeakClientPtr.expired()) {
|
||||
TClient& Client = *WeakClientPtr.lock();
|
||||
debug("removing client " + Client.GetName() + " (" + std::to_string(ClientCount()) + ")");
|
||||
beammp_debug("removing client " + Client.GetName() + " (" + std::to_string(ClientCount()) + ")");
|
||||
Client.ClearCars();
|
||||
WriteLock Lock(mClientsMutex);
|
||||
mClients.erase(WeakClientPtr.lock());
|
||||
@ -39,7 +39,7 @@ void TServer::RemoveClient(const std::weak_ptr<TClient>& WeakClientPtr) {
|
||||
}
|
||||
|
||||
std::weak_ptr<TClient> TServer::InsertNewClient() {
|
||||
debug("inserting new client (" + std::to_string(ClientCount()) + ")");
|
||||
beammp_debug("inserting new client (" + std::to_string(ClientCount()) + ")");
|
||||
WriteLock Lock(mClientsMutex);
|
||||
auto [Iter, Replaced] = mClients.insert(std::make_shared<TClient>(*this));
|
||||
return *Iter;
|
||||
@ -91,7 +91,7 @@ void TServer::GlobalParser(const std::weak_ptr<TClient>& Client, std::string Pac
|
||||
switch (Code) {
|
||||
case 'H': // initial connection
|
||||
#ifdef DEBUG
|
||||
debug(std::string("got 'H' packet: '") + Packet + "' (" + std::to_string(Packet.size()) + ")");
|
||||
beammp_debug(std::string("got 'H' packet: '") + Packet + "' (" + std::to_string(Packet.size()) + ")");
|
||||
#endif
|
||||
if (!Network.SyncClient(Client)) {
|
||||
// TODO handle
|
||||
@ -109,19 +109,19 @@ void TServer::GlobalParser(const std::weak_ptr<TClient>& Client, std::string Pac
|
||||
return;
|
||||
case 'O':
|
||||
if (Packet.length() > 1000) {
|
||||
debug(("Received data from: ") + LockedClient->GetName() + (" Size: ") + std::to_string(Packet.length()));
|
||||
beammp_debug(("Received data from: ") + LockedClient->GetName() + (" Size: ") + std::to_string(Packet.length()));
|
||||
}
|
||||
ParseVehicle(*LockedClient, Packet, Network);
|
||||
return;
|
||||
case 'J':
|
||||
#ifdef DEBUG
|
||||
debug(std::string(("got 'J' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")"));
|
||||
beammp_debug(std::string(("got 'J' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")"));
|
||||
#endif
|
||||
Network.SendToAll(LockedClient.get(), Packet, false, true);
|
||||
return;
|
||||
case 'C':
|
||||
#ifdef DEBUG
|
||||
debug(std::string(("got 'C' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")"));
|
||||
beammp_debug(std::string(("got 'C' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")"));
|
||||
#endif
|
||||
if (Packet.length() < 4 || Packet.find(':', 3) == std::string::npos)
|
||||
break;
|
||||
@ -132,12 +132,12 @@ void TServer::GlobalParser(const std::weak_ptr<TClient>& Client, std::string Pac
|
||||
return;
|
||||
case 'E':
|
||||
#ifdef DEBUG
|
||||
debug(std::string(("got 'E' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")"));
|
||||
beammp_debug(std::string(("got 'E' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")"));
|
||||
#endif
|
||||
HandleEvent(*LockedClient, Packet);
|
||||
return;
|
||||
case 'N':
|
||||
debug("got 'N' packet (" + std::to_string(Packet.size()) + ")");
|
||||
beammp_debug("got 'N' packet (" + std::to_string(Packet.size()) + ")");
|
||||
Network.SendToAll(LockedClient.get(), Packet, false, true);
|
||||
default:
|
||||
return;
|
||||
@ -168,7 +168,7 @@ bool TServer::IsUnicycle(TClient& c, const std::string& CarJson) {
|
||||
rapidjson::Document Car;
|
||||
Car.Parse(CarJson.c_str(), CarJson.size());
|
||||
if (Car.HasParseError()) {
|
||||
error("Failed to parse vehicle data -> " + CarJson);
|
||||
beammp_error("Failed to parse vehicle data -> " + CarJson);
|
||||
} else if (Car["jbm"].IsString() && std::string(Car["jbm"].GetString()) == "unicycle") {
|
||||
return true;
|
||||
}
|
||||
@ -199,11 +199,11 @@ void TServer::ParseVehicle(TClient& c, const std::string& Pckt, TNetwork& Networ
|
||||
switch (Code) { //Spawned Destroyed Switched/Moved NotFound Reset
|
||||
case 's':
|
||||
#ifdef DEBUG
|
||||
debug(std::string(("got 'Os' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")"));
|
||||
beammp_debug(std::string(("got 'Os' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")"));
|
||||
#endif
|
||||
if (Data.at(0) == '0') {
|
||||
int CarID = c.GetOpenCarID();
|
||||
debug(c.GetName() + (" created a car with ID ") + std::to_string(CarID));
|
||||
beammp_debug(c.GetName() + (" created a car with ID ") + std::to_string(CarID));
|
||||
|
||||
std::string CarJson = Packet.substr(5);
|
||||
Packet = "Os:" + c.GetRoles() + ":" + c.GetName() + ":" + std::to_string(c.GetID()) + "-" + std::to_string(CarID) + ":" + CarJson;
|
||||
@ -220,13 +220,13 @@ void TServer::ParseVehicle(TClient& c, const std::string& Pckt, TNetwork& Networ
|
||||
if (!Network.Respond(c, Destroy, true)) {
|
||||
// TODO: handle
|
||||
}
|
||||
debug(c.GetName() + (" (force : car limit/lua) removed ID ") + std::to_string(CarID));
|
||||
beammp_debug(c.GetName() + (" (force : car limit/lua) removed ID ") + std::to_string(CarID));
|
||||
}
|
||||
}
|
||||
return;
|
||||
case 'c':
|
||||
#ifdef DEBUG
|
||||
debug(std::string(("got 'Oc' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")"));
|
||||
beammp_debug(std::string(("got 'Oc' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")"));
|
||||
#endif
|
||||
pid = Data.substr(0, Data.find('-'));
|
||||
vid = Data.substr(Data.find('-') + 1, Data.find(':', 1) - Data.find('-') - 1);
|
||||
@ -259,7 +259,7 @@ void TServer::ParseVehicle(TClient& c, const std::string& Pckt, TNetwork& Networ
|
||||
return;
|
||||
case 'd':
|
||||
#ifdef DEBUG
|
||||
debug(std::string(("got 'Od' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")"));
|
||||
beammp_debug(std::string(("got 'Od' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")"));
|
||||
#endif
|
||||
pid = Data.substr(0, Data.find('-'));
|
||||
vid = Data.substr(Data.find('-') + 1);
|
||||
@ -275,12 +275,12 @@ void TServer::ParseVehicle(TClient& c, const std::string& Pckt, TNetwork& Networ
|
||||
TriggerLuaEvent(("onVehicleDeleted"), false, nullptr,
|
||||
std::make_unique<TLuaArg>(TLuaArg { { c.GetID(), VID } }), false);
|
||||
c.DeleteCar(VID);
|
||||
debug(c.GetName() + (" deleted car with ID ") + std::to_string(VID));
|
||||
beammp_debug(c.GetName() + (" deleted car with ID ") + std::to_string(VID));
|
||||
}
|
||||
return;
|
||||
case 'r':
|
||||
#ifdef DEBUG
|
||||
debug(std::string(("got 'Or' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")"));
|
||||
beammp_debug(std::string(("got 'Or' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")"));
|
||||
#endif
|
||||
Pos = int(Data.find('-'));
|
||||
pid = Data.substr(0, Pos++);
|
||||
@ -301,13 +301,13 @@ void TServer::ParseVehicle(TClient& c, const std::string& Pckt, TNetwork& Networ
|
||||
return;
|
||||
case 't':
|
||||
#ifdef DEBUG
|
||||
debug(std::string(("got 'Ot' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")"));
|
||||
beammp_debug(std::string(("got 'Ot' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")"));
|
||||
#endif
|
||||
Network.SendToAll(&c, Packet, false, true);
|
||||
return;
|
||||
default:
|
||||
#ifdef DEBUG
|
||||
warn(std::string(("possibly not implemented: '") + Packet + ("' (") + std::to_string(Packet.size()) + (")")));
|
||||
beammp_warn(std::string(("possibly not implemented: '") + Packet + ("' (") + std::to_string(Packet.size()) + (")")));
|
||||
#endif // DEBUG
|
||||
return;
|
||||
}
|
||||
@ -316,32 +316,32 @@ void TServer::ParseVehicle(TClient& c, const std::string& Pckt, TNetwork& Networ
|
||||
void TServer::Apply(TClient& c, int VID, const std::string& pckt) {
|
||||
auto FoundPos = pckt.find('{');
|
||||
if (FoundPos == std::string::npos) {
|
||||
error("Malformed packet received, no '{' found");
|
||||
beammp_error("Malformed packet received, no '{' found");
|
||||
return;
|
||||
}
|
||||
std::string Packet = pckt.substr(FoundPos);
|
||||
std::string VD = c.GetCarData(VID);
|
||||
if (VD.empty()) {
|
||||
error("Tried to apply change to vehicle that does not exist");
|
||||
beammp_error("Tried to apply change to vehicle that does not exist");
|
||||
return;
|
||||
}
|
||||
std::string Header = VD.substr(0, VD.find('{'));
|
||||
|
||||
FoundPos = VD.find('{');
|
||||
if (FoundPos == std::string::npos) {
|
||||
error("Malformed packet received, no '{' found");
|
||||
beammp_error("Malformed packet received, no '{' found");
|
||||
return;
|
||||
}
|
||||
VD = VD.substr(FoundPos);
|
||||
rapidjson::Document Veh, Pack;
|
||||
Veh.Parse(VD.c_str());
|
||||
if (Veh.HasParseError()) {
|
||||
error("Could not get vehicle config!");
|
||||
beammp_error("Could not get vehicle config!");
|
||||
return;
|
||||
}
|
||||
Pack.Parse(Packet.c_str());
|
||||
if (Pack.HasParseError() || Pack.IsNull()) {
|
||||
error("Could not get active vehicle config!");
|
||||
beammp_error("Could not get active vehicle config!");
|
||||
return;
|
||||
}
|
||||
|
||||
@ -359,7 +359,7 @@ void TServer::Apply(TClient& c, int VID, const std::string& pckt) {
|
||||
}
|
||||
|
||||
void TServer::InsertClient(const std::shared_ptr<TClient>& NewClient) {
|
||||
debug("inserting client (" + std::to_string(ClientCount()) + ")");
|
||||
beammp_debug("inserting client (" + std::to_string(ClientCount()) + ")");
|
||||
WriteLock Lock(mClientsMutex); //TODO why is there 30+ threads locked here
|
||||
(void)mClients.insert(NewClient);
|
||||
}
|
||||
|
@ -7,12 +7,12 @@ TVehicleData::TVehicleData(int ID, std::string Data)
|
||||
: mID(ID)
|
||||
, mData(std::move(Data)) {
|
||||
#ifdef DEBUG
|
||||
debug("vehicle " + std::to_string(mID) + " constructed");
|
||||
beammp_debug("vehicle " + std::to_string(mID) + " constructed");
|
||||
#endif
|
||||
}
|
||||
|
||||
TVehicleData::~TVehicleData() {
|
||||
#ifdef DEBUG
|
||||
debug("vehicle " + std::to_string(mID) + " destroyed");
|
||||
beammp_debug("vehicle " + std::to_string(mID) + " destroyed");
|
||||
#endif
|
||||
}
|
||||
|
12
src/main.cpp
12
src/main.cpp
@ -15,18 +15,18 @@
|
||||
void UnixSignalHandler(int sig) {
|
||||
switch (sig) {
|
||||
case SIGPIPE:
|
||||
warn("ignoring SIGPIPE");
|
||||
beammp_warn("ignoring SIGPIPE");
|
||||
break;
|
||||
case SIGTERM:
|
||||
info("gracefully shutting down via SIGTERM");
|
||||
beammp_info("gracefully shutting down via SIGTERM");
|
||||
Application::GracefullyShutdown();
|
||||
break;
|
||||
case SIGINT:
|
||||
info("gracefully shutting down via SIGINT");
|
||||
beammp_info("gracefully shutting down via SIGINT");
|
||||
Application::GracefullyShutdown();
|
||||
break;
|
||||
default:
|
||||
debug("unhandled signal: " + std::to_string(sig));
|
||||
beammp_debug("unhandled signal: " + std::to_string(sig));
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -35,7 +35,7 @@ void UnixSignalHandler(int sig) {
|
||||
int main(int argc, char** argv) {
|
||||
#ifdef __unix
|
||||
#if DEBUG
|
||||
info("registering handlers for SIGINT, SIGTERM, SIGPIPE");
|
||||
beammp_info("registering handlers for SIGINT, SIGTERM, SIGPIPE");
|
||||
#endif // DEBUG
|
||||
signal(SIGPIPE, UnixSignalHandler);
|
||||
signal(SIGTERM, UnixSignalHandler);
|
||||
@ -54,7 +54,7 @@ int main(int argc, char** argv) {
|
||||
TConfig Config;
|
||||
|
||||
if (Config.Failed()) {
|
||||
info("Closing in 10 seconds");
|
||||
beammp_info("Closing in 10 seconds");
|
||||
std::this_thread::sleep_for(std::chrono::seconds(10));
|
||||
return 1;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user