2012-02-07 02:44:42 -05:00
2012-02-08 05:02:46 -05:00
// cServer.h
// Interfaces to the cServer object representing the network server
2011-10-03 14:41:19 -04:00
# pragma once
2012-02-08 05:02:46 -05:00
# ifndef CSERVER_H_INCLUDED
# define CSERVER_H_INCLUDED
# include "cSocketThreads.h"
2011-10-03 14:41:19 -04:00
class cPlayer ;
class cClientHandle ;
class cPacket ;
2012-02-08 05:02:46 -05:00
2012-02-25 19:36:51 -05:00
typedef std : : list < cClientHandle * > cClientHandleList ;
2012-02-08 05:02:46 -05:00
2011-10-03 14:41:19 -04:00
class cServer //tolua_export
{ //tolua_export
public : //tolua_export
static cServer * GetServer ( ) ; //tolua_export
bool InitServer ( int a_Port = 25565 ) ;
int GetPort ( ) { return m_iServerPort ; }
bool IsConnected ( ) { return m_bIsConnected ; } // returns connection status
void StartListenClient ( ) ; // Listen to client
2012-03-10 16:34:47 -05:00
void Broadcast ( const cPacket & a_Packet , cClientHandle * a_Exclude = NULL ) ;
2011-10-03 14:41:19 -04:00
bool Tick ( float a_Dt ) ;
void StartListenThread ( ) ;
bool Command ( cClientHandle & a_Client , const char * a_Cmd ) ;
void ServerCommand ( const char * a_Cmd ) ; //tolua_export
void Shutdown ( ) ;
void SendMessage ( const char * a_Message , cPlayer * a_Player = 0 , bool a_bExclude = false ) ; //tolua_export
2012-02-01 17:38:03 -05:00
2012-03-09 08:42:28 -05:00
void KickUser ( int a_ClientID , const AString & a_Reason ) ;
void AuthenticateUser ( int a_ClientID ) ; // Called by cAuthenticator to auth the specified user
2011-10-03 14:41:19 -04:00
static void ServerListenThread ( void * a_Args ) ;
2012-02-01 17:38:03 -05:00
const AString & GetServerID ( void ) const ;
2012-02-08 05:02:46 -05:00
2012-02-26 07:55:42 -05:00
void ClientDestroying ( const cClientHandle * a_Client ) ; // Called by cClientHandle::Destroy(); stop m_SocketThreads from calling back into a_Client
2012-02-08 05:02:46 -05:00
2012-02-25 19:36:51 -05:00
void NotifyClientWrite ( const cClientHandle * a_Client ) ; // Notifies m_SocketThreads that client has something to be written
void WriteToClient ( const cSocket * a_Socket , const AString & a_Data ) ; // Queues outgoing data for the socket through m_SocketThreads
void QueueClientClose ( const cSocket * a_Socket ) ; // Queues the socket to close when all its outgoing data is sent
2012-02-26 07:55:42 -05:00
void RemoveClient ( const cSocket * a_Socket ) ; // Removes the socket from m_SocketThreads
2011-10-03 14:41:19 -04:00
private :
2012-02-08 05:02:46 -05:00
2011-10-03 14:41:19 -04:00
friend class cRoot ; // so cRoot can create and destroy cServer
2012-02-08 05:02:46 -05:00
2012-02-25 19:36:51 -05:00
/// 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 ) ;
} ;
2011-10-03 14:41:19 -04:00
struct sServerState ;
sServerState * m_pState ;
2012-02-08 05:02:46 -05:00
2012-02-25 19:36:51 -05:00
cNotifyWriteThread m_NotifyWriteThread ;
cCriticalSection m_CSClients ; // Locks client list
cClientHandleList m_Clients ; // Clients that are connected to the server
2012-02-08 05:02:46 -05:00
cSocketThreads m_SocketThreads ;
2012-02-23 17:51:03 -05:00
int m_ClientViewDistance ; // The default view distance for clients; settable in Settings.ini
2011-10-03 14:41:19 -04:00
// Time since server was started
float m_Millisecondsf ;
unsigned int m_Milliseconds ;
bool m_bIsConnected ; // true - connected false - not connected
int m_iServerPort ;
bool m_bRestarting ;
2012-02-25 19:36:51 -05:00
cServer ( ) ;
~ cServer ( ) ;
2011-10-03 14:41:19 -04:00
} ; //tolua_export
2012-02-08 05:02:46 -05:00
# endif // CSERVER_H_INCLUDED