Skip to content

Commit

Permalink
Merge pull request #98 from BeamMP/rc-v3.0.2
Browse files Browse the repository at this point in the history
RC v3.0.2
  • Loading branch information
lionkor committed Jul 9, 2022
2 parents 2827007 + f8d6223 commit 05251ef
Show file tree
Hide file tree
Showing 20 changed files with 264 additions and 117 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cmake-windows.yml
Expand Up @@ -20,7 +20,7 @@ jobs:
with:
vcpkgArguments: 'lua zlib rapidjson openssl websocketpp curl'
vcpkgDirectory: '${{ runner.workspace }}/b/vcpkg'
vcpkgGitCommitId: '8dddc6c899ce6fdbeab38b525a31e7f23cb2d5bb'
vcpkgGitCommitId: 'a106de33bbee694e3be6243718aa2a549a692832'
vcpkgTriplet: 'x64-windows-static'

- name: Create Build Environment
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release-build.yml
Expand Up @@ -85,7 +85,7 @@ jobs:
with:
vcpkgArguments: 'lua zlib rapidjson openssl websocketpp curl'
vcpkgDirectory: '${{ runner.workspace }}/b/vcpkg'
vcpkgGitCommitId: '8dddc6c899ce6fdbeab38b525a31e7f23cb2d5bb'
vcpkgGitCommitId: 'a106de33bbee694e3be6243718aa2a549a692832'
vcpkgTriplet: 'x64-windows-static'

- name: Create Build Environment
Expand Down
9 changes: 5 additions & 4 deletions CMakeLists.txt
Expand Up @@ -37,7 +37,7 @@ if (WIN32)
endif()

