Remove UpdateInitAttributes from CNetClient

The init-attributes can only be changed before the worker runs. It isn't
used before the worker runs. This changes it so that it has to be passed
when the server starts.
With this the `m_InitAttributesQueue` can be removed.
This commit is contained in:
phosit
2026-03-03 19:09:17 +01:00
parent 578aea3b09
commit 314e2a58cf
3 changed files with 26 additions and 52 deletions
+16 -31
View File
@@ -162,7 +162,7 @@ bool CNetServerWorker::CheckPassword(const std::string& password, const std::str
}
bool CNetServerWorker::SetupConnection(const u16 port)
bool CNetServerWorker::SetupConnection(const u16 port, std::string initAttributes)
{
ENSURE(m_State == SERVER_STATE_UNCONNECTED);
ENSURE(!m_Host);
@@ -187,7 +187,8 @@ bool CNetServerWorker::SetupConnection(const u16 port)
m_State = SERVER_STATE_PREGAME;
// Launch the worker thread
m_WorkerThread = std::thread(Threading::HandleExceptions<RunThread>::Wrapper, this);
m_WorkerThread = std::thread{Threading::HandleExceptions<RunThread>::Wrapper, this,
std::move(initAttributes)};
#if CONFIG2_MINIUPNPC
// Launch the UPnP thread
@@ -383,14 +384,14 @@ bool CNetServerWorker::Multicast(const CNetMessage* message,
return ok;
}
void CNetServerWorker::RunThread(CNetServerWorker* data)
void CNetServerWorker::RunThread(CNetServerWorker* data, const std::string& initAttributes)
{
debug_SetThreadName("NetServer");
data->Run();
data->Run(initAttributes);
}
void CNetServerWorker::Run()
void CNetServerWorker::Run(const std::string& initAttributes)
{
// The script context uses the profiler and therefore the thread must be registered before the context is created
g_Profiler2.RegisterCurrentThread("Net server");
@@ -400,6 +401,14 @@ void CNetServerWorker::Run()
m_ScriptInterface = new ScriptInterface("Engine", "Net server", netServerContext);
m_InitAttributes.init(m_ScriptInterface->GetGeneralJSContext(), JS::UndefinedValue());
if (!initAttributes.empty())
{
ScriptRequest rq(m_ScriptInterface);
JS::RootedValue gameAttributesVal(rq.cx);
Script::ParseJSON(rq, std::move(initAttributes), &gameAttributesVal);
m_InitAttributes = gameAttributesVal;
}
while (true)
{
if (!RunStep())
@@ -426,7 +435,6 @@ bool CNetServerWorker::RunStep()
ScriptRequest rq(m_ScriptInterface);
std::vector<bool> newStartGame;
std::vector<std::string> newGameAttributes;
std::vector<std::pair<CStr, CStr>> newLobbyAuths;
std::vector<u32> newTurnLength;
@@ -437,23 +445,10 @@ bool CNetServerWorker::RunStep()
return false;
newStartGame.swap(m_StartGameQueue);
newGameAttributes.swap(m_InitAttributesQueue);
newLobbyAuths.swap(m_LobbyAuthQueue);
newTurnLength.swap(m_TurnLengthQueue);
}
if (!newGameAttributes.empty())
{
if (m_State != SERVER_STATE_UNCONNECTED && m_State != SERVER_STATE_PREGAME)
LOGERROR("NetServer: Init Attributes cannot be changed after the server starts loading.");
else
{
JS::RootedValue gameAttributesVal(rq.cx);
Script::ParseJSON(rq, newGameAttributes.back(), &gameAttributesVal);
m_InitAttributes = gameAttributesVal;
}
}
if (!newTurnLength.empty())
SetTurnLength(newTurnLength.back());
@@ -1703,9 +1698,9 @@ bool CNetServer::UseLobbyAuth() const
return m_LobbyAuth;
}
bool CNetServer::SetupConnection(const u16 port)
bool CNetServer::SetupConnection(const u16 port, std::string initAttributes)
{
return m_Worker->SetupConnection(port);
return m_Worker->SetupConnection(port, std::move(initAttributes));
}
CStr CNetServer::GetPublicIp() const
@@ -1762,16 +1757,6 @@ void CNetServer::StartGame()
m_Worker->m_StartGameQueue.push_back(true);
}
void CNetServer::UpdateInitAttributes(JS::MutableHandleValue attrs, const ScriptRequest& rq)
{
// Pass the attributes as JSON, since that's the easiest safe
// cross-thread way of passing script data
std::string attrsJSON = Script::StringifyJSON(rq, attrs, false);
std::lock_guard<std::mutex> lock(m_Worker->m_WorkerMutex);
m_Worker->m_InitAttributesQueue.push_back(attrsJSON);
}
void CNetServer::OnLobbyAuth(const CStr& name, const CStr& token)
{
std::lock_guard<std::mutex> lock(m_Worker->m_WorkerMutex);