2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
// ReDucTor is an awesome guy who helped me a lot
|
|
|
|
|
|
|
|
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
|
|
|
|
2012-09-23 18:09:57 -04:00
|
|
|
#include "Server.h"
|
|
|
|
#include "ClientHandle.h"
|
2012-09-23 17:23:33 -04:00
|
|
|
#include "OSSupport/Timer.h"
|
2012-09-23 16:53:08 -04:00
|
|
|
#include "Mobs/Monster.h"
|
2012-09-23 17:23:33 -04:00
|
|
|
#include "OSSupport/Socket.h"
|
2012-09-23 18:09:57 -04:00
|
|
|
#include "Root.h"
|
|
|
|
#include "World.h"
|
2012-06-14 09:06:06 -04:00
|
|
|
#include "ChunkDef.h"
|
2012-09-23 18:09:57 -04:00
|
|
|
#include "PluginManager.h"
|
|
|
|
#include "GroupManager.h"
|
|
|
|
#include "ChatColor.h"
|
|
|
|
#include "Player.h"
|
|
|
|
#include "Inventory.h"
|
|
|
|
#include "Item.h"
|
|
|
|
#include "FurnaceRecipe.h"
|
|
|
|
#include "Tracer.h"
|
|
|
|
#include "WebAdmin.h"
|
2012-09-23 16:03:26 -04:00
|
|
|
#include "Protocol/ProtocolRecognizer.h"
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
#include "MersenneTwister.h"
|
|
|
|
|
|
|
|
#include "../iniFile/iniFile.h"
|
|
|
|
#include "Vector3f.h"
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include "zlib.h"
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-10-19 16:51:46 -04:00
|
|
|
|
2012-10-19 18:09:33 -04:00
|
|
|
// For the "dumpmem" server command:
|
|
|
|
/// Synchronize this with main.cpp - the leak finder needs initialization before it can be used to dump memory
|
|
|
|
#define ENABLE_LEAK_FINDER
|
|
|
|
|
|
|
|
#if defined(_MSC_VER) && defined(_DEBUG) && defined(ENABLE_LEAK_FINDER)
|
|
|
|
#pragma warning(push)
|
|
|
|
#pragma warning(disable:4100)
|
|
|
|
#include "LeakFinder.h"
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
typedef std::list< cClientHandle* > ClientList;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct cServer::sServerState
|
|
|
|
{
|
|
|
|
sServerState()
|
2013-03-04 16:13:08 -05:00
|
|
|
: pTickThread(NULL)
|
|
|
|
, bStopTickThread(false)
|
2012-06-14 09:06:06 -04:00
|
|
|
{}
|
|
|
|
|
|
|
|
cThread* pTickThread; bool bStopTickThread;
|
|
|
|
|
|
|
|
cEvent RestartEvent;
|
|
|
|
std::string ServerID;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cServer::ClientDestroying(const cClientHandle * a_Client)
|
|
|
|
{
|
|
|
|
m_SocketThreads.StopReading(a_Client);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cServer::NotifyClientWrite(const cClientHandle * a_Client)
|
|
|
|
{
|
|
|
|
m_NotifyWriteThread.NotifyClientWrite(a_Client);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-09-25 04:23:19 -04:00
|
|
|
void cServer::WriteToClient(const cClientHandle * a_Client, const AString & a_Data)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-09-25 04:23:19 -04:00
|
|
|
m_SocketThreads.Write(a_Client, a_Data);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-09-25 04:23:19 -04:00
|
|
|
void cServer::QueueClientClose(const cClientHandle * a_Client)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-09-25 04:23:19 -04:00
|
|
|
m_SocketThreads.QueueClose(a_Client);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-09-25 04:23:19 -04:00
|
|
|
void cServer::RemoveClient(const cClientHandle * a_Client)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-09-25 04:23:19 -04:00
|
|
|
m_SocketThreads.RemoveClient(a_Client);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-11-11 09:23:47 -05:00
|
|
|
bool cServer::InitServer(cIniFile & a_SettingsIni)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-11-11 09:23:47 -05:00
|
|
|
if (m_bIsConnected)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
LOGERROR("ERROR: Trying to initialize server while server is already running!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("/============================\\\n");
|
|
|
|
printf("| Custom Minecraft Server |\n");
|
|
|
|
printf("| Created by Kevin Bansberg |\n");
|
|
|
|
printf("| A.K.A. FakeTruth |\n");
|
|
|
|
printf("| Monsters by Alex Sonek |\n");
|
|
|
|
printf("| A.K.A. Duralex |\n");
|
|
|
|
printf("| Stuff by Mattes D |\n");
|
|
|
|
printf("| A.K.A. _Xoft(o) |\n");
|
|
|
|
printf("\\============================/\n");
|
|
|
|
printf("More info: WWW.MC-SERVER.ORG\n");
|
|
|
|
printf(" WWW.AE-C.NET\n");
|
|
|
|
printf(" WWW.RBTHINKTANK.COM\n");
|
|
|
|
printf("email: faketruth@gmail.com\n\n");
|
|
|
|
|
|
|
|
LOG("Starting up server.");
|
2013-01-11 23:46:01 -05:00
|
|
|
LOGINFO("Compatible clients: %s", MCS_CLIENT_VERSIONS);
|
|
|
|
LOGINFO("Compatible protocol versions %s", MCS_PROTOCOL_VERSIONS);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2012-11-11 09:23:47 -05:00
|
|
|
if (cSocket::WSAStartup() != 0) // Only does anything on Windows, but whatever
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
LOGERROR("WSAStartup() != 0");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-03-05 04:53:29 -05:00
|
|
|
bool HasAnyPorts = false;
|
2013-03-04 16:13:08 -05:00
|
|
|
AString Ports = a_SettingsIni.GetValueSet("Server", "Port", "25565");
|
2013-03-05 04:53:29 -05:00
|
|
|
m_ListenThreadIPv4.SetReuseAddr(true);
|
|
|
|
if (m_ListenThreadIPv4.Initialize(Ports))
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-03-05 04:53:29 -05:00
|
|
|
HasAnyPorts = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ports = a_SettingsIni.GetValueSet("Server", "PortsIPv6", "25565");
|
|
|
|
m_ListenThreadIPv6.SetReuseAddr(true);
|
|
|
|
if (m_ListenThreadIPv6.Initialize(Ports))
|
|
|
|
{
|
|
|
|
HasAnyPorts = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!HasAnyPorts)
|
|
|
|
{
|
|
|
|
LOGERROR("Couldn't open any ports. Aborting the server");
|
2012-06-14 09:06:06 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-06-27 11:14:20 -04:00
|
|
|
m_RCONServer.Initialize(a_SettingsIni);
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
m_bIsConnected = true;
|
|
|
|
|
2012-11-11 09:23:47 -05:00
|
|
|
m_pState->ServerID = "-";
|
|
|
|
if (a_SettingsIni.GetValueSetB("Authentication", "Authenticate", true))
|
|
|
|
{
|
|
|
|
MTRand mtrand1;
|
|
|
|
unsigned int r1 = (mtrand1.randInt()%1147483647) + 1000000000;
|
|
|
|
unsigned int r2 = (mtrand1.randInt()%1147483647) + 1000000000;
|
|
|
|
std::ostringstream sid;
|
|
|
|
sid << std::hex << r1;
|
|
|
|
sid << std::hex << r2;
|
|
|
|
std::string ServerID = sid.str();
|
|
|
|
ServerID.resize(16, '0');
|
|
|
|
m_pState->ServerID = ServerID;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_ClientViewDistance = a_SettingsIni.GetValueSetI("Server", "DefaultViewDistance", cClientHandle::DEFAULT_VIEW_DISTANCE);
|
|
|
|
if (m_ClientViewDistance < cClientHandle::MIN_VIEW_DISTANCE)
|
|
|
|
{
|
|
|
|
m_ClientViewDistance = cClientHandle::MIN_VIEW_DISTANCE;
|
|
|
|
LOGINFO("Setting default viewdistance to the minimum of %d", m_ClientViewDistance);
|
|
|
|
}
|
|
|
|
if (m_ClientViewDistance > cClientHandle::MAX_VIEW_DISTANCE)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-11-11 09:23:47 -05:00
|
|
|
m_ClientViewDistance = cClientHandle::MAX_VIEW_DISTANCE;
|
|
|
|
LOGINFO("Setting default viewdistance to the maximum of %d", m_ClientViewDistance);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
m_NotifyWriteThread.Start(this);
|
|
|
|
|
2012-08-30 17:06:13 -04:00
|
|
|
PrepareKeys();
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-03-04 16:13:08 -05:00
|
|
|
cServer::cServer(void)
|
|
|
|
: m_pState(new sServerState)
|
2013-06-27 11:14:20 -04:00
|
|
|
, m_ListenThreadIPv4(*this, cSocket::IPv4, "Client")
|
|
|
|
, m_ListenThreadIPv6(*this, cSocket::IPv6, "Client")
|
2013-03-04 16:13:08 -05:00
|
|
|
, m_Millisecondsf(0)
|
|
|
|
, m_Milliseconds(0)
|
|
|
|
, m_bIsConnected(false)
|
|
|
|
, m_bRestarting(false)
|
2013-06-27 11:14:20 -04:00
|
|
|
, m_RCONServer(*this)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cServer::~cServer()
|
|
|
|
{
|
|
|
|
// TODO: Shut down the server gracefully
|
|
|
|
m_pState->bStopTickThread = true;
|
|
|
|
delete m_pState->pTickThread; m_pState->pTickThread = NULL;
|
|
|
|
|
|
|
|
delete m_pState;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-30 17:06:13 -04:00
|
|
|
void cServer::PrepareKeys(void)
|
|
|
|
{
|
|
|
|
// TODO: Save and load key for persistence across sessions
|
|
|
|
// But generating the key takes only a moment, do we even need that?
|
|
|
|
|
|
|
|
LOG("Generating protocol encryption keypair...");
|
2012-09-04 06:17:27 -04:00
|
|
|
|
|
|
|
time_t CurTime = time(NULL);
|
|
|
|
CryptoPP::RandomPool rng;
|
|
|
|
rng.Put((const byte *)&CurTime, sizeof(CurTime));
|
2012-08-30 17:06:13 -04:00
|
|
|
m_PrivateKey.GenerateRandomWithKeySize(rng, 1024);
|
|
|
|
CryptoPP::RSA::PublicKey pk(m_PrivateKey);
|
|
|
|
m_PublicKey = pk;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-03-04 16:13:08 -05:00
|
|
|
void cServer::OnConnectionAccepted(cSocket & a_Socket)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-03-04 16:13:08 -05:00
|
|
|
if (!a_Socket.IsValid())
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-03-04 16:13:08 -05:00
|
|
|
const AString & ClientIP = a_Socket.GetIPString();
|
2012-06-14 09:06:06 -04:00
|
|
|
if (ClientIP.empty())
|
|
|
|
{
|
|
|
|
LOGWARN("cServer: A client connected, but didn't present its IP, disconnecting.");
|
2013-03-04 16:13:08 -05:00
|
|
|
a_Socket.CloseSocket();
|
2012-06-14 09:06:06 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG("Client \"%s\" connected!", ClientIP.c_str());
|
|
|
|
|
2013-03-04 16:13:08 -05:00
|
|
|
cClientHandle * NewHandle = new cClientHandle(&a_Socket, m_ClientViewDistance);
|
|
|
|
if (!m_SocketThreads.AddClient(a_Socket, NewHandle))
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
// For some reason SocketThreads have rejected the handle, clean it up
|
2013-03-04 16:13:08 -05:00
|
|
|
LOGERROR("Client \"%s\" cannot be handled, server probably unstable", ClientIP.c_str());
|
|
|
|
a_Socket.CloseSocket();
|
2012-06-14 09:06:06 -04:00
|
|
|
delete NewHandle;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cCSLock Lock(m_CSClients);
|
2013-03-04 16:13:08 -05:00
|
|
|
m_Clients.push_back(NewHandle);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cServer::BroadcastChat(const AString & a_Message, const cClientHandle * a_Exclude)
|
|
|
|
{
|
|
|
|
cCSLock Lock(m_CSClients);
|
|
|
|
for (ClientList::iterator itr = m_Clients.begin(); itr != m_Clients.end(); ++itr)
|
|
|
|
{
|
|
|
|
if ((*itr == a_Exclude) || !(*itr)->IsLoggedIn())
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
(*itr)->SendChat(a_Message);
|
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool cServer::Tick(float a_Dt)
|
|
|
|
{
|
|
|
|
m_Millisecondsf += a_Dt;
|
2013-04-03 12:18:51 -04:00
|
|
|
if (m_Millisecondsf > 1.f)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
m_Milliseconds += (int)m_Millisecondsf;
|
|
|
|
m_Millisecondsf = m_Millisecondsf - (int)m_Millisecondsf;
|
|
|
|
}
|
|
|
|
|
2013-04-03 12:18:51 -04:00
|
|
|
cRoot::Get()->TickWorlds(a_Dt); // TODO - Maybe give all worlds their own thread?
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
cClientHandleList RemoveClients;
|
|
|
|
{
|
|
|
|
cCSLock Lock(m_CSClients);
|
|
|
|
for (cClientHandleList::iterator itr = m_Clients.begin(); itr != m_Clients.end();)
|
|
|
|
{
|
|
|
|
if ((*itr)->IsDestroyed())
|
|
|
|
{
|
|
|
|
RemoveClients.push_back(*itr); // Remove the client later, when CS is not held, to avoid deadlock ( http://forum.mc-server.org/showthread.php?tid=374 )
|
|
|
|
itr = m_Clients.erase(itr);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
(*itr)->Tick(a_Dt);
|
|
|
|
++itr;
|
|
|
|
} // for itr - m_Clients[]
|
|
|
|
}
|
|
|
|
for (cClientHandleList::iterator itr = RemoveClients.begin(); itr != RemoveClients.end(); ++itr)
|
|
|
|
{
|
|
|
|
delete *itr;
|
|
|
|
} // for itr - RemoveClients[]
|
|
|
|
|
2013-04-03 12:18:51 -04:00
|
|
|
cRoot::Get()->GetPluginManager()->Tick(a_Dt);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-04-03 12:18:51 -04:00
|
|
|
if (!m_bRestarting)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_bRestarting = false;
|
|
|
|
m_pState->RestartEvent.Set();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ServerTickThread( void * a_Param )
|
|
|
|
{
|
|
|
|
LOG("ServerTickThread");
|
|
|
|
cServer *CServerObj = (cServer*)a_Param;
|
|
|
|
|
|
|
|
cTimer Timer;
|
|
|
|
|
|
|
|
long long msPerTick = 50; // TODO - Put this in server config file
|
|
|
|
long long LastTime = Timer.GetNowTime();
|
|
|
|
|
|
|
|
bool bKeepGoing = true;
|
|
|
|
while( bKeepGoing )
|
|
|
|
{
|
|
|
|
long long NowTime = Timer.GetNowTime();
|
|
|
|
float DeltaTime = (float)(NowTime-LastTime);
|
|
|
|
bKeepGoing = CServerObj->Tick( DeltaTime );
|
|
|
|
long long TickTime = Timer.GetNowTime() - NowTime;
|
|
|
|
|
|
|
|
if( TickTime < msPerTick ) // Stretch tick time until it's at least msPerTick
|
|
|
|
{
|
|
|
|
cSleep::MilliSleep( (unsigned int)( msPerTick - TickTime ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
LastTime = NowTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG("TICK THREAD STOPPED");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-03-04 16:13:08 -05:00
|
|
|
bool cServer::Start(void)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
m_pState->pTickThread = new cThread( ServerTickThread, this, "cServer::ServerTickThread" );
|
2013-03-05 04:53:29 -05:00
|
|
|
if (!m_ListenThreadIPv4.Start())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!m_ListenThreadIPv6.Start())
|
2013-03-04 16:13:08 -05:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
m_pState->pTickThread->Start( true );
|
2013-03-04 16:13:08 -05:00
|
|
|
return true;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-06-22 15:08:34 -04:00
|
|
|
bool cServer::Command(cClientHandle & a_Client, AString & a_Cmd)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-08-18 06:38:15 -04:00
|
|
|
return cRoot::Get()->GetPluginManager()->CallHookChat(a_Client.GetPlayer(), a_Cmd);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-02-15 08:00:59 -05:00
|
|
|
void cServer::ExecuteConsoleCommand(const AString & a_Cmd)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-08-18 06:38:15 -04:00
|
|
|
AStringVector split = StringSplit(a_Cmd, " ");
|
|
|
|
if (split.empty())
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-02-15 08:00:59 -05:00
|
|
|
// Special handling: "stop" and "restart" are built in
|
2012-08-18 06:38:15 -04:00
|
|
|
if ((split[0].compare("stop") == 0) || (split[0].compare("restart") == 0))
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-02-15 08:00:59 -05:00
|
|
|
|
|
|
|
// There is currently no way a plugin can do these (and probably won't ever be):
|
2012-06-14 09:06:06 -04:00
|
|
|
if (split[0].compare("chunkstats") == 0)
|
|
|
|
{
|
2012-08-15 08:08:11 -04:00
|
|
|
cRoot::Get()->LogChunkStats();
|
2012-06-14 09:06:06 -04:00
|
|
|
return;
|
|
|
|
}
|
2012-10-19 18:09:33 -04:00
|
|
|
#if defined(_MSC_VER) && defined(_DEBUG) && defined(ENABLE_LEAK_FINDER)
|
|
|
|
if (split[0].compare("dumpmem") == 0)
|
|
|
|
{
|
|
|
|
LeakFinderXmlOutput Output("memdump.xml");
|
|
|
|
DumpUsedMemory(&Output);
|
|
|
|
return;
|
|
|
|
}
|
2013-03-23 15:04:39 -04:00
|
|
|
|
|
|
|
if (split[0].compare("killmem") == 0)
|
|
|
|
{
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
new char[100 * 1024 * 1024]; // Allocate and leak 100 MiB in a loop -> fill memory and kill MCS
|
|
|
|
}
|
|
|
|
}
|
2012-10-19 18:09:33 -04:00
|
|
|
#endif
|
|
|
|
|
2013-02-15 08:00:59 -05:00
|
|
|
if (cPluginManager::Get()->ExecuteConsoleCommand(split))
|
2012-11-20 16:28:43 -05:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-02-15 08:00:59 -05:00
|
|
|
LOG("Unknown command, type 'help' for all commands.\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cServer::BindBuiltInConsoleCommands(void)
|
|
|
|
{
|
|
|
|
cPluginManager * PlgMgr = cPluginManager::Get();
|
|
|
|
PlgMgr->BindConsoleCommand("restart", NULL, "Restarts the server cleanly");
|
|
|
|
PlgMgr->BindConsoleCommand("stop", NULL, "Stops the server cleanly");
|
|
|
|
PlgMgr->BindConsoleCommand("chunkstats", NULL, "Displays detailed chunk memory statistics");
|
|
|
|
#if defined(_MSC_VER) && defined(_DEBUG) && defined(ENABLE_LEAK_FINDER)
|
|
|
|
PlgMgr->BindConsoleCommand("dumpmem", NULL, "Dumps all used memory blocks together with their callstacks into memdump.xml");
|
|
|
|
#endif
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-24 03:58:26 -04:00
|
|
|
void cServer::SendMessage(const AString & a_Message, cPlayer * a_Player /* = NULL */, bool a_bExclude /* = false */ )
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-08-24 03:58:26 -04:00
|
|
|
if ((a_Player != NULL) && !a_bExclude)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-08-24 03:58:26 -04:00
|
|
|
cClientHandle * Client = a_Player->GetClientHandle();
|
|
|
|
if (Client != NULL)
|
|
|
|
{
|
|
|
|
Client->SendChat(a_Message);
|
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-08-24 03:58:26 -04:00
|
|
|
BroadcastChat(a_Message, (a_Player != NULL) ? a_Player->GetClientHandle() : NULL);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cServer::Shutdown()
|
|
|
|
{
|
2013-03-05 04:53:29 -05:00
|
|
|
m_ListenThreadIPv4.Stop();
|
|
|
|
m_ListenThreadIPv6.Stop();
|
2013-03-04 16:13:08 -05:00
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
m_bRestarting = true;
|
|
|
|
m_pState->RestartEvent.Wait();
|
|
|
|
|
|
|
|
cRoot::Get()->SaveAllChunks();
|
|
|
|
|
|
|
|
cCSLock Lock(m_CSClients);
|
|
|
|
for( ClientList::iterator itr = m_Clients.begin(); itr != m_Clients.end(); ++itr )
|
|
|
|
{
|
2012-06-19 13:34:22 -04:00
|
|
|
(*itr)->Destroy();
|
2012-06-14 09:06:06 -04:00
|
|
|
delete *itr;
|
|
|
|
}
|
|
|
|
m_Clients.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const AString & cServer::GetServerID(void) const
|
|
|
|
{
|
|
|
|
return m_pState->ServerID;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cServer::KickUser(int a_ClientID, const AString & a_Reason)
|
|
|
|
{
|
|
|
|
cCSLock Lock(m_CSClients);
|
|
|
|
for (ClientList::iterator itr = m_Clients.begin(); itr != m_Clients.end(); ++itr)
|
|
|
|
{
|
|
|
|
if ((*itr)->GetUniqueID() == a_ClientID)
|
|
|
|
{
|
|
|
|
(*itr)->Kick(a_Reason);
|
|
|
|
}
|
|
|
|
} // for itr - m_Clients[]
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cServer::AuthenticateUser(int a_ClientID)
|
|
|
|
{
|
|
|
|
cCSLock Lock(m_CSClients);
|
|
|
|
for (ClientList::iterator itr = m_Clients.begin(); itr != m_Clients.end(); ++itr)
|
|
|
|
{
|
|
|
|
if ((*itr)->GetUniqueID() == a_ClientID)
|
|
|
|
{
|
|
|
|
(*itr)->Authenticate();
|
|
|
|
}
|
|
|
|
} // for itr - m_Clients[]
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// cServer::cClientPacketThread:
|
|
|
|
|
|
|
|
cServer::cNotifyWriteThread::cNotifyWriteThread(void) :
|
|
|
|
super("ClientPacketThread"),
|
|
|
|
m_Server(NULL)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cServer::cNotifyWriteThread::~cNotifyWriteThread()
|
|
|
|
{
|
|
|
|
m_ShouldTerminate = true;
|
|
|
|
m_Event.Set();
|
|
|
|
Wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool cServer::cNotifyWriteThread::Start(cServer * a_Server)
|
|
|
|
{
|
|
|
|
m_Server = a_Server;
|
|
|
|
return super::Start();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cServer::cNotifyWriteThread::Execute(void)
|
|
|
|
{
|
|
|
|
cClientHandleList Clients;
|
|
|
|
while (!m_ShouldTerminate)
|
|
|
|
{
|
|
|
|
cCSLock Lock(m_CS);
|
|
|
|
while (m_Clients.size() == 0)
|
|
|
|
{
|
|
|
|
cCSUnlock Unlock(Lock);
|
|
|
|
m_Event.Wait();
|
|
|
|
if (m_ShouldTerminate)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy the clients to notify and unlock the CS:
|
|
|
|
Clients.splice(Clients.begin(), m_Clients);
|
|
|
|
Lock.Unlock();
|
|
|
|
|
|
|
|
for (cClientHandleList::iterator itr = Clients.begin(); itr != Clients.end(); ++itr)
|
|
|
|
{
|
|
|
|
m_Server->m_SocketThreads.NotifyWrite(*itr);
|
|
|
|
} // for itr - Clients[]
|
|
|
|
Clients.clear();
|
|
|
|
} // while (!mShouldTerminate)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cServer::cNotifyWriteThread::NotifyClientWrite(const cClientHandle * a_Client)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
cCSLock Lock(m_CS);
|
|
|
|
m_Clients.remove(const_cast<cClientHandle *>(a_Client)); // Put it there only once
|
|
|
|
m_Clients.push_back(const_cast<cClientHandle *>(a_Client));
|
|
|
|
}
|
|
|
|
m_Event.Set();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|