include_directories("include/sentry-native/include")
set(SENTRY_BUILD_SHARED_LIBS OFF)
set(BUILD_SHARED_LIBS OFF)
if (MSVC)
set(SENTRY_BUILD_RUNTIMESTATIC ON)
endif()
Expand All @@ -60,6 +60,10 @@ if (MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /bigobj")
endif ()

message(STATUS "Adding local source dependencies")
# this has to happen before -DDEBUG since it wont compile properly with -DDEBUG
add_subdirectory(deps)

message(STATUS "Setting compiler flags")
if (WIN32)

Expand All @@ -78,9 +82,6 @@ elseif (UNIX)
endif ()


message(STATUS "Adding local source dependencies")
# this has to happen before -DDEBUG since it wont compile properly with -DDEBUG
add_subdirectory(deps)

set(CMAKE_CXX_STANDARD 17)

Expand Down
11 changes: 11 additions & 0 deletions Changelog.md
@@ -1,3 +1,14 @@
# v3.0.2

- ADDED Periodic update message if a new server is released
- ADDED Config setting for the IP the http server listens on
- CHANGED Default MaxPlayers to 8
- CHANGED Default http server listen IP to localhost
- FIXED `MP.CreateEventTimer` filling up the queue (see <https://wiki.beammp.com/en/Scripting/new-lua-scripting#mpcreateeventtimerevent_name-string-interval_ms-number-strategy-number-since-v302>)
- FIXED `MP.TriggerClientEvent` not kicking the client if it failed
- FIXED Lua result queue handling not checking all results
- FIXED bug which caused ServerConfig.toml to generate incorrectly

# v3.0.1

- ADDED Backup URLs to UpdateCheck (will fail less often now)
Expand Down
2 changes: 1 addition & 1 deletion deps/commandline
Submodule commandline updated 5 files
+10 −7 CMakeLists.txt
+11 −6 README.md
+132 −42 commandline.cpp
+16 −9 commandline.h
+28 −2 main.cpp
2 changes: 1 addition & 1 deletion include/Client.h
Expand Up @@ -92,7 +92,7 @@ class TClient final {
std::queue<std::string> mPacketsSync;
std::unordered_map<std::string, std::string> mIdentifiers;
bool mIsGuest = false;
std::mutex mVehicleDataMutex;
mutable std::mutex mVehicleDataMutex;
TSetOfVehicleData mVehicleData;
std::string mName = "Unknown Client";
SOCKET mSocket[2] { SOCKET(0), SOCKET(0) };
Expand Down
9 changes: 6 additions & 3 deletions include/Common.h
Expand Up @@ -7,6 +7,7 @@ extern TSentry Sentry;
#include <atomic>
#include <cstring>
#include <deque>
#include <filesystem>
#include <functional>
#include <memory>
#include <mutex>
Expand Down Expand Up @@ -43,7 +44,7 @@ class Application final {
std::string SSLKeyPath { "./.ssl/HttpServer/key.pem" };
std::string SSLCertPath { "./.ssl/HttpServer/cert.pem" };
bool HTTPServerEnabled { false };
int MaxPlayers { 10 };
int MaxPlayers { 8 };
bool Private { true };
int MaxCars { 1 };
bool DebugModeEnabled { false };
Expand All @@ -52,7 +53,9 @@ class Application final {
bool SendErrors { true };
bool SendErrorsMessageEnabled { true };
int HTTPServerPort { 8080 };
bool HTTPServerUseSSL { true };
std::string HTTPServerIP { "127.0.0.1" };
bool HTTPServerUseSSL { false };
bool HideUpdateMessages { false };
[[nodiscard]] bool HasCustomIP() const { return !CustomIP.empty(); }
};

Expand Down Expand Up @@ -119,7 +122,7 @@ class Application final {
static inline std::mutex mShutdownHandlersMutex {};
static inline std::deque<TShutdownHandler> mShutdownHandlers {};

static inline Version mVersion { 3, 0, 1 };
static inline Version mVersion { 3, 0, 2 };
};

std::string ThreadName(bool DebugModeOverride = false);
Expand Down
3 changes: 3 additions & 0 deletions include/TConsole.h
Expand Up @@ -15,6 +15,7 @@ class TConsole {
void WriteRaw(const std::string& str);
void InitializeLuaConsole(TLuaEngine& Engine);
void BackupOldLog();
void StartLoggingToFile();
Commandline& Internal() { return mCommandline; }

private:
Expand All @@ -37,4 +38,6 @@ class TConsole {
bool mFirstTime { true };
std::string mStateId;
const std::string mDefaultStateId = "BEAMMP_SERVER_CONSOLE";
std::ofstream mLogFileStream;
std::mutex mLogFileStreamMtx;
};
24 changes: 20 additions & 4 deletions include/TLuaEngine.h
Expand Up @@ -6,6 +6,7 @@
#include <condition_variable>
#include <filesystem>
#include <initializer_list>
#include <list>
#include <lua.hpp>
#include <memory>
#include <mutex>
Expand Down Expand Up @@ -72,6 +73,18 @@ class TPluginMonitor : IThreaded {

class TLuaEngine : IThreaded {
public:
enum CallStrategy : int {
BestEffort,
Precise,
};

struct QueuedFunction {
std::string FunctionName;
std::shared_ptr<TLuaResult> Result;
std::vector<TLuaArgTypes> Args;
std::string EventName; // optional, may be empty
};

TLuaEngine();
~TLuaEngine() noexcept {
beammp_debug("Lua Engine terminated");
Expand Down Expand Up @@ -145,7 +158,7 @@ class TLuaEngine : IThreaded {
return Results; //
}
std::set<std::string> GetEventHandlersForState(const std::string& EventName, TLuaStateId StateId);
void CreateEventTimer(const std::string& EventName, TLuaStateId StateId, size_t IntervalMS);
void CreateEventTimer(const std::string& EventName, TLuaStateId StateId, size_t IntervalMS, CallStrategy Strategy);
void CancelEventTimers(const std::string& EventName, TLuaStateId StateId);
sol::state_view GetStateForPlugin(const fs::path& PluginPath);
TLuaStateId GetStateIDForPlugin(const fs::path& PluginPath);
Expand All @@ -166,6 +179,7 @@ class TLuaEngine : IThreaded {
~StateThreadData() noexcept { beammp_debug("\"" + mStateId + "\" destroyed"); }
[[nodiscard]] std::shared_ptr<TLuaResult> EnqueueScript(const TLuaChunk& Script);
[[nodiscard]] std::shared_ptr<TLuaResult> EnqueueFunctionCall(const std::string& FunctionName, const std::vector<TLuaArgTypes>& Args);
[[nodiscard]] std::shared_ptr<TLuaResult> EnqueueFunctionCallFromCustomEvent(const std::string& FunctionName, const std::vector<TLuaArgTypes>& Args, const std::string& EventName, CallStrategy Strategy);
void RegisterEvent(const std::string& EventName, const std::string& FunctionName);
void AddPath(const fs::path& Path); // to be added to path and cpath
void operator()() override;
Expand All @@ -188,7 +202,7 @@ class TLuaEngine : IThreaded {
std::thread mThread;
std::queue<std::pair<TLuaChunk, std::shared_ptr<TLuaResult>>> mStateExecuteQueue;
std::recursive_mutex mStateExecuteQueueMutex;
std::queue<std::tuple<std::string, std::shared_ptr<TLuaResult>, std::vector<TLuaArgTypes>>> mStateFunctionQueue;
std::vector<QueuedFunction> mStateFunctionQueue;
std::mutex mStateFunctionQueueMutex;
std::condition_variable mStateFunctionQueueCond;
TLuaEngine* mEngine;
Expand All @@ -202,6 +216,7 @@ class TLuaEngine : IThreaded {
std::chrono::high_resolution_clock::time_point LastCompletion {};
std::string EventName;
TLuaStateId StateId;
CallStrategy Strategy;
bool Expired();
void Reset();
};
Expand All @@ -218,8 +233,9 @@ class TLuaEngine : IThreaded {
std::recursive_mutex mLuaEventsMutex;
std::vector<TimedEvent> mTimedEvents;
std::recursive_mutex mTimedEventsMutex;
std::queue<std::shared_ptr<TLuaResult>> mResultsToCheck;
std::recursive_mutex mResultsToCheckMutex;
std::list<std::shared_ptr<TLuaResult>> mResultsToCheck;
std::mutex mResultsToCheckMutex;
std::condition_variable mResultsToCheckCond;
};

// std::any TriggerLuaEvent(const std::string& Event, bool local, TLuaPlugin* Caller, std::shared_ptr<TLuaArg> arg, bool Wait);
1 change: 1 addition & 0 deletions src/Client.cpp
Expand Up @@ -27,6 +27,7 @@ void TClient::ClearCars() {
int TClient::GetOpenCarID() const {
int OpenID = 0;
bool found;
std::unique_lock lock(mVehicleDataMutex);
do {
found = true;
for (auto& v : mVehicleData) {
Expand Down
26 changes: 17 additions & 9 deletions src/Common.cpp
Expand Up @@ -97,6 +97,7 @@ void Application::SetSubsystemStatus(const std::string& Subsystem, Status status

void Application::CheckForUpdates() {
Application::SetSubsystemStatus("UpdateCheck", Application::Status::Starting);
static bool FirstTime = true;
// checks current version against latest version
std::regex VersionRegex { R"(\d+\.\d+\.\d+\n*)" };
for (const auto& url : GetBackendUrlsInOrder()) {
Expand All @@ -107,24 +108,31 @@ void Application::CheckForUpdates() {
auto RemoteVersion = Version(VersionStrToInts(Response));
if (IsOutdated(MyVersion, RemoteVersion)) {
std::string RealVersionString = RemoteVersion.AsString();
beammp_warn(std::string(ANSI_YELLOW_BOLD) + "NEW VERSION OUT! There's a new version (v" + RealVersionString + ") of the BeamMP-Server available! For more info visit https://wiki.beammp.com/en/home/server-maintenance#updating-the-server." + std::string(ANSI_RESET));
beammp_warn(std::string(ANSI_YELLOW_BOLD) + "NEW VERSION IS OUT! Please update to the new version (v" + RealVersionString + ") of the BeamMP-Server! Download it here: https://beammp.com/! For a guide on how to update, visit: https://wiki.beammp.com/en/home/server-maintenance#updating-the-server" + std::string(ANSI_RESET));
} else {
beammp_info("Server up-to-date!");
if (FirstTime) {
beammp_info("Server up-to-date!");
}
}
Application::SetSubsystemStatus("UpdateCheck", Application::Status::Good);
break;
} else {
beammp_debug("Failed to fetch version from: " + url);
beammp_trace("got " + Response);
auto Lock = Sentry.CreateExclusiveContext();
Sentry.SetContext("get-response", { { "response", Response } });
Sentry.LogError("failed to get server version", _file_basename, _line);
Application::SetSubsystemStatus("UpdateCheck", Application::Status::Bad);
if (FirstTime) {
beammp_debug("Failed to fetch version from: " + url);
beammp_trace("got " + Response);
auto Lock = Sentry.CreateExclusiveContext();
Sentry.SetContext("get-response", { { "response", Response } });
Sentry.LogError("failed to get server version", _file_basename, _line);
Application::SetSubsystemStatus("UpdateCheck", Application::Status::Bad);
}
}
}
if (Application::GetSubsystemStatuses().at("UpdateCheck") == Application::Status::Bad) {
beammp_warn("Unable to fetch version info from backend.");
if (FirstTime) {
beammp_warn("Unable to fetch version info from backend.");
}
}
FirstTime = false;
}

// thread name stuff
Expand Down
12 changes: 10 additions & 2 deletions src/Http.cpp
Expand Up @@ -293,7 +293,7 @@ Http::Server::THttpServerInstance::THttpServerInstance() {
mThread.detach();
}

void Http::Server::THttpServerInstance::operator()() {
void Http::Server::THttpServerInstance::operator()() try {
beammp_info("HTTP(S) Server started on port " + std::to_string(Application::Settings.HTTPServerPort));
std::unique_ptr<httplib::Server> HttpLibServerInstance;
if (Application::Settings.HTTPServerUseSSL) {
Expand Down Expand Up @@ -370,6 +370,14 @@ void Http::Server::THttpServerInstance::operator()() {
HttpLibServerInstance->Get({ 0x2f, 0x6b, 0x69, 0x74, 0x74, 0x79 }, [](const httplib::Request&, httplib::Response& res) {
res.set_content(std::string(Magic), "text/plain");
});
HttpLibServerInstance->set_logger([](const httplib::Request& Req, const httplib::Response& Res) {
beammp_debug("Http Server: " + Req.method + " " + Req.target + " -> " + std::to_string(Res.status));
});
Application::SetSubsystemStatus("HTTPServer", Application::Status::Good);
HttpLibServerInstance->listen("0.0.0.0", Application::Settings.HTTPServerPort);
auto ret = HttpLibServerInstance->listen(Application::Settings.HTTPServerIP.c_str(), Application::Settings.HTTPServerPort);
if (!ret) {
beammp_error("Failed to start http server (failed to listen). Please ensure the http server is configured properly in the ServerConfig.toml, or turn it off if you don't need it.");
}
} catch (const std::exception& e) {
beammp_error("Failed to start http server. Please ensure the http server is configured properly in the ServerConfig.toml, or turn it off if you don't need it. Error: " + std::string(e.what()));
}
3 changes: 2 additions & 1 deletion src/LuaAPI.cpp
Expand Up @@ -112,7 +112,8 @@ bool LuaAPI::MP::TriggerClientEvent(int PlayerID, const std::string& EventName,
}
auto c = MaybeClient.value().lock();
if (!Engine->Network().Respond(*c, Packet, true)) {
beammp_lua_error("Respond failed");
beammp_lua_error("Respond failed, dropping client " + std::to_string(PlayerID));
Engine->Network().ClientKick(*c, "Disconnected after failing to receive packets");
return false;
}
}
Expand Down

0 comments on commit 05251ef

Please sign in to comment.