Changeset View
Standalone View
source/network/NetClient.cpp
Show All 19 Lines | |||||
#include "NetClient.h" | #include "NetClient.h" | ||||
#include "NetClientTurnManager.h" | #include "NetClientTurnManager.h" | ||||
#include "NetMessage.h" | #include "NetMessage.h" | ||||
#include "NetSession.h" | #include "NetSession.h" | ||||
#include "lib/byte_order.h" | #include "lib/byte_order.h" | ||||
#include "lib/external_libraries/enet.h" | #include "lib/external_libraries/enet.h" | ||||
#include "lib/external_libraries/libsdl.h" | |||||
#include "lib/sysdep/sysdep.h" | #include "lib/sysdep/sysdep.h" | ||||
#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 "scriptinterface/ScriptInterface.h" | #include "scriptinterface/ScriptInterface.h" | ||||
#include "simulation2/Simulation2.h" | #include "simulation2/Simulation2.h" | ||||
#include "network/StunClient.h" | |||||
CNetClient *g_NetClient = NULL; | CNetClient *g_NetClient = NULL; | ||||
Silier: ok, need to solve this one | |||||
/** | /** | ||||
* Async task for receiving the initial game state when rejoining an | * Async task for receiving the initial game state when rejoining an | ||||
* in-progress network game. | * in-progress network game. | ||||
*/ | */ | ||||
class CNetFileReceiveTask_ClientRejoin : public CNetFileReceiveTask | class CNetFileReceiveTask_ClientRejoin : public CNetFileReceiveTask | ||||
{ | { | ||||
NONCOPYABLE(CNetFileReceiveTask_ClientRejoin); | NONCOPYABLE(CNetFileReceiveTask_ClientRejoin); | ||||
Show All 21 Lines | |||||
CNetClient::CNetClient(CGame* game, bool isLocalClient) : | CNetClient::CNetClient(CGame* game, bool isLocalClient) : | ||||
m_Session(NULL), | m_Session(NULL), | ||||
m_UserName(L"anonymous"), | m_UserName(L"anonymous"), | ||||
m_HostID((u32)-1), m_ClientTurnManager(NULL), m_Game(game), | m_HostID((u32)-1), m_ClientTurnManager(NULL), m_Game(game), | ||||
m_GameAttributes(game->GetSimulation2()->GetScriptInterface().GetGeneralJSContext()), | m_GameAttributes(game->GetSimulation2()->GetScriptInterface().GetGeneralJSContext()), | ||||
m_IsLocalClient(isLocalClient), | m_IsLocalClient(isLocalClient), | ||||
m_LastConnectionCheck(0), | m_LastConnectionCheck(0), | ||||
m_ServerAddress(""), | |||||
m_ServerPort(0), | |||||
m_Rejoin(false) | m_Rejoin(false) | ||||
{ | { | ||||
m_Game->SetTurnManager(NULL); // delete the old local turn manager so we don't accidentally use it | m_Game->SetTurnManager(NULL); // delete the old local turn manager so we don't accidentally use it | ||||
void* context = this; | void* context = this; | ||||
JS_AddExtraGCRootsTracer(GetScriptInterface().GetGeneralJSContext(), CNetClient::Trace, this); | JS_AddExtraGCRootsTracer(GetScriptInterface().GetGeneralJSContext(), CNetClient::Trace, this); | ||||
▲ Show 20 Lines • Show All 75 Lines • ▼ Show 20 Lines | void CNetClient::SetUserName(const CStrW& username) | ||||
m_UserName = username; | m_UserName = username; | ||||
} | } | ||||
void CNetClient::SetHostingPlayerName(const CStr& hostingPlayerName) | void CNetClient::SetHostingPlayerName(const CStr& hostingPlayerName) | ||||
{ | { | ||||
m_HostingPlayerName = hostingPlayerName; | m_HostingPlayerName = hostingPlayerName; | ||||
} | } | ||||
bool CNetClient::SetupConnection(const CStr& server, const u16 port, ENetHost* enetClient) | bool CNetClient::SetupConnection(ENetHost* enetClient) | ||||
{ | { | ||||
Not Done Inline ActionsLikewise, I'd ENSURE(!m_serverAddress.empty()) wraitii: Likewise, I'd `ENSURE(!m_serverAddress.empty())` | |||||
Done Inline Actionsvs does not like it Silier: vs does not like it | |||||
CNetClientSession* session = new CNetClientSession(*this); | CNetClientSession* session = new CNetClientSession(*this); | ||||
bool ok = session->Connect(server, port, m_IsLocalClient, enetClient); | bool ok = session->Connect(m_ServerAddress, m_ServerPort, m_IsLocalClient, enetClient); | ||||
SetAndOwnSession(session); | SetAndOwnSession(session); | ||||
return ok; | return ok; | ||||
} | } | ||||
void CNetClient::SetupServerData(CStr address, u16 port, bool stun) | |||||
{ | |||||
if (m_Session) | |||||
return; | |||||
Done Inline ActionsI would ENSURE (!m_Session), this is called from C++ code only. wraitii: I would `ENSURE (!m_Session)`, this is called from C++ code only. | |||||
m_ServerAddress = address; | |||||
m_ServerPort = port; | |||||
m_UseSTUN = stun; | |||||
} | |||||
bool CNetClient::TryToConnect(const CStr& hostJID) | |||||
Not Done Inline ActionsThis method and the onr below could be const I think Stan: This method and the onr below could be const I think | |||||
Done Inline Actionscant, pushguimessage is function of CNetClient and cant be const Silier: cant, pushguimessage is function of CNetClient and cant be const | |||||
{ | |||||
if (m_Session) | |||||
return false; | |||||
if (m_ServerAddress.empty()) | |||||
{ | |||||
Done Inline Actionsnuke Silier: nuke | |||||
PushGuiMessage( | |||||
"type", "netstatus", | |||||
"status", "disconnected", | |||||
"reason", static_cast<i32>(NDR_SERVER_REFUSED)); | |||||
return false; | |||||
} | |||||
Not Done Inline ActionsI think you should use this function elsewhere, as you're introducing duplication otherwise. wraitii: I think you should use this function elsewhere, as you're introducing duplication otherwise. | |||||
ENetHost* enetClient = nullptr; | |||||
if (g_XmppClient && m_UseSTUN) | |||||
Done Inline Actionsnuke Silier: nuke | |||||
{ | |||||
// Find an unused port | |||||
for (int i = 0; i < 5 && !enetClient; ++i) | |||||
{ | |||||
// Ports below 1024 are privileged on unix | |||||
StanUnsubmitted Not Done Inline ActionsIsn't it above? also I believe Unix takes a capitalized letter. Stan: Isn't it above? also I believe Unix takes a capitalized letter. | |||||
SilierAuthorUnsubmitted Done Inline Actionswhat do you mean by above? Silier: what do you mean by above? | |||||
StanUnsubmitted Not Done Inline ActionsI mean 1024 + something is usually above 1024 no? Stan: I mean 1024 + something is usually above 1024 no? | |||||
u16 port = 1024 + rand() % (UINT16_MAX - 1024); | |||||
ENetAddress hostAddr{ ENET_HOST_ANY, port }; | |||||
enetClient = enet_host_create(&hostAddr, 1, 1, 0, 0); | |||||
++hostAddr.port; | |||||
Done Inline Actionsgui message Silier: gui message | |||||
} | |||||
if (!enetClient) | |||||
{ | |||||
PushGuiMessage( | |||||
"type", "netstatus", | |||||
"status", "disconnected", | |||||
"reason", static_cast<i32>(NDR_STUN_PORT_FAILED)); | |||||
return false; | |||||
} | |||||
Done Inline Actionsgui message Silier: gui message | |||||
StunClient::StunEndpoint stunEndpoint; | |||||
if (!StunClient::FindStunEndpointJoin(*enetClient, stunEndpoint)) | |||||
Not Done Inline ActionsNo need to search for a port for the client. The code seems weird. vladislavbelov: No need to search for a port for the client. The code seems weird. | |||||
Done Inline Actionssee JSI_Network::StartNetworkJoin Silier: see JSI_Network::StartNetworkJoin | |||||
{ | |||||
Done Inline Actionsnukr Silier: nukr | |||||
PushGuiMessage( | |||||
"type", "netstatus", | |||||
"status", "disconnected", | |||||
Done Inline Actionsnuke Silier: nuke | |||||
"reason", static_cast<i32>(NDR_STUN_ENDPOINT_FAILED)); | |||||
return false; | |||||
} | |||||
Done Inline Actionsnuke Silier: nuke | |||||
g_XmppClient->SendStunEndpointToHost(stunEndpoint, hostJID); | |||||
SDL_Delay(1000); | |||||
Not Done Inline ActionsNot sure, but can't you use the state machine or so for that. Imarok: Not sure, but can't you use the state machine or so for that.
Blocking the whole game for one… | |||||
Done Inline Actionsgui message Silier: gui message | |||||
StunClient::SendHolePunchingMessages(*enetClient, m_ServerAddress, m_ServerPort); | |||||
} | |||||
if (!g_NetClient->SetupConnection(enetClient)) | |||||
{ | |||||
PushGuiMessage( | |||||
"type", "netstatus", | |||||
"status", "disconnected", | |||||
"reason", static_cast<i32>(NDR_UNKNOWN)); | |||||
return false; | |||||
} | |||||
Not Done Inline ActionsWhy so? And on which thread? vladislavbelov: Why so? And on which thread? | |||||
Done Inline ActionsBecause we need to wait for answer Silier: Because we need to wait for answer
ref: JSI_Network::StartNetworkJoin | |||||
Not Done Inline ActionsI assume there is no callback? Stan: I assume there is no callback? | |||||
return true; | |||||
} | |||||
void CNetClient::SetAndOwnSession(CNetClientSession* session) | void CNetClient::SetAndOwnSession(CNetClientSession* session) | ||||
{ | { | ||||
delete m_Session; | delete m_Session; | ||||
m_Session = session; | m_Session = session; | ||||
} | } | ||||
void CNetClient::DestroyConnection() | void CNetClient::DestroyConnection() | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 678 Lines • Show Last 20 Lines |
ok, need to solve this one