Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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:
CFsm

Public Member Functions

 CNetClient (CGame *game)
 Construct a client associated with the given game object. More...
 
virtual ~CNetClient ()
 
void SetUserName (const CStrW &username)
 Set the user's name that will be displayed to all players. More...
 
bool SetupConnection (const CStr &server)
 Set up a connection to the remote networked server. 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 Flush ()
 Flush any queued outgoing network messages. More...
 
CScriptValRooted GuiPoll ()
 Retrieves the next queued GUI message, and removes it from the queue. More...
 
void PushGuiMessage (const CScriptValRooted &message)
 Add a message to the queue, to be read by GuiPoll. More...
 
std::wstring TestReadGuiMessages ()
 Return a concatenation of all messages in the GUI queue, for test cases to easily verify the queue contents. More...
 
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 SendChatMessage (const std::wstring &text)
 
- Public Member Functions inherited from CFsm
 CFsm (void)
 
virtual ~CFsm (void)
 
virtual void Setup (void)
 Constructs the state machine. More...
 
void Shutdown (void)
 Clear event, action and condition lists and reset state machine. More...
 
void AddState (unsigned int state)
 
CFsmEventAddEvent (unsigned int eventType)
 
CFsmTransitionAddTransition (unsigned int state, unsigned int eventType, unsigned int nextState)
 
CFsmTransitionAddTransition (unsigned int state, unsigned int eventType, unsigned int nextState, void *pAction, void *pContext)
 
CFsmTransitionGetTransition (unsigned int state, unsigned int eventType) const
 
CFsmTransitionGetEventTransition (unsigned int eventType) const
 
void SetFirstState (unsigned int firstState)
 
void SetCurrState (unsigned int state)
 
unsigned int GetCurrState (void) const
 
void SetNextState (unsigned int nextState)
 
unsigned int GetNextState (void) const
 
const StateSetGetStates (void) const
 
const EventMapGetEvents (void) const
 
const TransitionListGetTransitions (void) const
 
bool Update (unsigned int eventType, void *pEventData)
 
bool IsValidState (unsigned int state) const
 
bool IsValidEvent (unsigned int eventType) const
 
virtual bool IsDone (void) const
 

Private Member Functions

 NONCOPYABLE (CNetClient)
 
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...
 
CStr GenerateGUID ()
 Initialise m_GUID with a random value. 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 OnAuthenticate (void *context, CFsmEvent *event)
 
static bool OnChat (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 OnLoadedGame (void *context, CFsmEvent *event)
 

Private Attributes

CGamem_Game
 
CStrW m_UserName
 
CNetClientSessionm_Session
 Current network session (or NULL if not connected) More...
 
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...
 
CScriptValRooted m_GameAttributes
 Latest copy of game setup attributes heard from the server. 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< CScriptValRootedm_GuiMessageQueue
 Queue of messages for GuiPoll. More...
 
std::string m_JoinSyncBuffer
 Serialized game state received when joining an in-progress game. 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).

Definition at line 57 of file NetClient.h.

Constructor & Destructor Documentation

CNetClient::CNetClient ( CGame game)

Construct a client associated with the given game object.

The game must exist for the lifetime of this object.

Definition at line 68 of file NetClient.cpp.

CNetClient::~CNetClient ( )
virtual

Definition at line 118 of file NetClient.cpp.

Member Function Documentation

void CNetClient::DestroyConnection ( )

Destroy the connection to the server.

This client probably cannot be used again.

Definition at line 144 of file NetClient.cpp.

void CNetClient::Flush ( )

Flush any queued outgoing network messages.

This should be called soon after sending a group of messages that may be batched together.

Definition at line 155 of file NetClient.cpp.

CStr CNetClient::GenerateGUID ( )
private

Initialise m_GUID with a random value.

Definition at line 559 of file NetClient.cpp.

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.

Definition at line 191 of file NetClient.cpp.

CScriptValRooted CNetClient::GuiPoll ( )

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

