summaryrefslogtreecommitdiffstats
path: root/src/Server.cpp
diff options
context:
space:
mode:
authorFeyo Korenhof <35343640+feyokorenhof@users.noreply.github.com>2021-05-26 18:07:32 +0200
committerGitHub <noreply@github.com>2021-05-26 18:07:32 +0200
commit9ddc3635d6a04ff4b78611df00c905cc86e166ae (patch)
treefa6da8cea0a2f0c9c05bb9ffa1880e25a911c6b8 /src/Server.cpp
parentRe-implement up/down placement metadata (#5219) (diff)
downloadcuberite-9ddc3635d6a04ff4b78611df00c905cc86e166ae.tar
cuberite-9ddc3635d6a04ff4b78611df00c905cc86e166ae.tar.gz
cuberite-9ddc3635d6a04ff4b78611df00c905cc86e166ae.tar.bz2
cuberite-9ddc3635d6a04ff4b78611df00c905cc86e166ae.tar.lz
cuberite-9ddc3635d6a04ff4b78611df00c905cc86e166ae.tar.xz
cuberite-9ddc3635d6a04ff4b78611df00c905cc86e166ae.tar.zst
cuberite-9ddc3635d6a04ff4b78611df00c905cc86e166ae.zip
Diffstat (limited to 'src/Server.cpp')
-rw-r--r--src/Server.cpp62
1 files changed, 61 insertions, 1 deletions
diff --git a/src/Server.cpp b/src/Server.cpp
index 3402a80fa..ae0df7544 100644
--- a/src/Server.cpp
+++ b/src/Server.cpp
@@ -117,7 +117,8 @@ cServer::cServer(void) :
m_MaxPlayers(0),
m_bIsHardcore(false),
m_TickThread(*this),
- m_ShouldAuthenticate(false)
+ m_ShouldAuthenticate(false),
+ m_UpTime(0)
{
// Initialize the LuaStateTracker singleton before the app goes multithreaded:
cLuaStateTracker::GetStats();
@@ -326,6 +327,9 @@ cTCPLink::cCallbacksPtr cServer::OnConnectionAccepted(const AString & a_RemoteIP
void cServer::Tick(float a_Dt)
{
+ // Update server uptime
+ m_UpTime++;
+
// Send the tick to the plugins, as well as let the plugin manager reload, if asked to (issue #102):
cPluginManager::Get()->Tick(a_Dt);
@@ -334,6 +338,9 @@ void cServer::Tick(float a_Dt)
// Tick all clients not yet assigned to a world:
TickClients(a_Dt);
+
+ // Process all queued tasks
+ TickQueuedTasks();
}
@@ -442,6 +449,20 @@ void cServer::QueueExecuteConsoleCommand(const AString & a_Cmd, cCommandOutputCa
+void cServer::ScheduleTask(cTickTime a_DelayTicks, std::function<void(cServer &)> a_Task)
+{
+ const auto TargetTick = a_DelayTicks + m_UpTime;
+ // Insert the task into the list of scheduled tasks
+ {
+ cCSLock Lock(m_CSTasks);
+ m_Tasks.emplace_back(TargetTick, std::move(a_Task));
+ }
+}
+
+
+
+
+
void cServer::ExecuteConsoleCommand(const AString & a_Cmd, cCommandOutputCallback & a_Output)
{
AStringVector split = StringSplit(a_Cmd, " ");
@@ -725,3 +746,42 @@ void cServer::TickCommands(void)
ExecuteConsoleCommand(Command.first, *Command.second);
}
}
+
+
+
+
+
+void cServer::TickQueuedTasks(void)
+{
+ // Move the tasks to be executed to a seperate vector to avoid deadlocks on
+ // accessing m_Tasks
+ decltype(m_Tasks) Tasks;
+ {
+ cCSLock Lock(m_CSTasks);
+ if (m_Tasks.empty())
+ {
+ return;
+ }
+
+ // Partition everything to be executed by returning false to move to end
+ // of list if time reached
+ auto MoveBeginIterator = std::partition(
+ m_Tasks.begin(), m_Tasks.end(),
+ [this](const decltype(m_Tasks)::value_type & a_Task)
+ {
+ return a_Task.first >= m_UpTime;
+ });
+
+ // Cut all the due tasks from m_Tasks into Tasks:
+ Tasks.insert(
+ Tasks.end(), std::make_move_iterator(MoveBeginIterator),
+ std::make_move_iterator(m_Tasks.end()));
+ m_Tasks.erase(MoveBeginIterator, m_Tasks.end());
+ }
+
+ // Execute each task:
+ for (const auto & Task : Tasks)
+ {
+ Task.second(*this);
+ } // for itr - m_Tasks[]
+}