Pyrogenesis  trunk
Public Member Functions | Static Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | Friends | List of all members
CNetClient Class Reference

Network client. More...

#include <NetClient.h>

Inheritance diagram for CNetClient:
Inheritance graph
[legend]
Collaboration diagram for CNetClient:
Collaboration graph
[legend]

Public Member Functions

 CNetClient (CGame *game)
 Construct a client associated with the given game object. More...
 
virtual ~CNetClient ()
 
void TraceMember (JSTracer *trc)
 
void SetUserName (const CStrW &username)
 Set the user's name that will be displayed to all players. More...
 
void SetHostJID (const CStr &jid)
 Store the JID of the host. More...
 
void SetControllerSecret (const std::string &secret)
 
bool IsController () const
 
void SetGamePassword (const CStr &hashedPassword)
 Set the game password. More...
 
CStr GetGUID () const
 Returns the GUID of the local client. More...
 
void SetupServerData (CStr address, u16 port, bool stun)
 Set connection data to the remote networked server. More...
 
bool SetupConnection (ENetHost *enetClient)
 Set up a connection to the remote networked server. More...
 
void SetupConnectionViaLobby ()
 Request connection information over the lobby. More...
 
bool TryToConnect (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...
 
void GuiPoll (JS::MutableHandleValue)
 Retrieves the next queued GUI message, and removes it from the queue. 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 ScriptInterfaceGetScriptInterface ()
 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 (u32 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 ScriptInterface &scriptInterface)
 
void SendAssignPlayerMessage (const int playerID, const CStr &guid)
 
void SendChatMessage (const std::wstring &text)
 
void SendReadyMessage (const int status)
 
void SendClearAllReadyMessage ()
 
void SendStartGameMessage (const CStr &initAttribs)
 
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
 CFsm ()
 
virtual ~CFsm ()
 
virtual void Setup ()
 Constructs the state machine. More...
 
void Shutdown ()
 Clear event, action and condition lists and reset state machine. More...
 
void AddState (unsigned int state)
 Adds the specified state to the internal list of states. More...
 
CFsmEventAddEvent (unsigned int eventType)
 Adds the specified event to the internal list of events. More...
 
CFsmTransitionAddTransition (unsigned int state, unsigned int eventType, unsigned int nextState)
 Adds a new transistion to the state machine. More...
 
CFsmTransitionAddTransition (unsigned int state, unsigned int eventType, unsigned int nextState, void *pAction, void *pContext)
 Adds a new transition to the state machine. More...
 
CFsmTransitionGetTransition (unsigned int state, unsigned int eventType) const
 Looks up the transition given the state, event and next state to transition to. More...
 
CFsmTransitionGetEventTransition (unsigned int eventType) const
 
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
 
const StateSetGetStates () const
 
const EventMapGetEvents () const
 
const TransitionListGetTransitions () const
 
bool Update (unsigned int eventType, void *pEventData)
 Updates the FSM and retrieves next state. More...
 
bool IsValidState (unsigned int state) const
 Verifies whether the specified state is managed by the FSM. More...
 
bool IsValidEvent (unsigned int eventType) const
 Verifies whether the specified event is managed by the FSM. More...
 
virtual bool IsDone () const
 Tests whether the state machine has finished its work. More...
 

Static Public Member Functions

static void Trace (JSTracer *trc, void *data)
 We assume that adding a tracing function that's only called during GC is better for performance than using a PersistentRooted<T> where each value needs to be added to the root set. More...
 

Private Member Functions

 NONCOPYABLE (CNetClient)
 
void SendAuthenticateMessage ()
 
void SetAndOwnSession (CNetClientSession *session)
 Take ownership of a session object, and use it for all network communication. More...
 
void PostPlayerAssignmentsToScript ()
 Push a message onto the GUI queue listing the current player assignments. More...
 

Static Private Member Functions

static bool OnConnect (void *context, CFsmEvent *event)
 
static bool OnHandshake (void *context, CFsmEvent *event)
 
static bool OnHandshakeResponse (void *context, CFsmEvent *event)
 
static bool OnAuthenticateRequest (void *context, CFsmEvent *event)
 
static bool OnAuthenticate (void *context, CFsmEvent *event)
 
static bool OnChat (void *context, CFsmEvent *event)
 
static bool OnReady (void *context, CFsmEvent *event)
 
static bool OnGameSetup (void *context, CFsmEvent *event)
 
static bool OnPlayerAssignment (void *context, CFsmEvent *event)
 
static bool OnInGame (void *context, CFsmEvent *event)
 
static bool OnGameStart (void *context, CFsmEvent *event)
 
static bool OnJoinSyncStart (void *context, CFsmEvent *event)
 
static bool OnJoinSyncEndCommandBatch (void *context, CFsmEvent *event)
 
static bool OnRejoined (void *context, CFsmEvent *event)
 
static bool OnKicked (void *context, CFsmEvent *event)
 
static bool OnClientTimeout (void *context, CFsmEvent *event)
 
static bool OnClientPerformance (void *context, CFsmEvent *event)
 
static bool OnClientsLoading (void *context, CFsmEvent *event)
 
static bool OnClientPaused (void *context, CFsmEvent *event)
 
static bool OnLoadedGame (void *context, CFsmEvent *event)
 

Private Attributes

CGamem_Game
 
CStrW m_UserName
 
CStr m_HostJID
 
CStr m_ServerAddress
 
u16 m_ServerPort
 
bool m_UseSTUN
 
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...
 
CNetClientSessionm_Session
 Current network session (or NULL if not connected) More...
 
std::thread m_PollingThread
 
CNetClientTurnManagerm_ClientTurnManager
 Turn manager associated with the current game (or NULL if we haven't started the game yet) More...
 
u32 m_HostID
 Unique-per-game identifier of this client, used to identify the sender of simulation commands. More...
 
bool m_Rejoin
 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< JS::Heap< JS::Value > > m_GuiMessageQueue
 Queue of messages for GuiPoll. More...
 
std::string m_JoinSyncBuffer
 Serialized game state received when joining an in-progress game. More...
 
std::time_t m_LastConnectionCheck
 Time when the server was last checked for timeouts and bad latency. More...
 

Friends

class CNetFileReceiveTask_ClientRejoin
 

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()

CNetClient::CNetClient ( CGame game)

Construct a client associated with the given game object.

The game must exist for the lifetime of this object.

◆ ~CNetClient()

CNetClient::~CNetClient ( )
virtual

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.

◆ GetGUID()

CStr CNetClient::GetGUID ( ) const
inline

Returns the GUID of the local client.

Used for distinguishing observers.

◆ GetScriptInterface()

const ScriptInterface & CNetClient::GetScriptInterface ( )

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

◆ GuiPoll()

void CNetClient::GuiPoll ( JS::MutableHandleValue  ret)

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

The returned value is in the GetScriptInterface() JS context.

This is the only mechanism for the networking code to send messages to the GUI - it is pull-based (instead of push) so the engine code does not need to know anything about the code structure of the GUI scripts.

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
next message, or the value 'undefined' if the queue is empty

◆ HandleConnect()

void CNetClient::HandleConnect ( )

Call when the network connection has been successfully initiated.

◆ HandleDisconnect()

void CNetClient::HandleDisconnect ( u32  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 ( void *  context,
CFsmEvent event 
)
staticprivate

◆ OnAuthenticateRequest()

bool CNetClient::OnAuthenticateRequest ( void *  context,
CFsmEvent event 
)
staticprivate

◆ OnChat()

bool CNetClient::OnChat ( void *  context,
CFsmEvent event 
)
staticprivate

◆ OnClientPaused()

bool CNetClient::OnClientPaused ( void *  context,
CFsmEvent event 
)
staticprivate

◆ OnClientPerformance()

bool CNetClient::OnClientPerformance ( void *  context,
CFsmEvent event 
)
staticprivate

◆ OnClientsLoading()

bool CNetClient::OnClientsLoading ( void *  context,
CFsmEvent event 
)
staticprivate

◆ OnClientTimeout()

bool CNetClient::OnClientTimeout ( void *  context,
CFsmEvent event 
)
staticprivate

◆ OnConnect()

bool CNetClient::OnConnect ( void *  context,
CFsmEvent event 
)
staticprivate

◆ OnGameSetup()

bool CNetClient::OnGameSetup ( void *  context,
CFsmEvent event 
)
staticprivate

◆ OnGameStart()

bool CNetClient::OnGameStart ( void *  context,
CFsmEvent event 
)
staticprivate

◆ OnHandshake()

bool CNetClient::OnHandshake ( void *  context,
CFsmEvent event 
)
staticprivate

◆ OnHandshakeResponse()

bool CNetClient::OnHandshakeResponse ( void *  context,
CFsmEvent event 
)
staticprivate

◆ OnInGame()

bool CNetClient::OnInGame ( void *  context,
CFsmEvent event 
)
staticprivate

◆ OnJoinSyncEndCommandBatch()

bool CNetClient::OnJoinSyncEndCommandBatch ( void *  context,
CFsmEvent event 
)
staticprivate

◆ OnJoinSyncStart()

bool CNetClient::OnJoinSyncStart ( void *  context,
CFsmEvent event 
)
staticprivate

◆ OnKicked()

bool CNetClient::OnKicked ( void *  context,
CFsmEvent event 
)
staticprivate

◆ OnLoadedGame()

bool CNetClient::OnLoadedGame ( void *  context,
CFsmEvent event 
)
staticprivate

◆ OnPlayerAssignment()

bool CNetClient::OnPlayerAssignment ( void *  context,
CFsmEvent event 
)
staticprivate

◆ OnReady()

bool CNetClient::OnReady ( void *  context,
CFsmEvent event 
)
staticprivate

◆ OnRejoined()

bool CNetClient::OnRejoined ( void *  context,
CFsmEvent 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)

◆ SendClearAllReadyMessage()

void CNetClient::SendClearAllReadyMessage ( )

◆ SendGameSetupMessage()

void CNetClient::SendGameSetupMessage ( JS::MutableHandleValue  attrs,
const ScriptInterface 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)

◆ SetAndOwnSession()

void CNetClient::SetAndOwnSession ( CNetClientSession session)
private

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

◆ SetControllerSecret()

void CNetClient::SetControllerSecret ( const std::string &  secret)

◆ SetGamePassword()

void CNetClient::SetGamePassword ( const CStr &  hashedPassword)

Set the game password.

Must be called after SetUserName, as that is used to hash further.

◆ SetHostJID()

void CNetClient::SetHostJID ( const CStr &  jid)

Store the JID of the host.

This is needed for the secure lobby authentication.

◆ SetupConnection()

bool CNetClient::SetupConnection ( ENetHost enetClient)

Set up a connection to the remote networked server.

Must call SetupServerData first.

Returns
true on success, false on connection failure

◆ SetupConnectionViaLobby()

void CNetClient::SetupConnectionViaLobby ( )

Request connection information over the lobby.

◆ SetupServerData()

void CNetClient::SetupServerData ( CStr  address,
u16  port,
bool  stun 
)

Set connection data to the remote networked server.

Parameters
addressIP address or host name to connect to

◆ SetUserName()

void CNetClient::SetUserName ( const CStrW &  username)

Set the user's name that will be displayed to all players.

This must not be called after the connection setup.

◆ ShouldShutdown()

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

◆ TestReadGuiMessages()

std::string CNetClient::TestReadGuiMessages ( )

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

◆ Trace()

static void CNetClient::Trace ( JSTracer *  trc,
void *  data 
)
inlinestatic

We assume that adding a tracing function that's only called during GC is better for performance than using a PersistentRooted<T> where each value needs to be added to the root set.

◆ TraceMember()

void CNetClient::TraceMember ( JSTracer *  trc)

◆ TryToConnect()

bool CNetClient::TryToConnect ( 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

Friends And Related Function Documentation

◆ CNetFileReceiveTask_ClientRejoin

friend class CNetFileReceiveTask_ClientRejoin
friend

Member Data Documentation

◆ m_ClientTurnManager

CNetClientTurnManager* CNetClient::m_ClientTurnManager
private

Turn manager associated with the current game (or NULL 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_GuiMessageQueue

std::deque<JS::Heap<JS::Value> > CNetClient::m_GuiMessageQueue
private

Queue of messages for GuiPoll.

◆ m_HostID

u32 CNetClient::m_HostID
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
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
private

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

◆ m_ServerAddress

CStr CNetClient::m_ServerAddress
private

◆ m_ServerPort

u16 CNetClient::m_ServerPort
private

◆ m_Session

CNetClientSession* CNetClient::m_Session
private

Current network session (or NULL if not connected)

◆ m_UserName

CStrW CNetClient::m_UserName
private

◆ m_UseSTUN

bool CNetClient::m_UseSTUN
private

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