34#include <js/RootingAPI.h>
35#include <js/TypeDecls.h>
48namespace Script {
class Interface; }
83 CNetClient(
CGame* game, std::string serverAddressOrHostname, std::uint16_t serverPont,
84 const CStrW& username = L
"anonymous",
const CStr& hostJID = {},
85 std::string hashedPassword = {}, std::string controllerSecret = {});
93 std::string hashedPassword,
XmppClient& xmppClient);
114 const CStr& hostJID,
bool localNetwork);
161 template<
typename... Args>
166 JS::RootedValue message(rq.
cx);
220 void SendChatMessage(
const std::wstring& text, std::optional<std::vector<std::string>> receivers);
233 void SendFlareMessage(
const CStr& positionX,
const CStr& positionY,
const CStr& positionZ);
263 std::uint16_t serverPont,
const CStrW& username = L
"anonymous",
const CStr& hostJID = {},
264 std::string hashedPassword = {}, std::string controllerSecret = {});
307 void StartGame(
const JS::MutableHandleValue initAttributes,
const std::string& savedState);
343 std::uint32_t
m_HostID{
static_cast<std::uint32_t
>(-1)};
struct _ENetHost ENetHost
Definition: NetClient.h:50
@ NCS_UNCONNECTED
Definition: NetClient.h:55
@ NCS_PREGAME
Definition: NetClient.h:59
@ NCS_JOIN_SYNCING
Definition: NetClient.h:61
@ NCS_CONNECT
Definition: NetClient.h:56
@ NCS_LOADING
Definition: NetClient.h:60
@ NCS_AUTHENTICATE
Definition: NetClient.h:58
@ NCS_INGAME
Definition: NetClient.h:62
@ NCS_HANDSHAKE
Definition: NetClient.h:57
CNetClient * g_NetClient
Global network client for the standard game.
Definition: NetClient.cpp:68
Various declarations shared by networking code.
std::map< CStr, PlayerAssignment > PlayerAssignmentMap
Definition: NetHost.h:56
Represents a signal in the state machine that a change has occurred.
Definition: FSM.h:34
Manages states, events, actions and transitions between states.
Definition: FSM.h:69
The container that holds the rules, resources and attributes of the game.
Definition: Game.h:49
The client end of a network session.
Definition: NetClientSession.h:51
Implementation of CTurnManager for network clients.
Definition: NetClientTurnManager.h:37
Network client.
Definition: NetClient.h:73
void CheckServerConnection()
Locally triggers a GUI message if the connection to the server is being lost or has bad latency.
Definition: NetClient.cpp:376
void HandleDisconnect(std::uint32_t reason)
Call when the network connection has been lost.
Definition: NetClient.cpp:510
static bool OnClientTimeout(CNetClient *client, CFsmEvent< CNetMessage * > *event)
Definition: NetClient.cpp:985
static bool OnClientPerformance(CNetClient *client, CFsmEvent< CNetMessage * > *event)
Definition: NetClient.cpp:1002
void SendChatMessage(const std::wstring &text, std::optional< std::vector< std::string > > receivers)
Definition: NetClient.cpp:550
static bool OnConnect(CNetClient *client, CFsmEvent< CNetMessage * > *event)
Definition: NetClient.cpp:732
const Script::Interface & GetScriptInterface()
Get the script interface associated with this network client, which is equivalent to the one used by ...
Definition: NetClient.cpp:466
static bool OnHandshakeResponse(CNetClient *client, CFsmEvent< CNetMessage * > *event)
Definition: NetClient.cpp:754
static bool OnGameStart(CNetClient *client, CFsmEvent< CNetMessage * > *event)
Definition: NetClient.cpp:882
static bool OnClientsLoading(CNetClient *client, CFsmEvent< CNetMessage * > *event)
Definition: NetClient.cpp:1026
static bool OnAuthenticate(CNetClient *client, CFsmEvent< CNetMessage * > *event)
Definition: NetClient.cpp:792
void SendAssignPlayerMessage(const int playerID, const CStr &guid)
Definition: NetClient.cpp:542
std::string TestReadGuiMessages()
Return a concatenation of all messages in the GUI queue, for test cases to easily verify the queue co...
Definition: NetClient.cpp:447
std::uint16_t m_ServerPort
Definition: NetClient.h:321
void SendStartSavedGameMessage(const CStr &initAttribs, const CStr &savedState)
Definition: NetClient.cpp:593
CStrW m_UserName
Definition: NetClient.h:317
CStr GetGUID() const
Returns the GUID of the local client.
Definition: NetClient.h:104
void PushGuiMessage(Args const &... args)
Add a message to the queue, to be read by GuiPoll.
Definition: NetClient.h:162
static bool OnFlare(CNetClient *client, CFsmEvent< CNetMessage * > *event)
Definition: NetClient.cpp:1104
void SendKickPlayerMessage(const CStrW &playerName, bool ban)
Call to kick/ban a client.
Definition: NetClient.cpp:608
std::string m_JoinSyncBuffer
Serialized game state received when joining an in-progress game.
Definition: NetClient.h:370
static bool OnGameSetup(CNetClient *client, CFsmEvent< CNetMessage * > *event)
Definition: NetClient.cpp:842
bool IsController() const
Definition: NetClient.h:98
static bool OnInGame(CNetClient *client, CFsmEvent< CNetMessage * > *event)
Definition: NetClient.cpp:1076
void PostPlayerAssignmentsToScript()
Push a message onto the GUI queue listing the current player assignments.
Definition: NetClient.cpp:471
std::thread m_PollingThread
Definition: NetClient.h:337
static bool OnRejoined(CNetClient *client, CFsmEvent< CNetMessage * > *event)
Definition: NetClient.cpp:958
static bool OnAuthenticateRequest(CNetClient *client, CFsmEvent< CNetMessage * > *event)
Definition: NetClient.cpp:784
CStr m_HostJID
Definition: NetClient.h:319
void SendRejoinedMessage()
Call when the client has rejoined a running match and finished the loading screen.
Definition: NetClient.cpp:602
bool TryToConnectWithSTUN(std::string serverAddressOrHostname, std::uint16_t serverPort, const CStr &hostJID, bool localNetwork)
Connect to the remote networked server using lobby.
Definition: NetClient.cpp:235
virtual ~CNetClient()
Definition: NetClient.cpp:199
std::deque< Script::StructuredClone > m_GuiMessageQueue
Queue of messages for GuiPoll.
Definition: NetClient.h:355
void SetupConnection(ENetHost *enetClient)
Set up a connection to the remote networked server.
Definition: NetClient.cpp:211
bool SendMessage(const CNetMessage *message)
Send a message to the server.
Definition: NetClient.cpp:497
std::string m_ControllerSecret
The 'secret' used to identify the controller of the game.
Definition: NetClient.h:329
void DestroyConnection()
Destroy the connection to the server.
Definition: NetClient.cpp:349
void SendGameSetupMessage(JS::MutableHandleValue attrs, const Script::Interface &scriptInterface)
Definition: NetClient.cpp:535
void SendReadyMessage(const int status)
Definition: NetClient.cpp:564
std::optional< GuiPollData > m_GuiMessagePoll
Definition: NetClient.h:367
CStr m_GUID
Globally unique identifier to distinguish users beyond the lifetime of a single network session.
Definition: NetClient.h:352
void SendStartGameMessage(const CStr &initAttribs)
Definition: NetClient.cpp:577
static bool OnJoinSyncStart(CNetClient *client, CFsmEvent< CNetMessage * > *event)
Definition: NetClient.cpp:919
static bool OnReady(CNetClient *client, CFsmEvent< CNetMessage * > *event)
Definition: NetClient.cpp:828
CStr m_Password
Password to join the game.
Definition: NetClient.h:326
bool m_Rejoin
True if the player is currently rejoining or has already rejoined the game.
Definition: NetClient.h:346
static bool OnSavedGameStart(CNetClient *client, CFsmEvent< CNetMessage * > *event)
Definition: NetClient.cpp:898
static bool OnPlayerAssignment(CNetClient *client, CFsmEvent< CNetMessage * > *event)
Definition: NetClient.cpp:855
bool ShouldShutdown() const
static bool OnLoadedGame(CNetClient *client, CFsmEvent< CNetMessage * > *event)
Definition: NetClient.cpp:1057
static bool OnHandshake(CNetClient *client, CFsmEvent< CNetMessage * > *event)
Definition: NetClient.cpp:743
CSrvHandshakeMessage m_ServerHandshake
Record of the server engine version and loaded mods.
Definition: NetClient.h:378
CStr m_ServerAddressOrHostname
Definition: NetClient.h:320
CNetClientSession * m_Session
Current network session (or nullptr if not connected)
Definition: NetClient.h:335
void LoadFinished()
Call when the game has started and all data files have been loaded, to signal to the server that we a...
Definition: NetClient.cpp:673
void FetchMessage()
Definition: NetClient.cpp:431
CNetClient(CGame *game, std::string serverAddressOrHostname, std::uint16_t serverPont, const CStrW &username=L"anonymous", const CStr &hostJID={}, std::string hashedPassword={}, std::string controllerSecret={})
Construct a client associated with the given game object.
Definition: NetClient.cpp:70
void SendFlareMessage(const CStr &positionX, const CStr &positionY, const CStr &positionZ)
Call when the client (player or observer) has sent a flare.
Definition: NetClient.cpp:584
bool HandleMessage(CNetMessage *message)
Call when a message has been received from the network.
Definition: NetClient.cpp:623
void SendClearAllReadyMessage()
Definition: NetClient.cpp:571
static bool OnKicked(CNetClient *client, CFsmEvent< CNetMessage * > *event)
Definition: NetClient.cpp:971
void Poll()
Poll the connection for messages from the server and process them, and send any queued messages.
Definition: NetClient.cpp:364
std::time_t m_LastConnectionCheck
Time when the server was last checked for timeouts and bad latency.
Definition: NetClient.h:375
void HandleConnect()
Call when the network connection has been successfully initiated.
Definition: NetClient.cpp:505
static bool OnJoinSyncEndCommandBatch(CNetClient *client, CFsmEvent< CNetMessage * > *event)
Definition: NetClient.cpp:944
bool m_IsController
Note that this is just a "gui hint" with no actual impact on being controller.
Definition: NetClient.h:332
void HandleGetServerDataFailed(const CStr &error)
Called when fetching connection data from the host failed, to inform JS code.
Definition: NetClient.cpp:222
PlayerAssignmentMap m_PlayerAssignments
Latest copy of player assignments heard from the server.
Definition: NetClient.h:349
JSObject * GetNextGUIMessage(const Script::Interface &guiInterface)
Retrieves the next queued GUI message, and removes it from the queue.
Definition: NetClient.cpp:408
void SendAuthenticateMessage()
Definition: NetClient.cpp:710
static bool OnChat(CNetClient *client, CFsmEvent< CNetMessage * > *event)
Definition: NetClient.cpp:814
void SetAndOwnSession(CNetClientSession *session)
Take ownership of a session object, and use it for all network communication.
Definition: NetClient.cpp:343
std::uint32_t m_HostID
Unique-per-game identifier of this client, used to identify the sender of simulation commands.
Definition: NetClient.h:343
std::string m_SavedState
Definition: NetClient.h:372
void Unregister(const Script::Interface *guiInterface)
Has to be called bevore the Script::Interface gets destroied so that no future messages are sent to i...
Definition: NetClient.cpp:418
void SendPausedMessage(bool pause)
Call when the client has paused or unpaused the game.
Definition: NetClient.cpp:616
CGame * m_Game
Definition: NetClient.h:316
static bool OnClientPaused(CNetClient *client, CFsmEvent< CNetMessage * > *event)
Definition: NetClient.cpp:1043
void StartGame(const JS::MutableHandleValue initAttributes, const std::string &savedState)
Starts a game with the specified init attributes and saved state.
Definition: NetClient.cpp:719
CNetClientTurnManager * m_ClientTurnManager
Turn manager associated with the current game (or nullptr if we haven't started the game yet)
Definition: NetClient.h:340
The base class for all network messages exchanged within the game.
Definition: NetMessage.h:42
Definition: Interface.h:93
JSContext * cx
Definition: Request.h:83
Definition: XmppClient.h:30
Abstraction around a SpiderMonkey JSContext.
Definition: DapInterface.h:32
JS::Value CreateObject(const Script::Request &rq)
Create a plain object (i.e.
Definition: Object.h:246
StructuredClone WriteStructuredClone(const Script::Request &rq, JS::HandleValue v)
Definition: StructuredClone.cpp:33
bool error(JSContext *cx, unsigned int argc, JS::Value *vp)
Definition: Interface.cpp:204
Definition: NetClient.h:358
const Script::Interface & interface
Definition: NetClient.h:359
JS::PersistentRootedObject promise
In the context of interface.
Definition: NetClient.h:365
Definition: NetClient.h:261