LCOV - code coverage report
Current view: top level - source/network - NetServerTurnManager.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 0 99 0.0 %
Date: 2026-12-03 01:19:39 Functions: 0 8 0.0 %

          Line data    Source code
       1             : /* Copyright (C) 2026 Wildfire Games.
       2             :  * This file is part of 0 A.D.
       3             :  *
       4             :  * 0 A.D. is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 2 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * 0 A.D. is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #include "precompiled.h"
      19             : 
      20             : #include "NetServerTurnManager.h"
      21             : 
      22             : #include "lib/debug.h"
      23             : #include "lib/utf8.h"
      24             : #include "network/NetHost.h"
      25             : #include "network/NetMessage.h"
      26             : #include "network/NetServer.h"
      27             : #include "network/NetServerSession.h"
      28             : #include "ps/CLogger.h"
      29             : #include "ps/ConfigDB.h"
      30             : #include "simulation2/helpers/SimulationCommand.h"
      31             : #include "simulation2/system/TurnManager.h"
      32             : 
      33             : #include <limits>
      34             : #include <utility>
      35             : 
      36             : #if 0
      37             : #include "ps/Util.h"
      38             : #define NETSERVERTURN_LOG(...) debug_printf(__VA_ARGS__)
      39             : #else
      40             : #define NETSERVERTURN_LOG(...)
      41             : #endif
      42             : 
      43           0 : CNetServerTurnManager::CNetServerTurnManager(CNetServerWorker& server)
      44           0 :     : m_NetServer(server), m_ReadyTurn(COMMAND_DELAY_MP - 1), m_TurnLength(DEFAULT_TURN_LENGTH)
      45             : {
      46             :     // Turn 0 is not actually executed, store a dummy value.
      47           0 :     m_SavedTurnLengths.push_back(0);
      48             :     // Turns [1..COMMAND_DELAY - 1] are special: all clients run them without waiting on a server command batch.
      49             :     // Because of this, they are always run with the default MP turn length.
      50           0 :     for (std::uint32_t i = 1; i < COMMAND_DELAY_MP; ++i)
      51           0 :         m_SavedTurnLengths.push_back(m_TurnLength);
      52           0 : }
      53             : 
      54           0 : void CNetServerTurnManager::NotifyFinishedClientCommands(CNetServerSession& session, turn_id_t turn)
      55             : {
      56           0 :     int client = session.GetHostID();
      57             : 
      58           0 :     NETSERVERTURN_LOG("NotifyFinishedClientCommands(client=%d, turn=%d)\n", client, turn);
      59             : 
      60             :     // Must be a client we've already heard of
      61           0 :     ENSURE(m_ClientsData.find(client) != m_ClientsData.end());
      62             : 
      63             :     // Clients must advance one turn at a time
      64           0 :     if (turn != m_ClientsData[client].readyTurn + 1)
      65             :     {
      66           0 :         LOGERROR("NotifyFinishedClientCommands: Client {} ({}) is ready for turn {}, but expected {}",
      67             :             client, utf8_from_wstring(session.GetUserName()), turn,
      68             :             m_ClientsData[client].readyTurn + 1);
      69             : 
      70           0 :         session.Disconnect(NDR_INCORRECT_READY_TURN_COMMANDS);
      71           0 :         return;
      72             :     }
      73             : 
      74           0 :     m_ClientsData[client].readyTurn = turn;
      75             : 
      76             :     // Check whether this was the final client to become ready
      77           0 :     CheckClientsReady();
      78             : }
      79             : 
      80           0 : void CNetServerTurnManager::CheckClientsReady()
      81             : {
      82           0 :     int max_observer_lag{g_ConfigDB.Get("network.observermaxlag", -1)};
      83             :     // Clamp to 0-10000 turns, below/above that is no limit.
      84           0 :     max_observer_lag = max_observer_lag < 0 ? -1 : max_observer_lag > 10000 ? -1 : max_observer_lag;
      85             : 
      86             :     // See if all clients (including self) are ready for a new turn
      87           0 :     for (const std::pair<const int, Client>& clientData : m_ClientsData)
      88             :     {
      89             :         // Observers are allowed to lag more than regular clients.
      90           0 :         if (clientData.second.isObserver && (max_observer_lag == -1 || clientData.second.readyTurn > m_ReadyTurn - max_observer_lag))
      91           0 :             continue;
      92           0 :         NETSERVERTURN_LOG("  %d: %d <=? %d\n", clientReady.first, clientReady.second, m_ReadyTurn);
      93           0 :         if (clientData.second.readyTurn <= m_ReadyTurn)
      94           0 :             return; // wasn't ready for m_ReadyTurn+1
      95             :     }
      96             : 
      97           0 :     ++m_ReadyTurn;
      98             : 
      99           0 :     NETSERVERTURN_LOG("CheckClientsReady: ready for turn %d\n", m_ReadyTurn);
     100             : 
     101             :     // Tell all clients that the next turn is ready
     102           0 :     CEndCommandBatchMessage msg;
     103           0 :     msg.m_TurnLength = m_TurnLength;
     104           0 :     msg.m_Turn = m_ReadyTurn;
     105           0 :     m_NetServer.Multicast(&msg, { NSS_INGAME });
     106             : 
     107           0 :     ENSURE(std::cmp_equal(m_SavedTurnLengths.size(), m_ReadyTurn));
     108           0 :     m_SavedTurnLengths.push_back(m_TurnLength);
     109           0 : }
     110             : 
     111           0 : void CNetServerTurnManager::NotifyFinishedClientUpdate(CNetServerSession& session, turn_id_t turn, const CStr& hash)
     112             : {
     113             : 
     114           0 :     int client = session.GetHostID();
     115           0 :     const CStrW& playername = session.GetUserName();
     116             : 
     117             :     // Clients must advance one turn at a time
     118           0 :     if (turn != m_ClientsData[client].simulatedTurn + 1)
     119             :     {
     120           0 :         LOGERROR("NotifyFinishedClientUpdate: Client {} ({}) is ready for turn {}, but expected {}",
     121             :             client, utf8_from_wstring(playername), turn, m_ClientsData[client].simulatedTurn + 1);
     122             : 
     123           0 :         session.Disconnect(NDR_INCORRECT_READY_TURN_SIMULATED);
     124           0 :         return;
     125             :     }
     126             : 
     127           0 :     m_ClientsData[client].simulatedTurn = turn;
     128             : 
     129             :     // Check for OOS only if in sync
     130           0 :     if (m_HasSyncError)
     131             :         return;
     132             : 
     133           0 :     m_ClientsData[client].playerName = playername;
     134           0 :     m_ClientStateHashes[turn][client] = hash;
     135             : 
     136             :     // Find the newest turn which we know all clients have simulated
     137           0 :     turn_id_t newest = std::numeric_limits<turn_id_t>::max();
     138           0 :     for (const std::pair<const int, Client>& clientData : m_ClientsData)
     139           0 :         if (clientData.second.simulatedTurn < newest)
     140             :             newest = clientData.second.simulatedTurn;
     141             : 
     142             :     // For every set of state hashes that all clients have simulated, check for OOS
     143           0 :     for (const std::pair<const turn_id_t, std::map<int, std::string>>& clientStateHash : m_ClientStateHashes)
     144             :     {
     145           0 :         if (clientStateHash.first > newest)
     146             :             break;
     147             : 
     148             :         // Assume the host is correct (maybe we should choose the most common instead to help debugging)
     149           0 :         std::string expected = clientStateHash.second.begin()->second;
     150             : 
     151             :         // Find all players that are OOS on that turn
     152           0 :         std::vector<CStrW> OOSPlayerNames;
     153           0 :         for (const std::pair<const int, std::string>& hashPair : clientStateHash.second)
     154             :         {
     155           0 :             NETSERVERTURN_LOG("sync check %d: %d = %hs\n", clientStateHash.first, hashPair.first, Hexify(hashPair.second).c_str());
     156           0 :             if (hashPair.second != expected)
     157             :             {
     158             :                 // Oh no, out of sync
     159           0 :                 m_HasSyncError = true;
     160           0 :                 m_ClientsData[hashPair.first].isOOS = true;
     161           0 :                 OOSPlayerNames.push_back(m_ClientsData[hashPair.first].playerName);
     162             :             }
     163             :         }
     164             : 
     165             :         // Tell everyone about it
     166           0 :         if (m_HasSyncError)
     167             :         {
     168           0 :             CSyncErrorMessage msg;
     169           0 :             msg.m_Turn = clientStateHash.first;
     170           0 :             msg.m_HashExpected = expected;
     171           0 :             for (const CStrW& oosPlayername : OOSPlayerNames)
     172             :             {
     173           0 :                 CSyncErrorMessage::S_m_PlayerNames h;
     174           0 :                 h.m_Name = oosPlayername;
     175           0 :                 msg.m_PlayerNames.push_back(h);
     176           0 :             }
     177           0 :             m_NetServer.Multicast(&msg, { NSS_INGAME });
     178           0 :             break;
     179           0 :         }
     180           0 :     }
     181             : 
     182             :     // Delete the saved hashes for all turns that we've already verified
     183           0 :     m_ClientStateHashes.erase(m_ClientStateHashes.begin(), m_ClientStateHashes.lower_bound(newest+1));
     184             : }
     185             : 
     186           0 : void CNetServerTurnManager::InitialiseClient(int client, turn_id_t turn, bool observer)
     187             : {
     188           0 :     NETSERVERTURN_LOG("InitialiseClient(client=%d, turn=%d)\n", client, turn);
     189             : 
     190           0 :     ENSURE(m_ClientsData.find(client) == m_ClientsData.end());
     191           0 :     Client& data = m_ClientsData[client];
     192           0 :     data.readyTurn = turn + COMMAND_DELAY_MP - 1;
     193           0 :     data.simulatedTurn = turn;
     194           0 :     data.isObserver = observer;
     195           0 : }
     196             : 
     197           0 : void CNetServerTurnManager::UninitialiseClient(int client)
     198             : {
     199           0 :     NETSERVERTURN_LOG("UninitialiseClient(client=%d)\n", client);
     200             : 
     201           0 :     ENSURE(m_ClientsData.find(client) != m_ClientsData.end());
     202           0 :     bool checkOOS = m_ClientsData[client].isOOS;
     203           0 :     m_ClientsData.erase(client);
     204             : 
     205           0 :     for (std::pair<const turn_id_t, std::map<int, std::string>>& clientStateHash : m_ClientStateHashes)
     206           0 :         clientStateHash.second.erase(client);
     207             : 
     208             :     // Check whether we're ready for the next turn now that we're not
     209             :     // waiting for this client any more
     210           0 :     CheckClientsReady();
     211             : 
     212             :     // Check whether we're still OOS.
     213           0 :     if (checkOOS)
     214             :     {
     215           0 :         for (const std::pair<const int, Client>& clientData : m_ClientsData)
     216           0 :             if (clientData.second.isOOS)
     217           0 :                 return;
     218           0 :         m_HasSyncError = false;
     219             :     }
     220             : }
     221             : 
     222           0 : void CNetServerTurnManager::SetTurnLength(std::uint32_t msecs)
     223             : {
     224           0 :     m_TurnLength = msecs;
     225           0 : }
     226             : 
     227           0 : std::uint32_t CNetServerTurnManager::GetSavedTurnLength(turn_id_t turn)
     228             : {
     229           0 :     ENSURE(turn <= m_ReadyTurn);
     230           0 :     return m_SavedTurnLengths.at(static_cast<size_t>(turn));
     231             : }

Generated by: LCOV version 1.16