1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-06-23 00:28:08 +02:00

Do not start listening for connections in single-player games

This commit is contained in:
Ivan Savenko
2025-02-02 19:11:05 +00:00
parent d9244cf061
commit 048fc503c3
7 changed files with 35 additions and 28 deletions

View File

@ -186,7 +186,7 @@ void CServerHandler::startLocalServerAndConnect(bool connectToLobby)
si->difficulty = lastDifficulty.Integer(); si->difficulty = lastDifficulty.Integer();
logNetwork->trace("\tStarting local server"); logNetwork->trace("\tStarting local server");
uint16_t srvport = serverRunner->start(getLocalPort(), connectToLobby, si); uint16_t srvport = serverRunner->start(getLocalPort(), loadMode == ELoadMode::MULTI, connectToLobby, si);
logNetwork->trace("\tConnecting to local server"); logNetwork->trace("\tConnecting to local server");
connectToServer(getLocalHostname(), srvport); connectToServer(getLocalHostname(), srvport);
logNetwork->trace("\tWaiting for connection"); logNetwork->trace("\tWaiting for connection");

View File

@ -34,7 +34,7 @@
ServerThreadRunner::ServerThreadRunner() = default; ServerThreadRunner::ServerThreadRunner() = default;
ServerThreadRunner::~ServerThreadRunner() = default; ServerThreadRunner::~ServerThreadRunner() = default;
uint16_t ServerThreadRunner::start(uint16_t cfgport, bool connectToLobby, std::shared_ptr<StartInfo> startingInfo) uint16_t ServerThreadRunner::start(uint16_t cfgport, bool listenForConnections, bool connectToLobby, std::shared_ptr<StartInfo> startingInfo)
{ {
// cfgport may be 0 -- the real port is returned after calling prepare() // cfgport may be 0 -- the real port is returned after calling prepare()
server = std::make_unique<CVCMIServer>(cfgport, true); server = std::make_unique<CVCMIServer>(cfgport, true);
@ -46,9 +46,9 @@ uint16_t ServerThreadRunner::start(uint16_t cfgport, bool connectToLobby, std::s
std::promise<uint16_t> promise; std::promise<uint16_t> promise;
threadRunLocalServer = boost::thread([this, connectToLobby, &promise]{ threadRunLocalServer = boost::thread([this, connectToLobby, listenForConnections, &promise]{
setThreadName("runServer"); setThreadName("runServer");
uint16_t port = server->prepare(connectToLobby); uint16_t port = server->prepare(connectToLobby, listenForConnections);
promise.set_value(port); promise.set_value(port);
server->run(); server->run();
}); });
@ -100,7 +100,7 @@ int ServerProcessRunner::exitCode()
return child->exit_code(); return child->exit_code();
} }
uint16_t ServerProcessRunner::start(uint16_t port, bool connectToLobby, std::shared_ptr<StartInfo> startingInfo) uint16_t ServerProcessRunner::start(uint16_t port, bool listenForConnections, bool connectToLobby, std::shared_ptr<StartInfo> startingInfo)
{ {
boost::filesystem::path serverPath = VCMIDirs::get().serverPath(); boost::filesystem::path serverPath = VCMIDirs::get().serverPath();
boost::filesystem::path logPath = VCMIDirs::get().userLogsPath() / "server_log.txt"; boost::filesystem::path logPath = VCMIDirs::get().userLogsPath() / "server_log.txt";

View File

@ -22,7 +22,7 @@ class CVCMIServer;
class IServerRunner class IServerRunner
{ {
public: public:
virtual uint16_t start(uint16_t port, bool connectToLobby, std::shared_ptr<StartInfo> startingInfo) = 0; virtual uint16_t start(uint16_t port, bool listenForConnections, bool connectToLobby, std::shared_ptr<StartInfo> startingInfo) = 0;
virtual void shutdown() = 0; virtual void shutdown() = 0;
virtual void wait() = 0; virtual void wait() = 0;
virtual int exitCode() = 0; virtual int exitCode() = 0;
@ -38,7 +38,7 @@ class ServerThreadRunner final : public IServerRunner, boost::noncopyable
std::unique_ptr<CVCMIServer> server; std::unique_ptr<CVCMIServer> server;
boost::thread threadRunLocalServer; boost::thread threadRunLocalServer;
public: public:
uint16_t start(uint16_t port, bool connectToLobby, std::shared_ptr<StartInfo> startingInfo) override; uint16_t start(uint16_t port, bool listenForConnections, bool connectToLobby, std::shared_ptr<StartInfo> startingInfo) override;
void shutdown() override; void shutdown() override;
void wait() override; void wait() override;
int exitCode() override; int exitCode() override;
@ -75,7 +75,7 @@ class ServerProcessRunner final : public IServerRunner, boost::noncopyable
std::unique_ptr<boost::process::child> child; std::unique_ptr<boost::process::child> child;
public: public:
uint16_t start(uint16_t port, bool connectToLobby, std::shared_ptr<StartInfo> startingInfo) override; uint16_t start(uint16_t port, bool listenForConnections, bool connectToLobby, std::shared_ptr<StartInfo> startingInfo) override;
void shutdown() override; void shutdown() override;
void wait() override; void wait() override;
int exitCode() override; int exitCode() override;

View File

@ -80,26 +80,33 @@ CVCMIServer::CVCMIServer(uint16_t port, bool runByClient)
CVCMIServer::~CVCMIServer() = default; CVCMIServer::~CVCMIServer() = default;
uint16_t CVCMIServer::prepare(bool connectToLobby) { uint16_t CVCMIServer::prepare(bool connectToLobby, bool listenForConnections) {
if(connectToLobby) { if(connectToLobby) {
lobbyProcessor = std::make_unique<GlobalLobbyProcessor>(*this); lobbyProcessor = std::make_unique<GlobalLobbyProcessor>(*this);
return 0; return 0;
} else { } else {
return startAcceptingIncomingConnections(); return startAcceptingIncomingConnections(listenForConnections);
} }
} }
uint16_t CVCMIServer::startAcceptingIncomingConnections() uint16_t CVCMIServer::startAcceptingIncomingConnections(bool listenForConnections)
{ {
networkServer = networkHandler->createServerTCP(*this);
port port
? logNetwork->info("Port %d will be used", port) ? logNetwork->info("Port %d will be used", port)
: logNetwork->info("Randomly assigned port will be used"); : logNetwork->info("Randomly assigned port will be used");
// config port may be 0 => srvport will contain the OS-assigned port value // config port may be 0 => srvport will contain the OS-assigned port value
networkServer = networkHandler->createServerTCP(*this);
if (listenForConnections)
{
auto srvport = networkServer->start(port); auto srvport = networkServer->start(port);
logNetwork->info("Listening for connections at port %d", srvport); logNetwork->info("Listening for connections at port %d", srvport);
return srvport; return srvport;
}
else
return 0;
} }
void CVCMIServer::onNewConnection(const std::shared_ptr<INetworkConnection> & connection) void CVCMIServer::onNewConnection(const std::shared_ptr<INetworkConnection> & connection)

View File

@ -63,7 +63,7 @@ public:
/// List of all active connections /// List of all active connections
std::vector<std::shared_ptr<CConnection>> activeConnections; std::vector<std::shared_ptr<CConnection>> activeConnections;
uint16_t prepare(bool connectToLobby); uint16_t prepare(bool connectToLobby, bool listenForConnections);
// INetworkListener impl // INetworkListener impl
void onDisconnected(const std::shared_ptr<INetworkConnection> & connection, const std::string & errorMessage) override; void onDisconnected(const std::shared_ptr<INetworkConnection> & connection, const std::string & errorMessage) override;
@ -82,7 +82,7 @@ public:
bool prepareToStartGame(); bool prepareToStartGame();
void prepareToRestart(); void prepareToRestart();
void startGameImmediately(); void startGameImmediately();
uint16_t startAcceptingIncomingConnections(); uint16_t startAcceptingIncomingConnections(bool listenForConnections);
void threadHandleClient(std::shared_ptr<CConnection> c); void threadHandleClient(std::shared_ptr<CConnection> c);

View File

@ -99,7 +99,7 @@ void GlobalLobbyProcessor::receiveServerLoginSuccess(const JsonNode & json)
{ {
// no-op, wait just for any new commands from lobby // no-op, wait just for any new commands from lobby
logGlobal->info("Lobby: Successfully connected to lobby server"); logGlobal->info("Lobby: Successfully connected to lobby server");
owner.startAcceptingIncomingConnections(); owner.startAcceptingIncomingConnections(true);
} }
void GlobalLobbyProcessor::receiveAccountJoinsRoom(const JsonNode & json) void GlobalLobbyProcessor::receiveAccountJoinsRoom(const JsonNode & json)

View File

@ -92,7 +92,7 @@ int main(int argc, const char * argv[])
port = opts["port"].as<uint16_t>(); port = opts["port"].as<uint16_t>();
CVCMIServer server(port, runByClient); CVCMIServer server(port, runByClient);
server.prepare(connectToLobby); server.prepare(connectToLobby, true);
server.run(); server.run();
// CVCMIServer destructor must be called here - before VLC cleanup // CVCMIServer destructor must be called here - before VLC cleanup