1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-02-05 13:04:54 +02:00

Merge pull request #3963 from IvanSavenko/heartbeat_fix

lobby server - Heartbeat fix
This commit is contained in:
Ivan Savenko 2024-05-13 10:13:31 +03:00 committed by GitHub
commit 4412bb4d57
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 18 additions and 10 deletions

View File

@ -60,16 +60,17 @@ void NetworkConnection::heartbeat()
constexpr auto heartbeatInterval = std::chrono::seconds(10); constexpr auto heartbeatInterval = std::chrono::seconds(10);
timer->expires_after(heartbeatInterval); timer->expires_after(heartbeatInterval);
timer->async_wait( [self = shared_from_this()](const auto & ec) timer->async_wait( [self = weak_from_this()](const auto & ec)
{ {
if (ec) if (ec)
return; return;
if (!self->socket->is_open()) auto locked = self.lock();
if (!locked)
return; return;
self->sendPacket({}); locked->sendPacket({});
self->heartbeat(); locked->heartbeat();
}); });
} }
@ -77,7 +78,7 @@ void NetworkConnection::onHeaderReceived(const boost::system::error_code & ecHea
{ {
if (ecHeader) if (ecHeader)
{ {
listener.onDisconnected(shared_from_this(), ecHeader.message()); onError(ecHeader.message());
return; return;
} }
@ -89,7 +90,7 @@ void NetworkConnection::onHeaderReceived(const boost::system::error_code & ecHea
if (messageSize > messageMaxSize) if (messageSize > messageMaxSize)
{ {
listener.onDisconnected(shared_from_this(), "Invalid packet size!"); onError("Invalid packet size!");
return; return;
} }
@ -110,7 +111,7 @@ void NetworkConnection::onPacketReceived(const boost::system::error_code & ec, u
{ {
if (ec) if (ec)
{ {
listener.onDisconnected(shared_from_this(), ec.message()); onError(ec.message());
return; return;
} }
@ -160,8 +161,7 @@ void NetworkConnection::onDataSent(const boost::system::error_code & ec)
dataToSend.pop_front(); dataToSend.pop_front();
if (ec) if (ec)
{ {
logNetwork->error("Failed to send package: %s", ec.message()); onError(ec.message());
listener.onDisconnected(shared_from_this(), ec.message());
return; return;
} }
@ -169,6 +169,12 @@ void NetworkConnection::onDataSent(const boost::system::error_code & ec)
doSendData(); doSendData();
} }
void NetworkConnection::onError(const std::string & message)
{
listener.onDisconnected(shared_from_this(), message);
close();
}
void NetworkConnection::close() void NetworkConnection::close()
{ {
boost::system::error_code ec; boost::system::error_code ec;

View File

@ -27,6 +27,7 @@ class NetworkConnection : public INetworkConnection, public std::enable_shared_f
INetworkConnectionListener & listener; INetworkConnectionListener & listener;
void heartbeat(); void heartbeat();
void onError(const std::string & message);
void startReceiving(); void startReceiving();
void onHeaderReceived(const boost::system::error_code & ec); void onHeaderReceived(const boost::system::error_code & ec);

View File

@ -212,6 +212,7 @@ static JsonNode loadLobbyGameRoomToJson(const LobbyGameRoom & gameRoom)
jsonEntry["status"].String() = LOBBY_ROOM_STATE_NAMES[vstd::to_underlying(gameRoom.roomState)]; jsonEntry["status"].String() = LOBBY_ROOM_STATE_NAMES[vstd::to_underlying(gameRoom.roomState)];
jsonEntry["playerLimit"].Integer() = gameRoom.playerLimit; jsonEntry["playerLimit"].Integer() = gameRoom.playerLimit;
jsonEntry["ageSeconds"].Integer() = gameRoom.age.count(); jsonEntry["ageSeconds"].Integer() = gameRoom.age.count();
if (!gameRoom.modsJson.empty()) // not present in match history
jsonEntry["mods"] = JsonNode(reinterpret_cast<const std::byte *>(gameRoom.modsJson.data()), gameRoom.modsJson.size()); jsonEntry["mods"] = JsonNode(reinterpret_cast<const std::byte *>(gameRoom.modsJson.data()), gameRoom.modsJson.size());
for(const auto & account : gameRoom.participants) for(const auto & account : gameRoom.participants)