Changeset View
Changeset View
Standalone View
Standalone View
source/network/NetServer.cpp
Show All 26 Lines | |||||
#include "lib/external_libraries/enet.h" | #include "lib/external_libraries/enet.h" | ||||
#include "lib/types.h" | #include "lib/types.h" | ||||
#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/ThreadUtil.h" | |||||
#include "scriptinterface/ScriptInterface.h" | #include "scriptinterface/ScriptInterface.h" | ||||
#include "scriptinterface/ScriptRuntime.h" | #include "scriptinterface/ScriptRuntime.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> | ||||
▲ Show 20 Lines • Show All 103 Lines • ▼ Show 20 Lines | |||||
} | } | ||||
CNetServerWorker::~CNetServerWorker() | CNetServerWorker::~CNetServerWorker() | ||||
{ | { | ||||
if (m_State != SERVER_STATE_UNCONNECTED) | if (m_State != SERVER_STATE_UNCONNECTED) | ||||
{ | { | ||||
// Tell the thread to shut down | // Tell the thread to shut down | ||||
{ | { | ||||
CScopeLock lock(m_WorkerMutex); | std::lock_guard<std::mutex> lock(m_WorkerMutex); | ||||
m_Shutdown = true; | m_Shutdown = true; | ||||
} | } | ||||
// Wait for it to shut down cleanly | // Wait for it to shut down cleanly | ||||
pthread_join(m_WorkerThread, NULL); | pthread_join(m_WorkerThread, NULL); | ||||
} | } | ||||
// Clean up resources | // Clean up resources | ||||
▲ Show 20 Lines • Show All 247 Lines • ▼ Show 20 Lines | bool CNetServerWorker::RunStep() | ||||
JSAutoRequest rq(cx); | JSAutoRequest rq(cx); | ||||
std::vector<bool> newStartGame; | std::vector<bool> newStartGame; | ||||
std::vector<std::string> newGameAttributes; | std::vector<std::string> newGameAttributes; | ||||
std::vector<std::pair<CStr, CStr>> newLobbyAuths; | std::vector<std::pair<CStr, CStr>> newLobbyAuths; | ||||
std::vector<u32> newTurnLength; | std::vector<u32> newTurnLength; | ||||
{ | { | ||||
CScopeLock lock(m_WorkerMutex); | std::lock_guard<std::mutex> lock(m_WorkerMutex); | ||||
if (m_Shutdown) | if (m_Shutdown) | ||||
return false; | return false; | ||||
newStartGame.swap(m_StartGameQueue); | newStartGame.swap(m_StartGameQueue); | ||||
newGameAttributes.swap(m_GameAttributesQueue); | newGameAttributes.swap(m_GameAttributesQueue); | ||||
newLobbyAuths.swap(m_LobbyAuthQueue); | newLobbyAuths.swap(m_LobbyAuthQueue); | ||||
newTurnLength.swap(m_TurnLengthQueue); | newTurnLength.swap(m_TurnLengthQueue); | ||||
▲ Show 20 Lines • Show All 1,161 Lines • ▼ Show 20 Lines | |||||
bool CNetServer::SetupConnection(const u16 port) | bool CNetServer::SetupConnection(const u16 port) | ||||
{ | { | ||||
return m_Worker->SetupConnection(port); | return m_Worker->SetupConnection(port); | ||||
} | } | ||||
void CNetServer::StartGame() | void CNetServer::StartGame() | ||||
{ | { | ||||
CScopeLock 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::UpdateGameAttributes(JS::MutableHandleValue attrs, const ScriptInterface& scriptInterface) | void CNetServer::UpdateGameAttributes(JS::MutableHandleValue attrs, const ScriptInterface& scriptInterface) | ||||
{ | { | ||||
// 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 = scriptInterface.StringifyJSON(attrs, false); | ||||
CScopeLock lock(m_Worker->m_WorkerMutex); | std::lock_guard<std::mutex> lock(m_Worker->m_WorkerMutex); | ||||
m_Worker->m_GameAttributesQueue.push_back(attrsJSON); | m_Worker->m_GameAttributesQueue.push_back(attrsJSON); | ||||
} | } | ||||
void CNetServer::OnLobbyAuth(const CStr& name, const CStr& token) | void CNetServer::OnLobbyAuth(const CStr& name, const CStr& token) | ||||
{ | { | ||||
CScopeLock lock(m_Worker->m_WorkerMutex); | std::lock_guard<std::mutex> lock(m_Worker->m_WorkerMutex); | ||||
m_Worker->m_LobbyAuthQueue.push_back(std::make_pair(name, token)); | m_Worker->m_LobbyAuthQueue.push_back(std::make_pair(name, token)); | ||||
} | } | ||||
void CNetServer::SetTurnLength(u32 msecs) | void CNetServer::SetTurnLength(u32 msecs) | ||||
{ | { | ||||
CScopeLock lock(m_Worker->m_WorkerMutex); | std::lock_guard<std::mutex> lock(m_Worker->m_WorkerMutex); | ||||
m_Worker->m_TurnLengthQueue.push_back(msecs); | m_Worker->m_TurnLengthQueue.push_back(msecs); | ||||
} | } | ||||
void CNetServer::SendHolePunchingMessage(const CStr& ip, u16 port) | void CNetServer::SendHolePunchingMessage(const CStr& ip, u16 port) | ||||
{ | { | ||||
m_Worker->SendHolePunchingMessage(ip, port); | m_Worker->SendHolePunchingMessage(ip, port); | ||||
} | } |
Wildfire Games · Phabricator