FindPublicIP in the CNetServer constructor

When using the lobby, after constructing `CNetServer` the public IP has
to be queried. The consructor knows whether the lobby is used, so do it
in the constructor.
This commit is contained in:
phosit
2026-03-03 21:20:33 +01:00
parent bc17e212bb
commit d5384ad742
3 changed files with 8 additions and 18 deletions
+8 -8
View File
@@ -1675,6 +1675,14 @@ CNetServer::CNetServer(const bool continueSavedGame, std::uint16_t port, const b
m_LobbyAuth{useLobbyAuth},
m_Password{std::move(password)}
{
if (!useLobbyAuth)
return;
// In lobby, we send our public ip and port on request to the players who want to connect.
// Thus we need to know our public IP and use STUN to get it.
std::lock_guard<std::mutex> lock(m_Worker->m_WorkerMutex);
if (!m_Worker->m_Host || !StunClient::FindPublicIP(*m_Worker->m_Host, m_PublicIp, m_PublicPort))
throw std::runtime_error{"Failed to resolve public IP-address."};
}
CNetServer::~CNetServer()
@@ -1705,14 +1713,6 @@ u16 CNetServer::GetLocalPort() const
return m_Worker->m_Host->address.port;
}
bool CNetServer::SetConnectionData()
{
std::lock_guard<std::mutex> lock(m_Worker->m_WorkerMutex);
if (!m_Worker->m_Host)
return false;
return StunClient::FindPublicIP(*m_Worker->m_Host, m_PublicIp, m_PublicPort);
}
bool CNetServer::CheckPasswordAndIncrement(const std::string& username, const std::string& password, const std::string& salt)
{
std::unordered_map<std::string, int>::iterator it = m_FailedAttempts.find(username);
-2
View File
@@ -130,8 +130,6 @@ public:
void SendHolePunchingMessage(const CStr& ip, u16 port);
bool SetConnectionData();
/**
* Return the externally accessible IP.
*/
@@ -108,14 +108,6 @@ void StartNetworkHost(const CStrW& playerName, const u16 serverPort, const CStr&
const std::string secret = ps_generate_guid();
g_NetServer = new CNetServer(continueSavedGame, serverPort, hasLobby, hashedPassword, secret);
// In lobby, we send our public ip and port on request to the players who want to connect.
// Thus we need to know our public IP and use STUN to get it.
if (hasLobby && !g_NetServer->SetConnectionData())
{
SAFE_DELETE(g_NetServer);
throw std::runtime_error{"Failed to resolve public IP-address."};
}
// Generate a secret to identify the host client.
g_Game = new CGame(storeReplay);