Changeset View
Changeset View
Standalone View
Standalone View
source/network/NetClientTurnManager.cpp
Show All 38 Lines | |||||
} | } | ||||
void CNetClientTurnManager::PostCommand(JS::HandleValue data) | void CNetClientTurnManager::PostCommand(JS::HandleValue data) | ||||
{ | { | ||||
NETCLIENTTURN_LOG("PostCommand()\n"); | NETCLIENTTURN_LOG("PostCommand()\n"); | ||||
// Transmit command to server | // Transmit command to server | ||||
CSimulationMessage msg(m_Simulation2.GetScriptInterface(), m_ClientId, m_PlayerId, m_CurrentTurn + COMMAND_DELAY, data); | CSimulationMessage msg(m_Simulation2.GetScriptInterface(), m_ClientId, m_PlayerId, m_CurrentTurn + COMMAND_DELAY, data); | ||||
m_NetClient.SendMessage(&msg); | m_NetClient.SendMessage(msg); | ||||
// Add to our local queue | // Add to our local queue | ||||
//AddCommand(m_ClientId, m_PlayerId, data, m_CurrentTurn + COMMAND_DELAY); | //AddCommand(m_ClientId, m_PlayerId, data, m_CurrentTurn + COMMAND_DELAY); | ||||
// TODO: we should do this when the server stops sending our commands back to us | // TODO: we should do this when the server stops sending our commands back to us | ||||
} | } | ||||
void CNetClientTurnManager::NotifyFinishedOwnCommands(u32 turn) | void CNetClientTurnManager::NotifyFinishedOwnCommands(u32 turn) | ||||
{ | { | ||||
NETCLIENTTURN_LOG("NotifyFinishedOwnCommands(%d)\n", turn); | NETCLIENTTURN_LOG("NotifyFinishedOwnCommands(%d)\n", turn); | ||||
CEndCommandBatchMessage msg; | CEndCommandBatchMessage msg; | ||||
msg.m_Turn = turn; | msg.m_Turn = turn; | ||||
// The turn-length field of the CEndCommandBatchMessage is currently only relevant | // The turn-length field of the CEndCommandBatchMessage is currently only relevant | ||||
// when sending it from the server to the clients. | // when sending it from the server to the clients. | ||||
// It could be used to verify that the client simulated the correct turn length. | // It could be used to verify that the client simulated the correct turn length. | ||||
msg.m_TurnLength = 0; | msg.m_TurnLength = 0; | ||||
m_NetClient.SendMessage(&msg); | m_NetClient.SendMessage(msg); | ||||
} | } | ||||
void CNetClientTurnManager::NotifyFinishedUpdate(u32 turn) | void CNetClientTurnManager::NotifyFinishedUpdate(u32 turn) | ||||
{ | { | ||||
bool quick = !TurnNeedsFullHash(turn); | bool quick = !TurnNeedsFullHash(turn); | ||||
std::string hash; | std::string hash; | ||||
{ | { | ||||
PROFILE3("state hash check"); | PROFILE3("state hash check"); | ||||
ENSURE(m_Simulation2.ComputeStateHash(hash, quick)); | ENSURE(m_Simulation2.ComputeStateHash(hash, quick)); | ||||
} | } | ||||
NETCLIENTTURN_LOG("NotifyFinishedUpdate(%d, %hs)\n", turn, Hexify(hash).c_str()); | NETCLIENTTURN_LOG("NotifyFinishedUpdate(%d, %hs)\n", turn, Hexify(hash).c_str()); | ||||
m_Replay.Hash(hash, quick); | m_Replay.Hash(hash, quick); | ||||
// Don't send the hash if OOS | // Don't send the hash if OOS | ||||
if (m_HasSyncError) | if (m_HasSyncError) | ||||
return; | return; | ||||
// Send message to the server | // Send message to the server | ||||
CSyncCheckMessage msg; | CSyncCheckMessage msg; | ||||
msg.m_Turn = turn; | msg.m_Turn = turn; | ||||
msg.m_Hash = hash; | msg.m_Hash = hash; | ||||
m_NetClient.SendMessage(&msg); | m_NetClient.SendMessage(CNetMessageBuffer(msg)); | ||||
} | } | ||||
void CNetClientTurnManager::OnDestroyConnection() | void CNetClientTurnManager::OnDestroyConnection() | ||||
{ | { | ||||
NotifyFinishedOwnCommands(m_CurrentTurn + COMMAND_DELAY); | NotifyFinishedOwnCommands(m_CurrentTurn + COMMAND_DELAY); | ||||
} | } | ||||
void CNetClientTurnManager::OnSimulationMessage(CSimulationMessage* msg) | void CNetClientTurnManager::OnSimulationMessage(CSimulationMessage* msg) | ||||
{ | { | ||||
// Command received from the server - store it for later execution | // Command received from the server - store it for later execution | ||||
AddCommand(msg->m_Client, msg->m_Player, msg->m_Data, msg->m_Turn); | AddCommand(msg->m_Client, msg->m_Player, msg->m_JSData.m_Data, msg->m_Turn); | ||||
} | } | ||||
void CNetClientTurnManager::OnSyncError(u32 turn, const CStr& expectedHash, const std::vector<CSyncErrorMessage::S_m_PlayerNames>& playerNames) | void CNetClientTurnManager::OnSyncError(u32 turn, const CStr& expectedHash, const std::vector<CStrW>& playerNames) | ||||
{ | { | ||||
CStr expectedHashHex(Hexify(expectedHash)); | CStr expectedHashHex(Hexify(expectedHash)); | ||||
NETCLIENTTURN_LOG("OnSyncError(%d, %hs)\n", turn, expectedHashHex.c_str()); | NETCLIENTTURN_LOG("OnSyncError(%d, %hs)\n", turn, expectedHashHex.c_str()); | ||||
// Only complain the first time | // Only complain the first time | ||||
if (m_HasSyncError) | if (m_HasSyncError) | ||||
return; | return; | ||||
m_HasSyncError = true; | m_HasSyncError = true; | ||||
std::string hash; | std::string hash; | ||||
ENSURE(m_Simulation2.ComputeStateHash(hash, !TurnNeedsFullHash(turn))); | ENSURE(m_Simulation2.ComputeStateHash(hash, !TurnNeedsFullHash(turn))); | ||||
OsPath path = psLogDir() / "oos_dump.txt"; | OsPath path = psLogDir() / "oos_dump.txt"; | ||||
std::ofstream file (OsString(path).c_str(), std::ofstream::out | std::ofstream::trunc); | std::ofstream file (OsString(path).c_str(), std::ofstream::out | std::ofstream::trunc); | ||||
m_Simulation2.DumpDebugState(file); | m_Simulation2.DumpDebugState(file); | ||||
file.close(); | file.close(); | ||||
std::stringstream playerNamesString; | std::stringstream playerNamesString; | ||||
std::vector<CStr> playerNamesStrings; | std::vector<CStr> playerNamesStrings; | ||||
playerNamesStrings.reserve(playerNames.size()); | playerNamesStrings.reserve(playerNames.size()); | ||||
for (size_t i = 0; i < playerNames.size(); ++i) | for (size_t i = 0; i < playerNames.size(); ++i) | ||||
{ | { | ||||
CStr name = utf8_from_wstring(playerNames[i].m_Name); | CStr name = utf8_from_wstring(playerNames[i]); | ||||
playerNamesString << (i == 0 ? "" : ", ") << name; | playerNamesString << (i == 0 ? "" : ", ") << name; | ||||
playerNamesStrings.push_back(name); | playerNamesStrings.push_back(name); | ||||
} | } | ||||
LOGERROR("Out-Of-Sync on turn %d\nPlayers: %s\nDumping state to %s", turn, playerNamesString.str().c_str(), path.string8()); | LOGERROR("Out-Of-Sync on turn %d\nPlayers: %s\nDumping state to %s", turn, playerNamesString.str().c_str(), path.string8()); | ||||
ScriptInterface& scriptInterface = m_NetClient.GetScriptInterface(); | ScriptInterface& scriptInterface = m_NetClient.GetScriptInterface(); | ||||
JSContext* cx = scriptInterface.GetContext(); | JSContext* cx = scriptInterface.GetContext(); | ||||
Show All 12 Lines |
Wildfire Games · Phabricator