2021-01-10 09:39:54 +01:00
|
|
|
/* Copyright (C) 2021 Wildfire Games.
|
2009-04-18 19:00:33 +02:00
|
|
|
* This file is part of 0 A.D.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2009-04-11 19:00:39 +02:00
|
|
|
#include "precompiled.h"
|
2010-06-08 00:19:05 +02:00
|
|
|
|
2009-04-11 19:00:39 +02:00
|
|
|
#include "NetClient.h"
|
2010-06-08 00:19:05 +02:00
|
|
|
|
2017-01-24 03:04:50 +01:00
|
|
|
#include "NetClientTurnManager.h"
|
2010-06-30 23:41:04 +02:00
|
|
|
#include "NetMessage.h"
|
|
|
|
#include "NetSession.h"
|
2010-06-08 00:19:05 +02:00
|
|
|
|
2011-10-27 18:46:48 +02:00
|
|
|
#include "lib/byte_order.h"
|
STUN + XMPP ICE implementation.
Allows lobby players to host games without having to configure their
router.
Differential Revision: https://code.wildfiregames.com/D364
Fixes #2305
Patch By: fcxSanya.
StunClient based on code by SuperTuxKart, relicensed with approval of
the according authors hilnius, hiker, Auria, deveee, Flakebi, leper,
konstin and KroArtem.
Added rfc5245 (ejabberd) support, a GUI option, refactoring and segfault
fixes by myself.
Tested By: user1, Sandarac, Sestroretsk1714, Vladislav, Grugnas,
javiergodas
Partially Reviewed By: leper, Philip, echotangoecho
This was SVN commit r19703.
2017-06-01 08:33:52 +02:00
|
|
|
#include "lib/external_libraries/enet.h"
|
Hide ip and port from users until they want to join, add optional password
Current issue with the lobby, is that we make ips of hosts public for
anyone to read. This patch consists of 3 parts.
1.) Removing ips and ports from lobby javascript
2.) Removing need of script on the server to attach public ips to game
stanza by asking the host using xmppclient as proxy.
3.) Implementing password protected matches, to deny this information to
not trusted players.
Further description:
Do not send ports and stunip to the bots.
Removed from stanza.
Do not send ip to the lobby.
Removed from mapping gamelist from backend to gui (still on the backend
side, because it is done by script on 0ad server).
Get ip and ports on request when trying to connect.
On the host side, ask stun server what is host's public ip and remember
it.
On the client side, send iq through xmppclient to the hosting player and
ask for ip, port and if Stun is used, then if answer is success,
continue
with connecting, else fail.
Add optional password for matches.
Add password required identifier to the stanza.
Allow host to setup password for the match. Hash it on the host side and
store inside Netserver. If no password is given, matches will behave
as it is not required.
On the client side, if password for the match is required, show
additional window before trying to connect and ask for password, then
hash it
and send with iq request for ip, port and stun.
Server will answer with ip, port and stun only if passwords matches,
else will asnwer with error string.
Some security:
Passwords are hashed before sending, so it is not easy to guess what
users typed. (per wraitii)
Hashes are using different salt as lobby hashing and not using usernames
as salt (as that is not doable), so they are different even typing the
same password as for the lobby account.
Client remembers which user was asked for connection data and iq's id of
request. If answer doesn't match these things, it is ignored. (thnx
user1)
Every request for connection data is logged with hostname of the
requester to the mainlog file (no ips).
If user gets iq to send connection data and is not hosting the match,
will respond with error string "not_server".
If server gets iq::result with connection data, request is ignored.
Differential revision: D3184
Reviewed by: @wraitii
Comments by: @Stan, @bb, @Imarok, @vladislavbelov
Tested in lobby
This was SVN commit r24728.
2021-01-20 19:31:39 +01:00
|
|
|
#include "lib/external_libraries/libsdl.h"
|
2010-07-01 01:40:51 +02:00
|
|
|
#include "lib/sysdep/sysdep.h"
|
2018-03-12 01:23:40 +01:00
|
|
|
#include "lobby/IXmppClient.h"
|
2009-04-11 19:00:39 +02:00
|
|
|
#include "ps/CConsole.h"
|
2010-06-08 00:19:05 +02:00
|
|
|
#include "ps/CLogger.h"
|
2011-10-27 18:46:48 +02:00
|
|
|
#include "ps/Compress.h"
|
2010-06-08 00:19:05 +02:00
|
|
|
#include "ps/CStr.h"
|
2009-04-11 19:00:39 +02:00
|
|
|
#include "ps/Game.h"
|
2011-10-27 18:46:48 +02:00
|
|
|
#include "ps/Loader.h"
|
2021-01-06 16:26:11 +01:00
|
|
|
#include "ps/Profile.h"
|
2021-01-10 09:39:54 +01:00
|
|
|
#include "ps/Threading.h"
|
2010-06-30 23:41:04 +02:00
|
|
|
#include "scriptinterface/ScriptInterface.h"
|
2021-05-14 12:18:03 +02:00
|
|
|
#include "scriptinterface/JSON.h"
|
2010-05-20 02:59:01 +02:00
|
|
|
#include "simulation2/Simulation2.h"
|
Hide ip and port from users until they want to join, add optional password
Current issue with the lobby, is that we make ips of hosts public for
anyone to read. This patch consists of 3 parts.
1.) Removing ips and ports from lobby javascript
2.) Removing need of script on the server to attach public ips to game
stanza by asking the host using xmppclient as proxy.
3.) Implementing password protected matches, to deny this information to
not trusted players.
Further description:
Do not send ports and stunip to the bots.
Removed from stanza.
Do not send ip to the lobby.
Removed from mapping gamelist from backend to gui (still on the backend
side, because it is done by script on 0ad server).
Get ip and ports on request when trying to connect.
On the host side, ask stun server what is host's public ip and remember
it.
On the client side, send iq through xmppclient to the hosting player and
ask for ip, port and if Stun is used, then if answer is success,
continue
with connecting, else fail.
Add optional password for matches.
Add password required identifier to the stanza.
Allow host to setup password for the match. Hash it on the host side and
store inside Netserver. If no password is given, matches will behave
as it is not required.
On the client side, if password for the match is required, show
additional window before trying to connect and ask for password, then
hash it
and send with iq request for ip, port and stun.
Server will answer with ip, port and stun only if passwords matches,
else will asnwer with error string.
Some security:
Passwords are hashed before sending, so it is not easy to guess what
users typed. (per wraitii)
Hashes are using different salt as lobby hashing and not using usernames
as salt (as that is not doable), so they are different even typing the
same password as for the lobby account.
Client remembers which user was asked for connection data and iq's id of
request. If answer doesn't match these things, it is ignored. (thnx
user1)
Every request for connection data is logged with hostname of the
requester to the mainlog file (no ips).
If user gets iq to send connection data and is not hosting the match,
will respond with error string "not_server".
If server gets iq::result with connection data, request is ignored.
Differential revision: D3184
Reviewed by: @wraitii
Comments by: @Stan, @bb, @Imarok, @vladislavbelov
Tested in lobby
This was SVN commit r24728.
2021-01-20 19:31:39 +01:00
|
|
|
#include "network/StunClient.h"
|
2009-04-11 19:00:39 +02:00
|
|
|
|
Increase MP Command delay to 4 turns, decrease MP turns to 200ms.
To hide network latency, MP turns send commands not for the next turn
but N turns after that (introduced in c684c211a2).
Further, MP turn length was increased to 500ms compared to 200ms SP
turns (introduced in 6a15b78c98).
Unfortunately, increasing MP turn length has negative consequences:
- makes pathfinding/unit motion much worse & unit behaviour worse in
general.
- makes the game more 'lag-spikey', since computations are done less
often, but thus then can take more time.
This diff essentially reverts 6a15b78c98, instead increasing
COMMAND_DELAY from 2 to 4 in MP. This:
- Reduces the 'inherent command lag' in MP from 1000ms to 800ms
- Increases the lag range at which MP will run smoothtly from 500ms to
600ms
- makes SP and MP turns behave identically again, removing the
hindrances described above.
As a side effect, single-player was not actually using COMMAND_DELAY,
this is now done (can be used to simulate MP command lag).
Refs #3752
Differential Revision: https://code.wildfiregames.com/D3275
This was SVN commit r25001.
2021-03-03 22:02:57 +01:00
|
|
|
/**
|
|
|
|
* Once ping goes above turn length * command delay,
|
|
|
|
* the game will start 'freezing' for other clients while we catch up.
|
|
|
|
* Since commands are sent client -> server -> client, divide by 2.
|
|
|
|
* (duplicated in NetServer.cpp to avoid having to fetch the constants in a header file)
|
|
|
|
*/
|
|
|
|
constexpr u32 NETWORK_BAD_PING = DEFAULT_TURN_LENGTH * COMMAND_DELAY_MP / 2;
|
|
|
|
|
2010-05-20 02:59:01 +02:00
|
|
|
CNetClient *g_NetClient = NULL;
|
|
|
|
|
2011-10-27 18:46:48 +02:00
|
|
|
/**
|
|
|
|
* Async task for receiving the initial game state when rejoining an
|
|
|
|
* in-progress network game.
|
|
|
|
*/
|
|
|
|
class CNetFileReceiveTask_ClientRejoin : public CNetFileReceiveTask
|
|
|
|
{
|
|
|
|
NONCOPYABLE(CNetFileReceiveTask_ClientRejoin);
|
|
|
|
public:
|
2021-03-22 11:13:27 +01:00
|
|
|
CNetFileReceiveTask_ClientRejoin(CNetClient& client, const CStr& initAttribs)
|
|
|
|
: m_Client(client), m_InitAttributes(initAttribs)
|
2011-10-27 18:46:48 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void OnComplete()
|
|
|
|
{
|
|
|
|
// We've received the game state from the server
|
|
|
|
|
|
|
|
// Save it so we can use it after the map has finished loading
|
|
|
|
m_Client.m_JoinSyncBuffer = m_Buffer;
|
|
|
|
|
|
|
|
// Pretend the server told us to start the game
|
|
|
|
CGameStartMessage start;
|
2021-03-22 11:13:27 +01:00
|
|
|
start.m_InitAttributes = m_InitAttributes;
|
2011-10-27 18:46:48 +02:00
|
|
|
m_Client.HandleMessage(&start);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
CNetClient& m_Client;
|
2021-03-22 11:13:27 +01:00
|
|
|
CStr m_InitAttributes;
|
2011-10-27 18:46:48 +02:00
|
|
|
};
|
|
|
|
|
2021-02-27 18:44:59 +01:00
|
|
|
CNetClient::CNetClient(CGame* game) :
|
2010-06-30 23:41:04 +02:00
|
|
|
m_Session(NULL),
|
|
|
|
m_UserName(L"anonymous"),
|
2017-10-25 00:05:24 +02:00
|
|
|
m_HostID((u32)-1), m_ClientTurnManager(NULL), m_Game(game),
|
2016-05-19 23:03:46 +02:00
|
|
|
m_LastConnectionCheck(0),
|
Hide ip and port from users until they want to join, add optional password
Current issue with the lobby, is that we make ips of hosts public for
anyone to read. This patch consists of 3 parts.
1.) Removing ips and ports from lobby javascript
2.) Removing need of script on the server to attach public ips to game
stanza by asking the host using xmppclient as proxy.
3.) Implementing password protected matches, to deny this information to
not trusted players.
Further description:
Do not send ports and stunip to the bots.
Removed from stanza.
Do not send ip to the lobby.
Removed from mapping gamelist from backend to gui (still on the backend
side, because it is done by script on 0ad server).
Get ip and ports on request when trying to connect.
On the host side, ask stun server what is host's public ip and remember
it.
On the client side, send iq through xmppclient to the hosting player and
ask for ip, port and if Stun is used, then if answer is success,
continue
with connecting, else fail.
Add optional password for matches.
Add password required identifier to the stanza.
Allow host to setup password for the match. Hash it on the host side and
store inside Netserver. If no password is given, matches will behave
as it is not required.
On the client side, if password for the match is required, show
additional window before trying to connect and ask for password, then
hash it
and send with iq request for ip, port and stun.
Server will answer with ip, port and stun only if passwords matches,
else will asnwer with error string.
Some security:
Passwords are hashed before sending, so it is not easy to guess what
users typed. (per wraitii)
Hashes are using different salt as lobby hashing and not using usernames
as salt (as that is not doable), so they are different even typing the
same password as for the lobby account.
Client remembers which user was asked for connection data and iq's id of
request. If answer doesn't match these things, it is ignored. (thnx
user1)
Every request for connection data is logged with hostname of the
requester to the mainlog file (no ips).
If user gets iq to send connection data and is not hosting the match,
will respond with error string "not_server".
If server gets iq::result with connection data, request is ignored.
Differential revision: D3184
Reviewed by: @wraitii
Comments by: @Stan, @bb, @Imarok, @vladislavbelov
Tested in lobby
This was SVN commit r24728.
2021-01-20 19:31:39 +01:00
|
|
|
m_ServerAddress(),
|
|
|
|
m_ServerPort(0),
|
2016-05-19 23:03:46 +02:00
|
|
|
m_Rejoin(false)
|
2009-04-11 19:00:39 +02:00
|
|
|
{
|
2010-06-30 23:41:04 +02:00
|
|
|
m_Game->SetTurnManager(NULL); // delete the old local turn manager so we don't accidentally use it
|
2009-04-11 19:00:39 +02:00
|
|
|
|
2010-06-30 23:41:04 +02:00
|
|
|
void* context = this;
|
2015-12-21 14:58:32 +01:00
|
|
|
|
2020-11-18 15:39:04 +01:00
|
|
|
JS_AddExtraGCRootsTracer(GetScriptInterface().GetGeneralJSContext(), CNetClient::Trace, this);
|
2009-04-11 19:00:39 +02:00
|
|
|
|
2010-06-30 23:41:04 +02:00
|
|
|
// Set up transitions for session
|
|
|
|
AddTransition(NCS_UNCONNECTED, (uint)NMT_CONNECT_COMPLETE, NCS_CONNECT, (void*)&OnConnect, context);
|
2009-04-11 19:00:39 +02:00
|
|
|
|
2010-06-30 23:41:04 +02:00
|
|
|
AddTransition(NCS_CONNECT, (uint)NMT_SERVER_HANDSHAKE, NCS_HANDSHAKE, (void*)&OnHandshake, context);
|
2009-04-11 19:00:39 +02:00
|
|
|
|
2010-06-30 23:41:04 +02:00
|
|
|
AddTransition(NCS_HANDSHAKE, (uint)NMT_SERVER_HANDSHAKE_RESPONSE, NCS_AUTHENTICATE, (void*)&OnHandshakeResponse, context);
|
2010-05-20 02:59:01 +02:00
|
|
|
|
2018-03-12 01:23:40 +01:00
|
|
|
AddTransition(NCS_AUTHENTICATE, (uint)NMT_AUTHENTICATE, NCS_AUTHENTICATE, (void*)&OnAuthenticateRequest, context);
|
2021-03-22 11:13:27 +01:00
|
|
|
AddTransition(NCS_AUTHENTICATE, (uint)NMT_AUTHENTICATE_RESULT, NCS_PREGAME, (void*)&OnAuthenticate, context);
|
2009-04-11 19:00:39 +02:00
|
|
|
|
2010-07-02 23:28:48 +02:00
|
|
|
AddTransition(NCS_PREGAME, (uint)NMT_CHAT, NCS_PREGAME, (void*)&OnChat, context);
|
2014-04-26 20:34:34 +02:00
|
|
|
AddTransition(NCS_PREGAME, (uint)NMT_READY, NCS_PREGAME, (void*)&OnReady, context);
|
2010-06-30 23:41:04 +02:00
|
|
|
AddTransition(NCS_PREGAME, (uint)NMT_GAME_SETUP, NCS_PREGAME, (void*)&OnGameSetup, context);
|
|
|
|
AddTransition(NCS_PREGAME, (uint)NMT_PLAYER_ASSIGNMENT, NCS_PREGAME, (void*)&OnPlayerAssignment, context);
|
2015-11-11 03:05:23 +01:00
|
|
|
AddTransition(NCS_PREGAME, (uint)NMT_KICKED, NCS_PREGAME, (void*)&OnKicked, context);
|
2016-02-04 18:14:46 +01:00
|
|
|
AddTransition(NCS_PREGAME, (uint)NMT_CLIENT_TIMEOUT, NCS_PREGAME, (void*)&OnClientTimeout, context);
|
|
|
|
AddTransition(NCS_PREGAME, (uint)NMT_CLIENT_PERFORMANCE, NCS_PREGAME, (void*)&OnClientPerformance, context);
|
2010-06-30 23:41:04 +02:00
|
|
|
AddTransition(NCS_PREGAME, (uint)NMT_GAME_START, NCS_LOADING, (void*)&OnGameStart, context);
|
2011-10-27 18:46:48 +02:00
|
|
|
AddTransition(NCS_PREGAME, (uint)NMT_JOIN_SYNC_START, NCS_JOIN_SYNCING, (void*)&OnJoinSyncStart, context);
|
|
|
|
|
|
|
|
AddTransition(NCS_JOIN_SYNCING, (uint)NMT_CHAT, NCS_JOIN_SYNCING, (void*)&OnChat, context);
|
|
|
|
AddTransition(NCS_JOIN_SYNCING, (uint)NMT_GAME_SETUP, NCS_JOIN_SYNCING, (void*)&OnGameSetup, context);
|
|
|
|
AddTransition(NCS_JOIN_SYNCING, (uint)NMT_PLAYER_ASSIGNMENT, NCS_JOIN_SYNCING, (void*)&OnPlayerAssignment, context);
|
2016-10-23 17:44:02 +02:00
|
|
|
AddTransition(NCS_JOIN_SYNCING, (uint)NMT_KICKED, NCS_JOIN_SYNCING, (void*)&OnKicked, context);
|
2016-02-04 18:14:46 +01:00
|
|
|
AddTransition(NCS_JOIN_SYNCING, (uint)NMT_CLIENT_TIMEOUT, NCS_JOIN_SYNCING, (void*)&OnClientTimeout, context);
|
|
|
|
AddTransition(NCS_JOIN_SYNCING, (uint)NMT_CLIENT_PERFORMANCE, NCS_JOIN_SYNCING, (void*)&OnClientPerformance, context);
|
2011-10-27 18:46:48 +02:00
|
|
|
AddTransition(NCS_JOIN_SYNCING, (uint)NMT_GAME_START, NCS_JOIN_SYNCING, (void*)&OnGameStart, context);
|
|
|
|
AddTransition(NCS_JOIN_SYNCING, (uint)NMT_SIMULATION_COMMAND, NCS_JOIN_SYNCING, (void*)&OnInGame, context);
|
|
|
|
AddTransition(NCS_JOIN_SYNCING, (uint)NMT_END_COMMAND_BATCH, NCS_JOIN_SYNCING, (void*)&OnJoinSyncEndCommandBatch, context);
|
|
|
|
AddTransition(NCS_JOIN_SYNCING, (uint)NMT_LOADED_GAME, NCS_INGAME, (void*)&OnLoadedGame, context);
|
2010-06-30 23:41:04 +02:00
|
|
|
|
2010-07-02 23:28:48 +02:00
|
|
|
AddTransition(NCS_LOADING, (uint)NMT_CHAT, NCS_LOADING, (void*)&OnChat, context);
|
2010-06-30 23:41:04 +02:00
|
|
|
AddTransition(NCS_LOADING, (uint)NMT_GAME_SETUP, NCS_LOADING, (void*)&OnGameSetup, context);
|
|
|
|
AddTransition(NCS_LOADING, (uint)NMT_PLAYER_ASSIGNMENT, NCS_LOADING, (void*)&OnPlayerAssignment, context);
|
2016-10-23 17:44:02 +02:00
|
|
|
AddTransition(NCS_LOADING, (uint)NMT_KICKED, NCS_LOADING, (void*)&OnKicked, context);
|
2016-02-04 18:14:46 +01:00
|
|
|
AddTransition(NCS_LOADING, (uint)NMT_CLIENT_TIMEOUT, NCS_LOADING, (void*)&OnClientTimeout, context);
|
|
|
|
AddTransition(NCS_LOADING, (uint)NMT_CLIENT_PERFORMANCE, NCS_LOADING, (void*)&OnClientPerformance, context);
|
2017-03-21 19:50:29 +01:00
|
|
|
AddTransition(NCS_LOADING, (uint)NMT_CLIENTS_LOADING, NCS_LOADING, (void*)&OnClientsLoading, context);
|
2010-06-30 23:41:04 +02:00
|
|
|
AddTransition(NCS_LOADING, (uint)NMT_LOADED_GAME, NCS_INGAME, (void*)&OnLoadedGame, context);
|
2009-04-11 19:00:39 +02:00
|
|
|
|
2015-05-03 04:06:17 +02:00
|
|
|
AddTransition(NCS_INGAME, (uint)NMT_REJOINED, NCS_INGAME, (void*)&OnRejoined, context);
|
2015-11-11 03:05:23 +01:00
|
|
|
AddTransition(NCS_INGAME, (uint)NMT_KICKED, NCS_INGAME, (void*)&OnKicked, context);
|
2016-02-04 18:14:46 +01:00
|
|
|
AddTransition(NCS_INGAME, (uint)NMT_CLIENT_TIMEOUT, NCS_INGAME, (void*)&OnClientTimeout, context);
|
|
|
|
AddTransition(NCS_INGAME, (uint)NMT_CLIENT_PERFORMANCE, NCS_INGAME, (void*)&OnClientPerformance, context);
|
2017-03-21 19:50:29 +01:00
|
|
|
AddTransition(NCS_INGAME, (uint)NMT_CLIENTS_LOADING, NCS_INGAME, (void*)&OnClientsLoading, context);
|
2016-05-20 00:10:38 +02:00
|
|
|
AddTransition(NCS_INGAME, (uint)NMT_CLIENT_PAUSED, NCS_INGAME, (void*)&OnClientPaused, context);
|
2010-07-02 23:28:48 +02:00
|
|
|
AddTransition(NCS_INGAME, (uint)NMT_CHAT, NCS_INGAME, (void*)&OnChat, context);
|
2010-06-30 23:41:04 +02:00
|
|
|
AddTransition(NCS_INGAME, (uint)NMT_GAME_SETUP, NCS_INGAME, (void*)&OnGameSetup, context);
|
|
|
|
AddTransition(NCS_INGAME, (uint)NMT_PLAYER_ASSIGNMENT, NCS_INGAME, (void*)&OnPlayerAssignment, context);
|
|
|
|
AddTransition(NCS_INGAME, (uint)NMT_SIMULATION_COMMAND, NCS_INGAME, (void*)&OnInGame, context);
|
|
|
|
AddTransition(NCS_INGAME, (uint)NMT_SYNC_ERROR, NCS_INGAME, (void*)&OnInGame, context);
|
|
|
|
AddTransition(NCS_INGAME, (uint)NMT_END_COMMAND_BATCH, NCS_INGAME, (void*)&OnInGame, context);
|
2009-04-11 19:00:39 +02:00
|
|
|
|
2010-06-30 23:41:04 +02:00
|
|
|
// Set first state
|
|
|
|
SetFirstState(NCS_UNCONNECTED);
|
2009-04-11 19:00:39 +02:00
|
|
|
}
|
|
|
|
|
2010-06-30 23:41:04 +02:00
|
|
|
CNetClient::~CNetClient()
|
2009-04-11 19:00:39 +02:00
|
|
|
{
|
2021-01-06 16:26:11 +01:00
|
|
|
// Try to flush messages before dying (probably fails).
|
|
|
|
if (m_ClientTurnManager)
|
|
|
|
m_ClientTurnManager->OnDestroyConnection();
|
|
|
|
|
2011-10-27 18:46:48 +02:00
|
|
|
DestroyConnection();
|
2020-11-18 15:39:04 +01:00
|
|
|
JS_RemoveExtraGCRootsTracer(GetScriptInterface().GetGeneralJSContext(), CNetClient::Trace, this);
|
2014-11-09 16:16:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CNetClient::TraceMember(JSTracer *trc)
|
|
|
|
{
|
2016-04-24 22:48:53 +02:00
|
|
|
for (JS::Heap<JS::Value>& guiMessage : m_GuiMessageQueue)
|
2020-11-12 09:04:24 +01:00
|
|
|
JS::TraceEdge(trc, &guiMessage, "m_GuiMessageQueue");
|
2009-04-11 19:00:39 +02:00
|
|
|
}
|
|
|
|
|
2010-06-30 23:41:04 +02:00
|
|
|
void CNetClient::SetUserName(const CStrW& username)
|
2009-04-11 19:00:39 +02:00
|
|
|
{
|
2011-04-30 15:01:45 +02:00
|
|
|
ENSURE(!m_Session); // must be called before we start the connection
|
2009-04-11 19:00:39 +02:00
|
|
|
|
2010-06-30 23:41:04 +02:00
|
|
|
m_UserName = username;
|
2009-04-11 19:00:39 +02:00
|
|
|
}
|
|
|
|
|
2021-05-09 14:51:32 +02:00
|
|
|
void CNetClient::SetHostJID(const CStr& jid)
|
2018-03-12 01:23:40 +01:00
|
|
|
{
|
2021-05-09 14:51:32 +02:00
|
|
|
m_HostJID = jid;
|
2018-03-12 01:23:40 +01:00
|
|
|
}
|
|
|
|
|
2021-01-23 19:04:36 +01:00
|
|
|
void CNetClient::SetGamePassword(const CStr& hashedPassword)
|
|
|
|
{
|
|
|
|
m_Password = hashedPassword;
|
|
|
|
}
|
|
|
|
|
2021-02-27 18:44:59 +01:00
|
|
|
void CNetClient::SetControllerSecret(const std::string& secret)
|
|
|
|
{
|
|
|
|
m_ControllerSecret = secret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
Hide ip and port from users until they want to join, add optional password
Current issue with the lobby, is that we make ips of hosts public for
anyone to read. This patch consists of 3 parts.
1.) Removing ips and ports from lobby javascript
2.) Removing need of script on the server to attach public ips to game
stanza by asking the host using xmppclient as proxy.
3.) Implementing password protected matches, to deny this information to
not trusted players.
Further description:
Do not send ports and stunip to the bots.
Removed from stanza.
Do not send ip to the lobby.
Removed from mapping gamelist from backend to gui (still on the backend
side, because it is done by script on 0ad server).
Get ip and ports on request when trying to connect.
On the host side, ask stun server what is host's public ip and remember
it.
On the client side, send iq through xmppclient to the hosting player and
ask for ip, port and if Stun is used, then if answer is success,
continue
with connecting, else fail.
Add optional password for matches.
Add password required identifier to the stanza.
Allow host to setup password for the match. Hash it on the host side and
store inside Netserver. If no password is given, matches will behave
as it is not required.
On the client side, if password for the match is required, show
additional window before trying to connect and ask for password, then
hash it
and send with iq request for ip, port and stun.
Server will answer with ip, port and stun only if passwords matches,
else will asnwer with error string.
Some security:
Passwords are hashed before sending, so it is not easy to guess what
users typed. (per wraitii)
Hashes are using different salt as lobby hashing and not using usernames
as salt (as that is not doable), so they are different even typing the
same password as for the lobby account.
Client remembers which user was asked for connection data and iq's id of
request. If answer doesn't match these things, it is ignored. (thnx
user1)
Every request for connection data is logged with hostname of the
requester to the mainlog file (no ips).
If user gets iq to send connection data and is not hosting the match,
will respond with error string "not_server".
If server gets iq::result with connection data, request is ignored.
Differential revision: D3184
Reviewed by: @wraitii
Comments by: @Stan, @bb, @Imarok, @vladislavbelov
Tested in lobby
This was SVN commit r24728.
2021-01-20 19:31:39 +01:00
|
|
|
bool CNetClient::SetupConnection(ENetHost* enetClient)
|
2009-04-11 19:00:39 +02:00
|
|
|
{
|
2010-07-06 21:54:17 +02:00
|
|
|
CNetClientSession* session = new CNetClientSession(*this);
|
2021-02-27 18:44:59 +01:00
|
|
|
bool ok = session->Connect(m_ServerAddress, m_ServerPort, enetClient);
|
2010-06-30 23:41:04 +02:00
|
|
|
SetAndOwnSession(session);
|
2021-01-10 09:39:54 +01:00
|
|
|
m_PollingThread = std::thread(Threading::HandleExceptions<CNetClientSession::RunNetLoop>::Wrapper, m_Session);
|
2010-06-30 23:41:04 +02:00
|
|
|
return ok;
|
|
|
|
}
|
2009-04-11 19:00:39 +02:00
|
|
|
|
Hide ip and port from users until they want to join, add optional password
Current issue with the lobby, is that we make ips of hosts public for
anyone to read. This patch consists of 3 parts.
1.) Removing ips and ports from lobby javascript
2.) Removing need of script on the server to attach public ips to game
stanza by asking the host using xmppclient as proxy.
3.) Implementing password protected matches, to deny this information to
not trusted players.
Further description:
Do not send ports and stunip to the bots.
Removed from stanza.
Do not send ip to the lobby.
Removed from mapping gamelist from backend to gui (still on the backend
side, because it is done by script on 0ad server).
Get ip and ports on request when trying to connect.
On the host side, ask stun server what is host's public ip and remember
it.
On the client side, send iq through xmppclient to the hosting player and
ask for ip, port and if Stun is used, then if answer is success,
continue
with connecting, else fail.
Add optional password for matches.
Add password required identifier to the stanza.
Allow host to setup password for the match. Hash it on the host side and
store inside Netserver. If no password is given, matches will behave
as it is not required.
On the client side, if password for the match is required, show
additional window before trying to connect and ask for password, then
hash it
and send with iq request for ip, port and stun.
Server will answer with ip, port and stun only if passwords matches,
else will asnwer with error string.
Some security:
Passwords are hashed before sending, so it is not easy to guess what
users typed. (per wraitii)
Hashes are using different salt as lobby hashing and not using usernames
as salt (as that is not doable), so they are different even typing the
same password as for the lobby account.
Client remembers which user was asked for connection data and iq's id of
request. If answer doesn't match these things, it is ignored. (thnx
user1)
Every request for connection data is logged with hostname of the
requester to the mainlog file (no ips).
If user gets iq to send connection data and is not hosting the match,
will respond with error string "not_server".
If server gets iq::result with connection data, request is ignored.
Differential revision: D3184
Reviewed by: @wraitii
Comments by: @Stan, @bb, @Imarok, @vladislavbelov
Tested in lobby
This was SVN commit r24728.
2021-01-20 19:31:39 +01:00
|
|
|
void CNetClient::SetupServerData(CStr address, u16 port, bool stun)
|
|
|
|
{
|
|
|
|
ENSURE(!m_Session);
|
|
|
|
|
|
|
|
m_ServerAddress = address;
|
|
|
|
m_ServerPort = port;
|
|
|
|
m_UseSTUN = stun;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CNetClient::HandleGetServerDataFailed(const CStr& error)
|
|
|
|
{
|
|
|
|
if (m_Session)
|
|
|
|
return;
|
|
|
|
|
|
|
|
PushGuiMessage(
|
|
|
|
"type", "serverdata",
|
|
|
|
"status", "failed",
|
|
|
|
"reason", error
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CNetClient::TryToConnect(const CStr& hostJID)
|
|
|
|
{
|
|
|
|
if (m_Session)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (m_ServerAddress.empty())
|
|
|
|
{
|
|
|
|
PushGuiMessage(
|
|
|
|
"type", "netstatus",
|
|
|
|
"status", "disconnected",
|
|
|
|
"reason", static_cast<i32>(NDR_SERVER_REFUSED));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ENetHost* enetClient = nullptr;
|
|
|
|
if (g_XmppClient && m_UseSTUN)
|
|
|
|
{
|
2021-05-17 17:14:10 +02:00
|
|
|
ENetAddress hostAddr{ ENET_HOST_ANY, ENET_PORT_ANY };
|
|
|
|
enetClient = enet_host_create(&hostAddr, 1, 1, 0, 0);
|
Hide ip and port from users until they want to join, add optional password
Current issue with the lobby, is that we make ips of hosts public for
anyone to read. This patch consists of 3 parts.
1.) Removing ips and ports from lobby javascript
2.) Removing need of script on the server to attach public ips to game
stanza by asking the host using xmppclient as proxy.
3.) Implementing password protected matches, to deny this information to
not trusted players.
Further description:
Do not send ports and stunip to the bots.
Removed from stanza.
Do not send ip to the lobby.
Removed from mapping gamelist from backend to gui (still on the backend
side, because it is done by script on 0ad server).
Get ip and ports on request when trying to connect.
On the host side, ask stun server what is host's public ip and remember
it.
On the client side, send iq through xmppclient to the hosting player and
ask for ip, port and if Stun is used, then if answer is success,
continue
with connecting, else fail.
Add optional password for matches.
Add password required identifier to the stanza.
Allow host to setup password for the match. Hash it on the host side and
store inside Netserver. If no password is given, matches will behave
as it is not required.
On the client side, if password for the match is required, show
additional window before trying to connect and ask for password, then
hash it
and send with iq request for ip, port and stun.
Server will answer with ip, port and stun only if passwords matches,
else will asnwer with error string.
Some security:
Passwords are hashed before sending, so it is not easy to guess what
users typed. (per wraitii)
Hashes are using different salt as lobby hashing and not using usernames
as salt (as that is not doable), so they are different even typing the
same password as for the lobby account.
Client remembers which user was asked for connection data and iq's id of
request. If answer doesn't match these things, it is ignored. (thnx
user1)
Every request for connection data is logged with hostname of the
requester to the mainlog file (no ips).
If user gets iq to send connection data and is not hosting the match,
will respond with error string "not_server".
If server gets iq::result with connection data, request is ignored.
Differential revision: D3184
Reviewed by: @wraitii
Comments by: @Stan, @bb, @Imarok, @vladislavbelov
Tested in lobby
This was SVN commit r24728.
2021-01-20 19:31:39 +01:00
|
|
|
|
|
|
|
if (!enetClient)
|
|
|
|
{
|
|
|
|
PushGuiMessage(
|
|
|
|
"type", "netstatus",
|
|
|
|
"status", "disconnected",
|
|
|
|
"reason", static_cast<i32>(NDR_STUN_PORT_FAILED));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-16 17:34:38 +02:00
|
|
|
CStr ip;
|
2021-05-17 17:14:10 +02:00
|
|
|
u16 port;
|
|
|
|
if (!StunClient::FindPublicIP(*enetClient, ip, port))
|
Hide ip and port from users until they want to join, add optional password
Current issue with the lobby, is that we make ips of hosts public for
anyone to read. This patch consists of 3 parts.
1.) Removing ips and ports from lobby javascript
2.) Removing need of script on the server to attach public ips to game
stanza by asking the host using xmppclient as proxy.
3.) Implementing password protected matches, to deny this information to
not trusted players.
Further description:
Do not send ports and stunip to the bots.
Removed from stanza.
Do not send ip to the lobby.
Removed from mapping gamelist from backend to gui (still on the backend
side, because it is done by script on 0ad server).
Get ip and ports on request when trying to connect.
On the host side, ask stun server what is host's public ip and remember
it.
On the client side, send iq through xmppclient to the hosting player and
ask for ip, port and if Stun is used, then if answer is success,
continue
with connecting, else fail.
Add optional password for matches.
Add password required identifier to the stanza.
Allow host to setup password for the match. Hash it on the host side and
store inside Netserver. If no password is given, matches will behave
as it is not required.
On the client side, if password for the match is required, show
additional window before trying to connect and ask for password, then
hash it
and send with iq request for ip, port and stun.
Server will answer with ip, port and stun only if passwords matches,
else will asnwer with error string.
Some security:
Passwords are hashed before sending, so it is not easy to guess what
users typed. (per wraitii)
Hashes are using different salt as lobby hashing and not using usernames
as salt (as that is not doable), so they are different even typing the
same password as for the lobby account.
Client remembers which user was asked for connection data and iq's id of
request. If answer doesn't match these things, it is ignored. (thnx
user1)
Every request for connection data is logged with hostname of the
requester to the mainlog file (no ips).
If user gets iq to send connection data and is not hosting the match,
will respond with error string "not_server".
If server gets iq::result with connection data, request is ignored.
Differential revision: D3184
Reviewed by: @wraitii
Comments by: @Stan, @bb, @Imarok, @vladislavbelov
Tested in lobby
This was SVN commit r24728.
2021-01-20 19:31:39 +01:00
|
|
|
{
|
|
|
|
PushGuiMessage(
|
|
|
|
"type", "netstatus",
|
|
|
|
"status", "disconnected",
|
|
|
|
"reason", static_cast<i32>(NDR_STUN_ENDPOINT_FAILED));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-16 17:34:38 +02:00
|
|
|
// If the host is on the same network, we risk failing to connect
|
|
|
|
// on routers that don't support NAT hairpinning/NAT loopback.
|
|
|
|
// To work around that, send again a connection data request, but for internal IP this time.
|
|
|
|
if (ip == m_ServerAddress)
|
|
|
|
{
|
|
|
|
g_XmppClient->SendIqGetConnectionData(m_HostJID, m_Password, true);
|
|
|
|
// Return true anyways - we're on a success path here.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-05-17 17:14:10 +02:00
|
|
|
g_XmppClient->SendStunEndpointToHost(ip, port, hostJID);
|
Hide ip and port from users until they want to join, add optional password
Current issue with the lobby, is that we make ips of hosts public for
anyone to read. This patch consists of 3 parts.
1.) Removing ips and ports from lobby javascript
2.) Removing need of script on the server to attach public ips to game
stanza by asking the host using xmppclient as proxy.
3.) Implementing password protected matches, to deny this information to
not trusted players.
Further description:
Do not send ports and stunip to the bots.
Removed from stanza.
Do not send ip to the lobby.
Removed from mapping gamelist from backend to gui (still on the backend
side, because it is done by script on 0ad server).
Get ip and ports on request when trying to connect.
On the host side, ask stun server what is host's public ip and remember
it.
On the client side, send iq through xmppclient to the hosting player and
ask for ip, port and if Stun is used, then if answer is success,
continue
with connecting, else fail.
Add optional password for matches.
Add password required identifier to the stanza.
Allow host to setup password for the match. Hash it on the host side and
store inside Netserver. If no password is given, matches will behave
as it is not required.
On the client side, if password for the match is required, show
additional window before trying to connect and ask for password, then
hash it
and send with iq request for ip, port and stun.
Server will answer with ip, port and stun only if passwords matches,
else will asnwer with error string.
Some security:
Passwords are hashed before sending, so it is not easy to guess what
users typed. (per wraitii)
Hashes are using different salt as lobby hashing and not using usernames
as salt (as that is not doable), so they are different even typing the
same password as for the lobby account.
Client remembers which user was asked for connection data and iq's id of
request. If answer doesn't match these things, it is ignored. (thnx
user1)
Every request for connection data is logged with hostname of the
requester to the mainlog file (no ips).
If user gets iq to send connection data and is not hosting the match,
will respond with error string "not_server".
If server gets iq::result with connection data, request is ignored.
Differential revision: D3184
Reviewed by: @wraitii
Comments by: @Stan, @bb, @Imarok, @vladislavbelov
Tested in lobby
This was SVN commit r24728.
2021-01-20 19:31:39 +01:00
|
|
|
|
|
|
|
SDL_Delay(1000);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-30 23:41:04 +02:00
|
|
|
void CNetClient::SetAndOwnSession(CNetClientSession* session)
|
2009-04-11 19:00:39 +02:00
|
|
|
{
|
2010-06-30 23:41:04 +02:00
|
|
|
delete m_Session;
|
|
|
|
m_Session = session;
|
|
|
|
}
|
2009-04-11 19:00:39 +02:00
|
|
|
|
2011-10-27 18:46:48 +02:00
|
|
|
void CNetClient::DestroyConnection()
|
|
|
|
{
|
2021-01-06 16:26:11 +01:00
|
|
|
if (m_Session)
|
|
|
|
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;
|
2011-10-27 18:46:48 +02:00
|
|
|
}
|
|
|
|
|
2010-06-30 23:41:04 +02:00
|
|
|
void CNetClient::Poll()
|
|
|
|
{
|
2016-02-04 18:14:46 +01:00
|
|
|
if (!m_Session)
|
|
|
|
return;
|
|
|
|
|
2021-01-06 16:26:11 +01:00
|
|
|
PROFILE3("NetClient::poll");
|
|
|
|
|
2016-02-04 18:14:46 +01:00
|
|
|
CheckServerConnection();
|
2021-01-06 16:26:11 +01:00
|
|
|
m_Session->ProcessPolledMessages();
|
2016-02-04 18:14:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CNetClient::CheckServerConnection()
|
|
|
|
{
|
|
|
|
// Trigger local warnings if the connection to the server is bad.
|
|
|
|
// At most once per second.
|
|
|
|
std::time_t now = std::time(nullptr);
|
|
|
|
if (now <= m_LastConnectionCheck)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_LastConnectionCheck = now;
|
|
|
|
|
|
|
|
// Report if we are losing the connection to the server
|
|
|
|
u32 lastReceived = m_Session->GetLastReceivedTime();
|
|
|
|
if (lastReceived > NETWORK_WARNING_TIMEOUT)
|
|
|
|
{
|
2019-09-07 18:51:44 +02:00
|
|
|
PushGuiMessage(
|
|
|
|
"type", "netwarn",
|
|
|
|
"warntype", "server-timeout",
|
|
|
|
"lastReceivedTime", lastReceived);
|
2016-02-04 18:14:46 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
Increase MP Command delay to 4 turns, decrease MP turns to 200ms.
To hide network latency, MP turns send commands not for the next turn
but N turns after that (introduced in c684c211a2).
Further, MP turn length was increased to 500ms compared to 200ms SP
turns (introduced in 6a15b78c98).
Unfortunately, increasing MP turn length has negative consequences:
- makes pathfinding/unit motion much worse & unit behaviour worse in
general.
- makes the game more 'lag-spikey', since computations are done less
often, but thus then can take more time.
This diff essentially reverts 6a15b78c98, instead increasing
COMMAND_DELAY from 2 to 4 in MP. This:
- Reduces the 'inherent command lag' in MP from 1000ms to 800ms
- Increases the lag range at which MP will run smoothtly from 500ms to
600ms
- makes SP and MP turns behave identically again, removing the
hindrances described above.
As a side effect, single-player was not actually using COMMAND_DELAY,
this is now done (can be used to simulate MP command lag).
Refs #3752
Differential Revision: https://code.wildfiregames.com/D3275
This was SVN commit r25001.
2021-03-03 22:02:57 +01:00
|
|
|
// Report if we have a bad ping to the server.
|
2016-02-04 18:14:46 +01:00
|
|
|
u32 meanRTT = m_Session->GetMeanRTT();
|
Increase MP Command delay to 4 turns, decrease MP turns to 200ms.
To hide network latency, MP turns send commands not for the next turn
but N turns after that (introduced in c684c211a2).
Further, MP turn length was increased to 500ms compared to 200ms SP
turns (introduced in 6a15b78c98).
Unfortunately, increasing MP turn length has negative consequences:
- makes pathfinding/unit motion much worse & unit behaviour worse in
general.
- makes the game more 'lag-spikey', since computations are done less
often, but thus then can take more time.
This diff essentially reverts 6a15b78c98, instead increasing
COMMAND_DELAY from 2 to 4 in MP. This:
- Reduces the 'inherent command lag' in MP from 1000ms to 800ms
- Increases the lag range at which MP will run smoothtly from 500ms to
600ms
- makes SP and MP turns behave identically again, removing the
hindrances described above.
As a side effect, single-player was not actually using COMMAND_DELAY,
this is now done (can be used to simulate MP command lag).
Refs #3752
Differential Revision: https://code.wildfiregames.com/D3275
This was SVN commit r25001.
2021-03-03 22:02:57 +01:00
|
|
|
if (meanRTT > NETWORK_BAD_PING)
|
2016-02-04 18:14:46 +01:00
|
|
|
{
|
2019-09-07 18:51:44 +02:00
|
|
|
PushGuiMessage(
|
|
|
|
"type", "netwarn",
|
|
|
|
"warntype", "server-latency",
|
|
|
|
"meanRTT", meanRTT);
|
2016-02-04 18:14:46 +01:00
|
|
|
}
|
2009-04-11 19:00:39 +02:00
|
|
|
}
|
|
|
|
|
2014-11-09 16:16:00 +01:00
|
|
|
void CNetClient::GuiPoll(JS::MutableHandleValue ret)
|
2009-04-11 19:00:39 +02:00
|
|
|
{
|
2010-06-30 23:41:04 +02:00
|
|
|
if (m_GuiMessageQueue.empty())
|
2014-11-09 16:16:00 +01:00
|
|
|
{
|
|
|
|
ret.setUndefined();
|
|
|
|
return;
|
|
|
|
}
|
2009-04-11 19:00:39 +02:00
|
|
|
|
2014-11-09 16:16:00 +01:00
|
|
|
ret.set(m_GuiMessageQueue.front());
|
2010-06-30 23:41:04 +02:00
|
|
|
m_GuiMessageQueue.pop_front();
|
2009-04-11 19:00:39 +02:00
|
|
|
}
|
|
|
|
|
2015-02-14 02:49:34 +01:00
|
|
|
std::string CNetClient::TestReadGuiMessages()
|
2010-06-30 23:41:04 +02:00
|
|
|
{
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 19:29:17 +01:00
|
|
|
ScriptRequest rq(GetScriptInterface());
|
2015-12-21 14:58:32 +01:00
|
|
|
|
2015-02-14 02:49:34 +01:00
|
|
|
std::string r;
|
2020-11-13 14:18:22 +01:00
|
|
|
JS::RootedValue msg(rq.cx);
|
2010-06-30 23:41:04 +02:00
|
|
|
while (true)
|
2009-04-11 19:00:39 +02:00
|
|
|
{
|
2014-11-09 16:16:00 +01:00
|
|
|
GuiPoll(&msg);
|
2014-08-03 19:29:49 +02:00
|
|
|
if (msg.isUndefined())
|
2010-06-30 23:41:04 +02:00
|
|
|
break;
|
2021-05-14 12:18:03 +02:00
|
|
|
r += Script::ToString(rq, &msg) + "\n";
|
2009-04-11 19:00:39 +02:00
|
|
|
}
|
2010-06-30 23:41:04 +02:00
|
|
|
return r;
|
2009-04-11 19:00:39 +02:00
|
|
|
}
|
|
|
|
|
2017-11-25 07:49:58 +01:00
|
|
|
const ScriptInterface& CNetClient::GetScriptInterface()
|
2009-04-11 19:00:39 +02:00
|
|
|
{
|
2010-06-30 23:41:04 +02:00
|
|
|
return m_Game->GetSimulation2()->GetScriptInterface();
|
|
|
|
}
|
2009-04-11 19:00:39 +02:00
|
|
|
|
2010-06-30 23:41:04 +02:00
|
|
|
void CNetClient::PostPlayerAssignmentsToScript()
|
|
|
|
{
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 19:29:17 +01:00
|
|
|
ScriptRequest rq(GetScriptInterface());
|
2015-12-21 14:58:32 +01:00
|
|
|
|
2020-11-13 14:18:22 +01:00
|
|
|
JS::RootedValue newAssignments(rq.cx);
|
2021-05-13 19:23:52 +02:00
|
|
|
Script::CreateObject(rq, &newAssignments);
|
2009-04-11 19:00:39 +02:00
|
|
|
|
2020-12-31 15:27:02 +01:00
|
|
|
for (const std::pair<const CStr, PlayerAssignment>& p : m_PlayerAssignments)
|
2009-04-11 19:00:39 +02:00
|
|
|
{
|
2020-11-13 14:18:22 +01:00
|
|
|
JS::RootedValue assignment(rq.cx);
|
2019-07-22 21:35:14 +02:00
|
|
|
|
2021-05-13 19:23:52 +02:00
|
|
|
Script::CreateObject(
|
2020-11-13 14:18:22 +01:00
|
|
|
rq,
|
2019-07-22 21:35:14 +02:00
|
|
|
&assignment,
|
2019-09-13 02:56:51 +02:00
|
|
|
"name", p.second.m_Name,
|
2019-07-22 21:35:14 +02:00
|
|
|
"player", p.second.m_PlayerID,
|
|
|
|
"status", p.second.m_Status);
|
|
|
|
|
2021-05-13 19:23:52 +02:00
|
|
|
Script::SetProperty(rq, newAssignments, p.first.c_str(), assignment);
|
2009-04-11 19:00:39 +02:00
|
|
|
}
|
|
|
|
|
2019-09-07 18:51:44 +02:00
|
|
|
PushGuiMessage(
|
|
|
|
"type", "players",
|
|
|
|
"newAssignments", newAssignments);
|
2009-04-11 19:00:39 +02:00
|
|
|
}
|
|
|
|
|
2010-06-30 23:41:04 +02:00
|
|
|
bool CNetClient::SendMessage(const CNetMessage* message)
|
2010-05-20 02:59:01 +02:00
|
|
|
{
|
2010-07-06 21:54:17 +02:00
|
|
|
if (!m_Session)
|
|
|
|
return false;
|
|
|
|
|
2010-06-30 23:41:04 +02:00
|
|
|
return m_Session->SendMessage(message);
|
2010-05-20 02:59:01 +02:00
|
|
|
}
|
|
|
|
|
2010-06-30 23:41:04 +02:00
|
|
|
void CNetClient::HandleConnect()
|
2009-04-11 19:00:39 +02:00
|
|
|
{
|
2010-06-30 23:41:04 +02:00
|
|
|
Update((uint)NMT_CONNECT_COMPLETE, NULL);
|
|
|
|
}
|
2009-04-11 19:00:39 +02:00
|
|
|
|
2010-07-06 21:54:17 +02:00
|
|
|
void CNetClient::HandleDisconnect(u32 reason)
|
2010-06-30 23:41:04 +02:00
|
|
|
{
|
2019-09-07 18:51:44 +02:00
|
|
|
PushGuiMessage(
|
|
|
|
"type", "netstatus",
|
|
|
|
"status", "disconnected",
|
|
|
|
"reason", reason);
|
2010-07-06 21:54:17 +02:00
|
|
|
|
2021-01-06 16:26:11 +01:00
|
|
|
DestroyConnection();
|
2010-07-06 21:54:17 +02:00
|
|
|
|
|
|
|
// Update the state immediately to UNCONNECTED (don't bother with FSM transitions since
|
|
|
|
// we'd need one for every single state, and we don't need to use per-state actions)
|
|
|
|
SetCurrState(NCS_UNCONNECTED);
|
2010-07-02 23:28:48 +02:00
|
|
|
}
|
|
|
|
|
2017-11-25 07:49:58 +01:00
|
|
|
void CNetClient::SendGameSetupMessage(JS::MutableHandleValue attrs, const ScriptInterface& scriptInterface)
|
2016-06-04 14:08:30 +02:00
|
|
|
{
|
|
|
|
CGameSetupMessage gameSetup(scriptInterface);
|
|
|
|
gameSetup.m_Data = attrs;
|
|
|
|
SendMessage(&gameSetup);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CNetClient::SendAssignPlayerMessage(const int playerID, const CStr& guid)
|
|
|
|
{
|
|
|
|
CAssignPlayerMessage assignPlayer;
|
|
|
|
assignPlayer.m_PlayerID = playerID;
|
|
|
|
assignPlayer.m_GUID = guid;
|
|
|
|
SendMessage(&assignPlayer);
|
|
|
|
}
|
|
|
|
|
2010-07-02 23:28:48 +02:00
|
|
|
void CNetClient::SendChatMessage(const std::wstring& text)
|
|
|
|
{
|
|
|
|
CChatMessage chat;
|
|
|
|
chat.m_Message = text;
|
|
|
|
SendMessage(&chat);
|
2010-06-30 23:41:04 +02:00
|
|
|
}
|
2009-04-11 19:00:39 +02:00
|
|
|
|
2014-04-26 20:34:34 +02:00
|
|
|
void CNetClient::SendReadyMessage(const int status)
|
|
|
|
{
|
|
|
|
CReadyMessage readyStatus;
|
|
|
|
readyStatus.m_Status = status;
|
|
|
|
SendMessage(&readyStatus);
|
|
|
|
}
|
|
|
|
|
2016-06-04 14:08:30 +02:00
|
|
|
void CNetClient::SendClearAllReadyMessage()
|
|
|
|
{
|
|
|
|
CClearAllReadyMessage clearAllReady;
|
|
|
|
SendMessage(&clearAllReady);
|
|
|
|
}
|
|
|
|
|
2021-03-22 11:13:27 +01:00
|
|
|
void CNetClient::SendStartGameMessage(const CStr& initAttribs)
|
2016-06-04 14:08:30 +02:00
|
|
|
{
|
|
|
|
CGameStartMessage gameStart;
|
2021-03-22 11:13:27 +01:00
|
|
|
gameStart.m_InitAttributes = initAttribs;
|
2016-06-04 14:08:30 +02:00
|
|
|
SendMessage(&gameStart);
|
|
|
|
}
|
|
|
|
|
2015-05-03 04:06:17 +02:00
|
|
|
void CNetClient::SendRejoinedMessage()
|
|
|
|
{
|
|
|
|
CRejoinedMessage rejoinedMessage;
|
|
|
|
SendMessage(&rejoinedMessage);
|
|
|
|
}
|
|
|
|
|
2016-06-04 14:08:30 +02:00
|
|
|
void CNetClient::SendKickPlayerMessage(const CStrW& playerName, bool ban)
|
|
|
|
{
|
|
|
|
CKickedMessage kickPlayer;
|
|
|
|
kickPlayer.m_Name = playerName;
|
|
|
|
kickPlayer.m_Ban = ban;
|
|
|
|
SendMessage(&kickPlayer);
|
|
|
|
}
|
|
|
|
|
2016-05-20 00:10:38 +02:00
|
|
|
void CNetClient::SendPausedMessage(bool pause)
|
|
|
|
{
|
|
|
|
CClientPausedMessage pausedMessage;
|
|
|
|
pausedMessage.m_Pause = pause;
|
|
|
|
SendMessage(&pausedMessage);
|
|
|
|
}
|
|
|
|
|
2010-06-30 23:41:04 +02:00
|
|
|
bool CNetClient::HandleMessage(CNetMessage* message)
|
|
|
|
{
|
2011-10-27 18:46:48 +02:00
|
|
|
// Handle non-FSM messages first
|
2013-11-07 21:07:24 +01:00
|
|
|
|
2019-09-17 16:18:46 +02:00
|
|
|
Status status = m_Session->GetFileTransferer().HandleMessageReceive(*message);
|
2011-10-27 18:46:48 +02:00
|
|
|
if (status == INFO::OK)
|
|
|
|
return true;
|
|
|
|
if (status != INFO::SKIPPED)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (message->GetType() == NMT_FILE_TRANSFER_REQUEST)
|
|
|
|
{
|
2019-09-07 18:51:44 +02:00
|
|
|
CFileTransferRequestMessage* reqMessage = static_cast<CFileTransferRequestMessage*>(message);
|
2011-10-27 18:46:48 +02:00
|
|
|
|
|
|
|
// TODO: we should support different transfer request types, instead of assuming
|
|
|
|
// it's always requesting the simulation state
|
|
|
|
|
|
|
|
std::stringstream stream;
|
|
|
|
|
2015-01-22 21:31:30 +01:00
|
|
|
LOGMESSAGERENDER("Serializing game at turn %u for rejoining player", m_ClientTurnManager->GetCurrentTurn());
|
2011-10-27 18:46:48 +02:00
|
|
|
u32 turn = to_le32(m_ClientTurnManager->GetCurrentTurn());
|
|
|
|
stream.write((char*)&turn, sizeof(turn));
|
|
|
|
|
|
|
|
bool ok = m_Game->GetSimulation2()->SerializeState(stream);
|
|
|
|
ENSURE(ok);
|
|
|
|
|
|
|
|
// Compress the content with zlib to save bandwidth
|
|
|
|
// (TODO: if this is still too large, compressing with e.g. LZMA works much better)
|
|
|
|
std::string compressed;
|
|
|
|
CompressZLib(stream.str(), compressed, true);
|
|
|
|
|
|
|
|
m_Session->GetFileTransferer().StartResponse(reqMessage->m_RequestID, compressed);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-06-30 23:41:04 +02:00
|
|
|
// Update FSM
|
|
|
|
bool ok = Update(message->GetType(), message);
|
|
|
|
if (!ok)
|
2015-01-22 21:31:30 +01:00
|
|
|
LOGERROR("Net client: Error running FSM update (type=%d state=%d)", (int)message->GetType(), (int)GetCurrState());
|
2010-06-30 23:41:04 +02:00
|
|
|
return ok;
|
|
|
|
}
|
2009-04-11 19:00:39 +02:00
|
|
|
|
2010-06-30 23:41:04 +02:00
|
|
|
void CNetClient::LoadFinished()
|
|
|
|
{
|
2011-10-27 18:46:48 +02:00
|
|
|
if (!m_JoinSyncBuffer.empty())
|
|
|
|
{
|
2011-10-29 16:53:13 +02:00
|
|
|
// We're rejoining a game, and just finished loading the initial map,
|
|
|
|
// so deserialize the saved game state now
|
|
|
|
|
2011-10-27 18:46:48 +02:00
|
|
|
std::string state;
|
|
|
|
DecompressZLib(m_JoinSyncBuffer, state, true);
|
|
|
|
|
|
|
|
std::stringstream stream(state);
|
|
|
|
|
|
|
|
u32 turn;
|
|
|
|
stream.read((char*)&turn, sizeof(turn));
|
|
|
|
turn = to_le32(turn);
|
|
|
|
|
2015-01-22 21:31:30 +01:00
|
|
|
LOGMESSAGE("Rejoining client deserializing state at turn %u\n", turn);
|
2011-10-27 18:46:48 +02:00
|
|
|
|
|
|
|
bool ok = m_Game->GetSimulation2()->DeserializeState(stream);
|
|
|
|
ENSURE(ok);
|
|
|
|
|
|
|
|
m_ClientTurnManager->ResetState(turn, turn);
|
|
|
|
|
2019-09-07 18:51:44 +02:00
|
|
|
PushGuiMessage(
|
|
|
|
"type", "netstatus",
|
|
|
|
"status", "join_syncing");
|
2011-10-29 16:53:13 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Connecting at the start of a game, so we'll wait for other players to finish loading
|
2019-09-07 18:51:44 +02:00
|
|
|
PushGuiMessage(
|
|
|
|
"type", "netstatus",
|
|
|
|
"status", "waiting_for_players");
|
2011-10-29 16:53:13 +02:00
|
|
|
}
|
2009-04-11 19:00:39 +02:00
|
|
|
|
2010-06-30 23:41:04 +02:00
|
|
|
CLoadedGameMessage loaded;
|
2011-10-27 18:46:48 +02:00
|
|
|
loaded.m_CurrentTurn = m_ClientTurnManager->GetCurrentTurn();
|
2010-06-30 23:41:04 +02:00
|
|
|
SendMessage(&loaded);
|
2009-04-11 19:00:39 +02:00
|
|
|
}
|
|
|
|
|
2018-03-12 01:23:40 +01:00
|
|
|
void CNetClient::SendAuthenticateMessage()
|
|
|
|
{
|
|
|
|
CAuthenticateMessage authenticate;
|
|
|
|
authenticate.m_Name = m_UserName;
|
2021-01-23 19:04:36 +01:00
|
|
|
authenticate.m_Password = m_Password;
|
2021-02-27 18:44:59 +01:00
|
|
|
authenticate.m_ControllerSecret = m_ControllerSecret;
|
2018-03-12 01:23:40 +01:00
|
|
|
SendMessage(&authenticate);
|
|
|
|
}
|
|
|
|
|
2010-06-30 23:41:04 +02:00
|
|
|
bool CNetClient::OnConnect(void* context, CFsmEvent* event)
|
2009-04-11 19:00:39 +02:00
|
|
|
{
|
2011-04-30 15:01:45 +02:00
|
|
|
ENSURE(event->GetType() == (uint)NMT_CONNECT_COMPLETE);
|
2009-04-11 19:00:39 +02:00
|
|
|
|
2019-09-07 18:51:44 +02:00
|
|
|
CNetClient* client = static_cast<CNetClient*>(context);
|
2009-04-11 19:00:39 +02:00
|
|
|
|
2019-09-07 18:51:44 +02:00
|
|
|
client->PushGuiMessage(
|
|
|
|
"type", "netstatus",
|
|
|
|
"status", "connected");
|
2010-06-30 23:41:04 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2009-04-11 19:00:39 +02:00
|
|
|
|
2010-06-30 23:41:04 +02:00
|
|
|
bool CNetClient::OnHandshake(void* context, CFsmEvent* event)
|
|
|
|
{
|
2011-04-30 15:01:45 +02:00
|
|
|
ENSURE(event->GetType() == (uint)NMT_SERVER_HANDSHAKE);
|
2009-04-11 19:00:39 +02:00
|
|
|
|
2019-09-07 18:51:44 +02:00
|
|
|
CNetClient* client = static_cast<CNetClient*>(context);
|
2009-04-11 19:00:39 +02:00
|
|
|
|
2010-06-30 23:41:04 +02:00
|
|
|
CCliHandshakeMessage handshake;
|
|
|
|
handshake.m_MagicResponse = PS_PROTOCOL_MAGIC_RESPONSE;
|
|
|
|
handshake.m_ProtocolVersion = PS_PROTOCOL_VERSION;
|
|
|
|
handshake.m_SoftwareVersion = PS_PROTOCOL_VERSION;
|
|
|
|
client->SendMessage(&handshake);
|
2009-04-11 19:00:39 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-06-30 23:41:04 +02:00
|
|
|
bool CNetClient::OnHandshakeResponse(void* context, CFsmEvent* event)
|
2009-04-11 19:00:39 +02:00
|
|
|
{
|
2011-04-30 15:01:45 +02:00
|
|
|
ENSURE(event->GetType() == (uint)NMT_SERVER_HANDSHAKE_RESPONSE);
|
2009-04-11 19:00:39 +02:00
|
|
|
|
2019-09-07 18:51:44 +02:00
|
|
|
CNetClient* client = static_cast<CNetClient*>(context);
|
|
|
|
CSrvHandshakeResponseMessage* message = static_cast<CSrvHandshakeResponseMessage*>(event->GetParamRef());
|
2009-04-11 19:00:39 +02:00
|
|
|
|
2017-10-25 00:05:24 +02:00
|
|
|
client->m_GUID = message->m_GUID;
|
|
|
|
|
2018-03-12 01:23:40 +01:00
|
|
|
if (message->m_Flags & PS_NETWORK_FLAG_REQUIRE_LOBBYAUTH)
|
|
|
|
{
|
2021-05-09 14:51:32 +02:00
|
|
|
if (g_XmppClient && !client->m_HostJID.empty())
|
|
|
|
g_XmppClient->SendIqLobbyAuth(client->m_HostJID, client->m_GUID);
|
2018-03-12 01:23:40 +01:00
|
|
|
else
|
|
|
|
{
|
2019-09-07 18:51:44 +02:00
|
|
|
client->PushGuiMessage(
|
|
|
|
"type", "netstatus",
|
|
|
|
"status", "disconnected",
|
|
|
|
"reason", static_cast<i32>(NDR_LOBBY_AUTH_FAILED));
|
2018-03-12 01:23:40 +01:00
|
|
|
|
|
|
|
LOGMESSAGE("Net client: Couldn't send lobby auth xmpp message");
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2009-04-11 19:00:39 +02:00
|
|
|
|
2018-03-12 01:23:40 +01:00
|
|
|
client->SendAuthenticateMessage();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CNetClient::OnAuthenticateRequest(void* context, CFsmEvent* event)
|
|
|
|
{
|
|
|
|
ENSURE(event->GetType() == (uint)NMT_AUTHENTICATE);
|
|
|
|
|
2019-09-07 18:51:44 +02:00
|
|
|
CNetClient* client = static_cast<CNetClient*>(context);
|
2018-03-12 01:23:40 +01:00
|
|
|
client->SendAuthenticateMessage();
|
2010-06-30 23:41:04 +02:00
|
|
|
return true;
|
|
|
|
}
|
2009-04-11 19:00:39 +02:00
|
|
|
|
2010-06-30 23:41:04 +02:00
|
|
|
bool CNetClient::OnAuthenticate(void* context, CFsmEvent* event)
|
|
|
|
{
|
2011-04-30 15:01:45 +02:00
|
|
|
ENSURE(event->GetType() == (uint)NMT_AUTHENTICATE_RESULT);
|
2009-04-11 19:00:39 +02:00
|
|
|
|
2019-09-07 18:51:44 +02:00
|
|
|
CNetClient* client = static_cast<CNetClient*>(context);
|
|
|
|
CAuthenticateResultMessage* message = static_cast<CAuthenticateResultMessage*>(event->GetParamRef());
|
2009-04-11 19:00:39 +02:00
|
|
|
|
2015-01-22 21:37:38 +01:00
|
|
|
LOGMESSAGE("Net: Authentication result: host=%u, %s", message->m_HostID, utf8_from_wstring(message->m_Message));
|
2011-10-29 16:53:13 +02:00
|
|
|
|
2010-06-30 23:41:04 +02:00
|
|
|
client->m_HostID = message->m_HostID;
|
2016-05-19 23:03:46 +02:00
|
|
|
client->m_Rejoin = message->m_Code == ARC_OK_REJOINING;
|
2021-02-27 18:44:59 +01:00
|
|
|
client->m_IsController = message->m_IsController;
|
2009-04-11 19:00:39 +02:00
|
|
|
|
2019-09-07 18:51:44 +02:00
|
|
|
client->PushGuiMessage(
|
|
|
|
"type", "netstatus",
|
|
|
|
"status", "authenticated",
|
|
|
|
"rejoining", client->m_Rejoin);
|
2009-04-11 19:00:39 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-07-02 23:28:48 +02:00
|
|
|
bool CNetClient::OnChat(void* context, CFsmEvent* event)
|
|
|
|
{
|
2011-04-30 15:01:45 +02:00
|
|
|
ENSURE(event->GetType() == (uint)NMT_CHAT);
|
2010-07-02 23:28:48 +02:00
|
|
|
|
2019-09-07 18:51:44 +02:00
|
|
|
CNetClient* client = static_cast<CNetClient*>(context);
|
|
|
|
CChatMessage* message = static_cast<CChatMessage*>(event->GetParamRef());
|
2010-07-02 23:28:48 +02:00
|
|
|
|
2019-09-07 18:51:44 +02:00
|
|
|
client->PushGuiMessage(
|
|
|
|
"type", "chat",
|
|
|
|
"guid", message->m_GUID,
|
|
|
|
"text", message->m_Message);
|
2010-07-02 23:28:48 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-04-26 20:34:34 +02:00
|
|
|
bool CNetClient::OnReady(void* context, CFsmEvent* event)
|
|
|
|
{
|
|
|
|
ENSURE(event->GetType() == (uint)NMT_READY);
|
|
|
|
|
2019-09-07 18:51:44 +02:00
|
|
|
CNetClient* client = static_cast<CNetClient*>(context);
|
|
|
|
CReadyMessage* message = static_cast<CReadyMessage*>(event->GetParamRef());
|
2014-04-26 20:34:34 +02:00
|
|
|
|
2019-09-07 18:51:44 +02:00
|
|
|
client->PushGuiMessage(
|
|
|
|
"type", "ready",
|
|
|
|
"guid", message->m_GUID,
|
|
|
|
"status", message->m_Status);
|
2014-04-26 20:34:34 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-06-30 23:41:04 +02:00
|
|
|
bool CNetClient::OnGameSetup(void* context, CFsmEvent* event)
|
2009-04-11 19:00:39 +02:00
|
|
|
{
|
2011-04-30 15:01:45 +02:00
|
|
|
ENSURE(event->GetType() == (uint)NMT_GAME_SETUP);
|
2009-04-11 19:00:39 +02:00
|
|
|
|
2019-09-07 18:51:44 +02:00
|
|
|
CNetClient* client = static_cast<CNetClient*>(context);
|
|
|
|
CGameSetupMessage* message = static_cast<CGameSetupMessage*>(event->GetParamRef());
|
2009-04-11 19:00:39 +02:00
|
|
|
|
2019-09-07 18:51:44 +02:00
|
|
|
client->PushGuiMessage(
|
|
|
|
"type", "gamesetup",
|
|
|
|
"data", message->m_Data);
|
2009-04-11 19:00:39 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-06-30 23:41:04 +02:00
|
|
|
bool CNetClient::OnPlayerAssignment(void* context, CFsmEvent* event)
|
2009-04-11 19:00:39 +02:00
|
|
|
{
|
2011-04-30 15:01:45 +02:00
|
|
|
ENSURE(event->GetType() == (uint)NMT_PLAYER_ASSIGNMENT);
|
2009-04-11 19:00:39 +02:00
|
|
|
|
2019-09-07 18:51:44 +02:00
|
|
|
CNetClient* client = static_cast<CNetClient*>(context);
|
|
|
|
CPlayerAssignmentMessage* message = static_cast<CPlayerAssignmentMessage*>(event->GetParamRef());
|
2009-04-11 19:00:39 +02:00
|
|
|
|
2010-06-30 23:41:04 +02:00
|
|
|
// Unpack the message
|
2010-07-02 23:28:48 +02:00
|
|
|
PlayerAssignmentMap newPlayerAssignments;
|
2010-06-30 23:41:04 +02:00
|
|
|
for (size_t i = 0; i < message->m_Hosts.size(); ++i)
|
|
|
|
{
|
|
|
|
PlayerAssignment assignment;
|
2011-10-27 18:46:48 +02:00
|
|
|
assignment.m_Enabled = true;
|
2010-06-30 23:41:04 +02:00
|
|
|
assignment.m_Name = message->m_Hosts[i].m_Name;
|
|
|
|
assignment.m_PlayerID = message->m_Hosts[i].m_PlayerID;
|
2014-04-26 20:34:34 +02:00
|
|
|
assignment.m_Status = message->m_Hosts[i].m_Status;
|
2010-07-02 23:28:48 +02:00
|
|
|
newPlayerAssignments[message->m_Hosts[i].m_GUID] = assignment;
|
2009-04-11 19:00:39 +02:00
|
|
|
}
|
|
|
|
|
2010-07-02 23:28:48 +02:00
|
|
|
client->m_PlayerAssignments.swap(newPlayerAssignments);
|
|
|
|
|
2010-06-30 23:41:04 +02:00
|
|
|
client->PostPlayerAssignmentsToScript();
|
|
|
|
|
2009-04-11 19:00:39 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
Prevent players from disconnecting during the loading screen by increasing the timeout tolerance to 60 seconds for that period, fixes #5163.
The NetClient runs in the main thread, so any part of the loading screen
consuming several seconds makes that client timeout.
This is a workaround because threading the NetClient would have prevent
these timeouts, refs #3700.
Coutnerintuitively, since enet timeout tolerance is proportional to the
latency, the better the connection of the player, the more likely it was
to drop on gamestart.
This problem became very frequent in Alpha 23, at least due to the Aura
bugfix 583b6ec625, AIInterface being particularly slow and that not
having been disabled yet in the loading screen resulting in additional
10 second freezes during the loading screen, even on empty maps, refs
#5200, 8e168f85e6.
Differential Revision: https://code.wildfiregames.com/D1513
Based on patch by: causative
This was SVN commit r21842.
2018-06-07 00:09:38 +02:00
|
|
|
// This is called either when the host clicks the StartGame button or
|
|
|
|
// if this client rejoins and finishes the download of the simstate.
|
2010-06-30 23:41:04 +02:00
|
|
|
bool CNetClient::OnGameStart(void* context, CFsmEvent* event)
|
2010-05-20 02:59:01 +02:00
|
|
|
{
|
2011-04-30 15:01:45 +02:00
|
|
|
ENSURE(event->GetType() == (uint)NMT_GAME_START);
|
2010-05-20 02:59:01 +02:00
|
|
|
|
2019-09-07 18:51:44 +02:00
|
|
|
CNetClient* client = static_cast<CNetClient*>(context);
|
2021-03-22 11:13:27 +01:00
|
|
|
CGameStartMessage* message = static_cast<CGameStartMessage*>(event->GetParamRef());
|
2010-06-30 23:41:04 +02:00
|
|
|
|
|
|
|
// Find the player assigned to our GUID
|
|
|
|
int player = -1;
|
|
|
|
if (client->m_PlayerAssignments.find(client->m_GUID) != client->m_PlayerAssignments.end())
|
|
|
|
player = client->m_PlayerAssignments[client->m_GUID].m_PlayerID;
|
2009-04-11 19:00:39 +02:00
|
|
|
|
2010-08-07 00:16:05 +02:00
|
|
|
client->m_ClientTurnManager = new CNetClientTurnManager(
|
|
|
|
*client->m_Game->GetSimulation2(), *client, client->m_HostID, client->m_Game->GetReplayLogger());
|
2009-04-11 19:00:39 +02:00
|
|
|
|
2021-03-22 11:13:27 +01:00
|
|
|
// Parse init attributes.
|
|
|
|
const ScriptInterface& scriptInterface = client->m_Game->GetSimulation2()->GetScriptInterface();
|
|
|
|
ScriptRequest rq(scriptInterface);
|
|
|
|
JS::RootedValue initAttribs(rq.cx);
|
2021-05-14 12:18:03 +02:00
|
|
|
Script::ParseJSON(rq, message->m_InitAttributes, &initAttribs);
|
2021-03-22 11:13:27 +01:00
|
|
|
|
2010-06-30 23:41:04 +02:00
|
|
|
client->m_Game->SetPlayerID(player);
|
2021-03-22 11:13:27 +01:00
|
|
|
client->m_Game->StartGame(&initAttribs, "");
|
2009-04-11 19:00:39 +02:00
|
|
|
|
2021-03-22 11:13:27 +01:00
|
|
|
client->PushGuiMessage("type", "start",
|
|
|
|
"initAttributes", initAttribs);
|
2010-05-20 02:59:01 +02:00
|
|
|
|
2009-04-11 19:00:39 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-10-27 18:46:48 +02:00
|
|
|
bool CNetClient::OnJoinSyncStart(void* context, CFsmEvent* event)
|
|
|
|
{
|
|
|
|
ENSURE(event->GetType() == (uint)NMT_JOIN_SYNC_START);
|
|
|
|
|
2019-09-07 18:51:44 +02:00
|
|
|
CNetClient* client = static_cast<CNetClient*>(context);
|
2011-10-27 18:46:48 +02:00
|
|
|
|
2021-03-22 11:13:27 +01:00
|
|
|
CJoinSyncStartMessage* joinSyncStartMessage = (CJoinSyncStartMessage*)event->GetParamRef();
|
|
|
|
|
2011-10-27 18:46:48 +02:00
|
|
|
// The server wants us to start downloading the game state from it, so do so
|
|
|
|
client->m_Session->GetFileTransferer().StartTask(
|
2021-03-22 11:13:27 +01:00
|
|
|
shared_ptr<CNetFileReceiveTask>(new CNetFileReceiveTask_ClientRejoin(*client, joinSyncStartMessage->m_InitAttributes))
|
2011-10-27 18:46:48 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CNetClient::OnJoinSyncEndCommandBatch(void* context, CFsmEvent* event)
|
|
|
|
{
|
|
|
|
ENSURE(event->GetType() == (uint)NMT_END_COMMAND_BATCH);
|
|
|
|
|
2019-09-07 18:51:44 +02:00
|
|
|
CNetClient* client = static_cast<CNetClient*>(context);
|
2011-10-27 18:46:48 +02:00
|
|
|
|
|
|
|
CEndCommandBatchMessage* endMessage = (CEndCommandBatchMessage*)event->GetParamRef();
|
|
|
|
|
|
|
|
client->m_ClientTurnManager->FinishedAllCommands(endMessage->m_Turn, endMessage->m_TurnLength);
|
2013-11-07 21:07:24 +01:00
|
|
|
|
2011-10-27 18:46:48 +02:00
|
|
|
// Execute all the received commands for the latest turn
|
|
|
|
client->m_ClientTurnManager->UpdateFastForward();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-03-15 05:37:41 +01:00
|
|
|
bool CNetClient::OnRejoined(void* context, CFsmEvent* event)
|
2015-05-03 04:06:17 +02:00
|
|
|
{
|
|
|
|
ENSURE(event->GetType() == (uint)NMT_REJOINED);
|
|
|
|
|
2019-09-07 18:51:44 +02:00
|
|
|
CNetClient* client = static_cast<CNetClient*>(context);
|
|
|
|
CRejoinedMessage* message = static_cast<CRejoinedMessage*>(event->GetParamRef());
|
2015-05-03 04:06:17 +02:00
|
|
|
|
2019-09-07 18:51:44 +02:00
|
|
|
client->PushGuiMessage(
|
|
|
|
"type", "rejoined",
|
|
|
|
"guid", message->m_GUID);
|
2015-05-03 04:06:17 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-11-11 03:05:23 +01:00
|
|
|
bool CNetClient::OnKicked(void *context, CFsmEvent* event)
|
|
|
|
{
|
|
|
|
ENSURE(event->GetType() == (uint)NMT_KICKED);
|
|
|
|
|
2019-09-07 18:51:44 +02:00
|
|
|
CNetClient* client = static_cast<CNetClient*>(context);
|
|
|
|
CKickedMessage* message = static_cast<CKickedMessage*>(event->GetParamRef());
|
2015-11-11 03:05:23 +01:00
|
|
|
|
2019-09-07 18:51:44 +02:00
|
|
|
client->PushGuiMessage(
|
2019-07-22 21:35:14 +02:00
|
|
|
"username", message->m_Name,
|
2019-09-07 18:51:44 +02:00
|
|
|
"type", "kicked",
|
2019-07-22 21:35:14 +02:00
|
|
|
"banned", message->m_Ban != 0);
|
|
|
|
|
2015-11-11 03:05:23 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-02-04 18:14:46 +01:00
|
|
|
bool CNetClient::OnClientTimeout(void *context, CFsmEvent* event)
|
|
|
|
{
|
|
|
|
// Report the timeout of some other client
|
|
|
|
|
|
|
|
ENSURE(event->GetType() == (uint)NMT_CLIENT_TIMEOUT);
|
|
|
|
|
2019-09-07 18:51:44 +02:00
|
|
|
CNetClient* client = static_cast<CNetClient*>(context);
|
|
|
|
CClientTimeoutMessage* message = static_cast<CClientTimeoutMessage*>(event->GetParamRef());
|
2016-02-04 18:14:46 +01:00
|
|
|
|
2019-09-07 18:51:44 +02:00
|
|
|
client->PushGuiMessage(
|
|
|
|
"type", "netwarn",
|
|
|
|
"warntype", "client-timeout",
|
|
|
|
"guid", message->m_GUID,
|
|
|
|
"lastReceivedTime", message->m_LastReceivedTime);
|
2016-02-04 18:14:46 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CNetClient::OnClientPerformance(void *context, CFsmEvent* event)
|
|
|
|
{
|
|
|
|
// Performance statistics for one or multiple clients
|
|
|
|
|
|
|
|
ENSURE(event->GetType() == (uint)NMT_CLIENT_PERFORMANCE);
|
|
|
|
|
2019-09-07 18:51:44 +02:00
|
|
|
CNetClient* client = static_cast<CNetClient*>(context);
|
|
|
|
CClientPerformanceMessage* message = static_cast<CClientPerformanceMessage*>(event->GetParamRef());
|
2016-02-04 18:14:46 +01:00
|
|
|
|
|
|
|
// Display warnings for other clients with bad ping
|
2016-04-24 22:48:53 +02:00
|
|
|
for (size_t i = 0; i < message->m_Clients.size(); ++i)
|
2016-02-04 18:14:46 +01:00
|
|
|
{
|
Increase MP Command delay to 4 turns, decrease MP turns to 200ms.
To hide network latency, MP turns send commands not for the next turn
but N turns after that (introduced in c684c211a2).
Further, MP turn length was increased to 500ms compared to 200ms SP
turns (introduced in 6a15b78c98).
Unfortunately, increasing MP turn length has negative consequences:
- makes pathfinding/unit motion much worse & unit behaviour worse in
general.
- makes the game more 'lag-spikey', since computations are done less
often, but thus then can take more time.
This diff essentially reverts 6a15b78c98, instead increasing
COMMAND_DELAY from 2 to 4 in MP. This:
- Reduces the 'inherent command lag' in MP from 1000ms to 800ms
- Increases the lag range at which MP will run smoothtly from 500ms to
600ms
- makes SP and MP turns behave identically again, removing the
hindrances described above.
As a side effect, single-player was not actually using COMMAND_DELAY,
this is now done (can be used to simulate MP command lag).
Refs #3752
Differential Revision: https://code.wildfiregames.com/D3275
This was SVN commit r25001.
2021-03-03 22:02:57 +01:00
|
|
|
if (message->m_Clients[i].m_MeanRTT < NETWORK_BAD_PING || message->m_Clients[i].m_GUID == client->m_GUID)
|
2016-02-04 18:14:46 +01:00
|
|
|
continue;
|
|
|
|
|
2019-09-07 18:51:44 +02:00
|
|
|
client->PushGuiMessage(
|
|
|
|
"type", "netwarn",
|
|
|
|
"warntype", "client-latency",
|
|
|
|
"guid", message->m_Clients[i].m_GUID,
|
|
|
|
"meanRTT", message->m_Clients[i].m_MeanRTT);
|
2016-02-04 18:14:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-21 19:50:29 +01:00
|
|
|
bool CNetClient::OnClientsLoading(void *context, CFsmEvent *event)
|
|
|
|
{
|
|
|
|
ENSURE(event->GetType() == (uint)NMT_CLIENTS_LOADING);
|
|
|
|
|
2019-09-07 18:51:44 +02:00
|
|
|
CNetClient* client = static_cast<CNetClient*>(context);
|
|
|
|
CClientsLoadingMessage* message = static_cast<CClientsLoadingMessage*>(event->GetParamRef());
|
2017-03-21 19:50:29 +01:00
|
|
|
|
Prevent players from disconnecting during the loading screen by increasing the timeout tolerance to 60 seconds for that period, fixes #5163.
The NetClient runs in the main thread, so any part of the loading screen
consuming several seconds makes that client timeout.
This is a workaround because threading the NetClient would have prevent
these timeouts, refs #3700.
Coutnerintuitively, since enet timeout tolerance is proportional to the
latency, the better the connection of the player, the more likely it was
to drop on gamestart.
This problem became very frequent in Alpha 23, at least due to the Aura
bugfix 583b6ec625, AIInterface being particularly slow and that not
having been disabled yet in the loading screen resulting in additional
10 second freezes during the loading screen, even on empty maps, refs
#5200, 8e168f85e6.
Differential Revision: https://code.wildfiregames.com/D1513
Based on patch by: causative
This was SVN commit r21842.
2018-06-07 00:09:38 +02:00
|
|
|
std::vector<CStr> guids;
|
|
|
|
guids.reserve(message->m_Clients.size());
|
|
|
|
for (const CClientsLoadingMessage::S_m_Clients& mClient : message->m_Clients)
|
|
|
|
guids.push_back(mClient.m_GUID);
|
|
|
|
|
2019-09-07 18:51:44 +02:00
|
|
|
client->PushGuiMessage(
|
|
|
|
"type", "clients-loading",
|
|
|
|
"guids", guids);
|
2017-03-21 19:50:29 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-20 00:10:38 +02:00
|
|
|
bool CNetClient::OnClientPaused(void *context, CFsmEvent *event)
|
|
|
|
{
|
|
|
|
ENSURE(event->GetType() == (uint)NMT_CLIENT_PAUSED);
|
|
|
|
|
2019-09-07 18:51:44 +02:00
|
|
|
CNetClient* client = static_cast<CNetClient*>(context);
|
|
|
|
CClientPausedMessage* message = static_cast<CClientPausedMessage*>(event->GetParamRef());
|
2016-05-20 00:10:38 +02:00
|
|
|
|
2019-09-07 18:51:44 +02:00
|
|
|
client->PushGuiMessage(
|
|
|
|
"type", "paused",
|
|
|
|
"pause", message->m_Pause != 0,
|
|
|
|
"guid", message->m_GUID);
|
2016-05-20 00:10:38 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-06-30 23:41:04 +02:00
|
|
|
bool CNetClient::OnLoadedGame(void* context, CFsmEvent* event)
|
2009-04-11 19:00:39 +02:00
|
|
|
{
|
2011-04-30 15:01:45 +02:00
|
|
|
ENSURE(event->GetType() == (uint)NMT_LOADED_GAME);
|
2009-04-11 19:00:39 +02:00
|
|
|
|
2019-09-07 18:51:44 +02:00
|
|
|
CNetClient* client = static_cast<CNetClient*>(context);
|
2009-04-11 19:00:39 +02:00
|
|
|
|
2010-06-30 23:41:04 +02:00
|
|
|
// All players have loaded the game - start running the turn manager
|
|
|
|
// so that the game begins
|
|
|
|
client->m_Game->SetTurnManager(client->m_ClientTurnManager);
|
2010-07-02 23:28:48 +02:00
|
|
|
|
2019-09-07 18:51:44 +02:00
|
|
|
client->PushGuiMessage(
|
|
|
|
"type", "netstatus",
|
|
|
|
"status", "active");
|
2010-06-30 23:41:04 +02:00
|
|
|
|
2016-05-19 23:03:46 +02:00
|
|
|
// If we have rejoined an in progress game, send the rejoined message to the server.
|
|
|
|
if (client->m_Rejoin)
|
|
|
|
client->SendRejoinedMessage();
|
|
|
|
|
2010-06-30 23:41:04 +02:00
|
|
|
return true;
|
2009-04-11 19:00:39 +02:00
|
|
|
}
|
|
|
|
|
2010-06-30 23:41:04 +02:00
|
|
|
bool CNetClient::OnInGame(void *context, CFsmEvent* event)
|
2009-04-11 19:00:39 +02:00
|
|
|
{
|
2010-06-30 23:41:04 +02:00
|
|
|
// TODO: should split each of these cases into a separate method
|
2009-04-11 19:00:39 +02:00
|
|
|
|
2019-09-07 18:51:44 +02:00
|
|
|
CNetClient* client = static_cast<CNetClient*>(context);
|
|
|
|
CNetMessage* message = static_cast<CNetMessage*>(event->GetParamRef());
|
2010-06-30 23:41:04 +02:00
|
|
|
|
|
|
|
if (message)
|
2009-04-11 19:00:39 +02:00
|
|
|
{
|
2010-06-30 23:41:04 +02:00
|
|
|
if (message->GetType() == NMT_SIMULATION_COMMAND)
|
|
|
|
{
|
|
|
|
CSimulationMessage* simMessage = static_cast<CSimulationMessage*> (message);
|
|
|
|
client->m_ClientTurnManager->OnSimulationMessage(simMessage);
|
|
|
|
}
|
|
|
|
else if (message->GetType() == NMT_SYNC_ERROR)
|
|
|
|
{
|
|
|
|
CSyncErrorMessage* syncMessage = static_cast<CSyncErrorMessage*> (message);
|
2015-08-30 19:47:18 +02:00
|
|
|
client->m_ClientTurnManager->OnSyncError(syncMessage->m_Turn, syncMessage->m_HashExpected, syncMessage->m_PlayerNames);
|
2010-06-30 23:41:04 +02:00
|
|
|
}
|
|
|
|
else if (message->GetType() == NMT_END_COMMAND_BATCH)
|
|
|
|
{
|
|
|
|
CEndCommandBatchMessage* endMessage = static_cast<CEndCommandBatchMessage*> (message);
|
2010-08-13 18:42:53 +02:00
|
|
|
client->m_ClientTurnManager->FinishedAllCommands(endMessage->m_Turn, endMessage->m_TurnLength);
|
2010-06-30 23:41:04 +02:00
|
|
|
}
|
2009-04-11 19:00:39 +02:00
|
|
|
}
|
|
|
|
|
2010-06-30 23:41:04 +02:00
|
|
|
return true;
|
2009-04-11 19:00:39 +02:00
|
|
|
}
|