Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
CNetClient Class Reference

Network client. More...

#include <NetClient.h>

Inheritance diagram for CNetClient:
Collaboration diagram for CNetClient:

Classes

struct  GuiPollData
 
struct  PrivateTag
 

Public Member Functions

 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. More...
 
 CNetClient (CGame *game, const CStrW &username, const CStr &hostJID, std::string hashedPassword, XmppClient &xmppClient)
 This constructor additionally requests connection information over the lobby. More...
 
virtual ~CNetClient ()
 
bool IsController () const
 
CStr GetGUID () const
 Returns the GUID of the local client. More...
 
bool TryToConnectWithSTUN (std::string serverAddressOrHostname, std::uint16_t serverPort, const CStr &hostJID, bool localNetwork)
 Connect to the remote networked server using lobby. More...
 
void DestroyConnection ()
 Destroy the connection to the server. More...
 
void Poll ()
 Poll the connection for messages from the server and process them, and send any queued messages. More...
 
void CheckServerConnection ()
 Locally triggers a GUI message if the connection to the server is being lost or has bad latency. More...
 
JSObject * GetNextGUIMessage (const Script::Interface &guiInterface)
 Retrieves the next queued GUI message, and removes it from the queue. More...
 
void Unregister (const Script::Interface *guiInterface)
 Has to be called bevore the Script::Interface gets destroied so that no future messages are sent to it. More...
 
template<typename... Args>
void PushGuiMessage (Args const &... args)
 Add a message to the queue, to be read by GuiPoll. More...
 
std::string TestReadGuiMessages ()
 Return a concatenation of all messages in the GUI queue, for test cases to easily verify the queue contents. More...
 
const Script::Interface & GetScriptInterface ()
 Get the script interface associated with this network client, which is equivalent to the one used by the CGame in the constructor. More...
 
bool SendMessage (const CNetMessage *message)
 Send a message to the server. More...
 
void HandleConnect ()
 Call when the network connection has been successfully initiated. More...
 
void HandleDisconnect (std::uint32_t reason)
 Call when the network connection has been lost. More...
 
bool HandleMessage (CNetMessage *message)
 Call when a message has been received from the network. More...
 
void LoadFinished ()
 Call when the game has started and all data files have been loaded, to signal to the server that we are ready to begin the game. More...
 
void SendGameSetupMessage (JS::MutableHandleValue attrs, const Script::Interface &scriptInterface)
 
void SendAssignPlayerMessage (const int playerID, const CStr &guid)
 
void SendChatMessage (const std::wstring &text, std::optional< std::vector< std::string > > receivers)
 
void SendReadyMessage (const int status)
 
void SendClearAllReadyMessage ()
 
void SendStartGameMessage (const CStr &initAttribs)
 
void SendStartSavedGameMessage (const CStr &initAttribs, const CStr &savedState)
 
void SendFlareMessage (const CStr &positionX, const CStr &positionY, const CStr &positionZ)
 Call when the client (player or observer) has sent a flare. More...
 
void SendRejoinedMessage ()
 Call when the client has rejoined a running match and finished the loading screen. More...
 
void SendKickPlayerMessage (const CStrW &playerName, bool ban)
 Call to kick/ban a client. More...
 
void SendPausedMessage (bool pause)
 Call when the client has paused or unpaused the game. More...
 
bool ShouldShutdown () const
 
void HandleGetServerDataFailed (const CStr &error)
 Called when fetching connection data from the host failed, to inform JS code. More...
 
- Public Member Functions inherited from CFsm< CNetClient, CNetMessage * >
void AddTransition (unsigned int state, unsigned int eventType, unsigned int nextState, Action *pAction=nullptr, CNetClient *pContext=nullptr)
 Adds a new transistion to the state machine. More...
 
void SetFirstState (unsigned int firstState)
 Sets the initial state for FSM. More...
 
void SetCurrState (unsigned int state)
 Sets the current state and update the last state to the current state. More...
 
unsigned int GetCurrState () const
 
void SetNextState (unsigned int nextState)
 
unsigned int GetNextState () const
 
bool Update (unsigned int eventType, CNetMessage * eventData)
 Updates the FSM and retrieves next state. More...
 
bool IsDone () const
 Tests whether the state machine has finished its work. More...
 

Private Member Functions

 NONCOPYABLE (CNetClient)
 
 CNetClient (PrivateTag, CGame *game, std::string serverAddressOrHostname, std::uint16_t serverPont, const CStrW &username=L"anonymous", const CStr &hostJID={}, std::string hashedPassword={}, std::string controllerSecret={})
 
void SendAuthenticateMessage ()
 
void SetupConnection (ENetHost *enetClient)
 Set up a connection to the remote networked server. More...
 
void SetAndOwnSession (CNetClientSession *session)
 Take ownership of a session object, and use it for all network communication. More...
 
void StartGame (const JS::MutableHandleValue initAttributes, const std::string &savedState)
 Starts a game with the specified init attributes and saved state. More...
 
void PostPlayerAssignmentsToScript ()
 Push a message onto the GUI queue listing the current player assignments. More...
 
void FetchMessage ()
 

Static Private Member Functions

static bool OnConnect (CNetClient *client, CFsmEvent< CNetMessage * > *event)
 
static bool OnHandshake (CNetClient *client, CFsmEvent< CNetMessage * > *event)
 
static bool OnHandshakeResponse (CNetClient *client, CFsmEvent< CNetMessage * > *event)
 
static bool OnAuthenticateRequest (CNetClient *client, CFsmEvent< CNetMessage * > *event)
 
static bool OnAuthenticate (CNetClient *client, CFsmEvent< CNetMessage * > *event)
 
static bool OnChat (CNetClient *client, CFsmEvent< CNetMessage * > *event)
 
static bool OnReady (CNetClient *client, CFsmEvent< CNetMessage * > *event)
 
static bool OnGameSetup (CNetClient *client, CFsmEvent< CNetMessage * > *event)
 
static bool OnPlayerAssignment (CNetClient *client, CFsmEvent< CNetMessage * > *event)
 
static bool OnInGame (CNetClient *client, CFsmEvent< CNetMessage * > *event)
 
static bool OnGameStart (CNetClient *client, CFsmEvent< CNetMessage * > *event)
 
static bool OnSavedGameStart (CNetClient *client, CFsmEvent< CNetMessage * > *event)
 
static bool OnJoinSyncStart (CNetClient *client, CFsmEvent< CNetMessage * > *event)
 
static bool OnJoinSyncEndCommandBatch (CNetClient *client, CFsmEvent< CNetMessage * > *event)
 
static bool OnFlare (CNetClient *client, CFsmEvent< CNetMessage * > *event)
 
static bool OnRejoined (CNetClient *client, CFsmEvent< CNetMessage * > *event)
 
static bool OnKicked (CNetClient *client, CFsmEvent< CNetMessage * > *event)
 
static bool OnClientTimeout (CNetClient *client, CFsmEvent< CNetMessage * > *event)
 
static bool OnClientPerformance (CNetClient *client, CFsmEvent< CNetMessage * > *event)
 
static bool OnClientsLoading (CNetClient *client, CFsmEvent< CNetMessage * > *event)
 
static bool OnClientPaused (CNetClient *client, CFsmEvent< CNetMessage * > *event)
 
static bool OnLoadedGame (CNetClient *client, CFsmEvent< CNetMessage * > *event)
 

Private Attributes

CGame * m_Game
 
CStrW m_UserName
 
CStr m_HostJID
 
CStr m_ServerAddressOrHostname
 
std::uint16_t m_ServerPort {0}
 
CStr m_Password
 Password to join the game. More...
 
std::string m_ControllerSecret
 The 'secret' used to identify the controller of the game. More...
 
bool m_IsController = false
 Note that this is just a "gui hint" with no actual impact on being controller. More...
 
CNetClientSession * m_Session {nullptr}
 Current network session (or nullptr if not connected) More...
 
std::thread m_PollingThread
 
