1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-06-17 00:07:41 +02:00

Switch turn timers handling to boost asio timer

This commit is contained in:
Ivan Savenko
2023-12-26 20:54:32 +02:00
parent 0196707083
commit aa7ecea683
7 changed files with 43 additions and 20 deletions

View File

@ -236,29 +236,29 @@ void CVCMIServer::run()
vmHelper.callStaticVoidMethod(CAndroidVMHelper::NATIVE_METHODS_DEFAULT_CLASS, "onServerReady");
}
#endif
static const int serverUpdateIntervalMilliseconds = 50;
auto clockInitial = std::chrono::steady_clock::now();
int64_t msPassedLast = 0;
while(state != EServerState::SHUTDOWN)
{
networkServer->run(std::chrono::milliseconds(serverUpdateIntervalMilliseconds));
const auto clockNow = std::chrono::steady_clock::now();
const auto clockPassed = clockNow - clockInitial;
const int64_t msPassedNow = std::chrono::duration_cast<std::chrono::milliseconds>(clockPassed).count();
const int64_t msDelta = msPassedNow - msPassedLast;
msPassedLast = msPassedNow;
if (state == EServerState::GAMEPLAY)
gh->tick(msDelta);
}
networkServer->run();
}
void CVCMIServer::onTimer()
{
// FIXME: move GameHandler updates here
if (state != EServerState::GAMEPLAY)
return;
static const auto serverUpdateInterval = std::chrono::milliseconds(100);
auto timeNow = std::chrono::steady_clock::now();
auto timePassedBefore = lastTimerUpdateTime - gameplayStartTime;
auto timePassedNow = timeNow - gameplayStartTime;
lastTimerUpdateTime = timeNow;
auto msPassedBefore = std::chrono::duration_cast<std::chrono::milliseconds>(timePassedBefore);
auto msPassedNow = std::chrono::duration_cast<std::chrono::milliseconds>(timePassedNow);
auto msDelta = msPassedNow - msPassedBefore;
if (msDelta.count())
gh->tick(msDelta.count());
networkServer->setTimer(serverUpdateInterval);
}
void CVCMIServer::establishOutgoingConnection()
@ -372,6 +372,8 @@ void CVCMIServer::startGameImmediately()
gh->start(si->mode == StartInfo::LOAD_GAME);
state = EServerState::GAMEPLAY;
lastTimerUpdateTime = gameplayStartTime = std::chrono::steady_clock::now();
onTimer();
}
void CVCMIServer::onDisconnected(const std::shared_ptr<NetworkConnection> & connection)