Compare commits

..

4 Commits

Author SHA1 Message Date
Lion Kortlepel b209e70d62 update dlhttp 2022-07-27 09:20:21 +02:00
Lion Kortlepel 5394122d77 add /modlist endpoint, DownloadThreads config option 2022-07-26 17:57:19 +02:00
Lion Kortlepel f48b8ff1a1 update dlhttp, make / work 2022-07-24 15:29:20 +02:00
Lion Kortlepel de2a3795cc update dlhttp 2022-07-24 14:19:49 +02:00
8 changed files with 70 additions and 38 deletions
+3
View File
@@ -31,3 +31,6 @@
[submodule "deps/doctest"] [submodule "deps/doctest"]
path = deps/doctest path = deps/doctest
url = https://github.com/doctest/doctest url = https://github.com/doctest/doctest
[submodule "deps/dlhttp"]
path = deps/dlhttp
url = https://github.com/lionkor/dlhttp
+16 -11
View File
@@ -9,16 +9,20 @@ project(BeamMP-Server
HOMEPAGE_URL https://beammp.com HOMEPAGE_URL https://beammp.com
LANGUAGES CXX C) LANGUAGES CXX C)
find_package(Git REQUIRED) option(BEAMMP_RUN_GIT "Run git to make sure submodules are updated (leave this on unless you know what you're doing)" ON)
# Update submodules as needed
option(GIT_SUBMODULE "Check submodules during build" ON) if (BEAMMP_RUN_GIT)
if(GIT_SUBMODULE) find_package(Git REQUIRED)
message(STATUS "Submodule update") # Update submodules as needed
execute_process(COMMAND ${GIT_EXECUTABLE} submodule update --init --recursive option(GIT_SUBMODULE "Check submodules during build" ON)
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} if(GIT_SUBMODULE)
RESULT_VARIABLE GIT_SUBMOD_RESULT) message(STATUS "Submodule update")
if(NOT GIT_SUBMOD_RESULT EQUAL "0") execute_process(COMMAND ${GIT_EXECUTABLE} submodule update --init --recursive
message(FATAL_ERROR "git submodule update --init --recursive failed with ${GIT_SUBMOD_RESULT}, please checkout submodules") WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
RESULT_VARIABLE GIT_SUBMOD_RESULT)
if(NOT GIT_SUBMOD_RESULT EQUAL "0")
message(FATAL_ERROR "git submodule update --init --recursive failed with ${GIT_SUBMOD_RESULT}, please checkout submodules")
endif()
endif() endif()
endif() endif()
@@ -52,7 +56,7 @@ elseif (WIN32)
STRING(REPLACE "/MDd" "/MTd" CMAKE_CXX_FLAGS_DEBUG ${CMAKE_CXX_FLAGS_DEBUG}) STRING(REPLACE "/MDd" "/MTd" CMAKE_CXX_FLAGS_DEBUG ${CMAKE_CXX_FLAGS_DEBUG})
# ------------------------ LINUX --------------------------------- # ------------------------ LINUX ---------------------------------
elseif (UNIX) elseif (UNIX)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wpedantic -static-libstdc++ -static-libgcc") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wpedantic")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0 -g") set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0 -g")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O2 -fno-builtin") set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O2 -fno-builtin")
option(SANITIZE "Turns on thread and UB sanitizers" OFF) option(SANITIZE "Turns on thread and UB sanitizers" OFF)
@@ -145,6 +149,7 @@ set(BeamMP_Libraries
${LUA_LIBRARIES} ${LUA_LIBRARIES}
commandline commandline
sentry sentry
dlhttp
) )
if (WIN32) if (WIN32)
+10 -3
View File
@@ -2,6 +2,13 @@ include_directories("${PROJECT_SOURCE_DIR}/deps")
include_directories("${PROJECT_SOURCE_DIR}/deps/commandline") include_directories("${PROJECT_SOURCE_DIR}/deps/commandline")
add_subdirectory("${PROJECT_SOURCE_DIR}/deps/commandline") add_subdirectory("${PROJECT_SOURCE_DIR}/deps/commandline")
add_subdirectory("${PROJECT_SOURCE_DIR}/deps/fmt") if (NOT TARGET fmt)
add_subdirectory("${PROJECT_SOURCE_DIR}/deps/sol2") add_subdirectory("${PROJECT_SOURCE_DIR}/deps/fmt")
add_subdirectory("${PROJECT_SOURCE_DIR}/deps/doctest") endif()
if (NOT TARGET sol2)
add_subdirectory("${PROJECT_SOURCE_DIR}/deps/sol2")
endif()
if (NOT TARGET doctest)
add_subdirectory("${PROJECT_SOURCE_DIR}/deps/doctest")
endif()
add_subdirectory("${PROJECT_SOURCE_DIR}/deps/dlhttp")
Vendored Submodule
+1
Submodule deps/dlhttp added at f507c6c4e6
+1
View File
@@ -55,6 +55,7 @@ public:
int MaxCars { 1 }; int MaxCars { 1 };
bool DebugModeEnabled { false }; bool DebugModeEnabled { false };
int Port { 30814 }; int Port { 30814 };
int DownloadThreads { 4 };
std::string CustomIP {}; std::string CustomIP {};
bool LogChat { true }; bool LogChat { true };
bool SendErrors { true }; bool SendErrors { true };
+23 -15
View File
@@ -18,6 +18,7 @@ static constexpr std::string_view StrDescription = "Description";
static constexpr std::string_view StrResourceFolder = "ResourceFolder"; static constexpr std::string_view StrResourceFolder = "ResourceFolder";
static constexpr std::string_view StrAuthKey = "AuthKey"; static constexpr std::string_view StrAuthKey = "AuthKey";
static constexpr std::string_view StrLogChat = "LogChat"; static constexpr std::string_view StrLogChat = "LogChat";
static constexpr std::string_view StrDownloadThreads = "DownloadThreads";
// Misc // Misc
static constexpr std::string_view StrSendErrors = "SendErrors"; static constexpr std::string_view StrSendErrors = "SendErrors";
@@ -103,6 +104,8 @@ void TConfig::FlushToFile() {
data["General"][StrMap.data()] = Application::Settings.MapName; data["General"][StrMap.data()] = Application::Settings.MapName;
data["General"][StrDescription.data()] = Application::Settings.ServerDesc; data["General"][StrDescription.data()] = Application::Settings.ServerDesc;
data["General"][StrResourceFolder.data()] = Application::Settings.Resource; data["General"][StrResourceFolder.data()] = Application::Settings.Resource;
data["General"][StrDownloadThreads.data()] = Application::Settings.DownloadThreads;
SetComment(data["General"][StrDownloadThreads.data()].comments(), " How many simultaneous downloads may run at the same time");
// Misc // Misc
data["Misc"][StrHideUpdateMessages.data()] = Application::Settings.HideUpdateMessages; data["Misc"][StrHideUpdateMessages.data()] = Application::Settings.HideUpdateMessages;
SetComment(data["Misc"][StrHideUpdateMessages.data()].comments(), " Hides the periodic update message which notifies you of a new server version. You should really keep this on and always update as soon as possible. For more information visit https://wiki.beammp.com/en/home/server-maintenance#updating-the-server. An update message will always appear at startup regardless."); SetComment(data["Misc"][StrHideUpdateMessages.data()].comments(), " Hides the periodic update message which notifies you of a new server version. You should really keep this on and always update as soon as possible. For more information visit https://wiki.beammp.com/en/home/server-maintenance#updating-the-server. An update message will always appear at startup regardless.");
@@ -186,6 +189,7 @@ void TConfig::ParseFromFile(std::string_view name) {
TryReadValue(data, "General", StrResourceFolder, Application::Settings.Resource); TryReadValue(data, "General", StrResourceFolder, Application::Settings.Resource);
TryReadValue(data, "General", StrAuthKey, Application::Settings.Key); TryReadValue(data, "General", StrAuthKey, Application::Settings.Key);
TryReadValue(data, "General", StrLogChat, Application::Settings.LogChat); TryReadValue(data, "General", StrLogChat, Application::Settings.LogChat);
TryReadValue(data, "General", StrDownloadThreads, Application::Settings.DownloadThreads);
// Misc // Misc
TryReadValue(data, "Misc", StrSendErrors, Application::Settings.SendErrors); TryReadValue(data, "Misc", StrSendErrors, Application::Settings.SendErrors);
TryReadValue(data, "Misc", StrHideUpdateMessages, Application::Settings.HideUpdateMessages); TryReadValue(data, "Misc", StrHideUpdateMessages, Application::Settings.HideUpdateMessages);
@@ -221,22 +225,26 @@ void TConfig::ParseFromFile(std::string_view name) {
} }
void TConfig::PrintDebug() { void TConfig::PrintDebug() {
beammp_debug(std::string(StrDebug) + ": " + std::string(Application::Settings.DebugModeEnabled ? "true" : "false")); beammp_debugf("{}: {}", StrDebug, Application::Settings.DebugModeEnabled ? "true" : "false");
beammp_debug(std::string(StrPrivate) + ": " + std::string(Application::Settings.Private ? "true" : "false")); beammp_debugf("{}: {}", StrPrivate, Application::Settings.Private ? "true" : "false");
beammp_debug(std::string(StrPort) + ": " + std::to_string(Application::Settings.Port)); beammp_debugf("{}: {}", StrPort, Application::Settings.Port);
beammp_debug(std::string(StrMaxCars) + ": " + std::to_string(Application::Settings.MaxCars)); beammp_debugf("{}: {}", StrMaxCars, Application::Settings.MaxCars);
beammp_debug(std::string(StrMaxPlayers) + ": " + std::to_string(Application::Settings.MaxPlayers)); beammp_debugf("{}: {}", StrMaxPlayers, Application::Settings.MaxPlayers);
beammp_debug(std::string(StrMap) + ": \"" + Application::Settings.MapName + "\""); beammp_debugf("{}: '{}'", StrMap, Application::Settings.MapName);
beammp_debug(std::string(StrName) + ": \"" + Application::Settings.ServerName + "\""); beammp_debugf("{}: '{}'", StrName, Application::Settings.ServerName);
beammp_debug(std::string(StrDescription) + ": \"" + Application::Settings.ServerDesc + "\""); beammp_debugf("{}: '{}'", StrDescription, Application::Settings.ServerDesc);
beammp_debug(std::string(StrLogChat) + ": \"" + (Application::Settings.LogChat ? "true" : "false") + "\""); beammp_debugf("{}: {}", StrLogChat, Application::Settings.LogChat ? "true" : "false");
beammp_debug(std::string(StrResourceFolder) + ": \"" + Application::Settings.Resource + "\""); beammp_debugf("{}: '{}'", StrResourceFolder, Application::Settings.Resource);
beammp_debug(std::string(StrSSLKeyPath) + ": \"" + Application::Settings.SSLKeyPath + "\""); beammp_debugf("{}: '{}'", StrSSLKeyPath, Application::Settings.SSLKeyPath);
beammp_debug(std::string(StrSSLCertPath) + ": \"" + Application::Settings.SSLCertPath + "\""); beammp_debugf("{}: '{}'", StrSSLCertPath, Application::Settings.SSLCertPath);
beammp_debug(std::string(StrHTTPServerPort) + ": \"" + std::to_string(Application::Settings.HTTPServerPort) + "\""); beammp_debugf("{}: {}", StrHTTPServerPort, Application::Settings.HTTPServerPort);
beammp_debug(std::string(StrHTTPServerIP) + ": \"" + Application::Settings.HTTPServerIP + "\""); beammp_debugf("{}: '{}'", StrHTTPServerIP, Application::Settings.HTTPServerIP);
beammp_debugf("{}: {}", StrDownloadThreads, Application::Settings.DownloadThreads);
if (Application::Settings.DownloadThreads < 1) {
beammp_warnf("'{}' of <1 is not allowed, will use 1", StrDownloadThreads);
}
// special! // special!
beammp_debug("Key Length: " + std::to_string(Application::Settings.Key.length()) + ""); beammp_debugf("Key Length: {}", Application::Settings.Key.length());
} }
void TConfig::ParseOldFormat() { void TConfig::ParseOldFormat() {
-9
View File
@@ -726,15 +726,6 @@ TLuaEngine::StateThreadData::StateThreadData(const std::string& Name, TLuaStateI
UtilTable.set_function("JsonUnflatten", &LuaAPI::MP::JsonUnflatten); UtilTable.set_function("JsonUnflatten", &LuaAPI::MP::JsonUnflatten);
UtilTable.set_function("JsonPrettify", &LuaAPI::MP::JsonPrettify); UtilTable.set_function("JsonPrettify", &LuaAPI::MP::JsonPrettify);
UtilTable.set_function("JsonMinify", &LuaAPI::MP::JsonMinify); UtilTable.set_function("JsonMinify", &LuaAPI::MP::JsonMinify);
UtilTable.set_function("Stress", [](size_t n) {
std::vector<std::thread> s;
for (size_t i = 0; i < n; ++i) {
s.emplace_back([]{ while (true); });
}
for (auto& t : s) {
t.detach();
}
});
UtilTable.set_function("Random", [this] { UtilTable.set_function("Random", [this] {
return mUniformRealDistribution01(mMersenneTwister); return mUniformRealDistribution01(mMersenneTwister);
}); });
+16
View File
@@ -6,6 +6,7 @@
#include <Http.h> #include <Http.h>
#include <array> #include <array>
#include <cstring> #include <cstring>
#include <dlhttp/dlhttp.h>
TNetwork::TNetwork(TServer& Server, TPPSMonitor& PPSMonitor, TResourceManager& ResourceManager) TNetwork::TNetwork(TServer& Server, TPPSMonitor& PPSMonitor, TResourceManager& ResourceManager)
: mServer(Server) : mServer(Server)
@@ -148,6 +149,12 @@ void TNetwork::TCPServerMain() {
} }
Application::SetSubsystemStatus("TCPNetwork", Application::Status::Good); Application::SetSubsystemStatus("TCPNetwork", Application::Status::Good);
beammp_info("Vehicle event network online"); beammp_info("Vehicle event network online");
const auto ThreadCount = std::max(1, Application::Settings.DownloadThreads);
beammp_debugf("Using {} download threads", ThreadCount);
dlhttp::AsyncContext Ctx(ThreadCount);
dlhttp::EndpointHandlerMap Map {
{ "/modlist", [this]() -> dlhttp::Response { return { mResourceManager.FileList() }; } },
};
do { do {
try { try {
if (Application::IsShuttingDown()) { if (Application::IsShuttingDown()) {
@@ -173,6 +180,15 @@ void TNetwork::TCPServerMain() {
if (ret < 0) { if (ret < 0) {
throw std::runtime_error("setsockopt recv timeout: " + GetPlatformAgnosticErrorString()); throw std::runtime_error("setsockopt recv timeout: " + GetPlatformAgnosticErrorString());
} }
// check for http
if (dlhttp::is_http(client.Socket)) {
beammp_info("IS HTTP!");
dlhttp::handle_http(client.Socket, Ctx, Map);
} else {
beammp_info("IS NOT HTTP!");
}
std::thread ID(&TNetwork::Identify, this, client); std::thread ID(&TNetwork::Identify, this, client);
ID.detach(); // TODO: Add to a queue and attempt to join periodically ID.detach(); // TODO: Add to a queue and attempt to join periodically
} catch (const std::exception& e) { } catch (const std::exception& e) {