Changeset View
Changeset View
Standalone View
Standalone View
source/network/NetClient.cpp
Show All 16 Lines | |||||
#include "precompiled.h" | #include "precompiled.h" | ||||
#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 "NetServer.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/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" | ||||
▲ Show 20 Lines • Show All 44 Lines • ▼ Show 20 Lines | virtual void OnComplete() | ||||
m_Client.HandleMessage(&start); | m_Client.HandleMessage(&start); | ||||
} | } | ||||
private: | private: | ||||
CNetClient& m_Client; | CNetClient& m_Client; | ||||
CStr m_InitAttributes; | CStr m_InitAttributes; | ||||
}; | }; | ||||
CNetClient::CNetClient(CGame* game) : | CNetClient::CNetClient(CGame* game) : CNetClient(game, game->GetSimulation2()->GetScriptInterface()) {} | ||||
CNetClient::CNetClient(CGame* game, const ScriptInterface& scriptInterface) : | |||||
m_Session(NULL), | m_Session(NULL), | ||||
m_ScriptInterface(scriptInterface), | |||||
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_LastConnectionCheck(0), | m_LastConnectionCheck(0), | ||||
m_ServerAddress(), | m_ServerAddress(), | ||||
m_ServerPort(0), | m_ServerPort(0), | ||||
m_Rejoin(false) | m_Rejoin(false) | ||||
{ | { | ||||
if (m_Game) | |||||
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); | ||||
// Set up transitions for session | // Set up transitions for session | ||||
AddTransition(NCS_UNCONNECTED, (uint)NMT_CONNECT_COMPLETE, NCS_CONNECT, (void*)&OnConnect, context); | AddTransition(NCS_UNCONNECTED, (uint)NMT_CONNECT_COMPLETE, NCS_CONNECT, (void*)&OnConnect, context); | ||||
▲ Show 20 Lines • Show All 286 Lines • ▼ Show 20 Lines | if (msg.isUndefined()) | ||||
break; | break; | ||||
r += Script::ToString(rq, &msg) + "\n"; | r += Script::ToString(rq, &msg) + "\n"; | ||||
} | } | ||||
return r; | return r; | ||||
} | } | ||||
const ScriptInterface& CNetClient::GetScriptInterface() | const ScriptInterface& CNetClient::GetScriptInterface() | ||||
{ | { | ||||
return m_Game->GetSimulation2()->GetScriptInterface(); | return m_ScriptInterface; | ||||
} | } | ||||
void CNetClient::PostPlayerAssignmentsToScript() | void CNetClient::PostPlayerAssignmentsToScript() | ||||
{ | { | ||||
ScriptRequest rq(GetScriptInterface()); | ScriptRequest rq(GetScriptInterface()); | ||||
JS::RootedValue newAssignments(rq.cx); | JS::RootedValue newAssignments(rq.cx); | ||||
Script::CreateObject(rq, &newAssignments); | Script::CreateObject(rq, &newAssignments); | ||||
▲ Show 20 Lines • Show All 181 Lines • ▼ Show 20 Lines | void CNetClient::LoadFinished() | ||||
{ | { | ||||
// Connecting at the start of a game, so we'll wait for other players to finish loading | // Connecting at the start of a game, so we'll wait for other players to finish loading | ||||
PushGuiMessage( | PushGuiMessage( | ||||
"type", "netstatus", | "type", "netstatus", | ||||
"status", "waiting_for_players"); | "status", "waiting_for_players"); | ||||
} | } | ||||
CLoadedGameMessage loaded; | CLoadedGameMessage loaded; | ||||
loaded.m_CurrentTurn = m_ClientTurnManager->GetCurrentTurn(); | loaded.m_CurrentTurn = m_ClientTurnManager ? m_ClientTurnManager->GetCurrentTurn() : 0; | ||||
SendMessage(&loaded); | SendMessage(&loaded); | ||||
} | } | ||||
void CNetClient::SendAuthenticateMessage() | void CNetClient::SendAuthenticateMessage() | ||||
{ | { | ||||
CAuthenticateMessage authenticate; | CAuthenticateMessage authenticate; | ||||
authenticate.m_Name = m_UserName; | authenticate.m_Name = m_UserName; | ||||
authenticate.m_Password = m_Password; | authenticate.m_Password = m_Password; | ||||
Show All 35 Lines | bool CNetClient::OnHandshakeResponse(void* context, CFsmEvent* event) | ||||
CNetClient* client = static_cast<CNetClient*>(context); | CNetClient* client = static_cast<CNetClient*>(context); | ||||
CSrvHandshakeResponseMessage* message = static_cast<CSrvHandshakeResponseMessage*>(event->GetParamRef()); | CSrvHandshakeResponseMessage* message = static_cast<CSrvHandshakeResponseMessage*>(event->GetParamRef()); | ||||
client->m_GUID = message->m_GUID; | client->m_GUID = message->m_GUID; | ||||
if (message->m_Flags & PS_NETWORK_FLAG_REQUIRE_LOBBYAUTH) | if (message->m_Flags & PS_NETWORK_FLAG_REQUIRE_LOBBYAUTH) | ||||
{ | { | ||||
if (g_XmppClient && !client->m_HostJID.empty()) | if (!client->m_Game) | ||||
{ | |||||
// This is a fake, headless client for the netserver. | |||||
ENSURE(g_NetServer); | |||||
LOGMESSAGE("sending fake Auth"); | |||||
g_NetServer->OnLobbyAuth("Fake Observer", client->m_GUID); | |||||
} | |||||
else if (g_XmppClient && !client->m_HostJID.empty()) | |||||
g_XmppClient->SendIqLobbyAuth(client->m_HostJID, client->m_GUID); | g_XmppClient->SendIqLobbyAuth(client->m_HostJID, client->m_GUID); | ||||
else | else | ||||
{ | { | ||||
client->PushGuiMessage( | client->PushGuiMessage( | ||||
"type", "netstatus", | "type", "netstatus", | ||||
"status", "disconnected", | "status", "disconnected", | ||||
"reason", static_cast<i32>(NDR_LOBBY_AUTH_FAILED)); | "reason", static_cast<i32>(NDR_LOBBY_AUTH_FAILED)); | ||||
▲ Show 20 Lines • Show All 115 Lines • ▼ Show 20 Lines | bool CNetClient::OnGameStart(void* context, CFsmEvent* event) | ||||
CNetClient* client = static_cast<CNetClient*>(context); | CNetClient* client = static_cast<CNetClient*>(context); | ||||
CGameStartMessage* message = static_cast<CGameStartMessage*>(event->GetParamRef()); | CGameStartMessage* message = static_cast<CGameStartMessage*>(event->GetParamRef()); | ||||
// Find the player assigned to our GUID | // Find the player assigned to our GUID | ||||
int player = -1; | int player = -1; | ||||
if (client->m_PlayerAssignments.find(client->m_GUID) != client->m_PlayerAssignments.end()) | if (client->m_PlayerAssignments.find(client->m_GUID) != client->m_PlayerAssignments.end()) | ||||
player = client->m_PlayerAssignments[client->m_GUID].m_PlayerID; | player = client->m_PlayerAssignments[client->m_GUID].m_PlayerID; | ||||
if (client->m_Game) | |||||
client->m_ClientTurnManager = new CNetClientTurnManager( | client->m_ClientTurnManager = new CNetClientTurnManager( | ||||
*client->m_Game->GetSimulation2(), *client, client->m_HostID, client->m_Game->GetReplayLogger()); | *client->m_Game->GetSimulation2(), *client, client->m_HostID, client->m_Game->GetReplayLogger()); | ||||
// Parse init attributes. | // Parse init attributes. | ||||
const ScriptInterface& scriptInterface = client->m_Game->GetSimulation2()->GetScriptInterface(); | const ScriptInterface& scriptInterface = client->GetScriptInterface(); | ||||
ScriptRequest rq(scriptInterface); | ScriptRequest rq(scriptInterface); | ||||
JS::RootedValue initAttribs(rq.cx); | JS::RootedValue initAttribs(rq.cx); | ||||
Script::ParseJSON(rq, message->m_InitAttributes, &initAttribs); | Script::ParseJSON(rq, message->m_InitAttributes, &initAttribs); | ||||
if (client->m_Game) | |||||
{ | |||||
client->m_Game->SetPlayerID(player); | client->m_Game->SetPlayerID(player); | ||||
client->m_Game->StartGame(&initAttribs, ""); | client->m_Game->StartGame(&initAttribs, ""); | ||||
} | |||||
client->PushGuiMessage("type", "start", | client->PushGuiMessage("type", "start", | ||||
"initAttributes", initAttribs); | "initAttributes", initAttribs); | ||||
return true; | return true; | ||||
} | } | ||||
bool CNetClient::OnJoinSyncStart(void* context, CFsmEvent* event) | bool CNetClient::OnJoinSyncStart(void* context, CFsmEvent* event) | ||||
▲ Show 20 Lines • Show All 136 Lines • ▼ Show 20 Lines | |||||
bool CNetClient::OnLoadedGame(void* context, CFsmEvent* event) | bool CNetClient::OnLoadedGame(void* context, CFsmEvent* event) | ||||
{ | { | ||||
ENSURE(event->GetType() == (uint)NMT_LOADED_GAME); | ENSURE(event->GetType() == (uint)NMT_LOADED_GAME); | ||||
CNetClient* client = static_cast<CNetClient*>(context); | CNetClient* client = static_cast<CNetClient*>(context); | ||||
// All players have loaded the game - start running the turn manager | // All players have loaded the game - start running the turn manager | ||||
// so that the game begins | // so that the game begins | ||||
if (client->m_Game) | |||||
client->m_Game->SetTurnManager(client->m_ClientTurnManager); | client->m_Game->SetTurnManager(client->m_ClientTurnManager); | ||||
client->PushGuiMessage( | client->PushGuiMessage( | ||||
"type", "netstatus", | "type", "netstatus", | ||||
"status", "active"); | "status", "active"); | ||||
// If we have rejoined an in progress game, send the rejoined message to the server. | // If we have rejoined an in progress game, send the rejoined message to the server. | ||||
if (client->m_Rejoin) | if (client->m_Rejoin) | ||||
client->SendRejoinedMessage(); | client->SendRejoinedMessage(); | ||||
return true; | return true; | ||||
} | } | ||||
bool CNetClient::OnInGame(void *context, CFsmEvent* event) | bool CNetClient::OnInGame(void *context, CFsmEvent* event) | ||||
{ | { | ||||
// TODO: should split each of these cases into a separate method | // TODO: should split each of these cases into a separate method | ||||
CNetClient* client = static_cast<CNetClient*>(context); | CNetClient* client = static_cast<CNetClient*>(context); | ||||
CNetMessage* message = static_cast<CNetMessage*>(event->GetParamRef()); | CNetMessage* message = static_cast<CNetMessage*>(event->GetParamRef()); | ||||
if (!client->m_ClientTurnManager) | |||||
return true; | |||||
if (message) | if (message) | ||||
{ | { | ||||
if (message->GetType() == NMT_SIMULATION_COMMAND) | if (message->GetType() == NMT_SIMULATION_COMMAND) | ||||
{ | { | ||||
CSimulationMessage* simMessage = static_cast<CSimulationMessage*> (message); | CSimulationMessage* simMessage = static_cast<CSimulationMessage*> (message); | ||||
client->m_ClientTurnManager->OnSimulationMessage(simMessage); | client->m_ClientTurnManager->OnSimulationMessage(simMessage); | ||||
} | } | ||||
else if (message->GetType() == NMT_SYNC_ERROR) | else if (message->GetType() == NMT_SYNC_ERROR) | ||||
Show All 13 Lines |
Wildfire Games · Phabricator