1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +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

@ -30,6 +30,7 @@ class DLL_LINKAGE INetworkServerListener : public INetworkConnectionListener
friend class NetworkServer;
protected:
virtual void onNewConnection(const std::shared_ptr<NetworkConnection> &) = 0;
virtual void onTimer() = 0;
~INetworkServerListener() = default;
};
@ -38,9 +39,9 @@ class DLL_LINKAGE INetworkClientListener : public INetworkConnectionListener
{
friend class NetworkClient;
protected:
virtual void onTimer() = 0;
virtual void onConnectionFailed(const std::string & errorMessage) = 0;
virtual void onConnectionEstablished(const std::shared_ptr<NetworkConnection> &) = 0;
virtual void onTimer() = 0;
~INetworkClientListener() = default;
};

View File

@ -80,4 +80,14 @@ void NetworkServer::onPacketReceived(const std::shared_ptr<NetworkConnection> &
listener.onPacketReceived(connection, message);
}
void NetworkServer::setTimer(std::chrono::milliseconds duration)
{
auto timer = std::make_shared<NetworkTimer>(*io, duration);
timer->async_wait([this, timer](const boost::system::error_code& error){
if (!error)
listener.onTimer();
});
}
VCMI_LIB_NAMESPACE_END

View File

@ -34,6 +34,7 @@ public:
void sendPacket(const std::shared_ptr<NetworkConnection> &, const std::vector<uint8_t> & message);
void closeConnection(const std::shared_ptr<NetworkConnection> &);
void setTimer(std::chrono::milliseconds duration);
void start(uint16_t port);
void run(std::chrono::milliseconds duration);

View File

@ -107,6 +107,11 @@ void LobbyServer::sendMessage(const std::shared_ptr<NetworkConnection> & target,
networkServer->sendPacket(target, payloadBuffer);
}
void LobbyServer::onTimer()
{
// no-op
}
void LobbyServer::onNewConnection(const std::shared_ptr<NetworkConnection> & connection)
{
}

View File

@ -56,6 +56,7 @@ class LobbyServer : public INetworkServerListener
void onNewConnection(const std::shared_ptr<NetworkConnection> &) override;
void onDisconnected(const std::shared_ptr<NetworkConnection> &) override;
void onPacketReceived(const std::shared_ptr<NetworkConnection> &, const std::vector<uint8_t> & message) override;
void onTimer() override;
void sendMessage(const std::shared_ptr<NetworkConnection> & target, const JsonNode & json);
public:

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)

View File

@ -55,6 +55,9 @@ class CVCMIServer : public LobbyInfo, public INetworkServerListener, public INet
/// Outgoing connection established by this server to game lobby for proxy mode (only in lobby game)
std::unique_ptr<NetworkClient> outgoingConnection;
std::chrono::steady_clock::time_point gameplayStartTime;
std::chrono::steady_clock::time_point lastTimerUpdateTime;
public:
/// List of all active connections
std::vector<std::shared_ptr<CConnection>> activeConnections;