mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2025-07-03 08:15:35 +00:00
Fix server event timing
This commit is contained in:
parent
e948edca8d
commit
e7f29ce04f
@ -3,6 +3,7 @@
|
||||
#include "TNetwork.h"
|
||||
#include "TServer.h"
|
||||
#include <any>
|
||||
#include <condition_variable>
|
||||
#include <filesystem>
|
||||
#include <initializer_list>
|
||||
#include <lua.hpp>
|
||||
@ -144,7 +145,8 @@ private:
|
||||
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::recursive_mutex mStateFunctionQueueMutex;
|
||||
std::mutex mStateFunctionQueueMutex;
|
||||
std::condition_variable mStateFunctionQueueCond;
|
||||
TLuaEngine* mEngine;
|
||||
sol::state_view mStateView { mState };
|
||||
std::queue<fs::path> mPaths;
|
||||
|
@ -11,35 +11,29 @@
|
||||
std::string GetDate() {
|
||||
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
|
||||
time_t tt = std::chrono::system_clock::to_time_t(now);
|
||||
tm local_tm {};
|
||||
#ifdef WIN32
|
||||
localtime_s(&local_tm, &tt);
|
||||
#else // unix
|
||||
localtime_r(&tt, &local_tm);
|
||||
#endif // WIN32
|
||||
std::stringstream date;
|
||||
int S = local_tm.tm_sec;
|
||||
int M = local_tm.tm_min;
|
||||
int H = local_tm.tm_hour;
|
||||
std::string Secs = (S > 9 ? std::to_string(S) : "0" + std::to_string(S));
|
||||
std::string Min = (M > 9 ? std::to_string(M) : "0" + std::to_string(M));
|
||||
std::string Hour = (H > 9 ? std::to_string(H) : "0" + std::to_string(H));
|
||||
date
|
||||
<< "["
|
||||
<< local_tm.tm_mday << "/"
|
||||
<< local_tm.tm_mon + 1 << "/"
|
||||
<< local_tm.tm_year + 1900 << " "
|
||||
<< Hour << ":"
|
||||
<< Min << ":"
|
||||
<< Secs
|
||||
<< "] ";
|
||||
/* TODO
|
||||
if (Debug) {
|
||||
date << ThreadName()
|
||||
<< " ";
|
||||
auto local_tm = std::localtime(&tt);
|
||||
char buf[30];
|
||||
std::string date;
|
||||
#if defined(DEBUG)
|
||||
if (Application::Settings.DebugModeEnabled) {
|
||||
std::strftime(buf, sizeof(buf), "[%d/%m/%y %T.", local_tm);
|
||||
date += buf;
|
||||
auto seconds = std::chrono::time_point_cast<std::chrono::seconds>(now);
|
||||
auto fraction = now - seconds;
|
||||
size_t ms = std::chrono::duration_cast<std::chrono::milliseconds>(fraction).count();
|
||||
char fracstr[5];
|
||||
std::sprintf(fracstr, "%0.3lu", ms);
|
||||
date += fracstr;
|
||||
date += "] ";
|
||||
} else {
|
||||
#endif
|
||||
std::strftime(buf, sizeof(buf), "[%d/%m/%y %T] ", local_tm);
|
||||
date += buf;
|
||||
#if defined(DEBUG)
|
||||
}
|
||||
*/
|
||||
return date.str();
|
||||
#endif
|
||||
|
||||
return date;
|
||||
}
|
||||
|
||||
void TConsole::BackupOldLog() {
|
||||
|
@ -45,11 +45,11 @@ void TLuaEngine::operator()() {
|
||||
beammp_lua_error("Calling \"onInit\" on \"" + Future->StateId + "\" failed: " + Future->ErrorMessage);
|
||||
}
|
||||
}
|
||||
std::queue<std::shared_ptr<TLuaResult>> ResultsToCheck;
|
||||
/*std::queue<std::shared_ptr<TLuaResult>> ResultsToCheck;
|
||||
std::recursive_mutex ResultsToCheckMutex;
|
||||
std::thread ResultCheckThread([&] {
|
||||
while (!mShutdown) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
||||
std::unique_lock Lock(ResultsToCheckMutex);
|
||||
if (!ResultsToCheck.empty()) {
|
||||
auto Res = ResultsToCheck.front();
|
||||
@ -62,6 +62,7 @@ void TLuaEngine::operator()() {
|
||||
Waited++;
|
||||
if (Waited > 1000) {
|
||||
beammp_lua_error(Res->Function + " in " + Res->StateId + " took >1s to respond, not printing possible errors");
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (Res->Error) {
|
||||
@ -70,6 +71,7 @@ void TLuaEngine::operator()() {
|
||||
}
|
||||
}
|
||||
});
|
||||
*/
|
||||
// event loop
|
||||
auto Before = std::chrono::high_resolution_clock::now();
|
||||
while (!mShutdown) {
|
||||
@ -80,15 +82,14 @@ void TLuaEngine::operator()() {
|
||||
Timer.Reset();
|
||||
auto Handlers = GetEventHandlersForState(Timer.EventName, Timer.StateId);
|
||||
std::unique_lock StateLock(mLuaStatesMutex);
|
||||
std::unique_lock Lock2(ResultsToCheckMutex);
|
||||
//std::unique_lock Lock2(ResultsToCheckMutex);
|
||||
for (auto& Handler : Handlers) {
|
||||
auto Res = mLuaStates[Timer.StateId]->EnqueueFunctionCall(Handler, {});
|
||||
ResultsToCheck.push(Res);
|
||||
//ResultsToCheck.push(Res);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// sleep for the remaining time to get to 1ms (our atom duration)
|
||||
if (std::chrono::high_resolution_clock::duration Diff;
|
||||
(Diff = std::chrono::high_resolution_clock::now() - Before)
|
||||
< std::chrono::milliseconds(10)) {
|
||||
@ -98,9 +99,10 @@ void TLuaEngine::operator()() {
|
||||
}
|
||||
Before = std::chrono::high_resolution_clock::now();
|
||||
}
|
||||
/*
|
||||
if (ResultCheckThread.joinable()) {
|
||||
ResultCheckThread.join();
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
||||
size_t TLuaEngine::CalculateMemoryUsage() {
|
||||
@ -478,6 +480,7 @@ std::shared_ptr<TLuaResult> TLuaEngine::StateThreadData::EnqueueFunctionCall(con
|
||||
Result->Function = FunctionName;
|
||||
std::unique_lock Lock(mStateFunctionQueueMutex);
|
||||
mStateFunctionQueue.push({ FunctionName, Result, Args });
|
||||
mStateFunctionQueueCond.notify_all();
|
||||
return Result;
|
||||
}
|
||||
|
||||
@ -535,16 +538,20 @@ void TLuaEngine::StateThreadData::operator()() {
|
||||
}
|
||||
{ // StateFunctionQueue Scope
|
||||
std::unique_lock Lock(mStateFunctionQueueMutex);
|
||||
if (!mStateFunctionQueue.empty()) {
|
||||
auto NotExpired = mStateFunctionQueueCond.wait_for(Lock,
|
||||
std::chrono::milliseconds(500),
|
||||
[&]() -> bool { return !mStateFunctionQueue.empty(); });
|
||||
if (NotExpired) {
|
||||
auto FnNameResultPair = std::move(mStateFunctionQueue.front());
|
||||
mStateFunctionQueue.pop();
|
||||
Lock.unlock();
|
||||
auto& StateId = std::get<0>(FnNameResultPair);
|
||||
auto& FnName = std::get<0>(FnNameResultPair);
|
||||
auto& Result = std::get<1>(FnNameResultPair);
|
||||
auto Args = std::get<2>(FnNameResultPair);
|
||||
Result->StateId = mStateId;
|
||||
sol::state_view StateView(mState);
|
||||
auto Fn = StateView[StateId];
|
||||
auto Fn = StateView[FnName];
|
||||
beammp_debug("something found in the queue: call to \"" + FnName + "\" in \"" + mStateId + "\"");
|
||||
if (Fn.valid() && Fn.get_type() == sol::type::function) {
|
||||
std::vector<sol::object> LuaArgs;
|
||||
for (const auto& Arg : Args) {
|
||||
@ -586,7 +593,6 @@ void TLuaEngine::StateThreadData::operator()() {
|
||||
}
|
||||
}
|
||||
}
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user