Changeset View
Changeset View
Standalone View
Standalone View
source/network/NetClient.cpp
/* Copyright (C) 2017 Wildfire Games. | /* Copyright (C) 2018 Wildfire Games. | ||||
* This file is part of 0 A.D. | * This file is part of 0 A.D. | ||||
* | * | ||||
* 0 A.D. is free software: you can redistribute it and/or modify | * 0 A.D. is free software: you can redistribute it and/or modify | ||||
* it under the terms of the GNU General Public License as published by | * it under the terms of the GNU General Public License as published by | ||||
* the Free Software Foundation, either version 2 of the License, or | * the Free Software Foundation, either version 2 of the License, or | ||||
* (at your option) any later version. | * (at your option) any later version. | ||||
* | * | ||||
* 0 A.D. is distributed in the hope that it will be useful, | * 0 A.D. is distributed in the hope that it will be useful, | ||||
Show All 11 Lines | |||||
#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/sysdep/sysdep.h" | #include "lib/sysdep/sysdep.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" | ||||
▲ Show 20 Lines • Show All 46 Lines • ▼ Show 20 Lines | CNetClient::CNetClient(CGame* game, bool isLocalClient) : | ||||
// 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); | ||||
AddTransition(NCS_CONNECT, (uint)NMT_SERVER_HANDSHAKE, NCS_HANDSHAKE, (void*)&OnHandshake, context); | AddTransition(NCS_CONNECT, (uint)NMT_SERVER_HANDSHAKE, NCS_HANDSHAKE, (void*)&OnHandshake, context); | ||||
AddTransition(NCS_HANDSHAKE, (uint)NMT_SERVER_HANDSHAKE_RESPONSE, NCS_AUTHENTICATE, (void*)&OnHandshakeResponse, context); | AddTransition(NCS_HANDSHAKE, (uint)NMT_SERVER_HANDSHAKE_RESPONSE, NCS_AUTHENTICATE, (void*)&OnHandshakeResponse, context); | ||||
AddTransition(NCS_AUTHENTICATE, (uint)NMT_AUTHENTICATE, NCS_AUTHENTICATE, (void*)&OnAuthenticateRequest, context); | |||||
AddTransition(NCS_AUTHENTICATE, (uint)NMT_AUTHENTICATE_RESULT, NCS_INITIAL_GAMESETUP, (void*)&OnAuthenticate, context); | AddTransition(NCS_AUTHENTICATE, (uint)NMT_AUTHENTICATE_RESULT, NCS_INITIAL_GAMESETUP, (void*)&OnAuthenticate, context); | ||||
AddTransition(NCS_INITIAL_GAMESETUP, (uint)NMT_GAME_SETUP, NCS_PREGAME, (void*)&OnGameSetup, context); | AddTransition(NCS_INITIAL_GAMESETUP, (uint)NMT_GAME_SETUP, NCS_PREGAME, (void*)&OnGameSetup, context); | ||||
AddTransition(NCS_PREGAME, (uint)NMT_CHAT, NCS_PREGAME, (void*)&OnChat, context); | AddTransition(NCS_PREGAME, (uint)NMT_CHAT, NCS_PREGAME, (void*)&OnChat, context); | ||||
AddTransition(NCS_PREGAME, (uint)NMT_READY, NCS_PREGAME, (void*)&OnReady, context); | AddTransition(NCS_PREGAME, (uint)NMT_READY, NCS_PREGAME, (void*)&OnReady, context); | ||||
AddTransition(NCS_PREGAME, (uint)NMT_GAME_SETUP, NCS_PREGAME, (void*)&OnGameSetup, context); | AddTransition(NCS_PREGAME, (uint)NMT_GAME_SETUP, NCS_PREGAME, (void*)&OnGameSetup, context); | ||||
AddTransition(NCS_PREGAME, (uint)NMT_PLAYER_ASSIGNMENT, NCS_PREGAME, (void*)&OnPlayerAssignment, context); | AddTransition(NCS_PREGAME, (uint)NMT_PLAYER_ASSIGNMENT, NCS_PREGAME, (void*)&OnPlayerAssignment, context); | ||||
▲ Show 20 Lines • Show All 54 Lines • ▼ Show 20 Lines | |||||
void CNetClient::SetUserName(const CStrW& username) | void CNetClient::SetUserName(const CStrW& username) | ||||
{ | { | ||||
ENSURE(!m_Session); // must be called before we start the connection | ENSURE(!m_Session); // must be called before we start the connection | ||||
m_UserName = username; | m_UserName = username; | ||||
} | } | ||||
void CNetClient::SetHostingPlayerName(const CStr& hostingPlayerName) | |||||
{ | |||||
m_HostingPlayerName = hostingPlayerName; | |||||
} | |||||
bool CNetClient::SetupConnection(const CStr& server, const u16 port, ENetHost* enetClient) | bool CNetClient::SetupConnection(const CStr& server, const u16 port, ENetHost* enetClient) | ||||
{ | { | ||||
CNetClientSession* session = new CNetClientSession(*this); | CNetClientSession* session = new CNetClientSession(*this); | ||||
bool ok = session->Connect(server, port, m_IsLocalClient, enetClient); | bool ok = session->Connect(server, port, m_IsLocalClient, enetClient); | ||||
SetAndOwnSession(session); | SetAndOwnSession(session); | ||||
return ok; | return ok; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 302 Lines • ▼ Show 20 Lines | else | ||||
PushGuiMessage(msg); | PushGuiMessage(msg); | ||||
} | } | ||||
CLoadedGameMessage loaded; | CLoadedGameMessage loaded; | ||||
loaded.m_CurrentTurn = m_ClientTurnManager->GetCurrentTurn(); | loaded.m_CurrentTurn = m_ClientTurnManager->GetCurrentTurn(); | ||||
SendMessage(&loaded); | SendMessage(&loaded); | ||||
} | } | ||||
void CNetClient::SendAuthenticateMessage() | |||||
{ | |||||
CAuthenticateMessage authenticate; | |||||
authenticate.m_Name = m_UserName; | |||||
authenticate.m_Password = L""; // TODO | |||||
elexis: (Weird that we had the pw field in a message that was only sent from the server to the client… | |||||
Not Done Inline ActionsIt's the other way around. (Look at my image in the top post) Imarok: It's the other way around. (Look at my image in the top post) | |||||
authenticate.m_IsLocalClient = m_IsLocalClient; | |||||
SendMessage(&authenticate); | |||||
} | |||||
bool CNetClient::OnConnect(void* context, CFsmEvent* event) | bool CNetClient::OnConnect(void* context, CFsmEvent* event) | ||||
{ | { | ||||
ENSURE(event->GetType() == (uint)NMT_CONNECT_COMPLETE); | ENSURE(event->GetType() == (uint)NMT_CONNECT_COMPLETE); | ||||
CNetClient* client = (CNetClient*)context; | CNetClient* client = (CNetClient*)context; | ||||
JSContext* cx = client->GetScriptInterface().GetContext(); | JSContext* cx = client->GetScriptInterface().GetContext(); | ||||
JSAutoRequest rq(cx); | JSAutoRequest rq(cx); | ||||
Show All 24 Lines | |||||
{ | { | ||||
ENSURE(event->GetType() == (uint)NMT_SERVER_HANDSHAKE_RESPONSE); | ENSURE(event->GetType() == (uint)NMT_SERVER_HANDSHAKE_RESPONSE); | ||||
CNetClient* client = (CNetClient*)context; | CNetClient* client = (CNetClient*)context; | ||||
CSrvHandshakeResponseMessage* message = (CSrvHandshakeResponseMessage*)event->GetParamRef(); | CSrvHandshakeResponseMessage* message = (CSrvHandshakeResponseMessage*)event->GetParamRef(); | ||||
client->m_GUID = message->m_GUID; | client->m_GUID = message->m_GUID; | ||||
CAuthenticateMessage authenticate; | if (message->m_Flags & PS_REQUIRE_LOBBYAUTH_FLAG) | ||||
authenticate.m_Name = client->m_UserName; | { | ||||
authenticate.m_Password = L""; // TODO | if (g_XmppClient && !client->m_HostingPlayerName.empty()) | ||||
authenticate.m_IsLocalClient = client->m_IsLocalClient; | g_XmppClient->SendIqLobbyAuth(client->m_HostingPlayerName, client->m_GUID); | ||||
client->SendMessage(&authenticate); | else | ||||
{ | |||||
Done Inline Actions-!= "" elexis: -`!= ""` | |||||
JSContext* cx = client->GetScriptInterface().GetContext(); | |||||
JSAutoRequest rq(cx); | |||||
JS::RootedValue msg(cx); | |||||
client->GetScriptInterface().Eval("({'type':'netstatus','status':'disconnected'})", &msg); | |||||
client->GetScriptInterface().SetProperty(msg, "reason", (int)NDR_LOBBY_AUTH_FAILED, false); | |||||
client->PushGuiMessage(msg); | |||||
LOGMESSAGE("Net client: Couldn't send lobby auth xmpp message"); | |||||
} | |||||
return true; | |||||
} | |||||
client->SendAuthenticateMessage(); | |||||
return true; | |||||
} | |||||
bool CNetClient::OnAuthenticateRequest(void* context, CFsmEvent* event) | |||||
{ | |||||
ENSURE(event->GetType() == (uint)NMT_AUTHENTICATE); | |||||
CNetClient* client = (CNetClient*)context; | |||||
client->SendAuthenticateMessage(); | |||||
return true; | return true; | ||||
} | } | ||||
bool CNetClient::OnAuthenticate(void* context, CFsmEvent* event) | bool CNetClient::OnAuthenticate(void* context, CFsmEvent* event) | ||||
{ | { | ||||
ENSURE(event->GetType() == (uint)NMT_AUTHENTICATE_RESULT); | ENSURE(event->GetType() == (uint)NMT_AUTHENTICATE_RESULT); | ||||
CNetClient* client = (CNetClient*)context; | CNetClient* client = (CNetClient*)context; | ||||
▲ Show 20 Lines • Show All 344 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator
(Weird that we had the pw field in a message that was only sent from the server to the client until now)