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

Network server worker thread. More...

#include <NetServer.h>

Collaboration diagram for CNetServerWorker:
Collaboration graph
[legend]

Public Member Functions

bool SendMessage (ENetPeer *peer, const CNetMessage *message)
 Send a message to the given network peer. More...
 
void KickPlayer (const CStrW &playerName, const bool ban)
 Disconnects a player from gamesetup or session. More...
 
bool Broadcast (const CNetMessage *message, const std::vector< NetServerSessionState > &targetStates)
 Send a message to all clients who match one of the given states. More...
 

Private Member Functions

 NONCOPYABLE (CNetServerWorker)
 
 CNetServerWorker (int autostartPlayers)
 
 ~CNetServerWorker ()
 
bool SetupConnection (const u16 port)
 Begin listening for network connections. More...
 
void AssignPlayer (int playerID, const CStr &guid)
 Call from the GUI to update the player assignments. More...
 
void StartGame ()
 Call from the GUI to notify all clients that they should start loading the game. More...
 
void UpdateGameAttributes (JS::MutableHandleValue attrs)
 Call from the GUI to update the game setup attributes. More...
 
CStrW DeduplicatePlayerName (const CStrW &original)
 Make a player name unique, if it matches any existing session's name. More...
 
ScriptInterfaceGetScriptInterface ()
 Get the script context used for game attributes. More...
 
void SetTurnLength (u32 msecs)
 Set the turn length to a fixed value. More...
 
void AddPlayer (const CStr &guid, const CStrW &name)
 
void RemovePlayer (const CStr &guid)
 
void SendPlayerAssignments ()
 
void ClearAllPlayerReady ()
 
void SetupSession (CNetServerSession *session)
 
bool HandleConnect (CNetServerSession *session)
 
void OnUserJoin (CNetServerSession *session)
 
void OnUserLeave (CNetServerSession *session)
 
bool CheckGameLoadStatus (CNetServerSession *changedSession)
 Checks if all clients have finished loading. More...
 
void ConstructPlayerAssignmentMessage (CPlayerAssignmentMessage &message)
 
void HandleMessageReceive (const CNetMessage *message, CNetServerSession *session)
 
void CheckClientConnections ()
 Send a network warning if the connection to a client is being lost or has bad latency. More...
 
void Run ()
 
bool RunStep ()
 

Static Private Member Functions

static CStrW SanitisePlayerName (const CStrW &original)
 Make a player name 'nicer' by limiting the length and removing forbidden characters etc. More...
 
static bool OnClientHandshake (void *context, CFsmEvent *event)
 
static bool OnAuthenticate (void *context, CFsmEvent *event)
 
static bool OnInGame (void *context, CFsmEvent *event)
 
static bool OnChat (void *context, CFsmEvent *event)
 
static bool OnReady (void *context, CFsmEvent *event)
 
static bool OnClearAllReady (void *context, CFsmEvent *event)
 
static bool OnGameSetup (void *context, CFsmEvent *event)
 
static bool OnAssignPlayer (void *context, CFsmEvent *event)
 
static bool OnStartGame (void *context, CFsmEvent *event)
 
static bool OnLoadedGame (void *context, CFsmEvent *event)
 
static bool OnJoinSyncingLoadedGame (void *context, CFsmEvent *event)
 
static bool OnRejoined (void *context, CFsmEvent *event)
 
static bool OnKickPlayer (void *context, CFsmEvent *event)
 
static bool OnDisconnect (void *context, CFsmEvent *event)
 
static bool OnClientPaused (void *context, CFsmEvent *event)
 
static void * SetupUPnP (void *)
 Try to find a UPnP root on the network and setup port forwarding. More...
 
static void * RunThread (void *data)
 

Private Attributes

ScriptInterfacem_ScriptInterface
 Internal script context for (de)serializing script messages, and for storing game attributes. More...
 
PlayerAssignmentMap m_PlayerAssignments
 
JS::PersistentRootedValue m_GameAttributes
 Stores the most current game attributes. More...
 
int m_AutostartPlayers
 
ENetHostm_Host
 
std::vector< CNetServerSession * > m_Sessions
 
CNetStatsTablem_Stats
 
NetServerState m_State
 
CStrW m_ServerName
 
CStrW m_WelcomeMessage
 
std::vector< u32m_BannedIPs
 
std::vector< CStrW > m_BannedPlayers
 
std::vector< CStr > m_PausingPlayers
 Holds the GUIDs of all currently paused players. More...
 
