Skip to content

Commit

Permalink
[cleanup] split cClient::handleNetMessages() and cServer::run()
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarod42 committed Aug 20, 2024
1 parent e751144 commit a2e17a0
Show file tree
Hide file tree
Showing 4 changed files with 224 additions and 205 deletions.
186 changes: 98 additions & 88 deletions src/lib/game/logic/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,113 +158,123 @@ void cClient::handleNetMessages()
std::unique_ptr<cNetMessage> message;
while (eventQueue.try_pop (message))
{
if (message->getType() != eNetMessageType::GAMETIME_SYNC_SERVER && message->getType() != eNetMessageType::RESYNC_MODEL)
if (handleNetMessage (*message))
{
nlohmann::json json;
cJsonArchiveOut jsonarchive (json);
jsonarchive << *message;
NetLog.debug (getActivePlayer().getName() + ": <-- " + json.dump (-1) + " @" + std::to_string (model.getGameTime()));
return;
}
}
}

switch (message->getType())
//------------------------------------------------------------------------------
bool cClient::handleNetMessage (cNetMessage& message)
{
if (message.getType() != eNetMessageType::GAMETIME_SYNC_SERVER && message.getType() != eNetMessageType::RESYNC_MODEL)
{
nlohmann::json json;
cJsonArchiveOut jsonarchive (json);
jsonarchive << message;
NetLog.debug (getActivePlayer().getName() + ": <-- " + json.dump (-1) + " @" + std::to_string (model.getGameTime()));
}

switch (message.getType())
{
case eNetMessageType::REPORT:
{
case eNetMessageType::REPORT:
{
if (message->playerNr != -1 && model.getPlayer (message->playerNr) == nullptr) continue;
if (message.playerNr != -1 && model.getPlayer (message.playerNr) == nullptr) return false;

cNetMessageReport* chatMessage = static_cast<cNetMessageReport*> (message.get());
reportMessageReceived (chatMessage->playerNr, chatMessage->report, activePlayer->getId());
}
break;
case eNetMessageType::ACTION:
{
if (model.getPlayer (message->playerNr) == nullptr) continue;
cNetMessageReport& chatMessage = static_cast<cNetMessageReport&> (message);
reportMessageReceived (chatMessage.playerNr, chatMessage.report, activePlayer->getId());
return false;
}
case eNetMessageType::ACTION:
{
if (model.getPlayer (message.playerNr) == nullptr) return false;

const cAction* action = static_cast<cAction*> (message.get());
action->execute (model);
}
break;
case eNetMessageType::GAMETIME_SYNC_SERVER:
{
const cNetMessageSyncServer* syncMessage = static_cast<cNetMessageSyncServer*> (message.get());
gameTimer->handleSyncMessage (*syncMessage, model.getGameTime());
return; //stop processing messages after receiving a sync message. Gametime needs to be increased before handling the next message.
}
break;
case eNetMessageType::RANDOM_SEED:
{
const cNetMessageRandomSeed* msg = static_cast<cNetMessageRandomSeed*> (message.get());
model.randomGenerator.seed (msg->seed);
}
break;
case eNetMessageType::REQUEST_GUI_SAVE_INFO:
const cAction& action = static_cast<const cAction&> (message);
action.execute (model);
return false;
}
case eNetMessageType::GAMETIME_SYNC_SERVER:
{
const auto& syncMessage = static_cast<const cNetMessageSyncServer&> (message);
gameTimer->handleSyncMessage (syncMessage, model.getGameTime());
return true; //stop processing messages after receiving a sync message. Gametime needs to be increased before handling the next message.
}
case eNetMessageType::RANDOM_SEED:
{
const auto& msg = static_cast<const cNetMessageRandomSeed&> (message);
model.randomGenerator.seed (msg.seed);
return false;
}
case eNetMessageType::REQUEST_GUI_SAVE_INFO:
{
const auto& msg = static_cast<const cNetMessageRequestGUISaveInfo&> (message);
guiSaveInfoRequested (msg.slot, msg.savingID);
return false;
}
case eNetMessageType::GUI_SAVE_INFO:
{
const auto& msg = static_cast<const cNetMessageGUISaveInfo&> (message);
if (msg.playerNr != activePlayer->getId()) return false;
guiSaveInfoReceived (msg);
return false;
}
case eNetMessageType::RESYNC_MODEL:
{
NetLog.debug (" Client: Received model data for resynchronization");
const auto& msg = static_cast<const cNetMessageResyncModel&> (message);
try
{
const cNetMessageRequestGUISaveInfo* msg = static_cast<cNetMessageRequestGUISaveInfo*> (message.get());
guiSaveInfoRequested (msg->slot, msg->savingID);
msg.apply (model);
recreateSurveyorMoveJobs();
gameTimer->sendSyncMessage (*this, model.getGameTime(), 0, 0);
}
break;
case eNetMessageType::GUI_SAVE_INFO:
catch (const std::runtime_error& e)
{
const cNetMessageGUISaveInfo* msg = static_cast<cNetMessageGUISaveInfo*> (message.get());
if (msg->playerNr != activePlayer->getId()) continue;
guiSaveInfoReceived (*msg);
NetLog.error (std::string (" Client: error loading received model data: ") + e.what());
}
break;
case eNetMessageType::RESYNC_MODEL:
{
NetLog.debug (" Client: Received model data for resynchronization");
const cNetMessageResyncModel* msg = static_cast<cNetMessageResyncModel*> (message.get());
try
{
msg->apply (model);
recreateSurveyorMoveJobs();
gameTimer->sendSyncMessage (*this, model.getGameTime(), 0, 0);
}
catch (const std::runtime_error& e)
{
NetLog.error (std::string (" Client: error loading received model data: ") + e.what());
}

//FIXME: deserializing model does not trigger signals on changed data members. Use this signal to trigger some gui updates
freezeModeChanged();
resynced();
}
break;
case eNetMessageType::FREEZE_MODES:
{
const cNetMessageFreezeModes* msg = static_cast<cNetMessageFreezeModes*> (message.get());
//FIXME: deserializing model does not trigger signals on changed data members. Use this signal to trigger some gui updates
freezeModeChanged();
resynced();
return false;
}
case eNetMessageType::FREEZE_MODES:
{
const auto& msg = static_cast<const cNetMessageFreezeModes&> (message);

// don't overwrite waitForServer flag
bool waitForServer = freezeModes.isEnabled (eFreezeMode::WaitForServer);
freezeModes = msg->freezeModes;
if (waitForServer) freezeModes.enable (eFreezeMode::WaitForServer);
// don't overwrite waitForServer flag
const bool waitForServer = freezeModes.isEnabled (eFreezeMode::WaitForServer);
freezeModes = msg.freezeModes;
if (waitForServer) freezeModes.enable (eFreezeMode::WaitForServer);

for (const auto& [playerId, conn] : msg->playerStates)
{
if (model.getPlayer (playerId) == nullptr)
{
NetLog.error (" Client: Invalid player id: " + std::to_string (playerId));
break;
}
}
if (msg->playerStates.size() != model.getPlayerList().size())
for (const auto& [playerId, conn] : msg.playerStates)
{
if (model.getPlayer (playerId) == nullptr)
{
NetLog.error (" Client: Wrong size of playerState map " + std::to_string (msg->playerStates.size()));
NetLog.error (" Client: Invalid player id: " + std::to_string (playerId));
break;
}
playerConnectionStates = msg->playerStates;

freezeModeChanged();
}
break;
case eNetMessageType::TCP_CLOSE:
if (msg.playerStates.size() != model.getPlayerList().size())
{
connectionToServerLost();
NetLog.error (" Client: Wrong size of playerState map " + std::to_string (msg.playerStates.size()));
return false;
}
break;
default:
NetLog.warn (" Client: received unknown net message type");
break;
playerConnectionStates = msg.playerStates;

freezeModeChanged();
return false;
}
case eNetMessageType::TCP_CLOSE:
{
connectionToServerLost();
return false;
}
default:
{
NetLog.warn (" Client: received unknown net message type");
return false;
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/lib/game/logic/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ class cClient : public INetMessageReceiver
void run();

private:
bool handleNetMessage (cNetMessage&);
/**
* sends a serialized copy of the netmessage to the server.
*/
Expand Down
Loading

0 comments on commit a2e17a0

Please sign in to comment.