Changeset View
Changeset View
Standalone View
Standalone View
source/network/NetClient.cpp
Show All 29 Lines | |||||
#include "lobby/IXmppClient.h" | #include "lobby/IXmppClient.h" | ||||
#include "ps/CConsole.h" | #include "ps/CConsole.h" | ||||
#include "ps/CLogger.h" | #include "ps/CLogger.h" | ||||
#include "ps/Compress.h" | #include "ps/Compress.h" | ||||
#include "ps/CStr.h" | #include "ps/CStr.h" | ||||
#include "ps/Game.h" | #include "ps/Game.h" | ||||
#include "ps/Loader.h" | #include "ps/Loader.h" | ||||
#include "ps/Profile.h" | #include "ps/Profile.h" | ||||
#include "ps/Threading.h" | |||||
#include "scriptinterface/ScriptInterface.h" | #include "scriptinterface/ScriptInterface.h" | ||||
#include "simulation2/Simulation2.h" | #include "simulation2/Simulation2.h" | ||||
#include "network/StunClient.h" | #include "network/StunClient.h" | ||||
/** | /** | ||||
* Once ping goes above turn length * command delay, | * Once ping goes above turn length * command delay, | ||||
* the game will start 'freezing' for other clients while we catch up. | * the game will start 'freezing' for other clients while we catch up. | ||||
* Since commands are sent client -> server -> client, divide by 2. | * Since commands are sent client -> server -> client, divide by 2. | ||||
▲ Show 20 Lines • Show All 139 Lines • ▼ Show 20 Lines | void CNetClient::SetGamePassword(const CStr& hashedPassword) | ||||
m_Password = hashedPassword; | m_Password = hashedPassword; | ||||
} | } | ||||
void CNetClient::SetControllerSecret(const std::string& secret) | void CNetClient::SetControllerSecret(const std::string& secret) | ||||
{ | { | ||||
m_ControllerSecret = secret; | m_ControllerSecret = secret; | ||||
} | } | ||||
bool CNetClient::SetupConnection(ENetHost* enetClient) | bool CNetClient::SetupConnection(ENetHost* enetClient) | ||||
{ | { | ||||
CNetClientSession* session = new CNetClientSession(*this); | CNetClientSession* session = new CNetClientSession(*this); | ||||
bool ok = session->Connect(m_ServerAddress, m_ServerPort, enetClient); | bool ok = session->Connect(m_ServerAddress, m_ServerPort, enetClient); | ||||
SetAndOwnSession(session); | SetAndOwnSession(session); | ||||
m_PollingThread = std::thread(Threading::HandleExceptions<CNetClientSession::RunNetLoop>::Wrapper, m_Session); | CNetClientSession::StartRecurrentTask(session); | ||||
return ok; | return ok; | ||||
} | } | ||||
void CNetClient::SetupServerData(CStr address, u16 port, bool stun) | void CNetClient::SetupServerData(CStr address, u16 port, bool stun) | ||||
{ | { | ||||
ENSURE(!m_Session); | ENSURE(!m_Session); | ||||
m_ServerAddress = address; | m_ServerAddress = address; | ||||
▲ Show 20 Lines • Show All 83 Lines • ▼ Show 20 Lines | |||||
{ | { | ||||
delete m_Session; | delete m_Session; | ||||
m_Session = session; | m_Session = session; | ||||
} | } | ||||
void CNetClient::DestroyConnection() | void CNetClient::DestroyConnection() | ||||
{ | { | ||||
if (m_Session) | if (m_Session) | ||||
{ | |||||
// This deletes session on its own. | |||||
m_Session->Shutdown(); | m_Session->Shutdown(); | ||||
if (m_PollingThread.joinable()) | |||||
// Use detach() over join() because we don't want to wait for the session | |||||
// (which may be polling or trying to send messages). | |||||
m_PollingThread.detach(); | |||||
// The polling thread will cleanup the session on its own, | |||||
// mark it as nullptr here so we know we're done using it. | |||||
m_Session = nullptr; | m_Session = nullptr; | ||||
} | } | ||||
} | |||||
void CNetClient::Poll() | void CNetClient::Poll() | ||||
{ | { | ||||
if (!m_Session) | if (!m_Session) | ||||
return; | return; | ||||
PROFILE3("NetClient::poll"); | PROFILE3("NetClient::poll"); | ||||
▲ Show 20 Lines • Show All 652 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator