Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
CNetServerWorker Class Reference

Network server worker thread. More...

#include <NetServer.h>

Collaboration diagram for CNetServerWorker:

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 (bool useLobbyAuth)
 
 ~CNetServerWorker ()
 
bool CheckPassword (const std::string &password, const std::string &salt) const
 
void SetPassword (const CStr &hashedPassword)
 
void SetControllerSecret (const std::string &secret)
 
bool SetupConnection (const u16 port)
 Begin listening for network connections. More...
 
void AssignPlayer (int playerID, const CStr &guid)
 The given GUID will be (re)assigned to the given player ID. More...
 
void StartGame (const CStr &initAttribs)
 Switch in game mode and notify all clients to start the game. More...
 
CStrW DeduplicatePlayerName (const CStrW &original)
 Make a player name unique, if it matches any existing session's name. More...
 
const ScriptInterface & GetScriptInterface ()
 Get the script context used for init attributes. More...
 
void SetTurnLength (u32 msecs)
 Set the turn length to a fixed value. More...
 
void ProcessLobbyAuth (const CStr &name, const CStr &token)
 
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 SendHolePunchingMessage (const CStr &ip, u16 port)
 
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 (CNetServerSession *session, CFsmEvent *event)
 
static bool OnAuthenticate (CNetServerSession *session, CFsmEvent *event)
 
static bool OnSimulationCommand (CNetServerSession *session, CFsmEvent *event)
 
static bool OnSyncCheck (CNetServerSession *session, CFsmEvent *event)
 
static bool OnEndCommandBatch (CNetServerSession *session, CFsmEvent *event)
 
static bool OnChat (CNetServerSession *session, CFsmEvent *event)
 
static bool OnReady (CNetServerSession *session, CFsmEvent *event)
 
static bool OnClearAllReady (CNetServerSession *session, CFsmEvent *event)
 
static bool OnGameSetup (CNetServerSession *session, CFsmEvent *event)
 
static bool OnAssignPlayer (CNetServerSession *session, CFsmEvent *event)
 
static bool OnGameStart (CNetServerSession *session, CFsmEvent *event)
 
static bool OnLoadedGame (CNetServerSession *session, CFsmEvent *event)
 
static bool OnJoinSyncingLoadedGame (CNetServerSession *session, CFsmEvent *event)
 
static bool OnRejoined (CNetServerSession *session, CFsmEvent *event)
 
static bool OnKickPlayer (CNetServerSession *session, CFsmEvent *event)
 
static bool OnDisconnect (CNetServerSession *session, CFsmEvent *event)
 
static bool OnClientPaused (CNetServerSession *session, CFsmEvent *event)
 
static void SetupUPnP ()
 Try to find a UPnP root on the network and setup port forwarding. More...
 
static void RunThread (CNetServerWorker *data)
 

Private Attributes

ScriptInterface * m_ScriptInterface
 Internal script context for (de)serializing script messages, and for storing init attributes. More...
 
PlayerAssignmentMap m_PlayerAssignments
 
JS::PersistentRootedValue m_InitAttributes
 Stores the most current init attributes. More...
 
const bool m_LobbyAuth
 Whether this match requires lobby authentication. More...
 
ENetHost * m_Host
 
std::vector< CNetServerSession * > m_Sessions
 
CNetStatsTable * m_Stats
 
NetServerState m_State
 
CStrW m_ServerName
 
std::vector< u32 > m_BannedIPs
 
std::vector< CStrW > m_BannedPlayers
 
CStr m_Password
 
std::vector< CStr > m_PausingPlayers
 Holds the GUIDs of all currently paused players. More...
 
u32 m_NextHostID
 
CNetServerTurnManager * m_ServerTurnManager
 
CStr m_ControllerGUID
 The GUID of the client in control of the game (the 'host' from the players' perspective). More...
 
std::string m_ControllerSecret
 The 'secret' used to identify the controller of the game. More...
 
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...
 
std::thread m_UPnPThread
 
std::thread m_WorkerThread
 
std::mutex m_WorkerMutex
 
bool m_Shutdown
 
std::vector< bool > m_StartGameQueue
 
std::vector< std::string > m_InitAttributesQueue
 
std::vector< std::pair< CStr, CStr > > m_LobbyAuthQueue
 
std::vector< u32 > m_TurnLengthQueue
 

Friends

class CNetServer
 

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:

  • SetupConnection and constructor/destructor must be called from the main thread.
  • The main thread may push commands onto the Queue members, while holding the m_WorkerMutex lock.
  • Public functions (SendMessage, Broadcast) must be called from the network server thread.

Constructor & Destructor Documentation

◆ CNetServerWorker()

CNetServerWorker::CNetServerWorker ( bool  useLobbyAuth)
private

◆ ~CNetServerWorker()

CNetServerWorker::~CNetServerWorker ( )
private

Member Function Documentation

◆ AddPlayer()

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

◆ AssignPlayer()

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

The given GUID will be (re)assigned to the given player ID.

Any player currently using that ID will be unassigned.

◆ Broadcast()

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

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

◆ CheckClientConnections()

void CNetServerWorker::CheckClientConnections ( )
private

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

◆ CheckGameLoadStatus()

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.

◆ CheckPassword()

bool CNetServerWorker::CheckPassword ( const std::string &  password,
const std::string &  salt 
) const
private

◆ ClearAllPlayerReady()

void CNetServerWorker::ClearAllPlayerReady ( )
private

◆ ConstructPlayerAssignmentMessage()

void CNetServerWorker::ConstructPlayerAssignmentMessage ( CPlayerAssignmentMessage &  message)
private

◆ DeduplicatePlayerName()

CStrW CNetServerWorker::DeduplicatePlayerName ( const CStrW &  original)
private

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

◆ GetScriptInterface()

const ScriptInterface & CNetServerWorker::GetScriptInterface ( )
private

Get the script context used for init attributes.

◆ HandleConnect()

bool CNetServerWorker::HandleConnect ( CNetServerSession *  session)
private

◆ HandleMessageReceive()

void CNetServerWorker::HandleMessageReceive ( const CNetMessage *  message,
CNetServerSession *  session 
)
private

◆ KickPlayer()

void CNetServerWorker::KickPlayer ( const CStrW &  playerName,
const bool  ban 
)

Disconnects a player from gamesetup or session.

◆ NONCOPYABLE()

CNetServerWorker::NONCOPYABLE ( CNetServerWorker  )
private

◆ OnAssignPlayer()

bool CNetServerWorker::OnAssignPlayer ( CNetServerSession *  session,
CFsmEvent *  event 
)
staticprivate

◆ OnAuthenticate()

bool CNetServerWorker::OnAuthenticate ( CNetServerSession *  session,
CFsmEvent *  event 
)
staticprivate

◆ OnChat()

bool CNetServerWorker::OnChat ( CNetServerSession *  session,
CFsmEvent *  event 
)
staticprivate

◆ OnClearAllReady()

bool CNetServerWorker::OnClearAllReady ( CNetServerSession *  session,
CFsmEvent *  event 
)
staticprivate

◆ OnClientHandshake()

bool CNetServerWorker::OnClientHandshake ( CNetServerSession *  session,
CFsmEvent *  event 
)
staticprivate

◆ OnClientPaused()

bool CNetServerWorker::OnClientPaused ( CNetServerSession *  session,
CFsmEvent *  event 
)
staticprivate

◆ OnDisconnect()

bool CNetServerWorker::OnDisconnect ( CNetServerSession *  session,
CFsmEvent *  event 
)
staticprivate

◆ OnEndCommandBatch()

bool CNetServerWorker::OnEndCommandBatch ( CNetServerSession *  session,
CFsmEvent *  event 
)
staticprivate

◆ OnGameSetup()

bool CNetServerWorker::OnGameSetup ( CNetServerSession *  session,
CFsmEvent *  event 
)
staticprivate

◆ OnGameStart()

bool CNetServerWorker::OnGameStart ( CNetServerSession *  session,
CFsmEvent *  event 
)
staticprivate

◆ OnJoinSyncingLoadedGame()

bool CNetServerWorker::OnJoinSyncingLoadedGame ( CNetServerSession *  session,
CFsmEvent *  event 
)
staticprivate

◆ OnKickPlayer()

bool CNetServerWorker::OnKickPlayer ( CNetServerSession *  session,
CFsmEvent *  event 
)
staticprivate

◆ OnLoadedGame()

bool CNetServerWorker::OnLoadedGame ( CNetServerSession *  session,
CFsmEvent *  event 
)
staticprivate

◆ OnReady()

bool CNetServerWorker::OnReady ( CNetServerSession *  session,
CFsmEvent *  event 
)
staticprivate

◆ OnRejoined()

bool CNetServerWorker::OnRejoined ( CNetServerSession *  session,
CFsmEvent *  event 
)
staticprivate

◆ OnSimulationCommand()

bool CNetServerWorker::OnSimulationCommand ( CNetServerSession *  session,
CFsmEvent *  event 
)
staticprivate

◆ OnSyncCheck()

bool CNetServerWorker::OnSyncCheck ( CNetServerSession *  session,
CFsmEvent *  event 
)
staticprivate

◆ OnUserJoin()

void CNetServerWorker::OnUserJoin ( CNetServerSession *  session)
private

◆ OnUserLeave()

void CNetServerWorker::OnUserLeave ( CNetServerSession *  session)
private

◆ ProcessLobbyAuth()

void CNetServerWorker::ProcessLobbyAuth ( const CStr &  name,
const CStr &  token 
)
private

◆ RemovePlayer()

void CNetServerWorker::RemovePlayer ( const CStr &  guid)
private

◆ Run()

void CNetServerWorker::Run ( )
private

◆ RunStep()

bool CNetServerWorker::RunStep ( )
private

◆ RunThread()

void CNetServerWorker::RunThread ( CNetServerWorker *  data)
staticprivate

◆ SanitisePlayerName()

CStrW CNetServerWorker::SanitisePlayerName ( const CStrW &  original)
staticprivate

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

◆ SendHolePunchingMessage()

void CNetServerWorker::SendHolePunchingMessage ( const CStr &  ip,
u16  port 
)
private

◆ SendMessage()

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

Send a message to the given network peer.

◆ SendPlayerAssignments()

void CNetServerWorker::SendPlayerAssignments ( )
private

◆ SetControllerSecret()

void CNetServerWorker::SetControllerSecret ( const std::string &  secret)
private

◆ SetPassword()

void CNetServerWorker::SetPassword ( const CStr &  hashedPassword)
private

◆ SetTurnLength()

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.

◆ SetupConnection()

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)

◆ SetupSession()

void CNetServerWorker::SetupSession ( CNetServerSession *  session)
private

◆ SetupUPnP()

void CNetServerWorker::SetupUPnP ( )
staticprivate

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

◆ StartGame()

void CNetServerWorker::StartGame ( const CStr &  initAttribs)
private

Switch in game mode and notify all clients to start the game.

Friends And Related Function Documentation

◆ CNetServer

friend class CNetServer
friend

Member Data Documentation

◆ m_BannedIPs

std::vector<u32> CNetServerWorker::m_BannedIPs
private

◆ m_BannedPlayers

std::vector<CStrW> CNetServerWorker::m_BannedPlayers
private

◆ m_ControllerGUID

CStr CNetServerWorker::m_ControllerGUID
private

The GUID of the client in control of the game (the 'host' from the players' perspective).

◆ m_ControllerSecret

std::string CNetServerWorker::m_ControllerSecret
private

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

◆ m_Host

ENetHost* CNetServerWorker::m_Host
private

◆ m_InitAttributes

JS::PersistentRootedValue CNetServerWorker::m_InitAttributes
private

Stores the most current init attributes.

NB: this is not guaranteed to be up-to-date until the server is LOADING or INGAME. At that point, the settings are frozen and ought to be identical to the simulation Init Attributes.

◆ m_InitAttributesQueue

std::vector<std::string> CNetServerWorker::m_InitAttributesQueue
private

◆ m_JoinSyncFile

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.

◆ m_LastConnectionCheck

std::time_t CNetServerWorker::m_LastConnectionCheck
private

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

◆ m_LobbyAuth

const bool CNetServerWorker::m_LobbyAuth
private

Whether this match requires lobby authentication.

◆ m_LobbyAuthQueue

std::vector<std::pair<CStr, CStr> > CNetServerWorker::m_LobbyAuthQueue
private

◆ m_NextHostID

u32 CNetServerWorker::m_NextHostID
private

◆ m_Password

CStr CNetServerWorker::m_Password
private

◆ m_PausingPlayers

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

Holds the GUIDs of all currently paused players.

◆ m_PlayerAssignments

PlayerAssignmentMap CNetServerWorker::m_PlayerAssignments
private

◆ m_SavedCommands

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.

◆ m_ScriptInterface

ScriptInterface* CNetServerWorker::m_ScriptInterface
private

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

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

◆ m_ServerName

CStrW CNetServerWorker::m_ServerName
private

◆ m_ServerTurnManager

CNetServerTurnManager* CNetServerWorker::m_ServerTurnManager
private

◆ m_Sessions

std::vector<CNetServerSession*> CNetServerWorker::m_Sessions
private

◆ m_Shutdown

bool CNetServerWorker::m_Shutdown
private

◆ m_StartGameQueue

std::vector<bool> CNetServerWorker::m_StartGameQueue
private

◆ m_State

NetServerState CNetServerWorker::m_State
private

◆ m_Stats

CNetStatsTable* CNetServerWorker::m_Stats
private

◆ m_TurnLengthQueue

std::vector<u32> CNetServerWorker::m_TurnLengthQueue
private

◆ m_UPnPThread

std::thread CNetServerWorker::m_UPnPThread
private

◆ m_WorkerMutex

std::mutex CNetServerWorker::m_WorkerMutex
private

◆ m_WorkerThread

std::thread CNetServerWorker::m_WorkerThread
private

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