2012-06-14 09:06:06 -04:00
// cServer.h
// Interfaces to the cServer object representing the network server
# pragma once
2012-09-23 17:23:33 -04:00
# include "OSSupport/SocketThreads.h"
2013-03-05 15:47:29 -05:00
# include "OSSupport/ListenThread.h"
2013-11-26 12:14:46 -05:00
# include "cryptopp/rsa.h"
# include "cryptopp/randpool.h"
2013-06-27 11:14:20 -04:00
# include "RCONServer.h"
2012-06-14 09:06:06 -04:00
2013-06-29 11:30:05 -04:00
// fwd:
2012-06-14 09:06:06 -04:00
class cPlayer ;
class cClientHandle ;
2012-11-11 09:23:47 -05:00
class cIniFile ;
2013-06-29 11:30:05 -04:00
class cCommandOutputCallback ;
2012-06-14 09:06:06 -04:00
typedef std : : list < cClientHandle * > cClientHandleList ;
2013-01-11 23:46:01 -05:00
class cServer // tolua_export
2013-03-04 16:13:08 -05:00
: public cListenThread : : cCallback
2013-01-11 23:46:01 -05:00
{ // tolua_export
public : // tolua_export
2013-12-21 10:38:37 -05:00
virtual ~ cServer ( ) { }
2012-11-11 09:23:47 -05:00
bool InitServer ( cIniFile & a_SettingsIni ) ;
2012-06-14 09:06:06 -04:00
2013-08-11 13:18:06 -04:00
// tolua_begin
const AString & GetDescription ( void ) const { return m_Description ; }
// Player counts:
int GetMaxPlayers ( void ) const { return m_MaxPlayers ; }
2013-08-14 04:24:34 -04:00
int GetNumPlayers ( void ) ;
2013-08-11 13:18:06 -04:00
void SetMaxPlayers ( int a_MaxPlayers ) { m_MaxPlayers = a_MaxPlayers ; }
2013-11-04 16:51:24 -05:00
// Hardcore mode or not:
bool IsHardcore ( void ) const { return m_bIsHardcore ; }
2013-08-12 01:55:53 -04:00
// tolua_end
2012-06-14 09:06:06 -04:00
2013-03-04 16:13:08 -05:00
bool Start ( void ) ;
2012-06-14 09:06:06 -04:00
2013-06-22 15:08:34 -04:00
bool Command ( cClientHandle & a_Client , AString & a_Cmd ) ;
2013-06-29 11:30:05 -04:00
/// Executes the console command, sends output through the specified callback
void ExecuteConsoleCommand ( const AString & a_Cmd , cCommandOutputCallback & a_Output ) ;
2013-11-13 09:56:40 -05:00
/// Lists all available console commands and their helpstrings
void PrintHelp ( const AStringVector & a_Split , cCommandOutputCallback & a_Output ) ;
2013-02-15 08:00:59 -05:00
/// Binds the built-in console commands with the plugin manager
static void BindBuiltInConsoleCommands ( void ) ;
2013-08-11 14:16:41 -04:00
void Shutdown ( void ) ;
2012-06-14 09:06:06 -04:00
void KickUser ( int a_ClientID , const AString & a_Reason ) ;
void AuthenticateUser ( int a_ClientID ) ; // Called by cAuthenticator to auth the specified user
2013-08-11 13:46:27 -04:00
const AString & GetServerID ( void ) const { return m_ServerID ; } // tolua_export
2012-06-14 09:06:06 -04:00
void ClientDestroying ( const cClientHandle * a_Client ) ; // Called by cClientHandle::Destroy(); stop m_SocketThreads from calling back into a_Client
void NotifyClientWrite ( const cClientHandle * a_Client ) ; // Notifies m_SocketThreads that client has something to be written
2012-09-25 04:23:19 -04:00
void WriteToClient ( const cClientHandle * a_Client , const AString & a_Data ) ; // Queues outgoing data for the client through m_SocketThreads
2012-06-14 09:06:06 -04:00
2012-09-25 04:23:19 -04:00
void QueueClientClose ( const cClientHandle * a_Client ) ; // Queues the clienthandle to close when all its outgoing data is sent
2012-06-14 09:06:06 -04:00
2012-09-25 04:23:19 -04:00
void RemoveClient ( const cClientHandle * a_Client ) ; // Removes the clienthandle from m_SocketThreads
2012-06-14 09:06:06 -04:00
2013-08-12 02:35:13 -04:00
/// Don't tick a_Client anymore, it will be ticked from its cPlayer instead
void ClientMovedToWorld ( const cClientHandle * a_Client ) ;
2013-08-14 04:24:34 -04:00
/// Notifies the server that a player was created; the server uses this to adjust the number of players
void PlayerCreated ( const cPlayer * a_Player ) ;
2013-08-14 13:11:54 -04:00
/// Notifies the server that a player is being destroyed; the server uses this to adjust the number of players
void PlayerDestroying ( const cPlayer * a_Player ) ;
2013-08-14 04:24:34 -04:00
2012-08-30 17:06:13 -04:00
CryptoPP : : RSA : : PrivateKey & GetPrivateKey ( void ) { return m_PrivateKey ; }
CryptoPP : : RSA : : PublicKey & GetPublicKey ( void ) { return m_PublicKey ; }
2012-06-14 09:06:06 -04:00
private :
friend class cRoot ; // so cRoot can create and destroy cServer
/// When NotifyClientWrite() is called, it is queued for this thread to process (to avoid deadlocks between cSocketThreads, cClientHandle and cChunkMap)
class cNotifyWriteThread :
public cIsThread
{
typedef cIsThread super ;
cEvent m_Event ; // Set when m_Clients gets appended
cServer * m_Server ;
cCriticalSection m_CS ;
cClientHandleList m_Clients ;
virtual void Execute ( void ) ;
public :
cNotifyWriteThread ( void ) ;
~ cNotifyWriteThread ( ) ;
bool Start ( cServer * a_Server ) ;
void NotifyClientWrite ( const cClientHandle * a_Client ) ;
} ;
2013-08-11 13:46:27 -04:00
/// The server tick thread takes care of the players who aren't yet spawned in a world
class cTickThread :
public cIsThread
{
typedef cIsThread super ;
public :
cTickThread ( cServer & a_Server ) ;
protected :
cServer & m_Server ;
// cIsThread overrides:
virtual void Execute ( void ) override ;
} ;
2012-06-14 09:06:06 -04:00
cNotifyWriteThread m_NotifyWriteThread ;
2013-03-05 04:53:29 -05:00
2013-06-27 11:14:20 -04:00
cListenThread m_ListenThreadIPv4 ;
cListenThread m_ListenThreadIPv6 ;
2012-06-14 09:06:06 -04:00
2013-08-13 16:45:29 -04:00
cCriticalSection m_CSClients ; ///< Locks client lists
cClientHandleList m_Clients ; ///< Clients that are connected to the server and not yet assigned to a cWorld
cClientHandleList m_ClientsToRemove ; ///< Clients that have just been moved into a world and are to be removed from m_Clients in the next Tick()
2012-06-14 09:06:06 -04:00
2013-08-14 04:24:34 -04:00
cCriticalSection m_CSPlayerCount ; ///< Locks the m_PlayerCount
int m_PlayerCount ; ///< Number of players currently playing in the server
cCriticalSection m_CSPlayerCountDiff ; ///< Locks the m_PlayerCountDiff
int m_PlayerCountDiff ; ///< Adjustment to m_PlayerCount to be applied in the Tick thread
2012-06-14 09:06:06 -04:00
cSocketThreads m_SocketThreads ;
int m_ClientViewDistance ; // The default view distance for clients; settable in Settings.ini
bool m_bIsConnected ; // true - connected false - not connected
bool m_bRestarting ;
2012-08-30 17:06:13 -04:00
2013-06-27 11:14:20 -04:00
CryptoPP : : RSA : : PrivateKey m_PrivateKey ;
CryptoPP : : RSA : : PublicKey m_PublicKey ;
cRCONServer m_RCONServer ;
2013-08-11 13:18:06 -04:00
AString m_Description ;
int m_MaxPlayers ;
2013-11-04 16:51:24 -05:00
bool m_bIsHardcore ;
2013-08-11 13:18:06 -04:00
2013-08-11 13:46:27 -04:00
cTickThread m_TickThread ;
cEvent m_RestartEvent ;
/// The server ID used for client authentication
AString m_ServerID ;
2012-06-14 09:06:06 -04:00
2013-03-04 16:13:08 -05:00
cServer ( void ) ;
2012-06-14 09:06:06 -04:00
2012-08-30 17:06:13 -04:00
/// Loads, or generates, if missing, RSA keys for protocol encryption
void PrepareKeys ( void ) ;
2013-03-04 16:13:08 -05:00
2013-08-11 13:46:27 -04:00
bool Tick ( float a_Dt ) ;
2013-08-13 16:45:29 -04:00
/// Ticks the clients in m_Clients, manages the list in respect to removing clients
void TickClients ( float a_Dt ) ;
2013-08-11 13:46:27 -04:00
2013-03-04 16:13:08 -05:00
// cListenThread::cCallback overrides:
virtual void OnConnectionAccepted ( cSocket & a_Socket ) override ;
2013-01-11 23:46:01 -05:00
} ; // tolua_export
2012-06-14 09:06:06 -04:00