Definition at line 161 of file NetClient.cpp.

void CNetClient::HandleConnect ( )

Call when the network connection has been successfully initiated.

Definition at line 224 of file NetClient.cpp.

void CNetClient::HandleDisconnect ( u32  reason)

Call when the network connection has been lost.

Definition at line 229 of file NetClient.cpp.

bool CNetClient::HandleMessage ( CNetMessage message)

Call when a message has been received from the network.

Definition at line 250 of file NetClient.cpp.

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.

Definition at line 293 of file NetClient.cpp.

CNetClient::NONCOPYABLE ( CNetClient  )
private
bool CNetClient::OnAuthenticate ( void *  context,
CFsmEvent event 
)
staticprivate

Definition at line 376 of file NetClient.cpp.

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

Definition at line 398 of file NetClient.cpp.

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

Definition at line 333 of file NetClient.cpp.

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

Definition at line 415 of file NetClient.cpp.

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

Definition at line 459 of file NetClient.cpp.

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

Definition at line 346 of file NetClient.cpp.

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

Definition at line 361 of file NetClient.cpp.

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

Definition at line 530 of file NetClient.cpp.

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

Definition at line 497 of file NetClient.cpp.

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

Definition at line 483 of file NetClient.cpp.

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

Definition at line 513 of file NetClient.cpp.

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

Definition at line 433 of file NetClient.cpp.

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

Definition at line 149 of file NetClient.cpp.

void CNetClient::PostPlayerAssignmentsToScript ( )
private

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

Definition at line 196 of file NetClient.cpp.

void CNetClient::PushGuiMessage ( const CScriptValRooted message)

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

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

Definition at line 171 of file NetClient.cpp.

void CNetClient::SendChatMessage ( const std::wstring &  text)

Definition at line 243 of file NetClient.cpp.

bool CNetClient::SendMessage ( const CNetMessage message)

Send a message to the server.

Parameters
messagemessage to send
Returns
true on success

Definition at line 216 of file NetClient.cpp.

void CNetClient::SetAndOwnSession ( CNetClientSession session)
private

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

Definition at line 138 of file NetClient.cpp.

bool CNetClient::SetupConnection ( const CStr &  server)

Set up a connection to the remote networked server.

Parameters
serverIP address or host name to connect to
Returns
true on success, false on connection failure

Definition at line 130 of file NetClient.cpp.

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.

Definition at line 123 of file NetClient.cpp.

std::wstring CNetClient::TestReadGuiMessages ( )

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

Definition at line 178 of file NetClient.cpp.

Friends And Related Function Documentation

friend class CNetFileReceiveTask_ClientRejoin
friend

Definition at line 61 of file NetClient.h.

Member Data Documentation

CNetClientTurnManager* CNetClient::m_ClientTurnManager
private

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

Definition at line 200 of file NetClient.h.

CGame* CNetClient::m_Game
private

Definition at line 193 of file NetClient.h.

CScriptValRooted CNetClient::m_GameAttributes
private

Latest copy of game setup attributes heard from the server.

Definition at line 206 of file NetClient.h.

CStr CNetClient::m_GUID
private

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

Definition at line 212 of file NetClient.h.

std::deque<CScriptValRooted> CNetClient::m_GuiMessageQueue
private

Queue of messages for GuiPoll.

Definition at line 217 of file NetClient.h.

u32 CNetClient::m_HostID
private

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

Definition at line 203 of file NetClient.h.

std::string CNetClient::m_JoinSyncBuffer
private

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

Definition at line 220 of file NetClient.h.

PlayerAssignmentMap CNetClient::m_PlayerAssignments
private

Latest copy of player assignments heard from the server.

Definition at line 209 of file NetClient.h.

CNetClientSession* CNetClient::m_Session
private

Current network session (or NULL if not connected)

Definition at line 197 of file NetClient.h.

CStrW CNetClient::m_UserName
private

Definition at line 194 of file NetClient.h.


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