u32 m_NextHostID
 
CNetServerTurnManagerm_ServerTurnManager
 
CStr m_HostGUID
 
std::vector< std::vector< CSimulationMessage > > m_SavedCommands
 A copy of all simulation commands received so far, indexed by turn number, to simplify support for rejoining etc. More...
 
std::string m_JoinSyncFile
 The latest copy of the simulation state, received from an existing client when a new client has asked to rejoin the game. More...
 
std::time_t m_LastConnectionCheck
 Time when the clients connections were last checked for timeouts and latency. More...
 
pthread_t m_UPnPThread
 
pthread_t m_WorkerThread
 
CMutex m_WorkerMutex
 
bool m_Shutdown
 
std::vector< bool > m_StartGameQueue
 
std::vector< std::string > m_GameAttributesQueue
 
std::vector< u32m_TurnLengthQueue
 

Friends

class CNetServer
 
class CNetFileReceiveTask_ServerRejoin
 

Detailed Description

Network server worker thread.

(This is run in a thread so that client/server communication is not delayed by the host player's framerate - the only delay should be the network latency.)

Thread-safety:

Constructor & Destructor Documentation

CNetServerWorker::CNetServerWorker ( int  autostartPlayers)
private
CNetServerWorker::~CNetServerWorker ( )
private

Member Function Documentation

void CNetServerWorker::AddPlayer ( const CStr &  guid,
const CStrW &  name 
)
private
void CNetServerWorker::AssignPlayer ( int  playerID,
const CStr &  guid 
)
private

Call from the GUI to update the player assignments.

The given GUID will be (re)assigned to the given player ID. Any player currently using that ID will be unassigned. The changes will be propagated to all clients.

bool CNetServerWorker::Broadcast ( const CNetMessage message,
const std::vector< NetServerSessionState > &  targetStates 
)

Send a message to all clients who match one of the given states.

void CNetServerWorker::CheckClientConnections ( )
private

Send a network warning if the connection to a client is being lost or has bad latency.

bool CNetServerWorker::CheckGameLoadStatus ( CNetServerSession changedSession)
private

Checks if all clients have finished loading.

If so informs the clients about that and change the server state.

Returns if all clients finished loading.

void CNetServerWorker::ClearAllPlayerReady ( )
private
void CNetServerWorker::ConstructPlayerAssignmentMessage ( CPlayerAssignmentMessage &  message)
private
CStrW CNetServerWorker::DeduplicatePlayerName ( const CStrW &  original)
private

Make a player name unique, if it matches any existing session's name.

ScriptInterface & CNetServerWorker::GetScriptInterface ( )
private

Get the script context used for game attributes.

bool CNetServerWorker::HandleConnect ( CNetServerSession session)
private
void CNetServerWorker::HandleMessageReceive ( const CNetMessage message,
CNetServerSession session 
)
private
void CNetServerWorker::KickPlayer ( const CStrW &  playerName,
const bool  ban 
)

Disconnects a player from gamesetup or session.

CNetServerWorker::NONCOPYABLE ( CNetServerWorker  )
private
bool CNetServerWorker::OnAssignPlayer ( void *  context,
CFsmEvent event 
)
staticprivate
bool CNetServerWorker::OnAuthenticate ( void *  context,
CFsmEvent event 
)
staticprivate
bool CNetServerWorker::OnChat ( void *  context,
CFsmEvent event 
)
staticprivate
bool CNetServerWorker::OnClearAllReady ( void *  context,
CFsmEvent event 
)
staticprivate
bool CNetServerWorker::OnClientHandshake ( void *  context,
CFsmEvent event 
)
staticprivate
bool CNetServerWorker::OnClientPaused ( void *  context,
CFsmEvent event 
)
staticprivate
bool CNetServerWorker::OnDisconnect ( void *  context,
CFsmEvent event 
)
staticprivate
bool CNetServerWorker::OnGameSetup ( void *  context,
CFsmEvent event 
)
staticprivate
bool CNetServerWorker::OnInGame ( void *  context,
CFsmEvent event 
)
staticprivate
bool CNetServerWorker::OnJoinSyncingLoadedGame ( void *  context,
CFsmEvent event 
)
staticprivate
bool CNetServerWorker::OnKickPlayer ( void *  context,
CFsmEvent event 
)
staticprivate
bool CNetServerWorker::OnLoadedGame ( void *  context,
CFsmEvent event 
)
staticprivate
bool CNetServerWorker::OnReady ( void *  context,
CFsmEvent event 
)
staticprivate
bool CNetServerWorker::OnRejoined ( void *  context,
CFsmEvent event 
)
staticprivate
bool CNetServerWorker::OnStartGame ( void *  context,
CFsmEvent event 
)
staticprivate
void CNetServerWorker::OnUserJoin ( CNetServerSession session)
private
void CNetServerWorker::OnUserLeave ( CNetServerSession session)
private
void CNetServerWorker::RemovePlayer ( const CStr &  guid)
private
void CNetServerWorker::Run ( )
private
bool CNetServerWorker::RunStep ( )
private
void * CNetServerWorker::RunThread ( void *  data)
staticprivate
CStrW CNetServerWorker::SanitisePlayerName ( const CStrW &  original)
staticprivate

Make a player name 'nicer' by limiting the length and removing forbidden characters etc.

bool CNetServerWorker::SendMessage ( ENetPeer peer,
const CNetMessage message 
)

Send a message to the given network peer.

void CNetServerWorker::SendPlayerAssignments ( )
private
void CNetServerWorker::SetTurnLength ( u32  msecs)
private

Set the turn length to a fixed value.

TODO: we should replace this with some adaptive lag-dependent computation.

bool CNetServerWorker::SetupConnection ( const u16  port)
private

Begin listening for network connections.

Returns
true on success, false on error (e.g. port already in use)
void CNetServerWorker::SetupSession ( CNetServerSession session)
private
void * CNetServerWorker::SetupUPnP ( void *  )
staticprivate

Try to find a UPnP root on the network and setup port forwarding.

void CNetServerWorker::StartGame ( )
private

Call from the GUI to notify all clients that they should start loading the game.

void CNetServerWorker::UpdateGameAttributes ( JS::MutableHandleValue  attrs)
private

Call from the GUI to update the game setup attributes.

This must be called at least once before starting the game. The changes will be propagated to all clients.

Parameters
attrsgame attributes, in the script context of GetScriptInterface()

Friends And Related Function Documentation

friend class CNetFileReceiveTask_ServerRejoin
friend
friend class CNetServer
friend

Member Data Documentation

int CNetServerWorker::m_AutostartPlayers
private
std::vector<u32> CNetServerWorker::m_BannedIPs
private
std::vector<CStrW> CNetServerWorker::m_BannedPlayers
private
JS::PersistentRootedValue CNetServerWorker::m_GameAttributes
private

Stores the most current game attributes.

std::vector<std::string> CNetServerWorker::m_GameAttributesQueue
private
ENetHost* CNetServerWorker::m_Host
private
CStr CNetServerWorker::m_HostGUID
private
std::string CNetServerWorker::m_JoinSyncFile
private

The latest copy of the simulation state, received from an existing client when a new client has asked to rejoin the game.

std::time_t CNetServerWorker::m_LastConnectionCheck
private

Time when the clients connections were last checked for timeouts and latency.

u32 CNetServerWorker::m_NextHostID
private
std::vector<CStr> CNetServerWorker::m_PausingPlayers
private

Holds the GUIDs of all currently paused players.

PlayerAssignmentMap CNetServerWorker::m_PlayerAssignments
private
std::vector<std::vector<CSimulationMessage> > CNetServerWorker::m_SavedCommands
private

A copy of all simulation commands received so far, indexed by turn number, to simplify support for rejoining etc.

TODO: verify this doesn't use too much RAM.

ScriptInterface* CNetServerWorker::m_ScriptInterface
private

Internal script context for (de)serializing script messages, and for storing game attributes.

(TODO: we shouldn't bother deserializing (except for debug printing of messages), we should just forward messages blindly and efficiently.)

CStrW CNetServerWorker::m_ServerName
private
CNetServerTurnManager* CNetServerWorker::m_ServerTurnManager
private
std::vector<CNetServerSession*> CNetServerWorker::m_Sessions
private
bool CNetServerWorker::m_Shutdown
private
std::vector<bool> CNetServerWorker::m_StartGameQueue
private
NetServerState CNetServerWorker::m_State
private
CNetStatsTable* CNetServerWorker::m_Stats
private
std::vector<u32> CNetServerWorker::m_TurnLengthQueue
private
pthread_t CNetServerWorker::m_UPnPThread
private
CStrW CNetServerWorker::m_WelcomeMessage
private
CMutex CNetServerWorker::m_WorkerMutex
private
pthread_t CNetServerWorker::m_WorkerThread
private

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