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 "NetClient.h"
21 : #include "NetClientTurnManager.h"
22 : #include "lib/debug.h"
23 : #include "lib/os_path.h"
24 : #include "lib/path.h"
25 : #include "lib/utf8.h"
26 : #include "network/NetMessage.h"
27 : #include "ps/CLogger.h"
28 : #include "ps/Profile.h"
29 : #include "ps/Pyrogenesis.h"
30 : #include "ps/Replay.h"
31 : #include "ps/Util.h"
32 : #include "simulation2/Simulation2.h"
33 :
34 : #include <cstddef>
35 : #include <fstream>
36 : #include <js/RootingAPI.h>
37 : #include <sstream>
38 : #include <string>
39 :
40 : #if 0
41 : #define NETCLIENTTURN_LOG(...) debug_printf(__VA_ARGS__)
42 : #else
43 : #define NETCLIENTTURN_LOG(...)
44 : #endif
45 :
46 : extern CStrW g_UniqueLogPostfix;
47 :
48 0 : CNetClientTurnManager::CNetClientTurnManager(CSimulation2& simulation, CNetClient& client, int clientId, IReplayLogger& replay)
49 0 : : CTurnManager(simulation, DEFAULT_TURN_LENGTH, COMMAND_DELAY_MP, clientId, replay), m_NetClient(client)
50 : {
51 0 : }
52 :
53 0 : void CNetClientTurnManager::PostCommand(JS::HandleValue data)
54 : {
55 0 : NETCLIENTTURN_LOG("PostCommand()\n");
56 :
57 : // Transmit command to server
58 0 : CSimulationMessage msg(m_Simulation2.GetScriptInterface(), m_ClientId, m_PlayerId, m_CurrentTurn + m_CommandDelay, data);
59 0 : m_NetClient.SendMessage(&msg);
60 :
61 : // Add to our local queue
62 : //AddCommand(m_ClientId, m_PlayerId, data, m_CurrentTurn + m_CommandDelay);
63 : // TODO: we should do this when the server stops sending our commands back to us
64 0 : }
65 :
66 0 : void CNetClientTurnManager::NotifyFinishedOwnCommands(turn_id_t turn)
67 : {
68 0 : NETCLIENTTURN_LOG("NotifyFinishedOwnCommands(%d)\n", turn);
69 :
70 0 : CEndCommandBatchMessage msg;
71 :
72 0 : msg.m_Turn = turn;
73 :
74 : // The turn-length field of the CEndCommandBatchMessage is currently only relevant
75 : // when sending it from the server to the clients.
76 : // It could be used to verify that the client simulated the correct turn length.
77 0 : msg.m_TurnLength = 0;
78 :
79 0 : m_NetClient.SendMessage(&msg);
80 0 : }
81 :
82 0 : void CNetClientTurnManager::NotifyFinishedUpdate(turn_id_t turn, const UpdateCallback&)
83 : {
84 0 : bool quick = !TurnNeedsFullHash(turn);
85 0 : std::string hash;
86 0 : {
87 0 : PROFILE3("state hash check");
88 0 : ENSURE(m_Simulation2.ComputeStateHash(hash, quick));
89 0 : }
90 :
91 0 : NETCLIENTTURN_LOG("NotifyFinishedUpdate(%d, %hs)\n", turn, Hexify(hash).c_str());
92 :
93 0 : m_Replay.Hash(hash, quick);
94 :
95 : // Send message to the server
96 0 : CSyncCheckMessage msg;
97 0 : msg.m_Turn = turn;
98 0 : msg.m_Hash = hash;
99 0 : m_NetClient.SendMessage(&msg);
100 0 : }
101 :
102 0 : void CNetClientTurnManager::OnDestroyConnection()
103 : {
104 : // Attempt to flush messages before leaving.
105 : // Notice the sending is not reliable and rarely makes it to the Server.
106 0 : if (m_NetClient.GetCurrState() == NCS_INGAME)
107 0 : NotifyFinishedOwnCommands(m_CurrentTurn + m_CommandDelay);
108 0 : }
109 :
110 0 : void CNetClientTurnManager::OnSimulationMessage(CSimulationMessage* msg)
111 : {
112 : // Command received from the server - store it for later execution
113 0 : AddCommand(msg->m_Client, msg->m_Player, msg->m_Data, static_cast<turn_id_t>(msg->m_Turn));
114 0 : }
115 :
116 0 : void CNetClientTurnManager::OnSyncError(turn_id_t turn, const CStr& expectedHash, const std::vector<CSyncErrorMessage::S_m_PlayerNames>& playerNames)
117 : {
118 0 : CStr expectedHashHex(Hexify(expectedHash));
119 0 : NETCLIENTTURN_LOG("OnSyncError(%d, %hs)\n", turn, expectedHashHex.c_str());
120 :
121 0 : std::string hash;
122 0 : ENSURE(m_Simulation2.ComputeStateHash(hash, !TurnNeedsFullHash(turn)));
123 :
124 0 : OsPath oosdumpPath(psLogDir() / (L"oos_dump" + g_UniqueLogPostfix + L".txt"));
125 0 : std::ofstream file (OsString(oosdumpPath), std::ofstream::out | std::ofstream::trunc);
126 0 : file << "oos turn: " << turn << std::endl;
127 0 : file << "net client turn: " << m_CurrentTurn << std::endl;
128 0 : m_Simulation2.DumpDebugState(file);
129 0 : file.close();
130 :
131 0 : std::ofstream binfile (OsString(oosdumpPath.ChangeExtension(L".dat")), std::ofstream::out | std::ofstream::trunc | std::ofstream::binary);
132 0 : m_Simulation2.SerializeState(binfile);
133 0 : binfile.close();
134 :
135 0 : std::stringstream playerNamesString;
136 0 : std::vector<CStr> playerNamesStrings;
137 0 : playerNamesStrings.reserve(playerNames.size());
138 0 : for (size_t i = 0; i < playerNames.size(); ++i)
139 : {
140 0 : CStr name = utf8_from_wstring(playerNames[i].m_Name);
141 0 : playerNamesString << (i == 0 ? "" : ", ") << name;
142 0 : playerNamesStrings.push_back(name);
143 0 : }
144 :
145 0 : LOGERROR("Out-Of-Sync on turn {}\nPlayers: {}\nDumping state to {}", turn,
146 : playerNamesString.str(), oosdumpPath.string8());
147 :
148 0 : m_NetClient.PushGuiMessage(
149 : "type", "out-of-sync",
150 : "turn", turn,
151 : "players", playerNamesStrings,
152 : "expectedHash", expectedHashHex,
153 0 : "hash", Hexify(hash),
154 0 : "path_oos_dump", wstring_from_utf8(oosdumpPath.string8()),
155 0 : "path_replay", wstring_from_utf8(m_Replay.GetDirectory().string8()));
156 0 : }
|