From 11c2708d83dbbc0b247877234085704d34049c76 Mon Sep 17 00:00:00 2001 From: Ivan Savenko Date: Wed, 27 Dec 2023 14:24:31 +0200 Subject: [PATCH] Simplify server networking code, disable player takeover by AI for now --- server/CVCMIServer.cpp | 89 +++++++++++++++++++----------------------- server/CVCMIServer.h | 3 -- 2 files changed, 41 insertions(+), 51 deletions(-) diff --git a/server/CVCMIServer.cpp b/server/CVCMIServer.cpp index d6240f1e8..2f22b4c80 100644 --- a/server/CVCMIServer.cpp +++ b/server/CVCMIServer.cpp @@ -381,8 +381,6 @@ void CVCMIServer::onDisconnected(const std::shared_ptr & conn logNetwork->error("Network error receiving a pack. Connection has been closed"); std::shared_ptr c = findConnection(connection); - - inactiveConnections.push_back(c); vstd::erase(activeConnections, c); if(activeConnections.empty() || hostClientId == c->connectionID) @@ -391,19 +389,12 @@ void CVCMIServer::onDisconnected(const std::shared_ptr & conn if(gh && state == EServerState::GAMEPLAY) { gh->handleClientDisconnection(c); + + auto lcd = std::make_unique(); + lcd->c = c; + lcd->clientId = c->connectionID; + handleReceivedPack(std::move(lcd)); } - - boost::unique_lock queueLock(mx); - -// if(c->connected) -// { -// auto lcd = std::make_unique(); -// lcd->c = c; -// lcd->clientId = c->connectionID; -// handleReceivedPack(std::move(lcd)); -// } -// -// logNetwork->info("Thread listening for %s ended", c->toString()); } void CVCMIServer::handleReceivedPack(std::unique_ptr pack) @@ -508,41 +499,43 @@ void CVCMIServer::clientDisconnected(std::shared_ptr c) state = EServerState::SHUTDOWN; return; } - - PlayerReinitInterface startAiPack; - startAiPack.playerConnectionId = PlayerSettings::PLAYER_AI; - - for(auto it = playerNames.begin(); it != playerNames.end();) - { - if(it->second.connection != c->connectionID) - { - ++it; - continue; - } - int id = it->first; - std::string playerLeftMsgText = boost::str(boost::format("%s (pid %d cid %d) left the game") % id % playerNames[id].name % c->connectionID); - announceTxt(playerLeftMsgText); //send lobby text, it will be ignored for non-lobby clients - auto * playerSettings = si->getPlayersSettings(id); - if(!playerSettings) - { - ++it; - continue; - } - - it = playerNames.erase(it); - setPlayerConnectedId(*playerSettings, PlayerSettings::PLAYER_AI); - - if(gh && si && state == EServerState::GAMEPLAY) - { - gh->playerMessages->broadcastMessage(playerSettings->color, playerLeftMsgText); - // gh->connections[playerSettings->color].insert(hostClient); - startAiPack.players.push_back(playerSettings->color); - } - } - - if(!startAiPack.players.empty()) - gh->sendAndApply(&startAiPack); + // TODO: close network connection + +// PlayerReinitInterface startAiPack; +// startAiPack.playerConnectionId = PlayerSettings::PLAYER_AI; +// +// for(auto it = playerNames.begin(); it != playerNames.end();) +// { +// if(it->second.connection != c->connectionID) +// { +// ++it; +// continue; +// } +// +// int id = it->first; +// std::string playerLeftMsgText = boost::str(boost::format("%s (pid %d cid %d) left the game") % id % playerNames[id].name % c->connectionID); +// announceTxt(playerLeftMsgText); //send lobby text, it will be ignored for non-lobby clients +// auto * playerSettings = si->getPlayersSettings(id); +// if(!playerSettings) +// { +// ++it; +// continue; +// } +// +// it = playerNames.erase(it); +// setPlayerConnectedId(*playerSettings, PlayerSettings::PLAYER_AI); +// +// if(gh && si && state == EServerState::GAMEPLAY) +// { +// gh->playerMessages->broadcastMessage(playerSettings->color, playerLeftMsgText); +// // gh->connections[playerSettings->color].insert(hostClient); +// startAiPack.players.push_back(playerSettings->color); +// } +// } +// +// if(!startAiPack.players.empty()) +// gh->sendAndApply(&startAiPack); } void CVCMIServer::reconnectPlayer(int connId) diff --git a/server/CVCMIServer.h b/server/CVCMIServer.h index 142b6bdb2..030e44e81 100644 --- a/server/CVCMIServer.h +++ b/server/CVCMIServer.h @@ -63,9 +63,6 @@ public: std::vector> activeConnections; private: - /// List of all connections that were closed (but can still reconnect later) - std::vector> inactiveConnections; - bool restartGameplay; // FIXME: this is just a hack boost::recursive_mutex mx;