diff --git a/client/CServerHandler.cpp b/client/CServerHandler.cpp index 952b6feb1..356262976 100644 --- a/client/CServerHandler.cpp +++ b/client/CServerHandler.cpp @@ -417,6 +417,9 @@ void CServerHandler::sendClientDisconnecting() logNetwork->info("Sent leaving signal to the server"); } sendLobbyPack(lcd); + + c->close(); + c.reset(); } void CServerHandler::setCampaignState(std::shared_ptr newCampaign) diff --git a/server/CVCMIServer.cpp b/server/CVCMIServer.cpp index 5b3eecdef..45dac8b32 100644 --- a/server/CVCMIServer.cpp +++ b/server/CVCMIServer.cpp @@ -211,21 +211,20 @@ void CVCMIServer::establishRemoteConnections() uuid = cmdLineOptions["lobby-uuid"].as(); int numOfConnections = cmdLineOptions["connections"].as(); - auto address = cmdLineOptions["lobby"].as(); - int port = cmdLineOptions["lobby-port"].as(); - logGlobal->info("Server is connecting to remote at %s:%d with uuid %s %d times", address, port, uuid, numOfConnections); - for(int i = 0; i < numOfConnections; ++i) - connectToRemote(address, port); + connectToRemote(); } -void CVCMIServer::connectToRemote(const std::string & addr, int port) +void CVCMIServer::connectToRemote() { std::shared_ptr c; try { - logNetwork->info("Establishing connection..."); - c = std::make_shared(addr, port, SERVER_NAME, uuid); + auto address = cmdLineOptions["lobby"].as(); + int port = cmdLineOptions["lobby-port"].as(); + + logNetwork->info("Establishing connection to remote at %s:%d with uuid %s", address, port, uuid); + c = std::make_shared(address, port, SERVER_NAME, uuid); } catch(...) { @@ -235,6 +234,7 @@ void CVCMIServer::connectToRemote(const std::string & addr, int port) if(c) { connections.insert(c); + remoteConnections.insert(c); c->handler = std::make_shared(&CVCMIServer::threadHandleClient, this, c); } } diff --git a/server/CVCMIServer.h b/server/CVCMIServer.h index 450fe3732..4e3295aa0 100644 --- a/server/CVCMIServer.h +++ b/server/CVCMIServer.h @@ -64,6 +64,7 @@ public: boost::program_options::variables_map cmdLineOptions; std::set> connections; + std::set> remoteConnections; std::set> hangingConnections; //keep connections of players disconnected during the game std::atomic currentClientId; @@ -78,7 +79,7 @@ public: void startGameImmidiately(); void establishRemoteConnections(); - void connectToRemote(const std::string & addr, int port); + void connectToRemote(); void startAsyncAccept(); void connectionAccepted(const boost::system::error_code & ec); void threadHandleClient(std::shared_ptr c); diff --git a/server/NetPacksLobbyServer.cpp b/server/NetPacksLobbyServer.cpp index dbb68d617..804874f94 100644 --- a/server/NetPacksLobbyServer.cpp +++ b/server/NetPacksLobbyServer.cpp @@ -189,6 +189,12 @@ void ApplyOnServerAfterAnnounceNetPackVisitor::visitLobbyClientDisconnected(Lobb srv.addToAnnounceQueue(std::move(ph)); } srv.updateAndPropagateLobbyState(); + + if(srv.getState() != EServerState::SHUTDOWN && srv.remoteConnections.count(pack.c)) + { + srv.remoteConnections -= pack.c; + srv.connectToRemote(); + } } void ClientPermissionsCheckerNetPackVisitor::visitLobbyChatMessage(LobbyChatMessage & pack)