Changeset View
Standalone View
source/network/scripting/JSInterface_Network.cpp
Show First 20 Lines • Show All 45 Lines • ▼ Show 20 Lines | bool JSI_Network::HasNetClient(ScriptInterface::CmptPrivate* UNUSED(pCmptPrivate)) | |||||||||||
return !!g_NetClient; | return !!g_NetClient; | |||||||||||
} | } | |||||||||||
JS::Value JSI_Network::FindStunEndpoint(ScriptInterface::CmptPrivate* pCmptPrivate, int port) | JS::Value JSI_Network::FindStunEndpoint(ScriptInterface::CmptPrivate* pCmptPrivate, int port) | |||||||||||
{ | { | |||||||||||
return StunClient::FindStunEndpointHost(*(pCmptPrivate->pScriptInterface), port); | return StunClient::FindStunEndpointHost(*(pCmptPrivate->pScriptInterface), port); | |||||||||||
} | } | |||||||||||
void JSI_Network::StartNetworkHost(ScriptInterface::CmptPrivate* pCmptPrivate, const CStrW& playerName, const u16 serverPort, const CStr& hostLobbyName) | void JSI_Network::StartNetworkHost(ScriptInterface::CmptPrivate* pCmptPrivate, const CStrW& playerName, const u16 serverPort, const CStr& hostLobbyName, bool useSTUN,const CStr& stunIp, const u16 stunPort) | |||||||||||
Stan: I think you need the UNUSED2 macro on pCmptPrivate to make the logs happy | ||||||||||||
Not Done Inline ActionsIt's used here, it's below wraitii: It's used here, it's below | ||||||||||||
{ | { | |||||||||||
ENSURE(!g_NetClient); | ENSURE(!g_NetClient); | |||||||||||
Not Done Inline ActionsCould be constexpr? Stan: Could be constexpr? | ||||||||||||
Done Inline Actionswhat is benefit of constexpr on int literal ? Silier: what is benefit of constexpr on int literal ? | ||||||||||||
Not Done Inline ActionsOne is known at compile time the other isn't Stan: One is known at compile time the other isn't
http://www.vishalchovatiya.com/when-to-use-const… | ||||||||||||
ENSURE(!g_NetServer); | ENSURE(!g_NetServer); | |||||||||||
ENSURE(!g_Game); | ENSURE(!g_Game); | |||||||||||
// Always use lobby authentication for lobby matches to prevent impersonation and smurfing, in particular through mods that implemented an UI for arbitrary or other players nicknames. | // Always use lobby authentication for lobby matches to prevent impersonation and smurfing, in particular through mods that implemented an UI for arbitrary or other players nicknames. | |||||||||||
g_NetServer = new CNetServer(!!g_XmppClient); | g_NetServer = new CNetServer(!!g_XmppClient); | |||||||||||
if (!g_NetServer->SetupConnection(serverPort)) | if (!g_NetServer->SetupConnection(serverPort)) | |||||||||||
{ | { | |||||||||||
ScriptRequest rq(pCmptPrivate->pScriptInterface); | ScriptRequest rq(pCmptPrivate->pScriptInterface); | |||||||||||
ScriptException::Raise(rq, "Failed to start server"); | ScriptException::Raise(rq, "Failed to start server"); | |||||||||||
SAFE_DELETE(g_NetServer); | SAFE_DELETE(g_NetServer); | |||||||||||
return; | return; | |||||||||||
} | } | |||||||||||
g_NetServer->SetUseSTUN(useSTUN); | ||||||||||||
g_NetServer->SetSTUN(stunIp, stunPort); | ||||||||||||
Done Inline ActionsSo you print twice an error? Stan: So you print twice an error? | ||||||||||||
g_Game = new CGame(true); | g_Game = new CGame(true); | |||||||||||
g_NetClient = new CNetClient(g_Game, true); | g_NetClient = new CNetClient(g_Game, true); | |||||||||||
g_NetClient->SetUserName(playerName); | g_NetClient->SetUserName(playerName); | |||||||||||
g_NetClient->SetHostingPlayerName(hostLobbyName); | g_NetClient->SetHostingPlayerName(hostLobbyName); | |||||||||||
g_NetClient->SetupServerData("127.0.0.1", serverPort, false); | ||||||||||||
if (!g_NetClient->SetupConnection("127.0.0.1", serverPort, nullptr)) | if (!g_NetClient->SetupConnection(nullptr)) | |||||||||||
{ | { | |||||||||||
Done Inline ActionsQ: this seems necessary because the host shares the IP, whereas before the lobby shared the host's IP? I'm not entirely sure why we add this. I think the "We need to get public ip" should be detailed with the "why" wraitii: Q: this seems necessary because the host shares the IP, whereas before the lobby shared the… | ||||||||||||
Done Inline ActionsIf user chooses to use stun in js, it gets stun endpoint and port there and if fails, it even doesnt call this function, if he gets stun endpoint, we do not want to call it here again so we pass it here. Silier: If user chooses to use stun in js, it gets stun endpoint and port there and if fails, it even… | ||||||||||||
Done Inline Actionsand yes, host needs to know his public ip, better to do it once Silier: and yes, host needs to know his public ip, better to do it once | ||||||||||||
Done Inline ActionsI think part of my confusion is that the StunIP is queried in JS, but the non-stun IP is not. I feel like just doing everything in C++ (or everything in JS) would be simpler. wraitii: I think part of my confusion is that the StunIP is queried in JS, but the non-stun IP is not. I… | ||||||||||||
ScriptRequest rq(pCmptPrivate->pScriptInterface); | ScriptRequest rq(pCmptPrivate->pScriptInterface); | |||||||||||
ScriptException::Raise(rq, "Failed to connect to server"); | ScriptException::Raise(rq, "Failed to connect to server"); | |||||||||||
SAFE_DELETE(g_NetClient); | SAFE_DELETE(g_NetClient); | |||||||||||
SAFE_DELETE(g_Game); | SAFE_DELETE(g_Game); | |||||||||||
} | } | |||||||||||
} | } | |||||||||||
void JSI_Network::StartNetworkJoin(ScriptInterface::CmptPrivate* pCmptPrivate, const CStrW& playerName, const CStr& serverAddress, u16 serverPort, bool useSTUN, const CStr& hostJID) | void JSI_Network::StartNetworkJoin(ScriptInterface::CmptPrivate* pCmptPrivate, const CStrW& playerName, const CStr& serverAddress, u16 serverPort, bool useSTUN, const CStr& hostJID) | |||||||||||
Not Done Inline Actions
UNUSED wraitii: UNUSED | ||||||||||||
Done Inline ActionsL162 :) Silier: L162 :) | ||||||||||||
{ | { | |||||||||||
ENSURE(!g_NetClient); | ENSURE(!g_NetClient); | |||||||||||
ENSURE(!g_NetServer); | ENSURE(!g_NetServer); | |||||||||||
ENSURE(!g_Game); | ENSURE(!g_Game); | |||||||||||
ENetHost* enetClient = nullptr; | ENetHost* enetClient = nullptr; | |||||||||||
if (g_XmppClient && useSTUN) | if (g_XmppClient && useSTUN) | |||||||||||
{ | { | |||||||||||
Show All 26 Lines | if (g_XmppClient && useSTUN) | |||||||||||
SDL_Delay(1000); | SDL_Delay(1000); | |||||||||||
} | } | |||||||||||
g_Game = new CGame(true); | g_Game = new CGame(true); | |||||||||||
g_NetClient = new CNetClient(g_Game, false); | g_NetClient = new CNetClient(g_Game, false); | |||||||||||
g_NetClient->SetUserName(playerName); | g_NetClient->SetUserName(playerName); | |||||||||||
g_NetClient->SetHostingPlayerName(hostJID.substr(0, hostJID.find("@"))); | g_NetClient->SetHostingPlayerName(hostJID.substr(0, hostJID.find("@"))); | |||||||||||
g_NetClient->SetupServerData(serverAddress, serverPort, useSTUN); | ||||||||||||
if (g_XmppClient && useSTUN) | if (g_XmppClient && useSTUN) | |||||||||||
StunClient::SendHolePunchingMessages(*enetClient, serverAddress, serverPort); | StunClient::SendHolePunchingMessages(*enetClient, serverAddress, serverPort); | |||||||||||
if (!g_NetClient->SetupConnection(serverAddress, serverPort, enetClient)) | if (!g_NetClient->SetupConnection(enetClient)) | |||||||||||
{ | { | |||||||||||
ScriptRequest rq(pCmptPrivate->pScriptInterface); | ScriptRequest rq(pCmptPrivate->pScriptInterface); | |||||||||||
ScriptException::Raise(rq, "Failed to connect to server"); | ScriptException::Raise(rq, "Failed to connect to server"); | |||||||||||
SAFE_DELETE(g_NetClient); | SAFE_DELETE(g_NetClient); | |||||||||||
SAFE_DELETE(g_Game); | SAFE_DELETE(g_Game); | |||||||||||
} | } | |||||||||||
} | } | |||||||||||
/*** | ||||||||||||
Done Inline Actions
bb: | ||||||||||||
* Require XmppClient to communicate with game server. | ||||||||||||
Done Inline ActionsLikewise, I think this won't be obvious in the future. Should specify that this is because sharing IPs is done by proxy, and why this is needed. wraitii: Likewise, I think this won't be obvious in the future. Should specify that this is because… | ||||||||||||
*/ | ||||||||||||
void JSI_Network::StartNetworkJoinLobby(ScriptInterface::CmptPrivate* pCmptPrivate, const CStrW& playerName, u16 serverPort, bool useSTUN, const CStr& hostJID) | ||||||||||||
{ | ||||||||||||
ENSURE(!!g_XmppClient); | ||||||||||||
// Get ip of server | ||||||||||||
/* | ||||||||||||
const CStr serverAddress = CStr(g_XmppClient->GetGameIp(*pCmptPrivate->pScriptInterface, hostJID.substr(0, hostJID.find("@")), useSTUN)); | ||||||||||||
if (serverAddress.size() == 0) | ||||||||||||
{ | ||||||||||||
ScriptRequest rq(pCmptPrivate->pScriptInterface); | ||||||||||||
ScriptException::Raise(rq, "Could not retrieve server address."); | ||||||||||||
return; | ||||||||||||
} | ||||||||||||
StartNetworkJoin(pCmptPrivate, playerName, serverAddress, serverPort, useSTUN, hostJID); | ||||||||||||
*/ | ||||||||||||
g_Game = new CGame(true); | ||||||||||||
g_NetClient = new CNetClient(g_Game, false); | ||||||||||||
g_NetClient->SetUserName(playerName); | ||||||||||||
g_NetClient->SetHostingPlayerName(hostJID.substr(0, hostJID.find("@"))); | ||||||||||||
g_XmppClient->SendIqGetConnectionData(hostJID); | ||||||||||||
} | ||||||||||||
void JSI_Network::DisconnectNetworkGame(ScriptInterface::CmptPrivate* UNUSED(pCmptPrivate)) | void JSI_Network::DisconnectNetworkGame(ScriptInterface::CmptPrivate* UNUSED(pCmptPrivate)) | |||||||||||
{ | { | |||||||||||
// TODO: we ought to do async reliable disconnections | // TODO: we ought to do async reliable disconnections | |||||||||||
SAFE_DELETE(g_NetServer); | SAFE_DELETE(g_NetServer); | |||||||||||
SAFE_DELETE(g_NetClient); | SAFE_DELETE(g_NetClient); | |||||||||||
SAFE_DELETE(g_Game); | SAFE_DELETE(g_Game); | |||||||||||
} | } | |||||||||||
▲ Show 20 Lines • Show All 79 Lines • ▼ Show 20 Lines | ||||||||||||
} | } | |||||||||||
void JSI_Network::RegisterScriptFunctions(const ScriptInterface& scriptInterface) | void JSI_Network::RegisterScriptFunctions(const ScriptInterface& scriptInterface) | |||||||||||
{ | { | |||||||||||
scriptInterface.RegisterFunction<u16, &GetDefaultPort>("GetDefaultPort"); | scriptInterface.RegisterFunction<u16, &GetDefaultPort>("GetDefaultPort"); | |||||||||||
scriptInterface.RegisterFunction<bool, &HasNetServer>("HasNetServer"); | scriptInterface.RegisterFunction<bool, &HasNetServer>("HasNetServer"); | |||||||||||
scriptInterface.RegisterFunction<bool, &HasNetClient>("HasNetClient"); | scriptInterface.RegisterFunction<bool, &HasNetClient>("HasNetClient"); | |||||||||||
scriptInterface.RegisterFunction<JS::Value, int, &FindStunEndpoint>("FindStunEndpoint"); | scriptInterface.RegisterFunction<JS::Value, int, &FindStunEndpoint>("FindStunEndpoint"); | |||||||||||
scriptInterface.RegisterFunction<void, CStrW, u16, CStr, &StartNetworkHost>("StartNetworkHost"); | scriptInterface.RegisterFunction<void, CStrW, u16, CStr, bool, CStr, u16, &StartNetworkHost>("StartNetworkHost"); | |||||||||||
scriptInterface.RegisterFunction<void, CStrW, CStr, u16, bool, CStr, &StartNetworkJoin>("StartNetworkJoin"); | scriptInterface.RegisterFunction<void, CStrW, CStr, u16, bool, CStr, &StartNetworkJoin>("StartNetworkJoin"); | |||||||||||
scriptInterface.RegisterFunction<void, CStrW, u16, bool, CStr, &StartNetworkJoinLobby>("StartNetworkJoinLobby"); | ||||||||||||
scriptInterface.RegisterFunction<void, &DisconnectNetworkGame>("DisconnectNetworkGame"); | scriptInterface.RegisterFunction<void, &DisconnectNetworkGame>("DisconnectNetworkGame"); | |||||||||||
scriptInterface.RegisterFunction<CStr, &GetPlayerGUID>("GetPlayerGUID"); | scriptInterface.RegisterFunction<CStr, &GetPlayerGUID>("GetPlayerGUID"); | |||||||||||
scriptInterface.RegisterFunction<JS::Value, &PollNetworkClient>("PollNetworkClient"); | scriptInterface.RegisterFunction<JS::Value, &PollNetworkClient>("PollNetworkClient"); | |||||||||||
scriptInterface.RegisterFunction<void, JS::HandleValue, &SetNetworkGameAttributes>("SetNetworkGameAttributes"); | scriptInterface.RegisterFunction<void, JS::HandleValue, &SetNetworkGameAttributes>("SetNetworkGameAttributes"); | |||||||||||
scriptInterface.RegisterFunction<void, int, CStr, &AssignNetworkPlayer>("AssignNetworkPlayer"); | scriptInterface.RegisterFunction<void, int, CStr, &AssignNetworkPlayer>("AssignNetworkPlayer"); | |||||||||||
scriptInterface.RegisterFunction<void, CStrW, bool, &KickPlayer>("KickPlayer"); | scriptInterface.RegisterFunction<void, CStrW, bool, &KickPlayer>("KickPlayer"); | |||||||||||
scriptInterface.RegisterFunction<void, CStrW, &SendNetworkChat>("SendNetworkChat"); | scriptInterface.RegisterFunction<void, CStrW, &SendNetworkChat>("SendNetworkChat"); | |||||||||||
scriptInterface.RegisterFunction<void, int, &SendNetworkReady>("SendNetworkReady"); | scriptInterface.RegisterFunction<void, int, &SendNetworkReady>("SendNetworkReady"); | |||||||||||
scriptInterface.RegisterFunction<void, &ClearAllPlayerReady>("ClearAllPlayerReady"); | scriptInterface.RegisterFunction<void, &ClearAllPlayerReady>("ClearAllPlayerReady"); | |||||||||||
scriptInterface.RegisterFunction<void, &StartNetworkGame>("StartNetworkGame"); | scriptInterface.RegisterFunction<void, &StartNetworkGame>("StartNetworkGame"); | |||||||||||
scriptInterface.RegisterFunction<void, int, &SetTurnLength>("SetTurnLength"); | scriptInterface.RegisterFunction<void, int, &SetTurnLength>("SetTurnLength"); | |||||||||||
} | } |
I think you need the UNUSED2 macro on pCmptPrivate to make the logs happy