2012-06-14 13:06:06 +00:00
// cServer.h
// Interfaces to the cServer object representing the network server
# pragma once
2012-09-23 21:23:33 +00:00
# include "OSSupport/SocketThreads.h"
2013-03-05 20:47:29 +00:00
# include "OSSupport/ListenThread.h"
2014-01-05 22:06:17 +00:00
# include "RCONServer.h"
# ifdef _MSC_VER
# pragma warning(push)
# pragma warning(disable:4127)
# pragma warning(disable:4244)
# pragma warning(disable:4231)
# pragma warning(disable:4189)
# pragma warning(disable:4702)
# endif
2014-04-29 09:04:54 +00:00
# include "PolarSSL++/RsaPrivateKey.h"
2014-01-05 22:06:17 +00:00
# ifdef _MSC_VER
# pragma warning(pop)
# endif
2012-06-14 13:06:06 +00:00
2013-06-29 15:30:05 +00:00
// fwd:
2012-06-14 13:06:06 +00:00
class cPlayer ;
class cClientHandle ;
2012-11-11 14:23:47 +00:00
class cIniFile ;
2013-06-29 15:30:05 +00:00
class cCommandOutputCallback ;
2012-06-14 13:06:06 +00:00
typedef std : : list < cClientHandle * > cClientHandleList ;
2013-01-12 04:46:01 +00:00
class cServer // tolua_export
2013-03-04 21:13:08 +00:00
: public cListenThread : : cCallback
2013-01-12 04:46:01 +00:00
{ // tolua_export
public : // tolua_export
2013-12-21 15:38:37 +00:00
virtual ~ cServer ( ) { }
2012-11-11 14:23:47 +00:00
bool InitServer ( cIniFile & a_SettingsIni ) ;
2012-06-14 13:06:06 +00:00
2013-08-11 17:18:06 +00:00
// tolua_begin
const AString & GetDescription ( void ) const { return m_Description ; }
// Player counts:
int GetMaxPlayers ( void ) const { return m_MaxPlayers ; }
2014-04-19 15:53:02 +00:00
int GetNumPlayers ( void ) const ;
2013-08-11 17:18:06 +00:00
void SetMaxPlayers ( int a_MaxPlayers ) { m_MaxPlayers = a_MaxPlayers ; }
2013-11-04 21:51:24 +00:00
// Hardcore mode or not:
bool IsHardcore ( void ) const { return m_bIsHardcore ; }
2013-08-12 05:55:53 +00:00
// tolua_end
2012-06-14 13:06:06 +00:00
2013-03-04 21:13:08 +00:00
bool Start ( void ) ;
2012-06-14 13:06:06 +00:00
2013-06-22 19:08:34 +00:00
bool Command ( cClientHandle & a_Client , AString & a_Cmd ) ;
2013-06-29 15:30:05 +00:00
2014-01-28 22:53:07 +00:00
/** Executes the console command, sends output through the specified callback */
2013-06-29 15:30:05 +00:00
void ExecuteConsoleCommand ( const AString & a_Cmd , cCommandOutputCallback & a_Output ) ;
2013-11-13 14:56:40 +00:00
2014-01-28 22:53:07 +00:00
/** Lists all available console commands and their helpstrings */
2013-11-13 14:56:40 +00:00
void PrintHelp ( const AStringVector & a_Split , cCommandOutputCallback & a_Output ) ;
2013-02-15 13:00:59 +00:00
2014-01-28 22:53:07 +00:00
/** Binds the built-in console commands with the plugin manager */
2013-02-15 13:00:59 +00:00
static void BindBuiltInConsoleCommands ( void ) ;
2013-08-11 18:16:41 +00:00
void Shutdown ( void ) ;
2012-06-14 13:06:06 +00:00
void KickUser ( int a_ClientID , const AString & a_Reason ) ;
2014-04-13 11:04:56 +00:00
void AuthenticateUser ( int a_ClientID , const AString & a_Name , const AString & a_UUID ) ; // Called by cAuthenticator to auth the specified user
2012-06-14 13:06:06 +00:00
2013-08-11 17:46:27 +00:00
const AString & GetServerID ( void ) const { return m_ServerID ; } // tolua_export
2012-06-14 13:06:06 +00:00
2014-01-19 18:31:43 +00:00
/** Called by cClientHandle's destructor; stop m_SocketThreads from calling back into a_Client */
void ClientDestroying ( const cClientHandle * a_Client ) ;
2012-06-14 13:06:06 +00:00
2014-01-19 18:31:43 +00:00
/** Notifies m_SocketThreads that client has something to be written */
void NotifyClientWrite ( const cClientHandle * a_Client ) ;
2012-06-14 13:06:06 +00:00
2012-09-25 08:23:19 +00:00
void WriteToClient ( const cClientHandle * a_Client , const AString & a_Data ) ; // Queues outgoing data for the client through m_SocketThreads
2012-06-14 13:06:06 +00:00
2012-09-25 08:23:19 +00:00
void RemoveClient ( const cClientHandle * a_Client ) ; // Removes the clienthandle from m_SocketThreads
2012-06-14 13:06:06 +00:00
2014-01-28 22:53:07 +00:00
/** Don't tick a_Client anymore, it will be ticked from its cPlayer instead */
2013-08-12 06:35:13 +00:00
void ClientMovedToWorld ( const cClientHandle * a_Client ) ;
2014-01-28 22:53:07 +00:00
/** Notifies the server that a player was created; the server uses this to adjust the number of players */
2013-08-14 08:24:34 +00:00
void PlayerCreated ( const cPlayer * a_Player ) ;
2014-01-28 22:53:07 +00:00
/** Notifies the server that a player is being destroyed; the server uses this to adjust the number of players */
2013-08-14 17:11:54 +00:00
void PlayerDestroying ( const cPlayer * a_Player ) ;
2014-01-07 15:31:06 +00:00
2014-01-10 20:31:05 +00:00
/** Returns base64 encoded favicon data (obtained from favicon.png) */
2014-01-07 16:53:40 +00:00
const AString & GetFaviconData ( void ) const { return m_FaviconData ; }
2013-08-14 08:24:34 +00:00
2014-04-29 09:04:54 +00:00
cRsaPrivateKey & GetPrivateKey ( void ) { return m_PrivateKey ; }
2014-01-23 22:35:23 +00:00
const AString & GetPublicKeyDER ( void ) const { return m_PublicKeyDER ; }
2012-08-30 21:06:13 +00:00
2014-01-28 22:53:07 +00:00
bool ShouldAuthenticate ( void ) const { return m_ShouldAuthenticate ; }
2012-06-14 13:06:06 +00:00
private :
friend class cRoot ; // so cRoot can create and destroy cServer
2014-01-28 22:53:07 +00:00
/** When NotifyClientWrite() is called, it is queued for this thread to process (to avoid deadlocks between cSocketThreads, cClientHandle and cChunkMap) */
2012-06-14 13:06:06 +00:00
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 ) ;
} ;
2014-01-28 22:53:07 +00:00
/** The server tick thread takes care of the players who aren't yet spawned in a world */
2013-08-11 17:46:27 +00:00
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 13:06:06 +00:00
cNotifyWriteThread m_NotifyWriteThread ;
2013-03-05 09:53:29 +00:00
2013-06-27 15:14:20 +00:00
cListenThread m_ListenThreadIPv4 ;
cListenThread m_ListenThreadIPv6 ;
2012-06-14 13:06:06 +00:00
2013-08-13 20:45:29 +00: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 13:06:06 +00:00
2014-04-19 15:53:02 +00:00
mutable cCriticalSection m_CSPlayerCount ; ///< Locks the m_PlayerCount
2013-08-14 08:24:34 +00:00
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 13:06:06 +00: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 21:06:13 +00:00
2014-01-23 22:35:23 +00:00
/** The private key used for the assymetric encryption start in the protocols */
2014-04-29 09:04:54 +00:00
cRsaPrivateKey m_PrivateKey ;
2014-01-23 22:35:23 +00:00
/** Public key for m_PrivateKey, ASN1-DER-encoded */
AString m_PublicKeyDER ;
2013-06-27 15:14:20 +00:00
cRCONServer m_RCONServer ;
2013-08-11 17:18:06 +00:00
AString m_Description ;
2014-01-07 15:40:59 +00:00
AString m_FaviconData ;
2013-08-11 17:18:06 +00:00
int m_MaxPlayers ;
2013-11-04 21:51:24 +00:00
bool m_bIsHardcore ;
2013-08-11 17:18:06 +00:00
2013-08-11 17:46:27 +00:00
cTickThread m_TickThread ;
cEvent m_RestartEvent ;
2014-01-28 22:53:07 +00:00
/** The server ID used for client authentication */
2013-08-11 17:46:27 +00:00
AString m_ServerID ;
2014-01-28 22:53:07 +00:00
/** If true, players will be online-authenticated agains Mojang servers.
This setting is the same as the " online-mode " setting in Vanilla . */
bool m_ShouldAuthenticate ;
2012-06-14 13:06:06 +00:00
2013-03-04 21:13:08 +00:00
cServer ( void ) ;
2012-06-14 13:06:06 +00:00
2014-01-28 22:53:07 +00:00
/** Loads, or generates, if missing, RSA keys for protocol encryption */
2012-08-30 21:06:13 +00:00
void PrepareKeys ( void ) ;
2013-03-04 21:13:08 +00:00
2013-08-11 17:46:27 +00:00
bool Tick ( float a_Dt ) ;
2013-08-13 20:45:29 +00:00
2014-01-28 22:53:07 +00:00
/** Ticks the clients in m_Clients, manages the list in respect to removing clients */
2013-08-13 20:45:29 +00:00
void TickClients ( float a_Dt ) ;
2013-08-11 17:46:27 +00:00
2013-03-04 21:13:08 +00:00
// cListenThread::cCallback overrides:
virtual void OnConnectionAccepted ( cSocket & a_Socket ) override ;
2013-01-12 04:46:01 +00:00
} ; // tolua_export
2012-06-14 13:06:06 +00:00