CNetClientTurnManager * m_ClientTurnManager {nullptr}
 Turn manager associated with the current game (or nullptr if we haven't started the game yet) More...
 
std::uint32_t m_HostID {static_cast<std::uint32_t>(-1)}
 Unique-per-game identifier of this client, used to identify the sender of simulation commands. More...
 
bool m_Rejoin {false}
 True if the player is currently rejoining or has already rejoined the game. More...
 
PlayerAssignmentMap m_PlayerAssignments
 Latest copy of player assignments heard from the server. More...
 
CStr m_GUID
 Globally unique identifier to distinguish users beyond the lifetime of a single network session. More...
 
std::deque< Script::StructuredClone > m_GuiMessageQueue
 Queue of messages for GuiPoll. More...
 
std::optional< GuiPollData > m_GuiMessagePoll
 
std::string m_JoinSyncBuffer
 Serialized game state received when joining an in-progress game. More...
 
std::string m_SavedState
 
std::time_t m_LastConnectionCheck {0}
 Time when the server was last checked for timeouts and bad latency. More...
 
CSrvHandshakeMessage m_ServerHandshake
 Record of the server engine version and loaded mods. More...
 

Detailed Description

Network client.

This code is run by every player (including the host, if they are not a dedicated server). It provides an interface between the GUI, the network (via CNetClientSession), and the game (via CGame and CNetClientTurnManager).

Constructor & Destructor Documentation

◆ CNetClient() [1/3]

CNetClient::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.

The game must exist for the lifetime of this object. The user's name will be displayed to all players. The JID of the host is needed for the secure lobby authentication.

◆ CNetClient() [2/3]

CNetClient::CNetClient ( CGame *  game,
const CStrW &  username,
const CStr &  hostJID,
std::string  hashedPassword,
XmppClient &  xmppClient 
)

This constructor additionally requests connection information over the lobby.

◆ ~CNetClient()

CNetClient::~CNetClient ( )
virtual

◆ CNetClient() [3/3]

CNetClient::CNetClient ( PrivateTag  ,
CGame *  game,
std::string  serverAddressOrHostname,
std::uint16_t  serverPont,
const CStrW &  username = L"anonymous",
const CStr &  hostJID = {},
std::string  hashedPassword = {},
std::string  controllerSecret = {} 
)
private

Member Function Documentation

◆ CheckServerConnection()

void CNetClient::CheckServerConnection ( )

Locally triggers a GUI message if the connection to the server is being lost or has bad latency.

◆ DestroyConnection()

void CNetClient::DestroyConnection ( )

Destroy the connection to the server.

This client probably cannot be used again.

◆ FetchMessage()

void CNetClient::FetchMessage ( )
private

◆ GetGUID()

CStr CNetClient::GetGUID ( ) const
inline

Returns the GUID of the local client.

Used for distinguishing observers.

◆ GetNextGUIMessage()

JSObject * CNetClient::GetNextGUIMessage ( const Script::Interface &  guiInterface)

Retrieves the next queued GUI message, and removes it from the queue.

The returned value is in the JS context of the provided Script::Interface.

This is the only mechanism for the networking code to send messages to the GUI.

The structure of the messages is { "type": "...", ... }. The exact types and associated data are not specified anywhere - the implementation and GUI scripts must make the same assumptions.

Returns
a promise resolving to the next message.

◆ GetScriptInterface()

const Script::Interface & CNetClient::GetScriptInterface ( )

Get the script interface associated with this network client, which is equivalent to the one used by the CGame in the constructor.

◆ HandleConnect()

void CNetClient::HandleConnect ( )

Call when the network connection has been successfully initiated.

◆ HandleDisconnect()

void CNetClient::HandleDisconnect ( std::uint32_t  reason)

Call when the network connection has been lost.

◆ HandleGetServerDataFailed()

void CNetClient::HandleGetServerDataFailed ( const CStr &  error)

Called when fetching connection data from the host failed, to inform JS code.

◆ HandleMessage()

bool CNetClient::HandleMessage ( CNetMessage *  message)

Call when a message has been received from the network.

◆ IsController()

bool CNetClient::IsController ( ) const
inline

◆ LoadFinished()

void CNetClient::LoadFinished ( )

Call when the game has started and all data files have been loaded, to signal to the server that we are ready to begin the game.

◆ NONCOPYABLE()

CNetClient::NONCOPYABLE ( CNetClient  )
private

◆ OnAuthenticate()

bool CNetClient::OnAuthenticate ( CNetClient *  client,
CFsmEvent< CNetMessage * > *  event 
)
staticprivate

◆ OnAuthenticateRequest()

bool CNetClient::OnAuthenticateRequest ( CNetClient *  client,
CFsmEvent< CNetMessage * > *  event 
)
staticprivate

◆ OnChat()

bool CNetClient::OnChat ( CNetClient *  client,
CFsmEvent< CNetMessage * > *  event 
)
staticprivate

◆ OnClientPaused()

bool CNetClient::OnClientPaused ( CNetClient *  client,
CFsmEvent< CNetMessage * > *  event 
)
staticprivate

◆ OnClientPerformance()

bool CNetClient::OnClientPerformance ( CNetClient *  client,
CFsmEvent< CNetMessage * > *  event 
)
staticprivate

◆ OnClientsLoading()

bool CNetClient::OnClientsLoading ( CNetClient *  client,
CFsmEvent< CNetMessage * > *  event 
)
staticprivate

◆ OnClientTimeout()

bool CNetClient::OnClientTimeout ( CNetClient *  client,
CFsmEvent< CNetMessage * > *  event 
)
staticprivate

◆ OnConnect()

bool CNetClient::OnConnect ( CNetClient *  client,
CFsmEvent< CNetMessage * > *  event 
)
staticprivate

◆ OnFlare()

bool CNetClient::OnFlare ( CNetClient *  client,
CFsmEvent< CNetMessage * > *  event 
)
staticprivate

◆ OnGameSetup()

bool CNetClient::OnGameSetup ( CNetClient *  client,
CFsmEvent< CNetMessage * > *  event 
)
staticprivate

◆ OnGameStart()

bool CNetClient::OnGameStart ( CNetClient *  client,
CFsmEvent< CNetMessage * > *  event 
)
staticprivate

◆ OnHandshake()

bool CNetClient::OnHandshake ( CNetClient *  client,
CFsmEvent< CNetMessage * > *  event 
)
staticprivate

◆ OnHandshakeResponse()

bool CNetClient::OnHandshakeResponse ( CNetClient *  client,
CFsmEvent< CNetMessage * > *  event 
)
staticprivate

◆ OnInGame()

bool CNetClient::OnInGame ( CNetClient *  client,
CFsmEvent< CNetMessage * > *  event 
)
staticprivate

◆ OnJoinSyncEndCommandBatch()

bool CNetClient::OnJoinSyncEndCommandBatch ( CNetClient *  client,
CFsmEvent< CNetMessage * > *  event 
)
staticprivate

◆ OnJoinSyncStart()

bool CNetClient::OnJoinSyncStart ( CNetClient *  client,
CFsmEvent< CNetMessage * > *  event 
)
staticprivate

◆ OnKicked()

bool CNetClient::OnKicked ( CNetClient *  client,
CFsmEvent< CNetMessage * > *  event 
)
staticprivate

◆ OnLoadedGame()

bool CNetClient::OnLoadedGame ( CNetClient *  client,
CFsmEvent< CNetMessage * > *  event 
)
staticprivate

◆ OnPlayerAssignment()

bool CNetClient::OnPlayerAssignment ( CNetClient *  client,
CFsmEvent< CNetMessage * > *  event 
)
staticprivate

◆ OnReady()

bool CNetClient::OnReady ( CNetClient *  client,
CFsmEvent< CNetMessage * > *  event 
)
staticprivate

◆ OnRejoined()

bool CNetClient::OnRejoined ( CNetClient *  client,
CFsmEvent< CNetMessage * > *  event 
)
staticprivate

◆ OnSavedGameStart()

bool CNetClient::OnSavedGameStart ( CNetClient *  client,
CFsmEvent< CNetMessage * > *  event 
)
staticprivate

◆ Poll()

void CNetClient::Poll ( )

Poll the connection for messages from the server and process them, and send any queued messages.

This must be called frequently (i.e. once per frame).

◆ PostPlayerAssignmentsToScript()

void CNetClient::PostPlayerAssignmentsToScript ( )
private

Push a message onto the GUI queue listing the current player assignments.

◆ PushGuiMessage()

template<typename... Args>
void CNetClient::PushGuiMessage ( Args const &...  args)
inline

Add a message to the queue, to be read by GuiPoll.

The script value must be in the GetScriptInterface() JS context.

◆ SendAssignPlayerMessage()

void CNetClient::SendAssignPlayerMessage ( const int  playerID,
const CStr &  guid 
)

◆ SendAuthenticateMessage()

void CNetClient::SendAuthenticateMessage ( )
private

◆ SendChatMessage()

void CNetClient::SendChatMessage ( const std::wstring &  text,
std::optional< std::vector< std::string > >  receivers 
)
Parameters
textThe message to send.
receiversThe GUID of the receiving clients. If empty send it to all clients.

◆ SendClearAllReadyMessage()

void CNetClient::SendClearAllReadyMessage ( )

◆ SendFlareMessage()

void CNetClient::SendFlareMessage ( const CStr &  positionX,
const CStr &  positionY,
const CStr &  positionZ 
)

Call when the client (player or observer) has sent a flare.

◆ SendGameSetupMessage()

void CNetClient::SendGameSetupMessage ( JS::MutableHandleValue  attrs,
const Script::Interface &  scriptInterface 
)

◆ SendKickPlayerMessage()

void CNetClient::SendKickPlayerMessage ( const CStrW &  playerName,
bool  ban 
)

Call to kick/ban a client.

◆ SendMessage()

bool CNetClient::SendMessage ( const CNetMessage *  message)

Send a message to the server.

Parameters
messagemessage to send
Returns
true on success

◆ SendPausedMessage()

void CNetClient::SendPausedMessage ( bool  pause)

Call when the client has paused or unpaused the game.

◆ SendReadyMessage()

void CNetClient::SendReadyMessage ( const int  status)

◆ SendRejoinedMessage()

void CNetClient::SendRejoinedMessage ( )

Call when the client has rejoined a running match and finished the loading screen.

◆ SendStartGameMessage()

void CNetClient::SendStartGameMessage ( const CStr &  initAttribs)

◆ SendStartSavedGameMessage()

void CNetClient::SendStartSavedGameMessage ( const CStr &  initAttribs,
const CStr &  savedState 
)

◆ SetAndOwnSession()

void CNetClient::SetAndOwnSession ( CNetClientSession *  session)
private

Take ownership of a session object, and use it for all network communication.

◆ SetupConnection()

void CNetClient::SetupConnection ( ENetHost *  enetClient)
private

Set up a connection to the remote networked server.

Returns
true on success, false on connection failure

◆ ShouldShutdown()

bool CNetClient::ShouldShutdown ( ) const
Returns
Whether the NetClient is shutting down.

◆ StartGame()

void CNetClient::StartGame ( const JS::MutableHandleValue  initAttributes,
const std::string &  savedState 
)
private

Starts a game with the specified init attributes and saved state.

Called by the start game and start saved game callbacks.

◆ TestReadGuiMessages()

std::string CNetClient::TestReadGuiMessages ( )

Return a concatenation of all messages in the GUI queue, for test cases to easily verify the queue contents.

◆ TryToConnectWithSTUN()

bool CNetClient::TryToConnectWithSTUN ( std::string  serverAddressOrHostname,
std::uint16_t  serverPort,
const CStr &  hostJID,
bool  localNetwork 
)

Connect to the remote networked server using lobby.

Push netstatus messages on failure.

Parameters
localNetwork- if true, assume we are trying to connect on the local network.
Returns
true on success, false on connection failure

◆ Unregister()

void CNetClient::Unregister ( const Script::Interface *  guiInterface)

Has to be called bevore the Script::Interface gets destroied so that no future messages are sent to it.

Member Data Documentation

◆ m_ClientTurnManager

CNetClientTurnManager* CNetClient::m_ClientTurnManager {nullptr}
private

Turn manager associated with the current game (or nullptr if we haven't started the game yet)

◆ m_ControllerSecret

std::string CNetClient::m_ControllerSecret
private

The 'secret' used to identify the controller of the game.

◆ m_Game

CGame* CNetClient::m_Game
private

◆ m_GUID

CStr CNetClient::m_GUID
private

Globally unique identifier to distinguish users beyond the lifetime of a single network session.

◆ m_GuiMessagePoll

std::optional<GuiPollData> CNetClient::m_GuiMessagePoll
private

◆ m_GuiMessageQueue

std::deque<Script::StructuredClone> CNetClient::m_GuiMessageQueue
private

Queue of messages for GuiPoll.

◆ m_HostID

std::uint32_t CNetClient::m_HostID {static_cast<std::uint32_t>(-1)}
private

Unique-per-game identifier of this client, used to identify the sender of simulation commands.

◆ m_HostJID

CStr CNetClient::m_HostJID
private

◆ m_IsController

bool CNetClient::m_IsController = false
private

Note that this is just a "gui hint" with no actual impact on being controller.

◆ m_JoinSyncBuffer

std::string CNetClient::m_JoinSyncBuffer
private

Serialized game state received when joining an in-progress game.

◆ m_LastConnectionCheck

std::time_t CNetClient::m_LastConnectionCheck {0}
private

Time when the server was last checked for timeouts and bad latency.

◆ m_Password

CStr CNetClient::m_Password
private

Password to join the game.

◆ m_PlayerAssignments

PlayerAssignmentMap CNetClient::m_PlayerAssignments
private

Latest copy of player assignments heard from the server.

◆ m_PollingThread

std::thread CNetClient::m_PollingThread
private

◆ m_Rejoin

bool CNetClient::m_Rejoin {false}
private

True if the player is currently rejoining or has already rejoined the game.

◆ m_SavedState

std::string CNetClient::m_SavedState
private

◆ m_ServerAddressOrHostname

CStr CNetClient::m_ServerAddressOrHostname
private

◆ m_ServerHandshake

CSrvHandshakeMessage CNetClient::m_ServerHandshake
private

Record of the server engine version and loaded mods.

◆ m_ServerPort

std::uint16_t CNetClient::m_ServerPort {0}
private

◆ m_Session

CNetClientSession* CNetClient::m_Session {nullptr}
private

Current network session (or nullptr if not connected)

◆ m_UserName

CStrW CNetClient::m_UserName
private

The documentation for this class was generated from the following files: