Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/source/network/NetServer.cpp
Show All 29 Lines | |||||
#include "network/StunClient.h" | #include "network/StunClient.h" | ||||
#include "ps/CLogger.h" | #include "ps/CLogger.h" | ||||
#include "ps/ConfigDB.h" | #include "ps/ConfigDB.h" | ||||
#include "ps/GUID.h" | #include "ps/GUID.h" | ||||
#include "ps/Profile.h" | #include "ps/Profile.h" | ||||
#include "ps/Threading.h" | #include "ps/Threading.h" | ||||
#include "scriptinterface/ScriptContext.h" | #include "scriptinterface/ScriptContext.h" | ||||
#include "scriptinterface/ScriptInterface.h" | #include "scriptinterface/ScriptInterface.h" | ||||
#include "scriptinterface/JSON.h" | |||||
#include "simulation2/Simulation2.h" | #include "simulation2/Simulation2.h" | ||||
#include "simulation2/system/TurnManager.h" | #include "simulation2/system/TurnManager.h" | ||||
#if CONFIG2_MINIUPNPC | #if CONFIG2_MINIUPNPC | ||||
#include <miniupnpc/miniwget.h> | #include <miniupnpc/miniwget.h> | ||||
#include <miniupnpc/miniupnpc.h> | #include <miniupnpc/miniupnpc.h> | ||||
#include <miniupnpc/upnpcommands.h> | #include <miniupnpc/upnpcommands.h> | ||||
#include <miniupnpc/upnperrors.h> | #include <miniupnpc/upnperrors.h> | ||||
▲ Show 20 Lines • Show All 78 Lines • ▼ Show 20 Lines | virtual void OnComplete() | ||||
// Store the received state file, and tell the client to start downloading it from us | // Store the received state file, and tell the client to start downloading it from us | ||||
// TODO: this will get kind of confused if there's multiple clients downloading in parallel; | // TODO: this will get kind of confused if there's multiple clients downloading in parallel; | ||||
// they'll race and get whichever happens to be the latest received by the server, | // they'll race and get whichever happens to be the latest received by the server, | ||||
// which should still work but isn't great | // which should still work but isn't great | ||||
m_Server.m_JoinSyncFile = m_Buffer; | m_Server.m_JoinSyncFile = m_Buffer; | ||||
// Send the init attributes alongside - these should be correct since the game should be started. | // Send the init attributes alongside - these should be correct since the game should be started. | ||||
CJoinSyncStartMessage message; | CJoinSyncStartMessage message; | ||||
message.m_InitAttributes = m_Server.GetScriptInterface().StringifyJSON(&m_Server.m_InitAttributes); | message.m_InitAttributes = Script::StringifyJSON(ScriptRequest(m_Server.GetScriptInterface()), &m_Server.m_InitAttributes); | ||||
session->SendMessage(&message); | session->SendMessage(&message); | ||||
} | } | ||||
private: | private: | ||||
CNetServerWorker& m_Server; | CNetServerWorker& m_Server; | ||||
u32 m_RejoinerHostID; | u32 m_RejoinerHostID; | ||||
}; | }; | ||||
▲ Show 20 Lines • Show All 277 Lines • ▼ Show 20 Lines | void CNetServerWorker::Run() | ||||
while (true) | while (true) | ||||
{ | { | ||||
if (!RunStep()) | if (!RunStep()) | ||||
break; | break; | ||||
// Implement autostart mode | // Implement autostart mode | ||||
if (m_State == SERVER_STATE_PREGAME && (int)m_PlayerAssignments.size() == m_AutostartPlayers) | if (m_State == SERVER_STATE_PREGAME && (int)m_PlayerAssignments.size() == m_AutostartPlayers) | ||||
StartGame(m_ScriptInterface->StringifyJSON(&m_InitAttributes)); | StartGame(Script::StringifyJSON(ScriptRequest(m_ScriptInterface), &m_InitAttributes)); | ||||
// Update profiler stats | // Update profiler stats | ||||
m_Stats->LatchHostState(m_Host); | m_Stats->LatchHostState(m_Host); | ||||
} | } | ||||
// Clear roots before deleting their context | // Clear roots before deleting their context | ||||
m_SavedCommands.clear(); | m_SavedCommands.clear(); | ||||
Show All 29 Lines | bool CNetServerWorker::RunStep() | ||||
if (!newGameAttributes.empty()) | if (!newGameAttributes.empty()) | ||||
{ | { | ||||
if (m_State != SERVER_STATE_UNCONNECTED && m_State != SERVER_STATE_PREGAME) | if (m_State != SERVER_STATE_UNCONNECTED && m_State != SERVER_STATE_PREGAME) | ||||
LOGERROR("NetServer: Init Attributes cannot be changed after the server starts loading."); | LOGERROR("NetServer: Init Attributes cannot be changed after the server starts loading."); | ||||
else | else | ||||
{ | { | ||||
JS::RootedValue gameAttributesVal(rq.cx); | JS::RootedValue gameAttributesVal(rq.cx); | ||||
GetScriptInterface().ParseJSON(newGameAttributes.back(), &gameAttributesVal); | Script::ParseJSON(rq, newGameAttributes.back(), &gameAttributesVal); | ||||
m_InitAttributes = gameAttributesVal; | m_InitAttributes = gameAttributesVal; | ||||
} | } | ||||
} | } | ||||
if (!newTurnLength.empty()) | if (!newTurnLength.empty()) | ||||
SetTurnLength(newTurnLength.back()); | SetTurnLength(newTurnLength.back()); | ||||
while (!newLobbyAuths.empty()) | while (!newLobbyAuths.empty()) | ||||
▲ Show 20 Lines • Show All 1,064 Lines • ▼ Show 20 Lines | for (PlayerAssignmentMap::iterator it = m_PlayerAssignments.begin(); it != m_PlayerAssignments.end();) | ||||
if (it->second.m_Enabled) | if (it->second.m_Enabled) | ||||
++it; | ++it; | ||||
else | else | ||||
it = m_PlayerAssignments.erase(it); | it = m_PlayerAssignments.erase(it); | ||||
SendPlayerAssignments(); | SendPlayerAssignments(); | ||||
// Update init attributes. They should no longer change. | // Update init attributes. They should no longer change. | ||||
m_ScriptInterface->ParseJSON(initAttribs, &m_InitAttributes); | Script::ParseJSON(ScriptRequest(m_ScriptInterface), initAttribs, &m_InitAttributes); | ||||
CGameStartMessage gameStart; | CGameStartMessage gameStart; | ||||
gameStart.m_InitAttributes = initAttribs; | gameStart.m_InitAttributes = initAttribs; | ||||
Broadcast(&gameStart, { NSS_PREGAME }); | Broadcast(&gameStart, { NSS_PREGAME }); | ||||
} | } | ||||
CStrW CNetServerWorker::SanitisePlayerName(const CStrW& original) | CStrW CNetServerWorker::SanitisePlayerName(const CStrW& original) | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 130 Lines • ▼ Show 20 Lines | |||||
} | } | ||||
void CNetServer::StartGame() | void CNetServer::StartGame() | ||||
{ | { | ||||
std::lock_guard<std::mutex> lock(m_Worker->m_WorkerMutex); | std::lock_guard<std::mutex> lock(m_Worker->m_WorkerMutex); | ||||
m_Worker->m_StartGameQueue.push_back(true); | m_Worker->m_StartGameQueue.push_back(true); | ||||
} | } | ||||
void CNetServer::UpdateInitAttributes(JS::MutableHandleValue attrs, const ScriptInterface& scriptInterface) | void CNetServer::UpdateInitAttributes(JS::MutableHandleValue attrs, const ScriptRequest& rq) | ||||
{ | { | ||||
// Pass the attributes as JSON, since that's the easiest safe | // Pass the attributes as JSON, since that's the easiest safe | ||||
// cross-thread way of passing script data | // cross-thread way of passing script data | ||||
std::string attrsJSON = scriptInterface.StringifyJSON(attrs, false); | std::string attrsJSON = Script::StringifyJSON(rq, attrs, false); | ||||
std::lock_guard<std::mutex> lock(m_Worker->m_WorkerMutex); | std::lock_guard<std::mutex> lock(m_Worker->m_WorkerMutex); | ||||
m_Worker->m_InitAttributesQueue.push_back(attrsJSON); | m_Worker->m_InitAttributesQueue.push_back(attrsJSON); | ||||
} | } | ||||
void CNetServer::OnLobbyAuth(const CStr& name, const CStr& token) | void CNetServer::OnLobbyAuth(const CStr& name, const CStr& token) | ||||
{ | { | ||||
std::lock_guard<std::mutex> lock(m_Worker->m_WorkerMutex); | std::lock_guard<std::mutex> lock(m_Worker->m_WorkerMutex); | ||||
Show All 13 Lines |
Wildfire Games · Phabricator