2012-01-29 14:28:19 -05:00
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
// ReDucTor is an awesome guy who helped me a lot
|
|
|
|
|
2012-01-29 14:28:19 -05:00
|
|
|
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
#include "cServer.h"
|
|
|
|
#include "cClientHandle.h"
|
|
|
|
#include "cSleep.h"
|
|
|
|
#include "cTimer.h"
|
|
|
|
#include "cMonster.h"
|
|
|
|
#include "cSocket.h"
|
|
|
|
#include "cRoot.h"
|
|
|
|
#include "cWorld.h"
|
|
|
|
#include "cChunk.h"
|
|
|
|
#include "cPluginManager.h"
|
|
|
|
#include "cGroupManager.h"
|
|
|
|
#include "cChatColor.h"
|
|
|
|
#include "cPlayer.h"
|
|
|
|
#include "cInventory.h"
|
|
|
|
#include "cItem.h"
|
|
|
|
#include "cRecipeChecker.h"
|
|
|
|
#include "cFurnaceRecipe.h"
|
|
|
|
#include "cTracer.h"
|
|
|
|
#include "cWebAdmin.h"
|
|
|
|
|
2011-12-25 08:44:22 -05:00
|
|
|
#include "MersenneTwister.h"
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
#include "../iniFile/iniFile.h"
|
|
|
|
#include "Vector3f.h"
|
|
|
|
|
|
|
|
#include "packets/cPacket_Chat.h"
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <iostream>
|
|
|
|
|
2012-01-29 14:28:19 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
extern "C" {
|
|
|
|
#include "zlib.h"
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool g_bWaterPhysics = false;
|
|
|
|
|
2011-11-01 17:57:08 -04:00
|
|
|
typedef std::list< cClientHandle* > ClientList;
|
|
|
|
|
2012-02-01 17:38:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
struct cServer::sServerState
|
|
|
|
{
|
|
|
|
sServerState()
|
|
|
|
: pListenThread( 0 )
|
|
|
|
, pTickThread( 0 )
|
|
|
|
, bStopListenThread( false )
|
|
|
|
, bStopTickThread( false )
|
|
|
|
{}
|
|
|
|
cSocket SListenClient; // socket listening for client calls
|
|
|
|
|
|
|
|
cThread* pListenThread; bool bStopListenThread;
|
|
|
|
cThread* pTickThread; bool bStopTickThread;
|
|
|
|
|
|
|
|
cEvent RestartEvent;
|
|
|
|
std::string ServerID;
|
|
|
|
};
|
|
|
|
|
2012-02-01 17:38:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cServer * cServer::GetServer()
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
|
|
|
LOGWARN("WARNING: Using deprecated function cServer::GetServer() use cRoot::Get()->GetServer() instead!");
|
|
|
|
return cRoot::Get()->GetServer();
|
|
|
|
}
|
|
|
|
|
2012-02-01 17:38:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
void cServer::ServerListenThread( void *a_Args )
|
|
|
|
{
|
|
|
|
LOG("ServerListenThread");
|
|
|
|
cServer* self = (cServer*)a_Args;
|
|
|
|
sServerState* m_pState = self->m_pState;
|
|
|
|
while( !m_pState->bStopListenThread )
|
|
|
|
{
|
|
|
|
self->StartListenClient();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-01 17:38:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-02-08 05:02:46 -05:00
|
|
|
void cServer::ClientDestroying(const cClientHandle * a_Client)
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2012-02-25 18:48:28 -05:00
|
|
|
m_SocketThreads.StopReading(a_Client);
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
|
2012-02-01 17:38:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-02-25 19:36:51 -05:00
|
|
|
void cServer::NotifyClientWrite(const cClientHandle * a_Client)
|
|
|
|
{
|
|
|
|
m_NotifyWriteThread.NotifyClientWrite(a_Client);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cServer::WriteToClient(const cSocket * a_Socket, const AString & a_Data)
|
|
|
|
{
|
|
|
|
m_SocketThreads.Write(a_Socket, a_Data);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cServer::QueueClientClose(const cSocket * a_Socket)
|
|
|
|
{
|
|
|
|
m_SocketThreads.QueueClose(a_Socket);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
bool cServer::InitServer( int a_Port )
|
|
|
|
{
|
|
|
|
if( m_bIsConnected )
|
|
|
|
{
|
|
|
|
LOGERROR("ERROR: Trying to initialize server while server is already running!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("/============================\\\n");
|
2012-02-01 17:38:03 -05:00
|
|
|
printf("| Custom Minecraft Server |\n");
|
2011-10-03 14:41:19 -04:00
|
|
|
printf("| Created by Kevin Bansberg |\n");
|
|
|
|
printf("| A.K.A. |\n");
|
|
|
|
printf("| FakeTruth |\n");
|
|
|
|
printf("| Monsters by Alex Sonek |\n");
|
|
|
|
printf("| A.K.A. Duralex |\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.");
|
|
|
|
|
2011-10-22 20:18:44 -04:00
|
|
|
if( cSocket::WSAStartup() != 0 ) // Only does anything on Windows, but whatever
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2011-10-22 20:18:44 -04:00
|
|
|
LOGERROR("WSAStartup() != 0");
|
2011-10-03 14:41:19 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-10-22 20:18:44 -04:00
|
|
|
m_pState->SListenClient = cSocket::CreateSocket();
|
2011-10-03 14:41:19 -04:00
|
|
|
|
|
|
|
if( !m_pState->SListenClient.IsValid() )
|
|
|
|
{
|
2012-02-05 11:24:16 -05:00
|
|
|
LOGERROR("m_SListenClient==INVALID_SOCKET (%s)", cSocket::GetErrorString( cSocket::GetLastError() ).c_str() );
|
2011-10-22 20:18:44 -04:00
|
|
|
return false;
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
|
2012-02-08 05:02:46 -05:00
|
|
|
if( m_pState->SListenClient.SetReuseAddress() == -1 )
|
2011-10-22 20:18:44 -04:00
|
|
|
{
|
2012-02-08 05:02:46 -05:00
|
|
|
LOGERROR("setsockopt == -1");
|
|
|
|
return false;
|
|
|
|
}
|
2011-10-03 14:41:19 -04:00
|
|
|
|
2011-10-22 20:18:44 -04:00
|
|
|
cSocket::SockAddr_In local;
|
|
|
|
local.Family = cSocket::ADDRESS_FAMILY_INTERNET;
|
|
|
|
local.Address = cSocket::INTERNET_ADDRESS_ANY;
|
|
|
|
local.Port = (unsigned short)a_Port; // 25565
|
|
|
|
|
|
|
|
if( m_pState->SListenClient.Bind( local ) != 0 )
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2012-02-05 11:24:16 -05:00
|
|
|
LOGERROR("bind fail (%s)", cSocket::GetErrorString( cSocket::GetLastError() ).c_str() );
|
2011-10-03 14:41:19 -04:00
|
|
|
return false;
|
|
|
|
}
|
2011-10-22 20:18:44 -04:00
|
|
|
|
|
|
|
if( m_pState->SListenClient.Listen( 10 ) != 0)
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2012-02-05 11:24:16 -05:00
|
|
|
LOGERROR("listen fail (%s)", cSocket::GetErrorString( cSocket::GetLastError() ).c_str() );
|
2011-10-03 14:41:19 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_iServerPort = a_Port;
|
|
|
|
LOG("Port %i has been bound, server is open for connections", m_iServerPort);
|
|
|
|
m_bIsConnected = true;
|
|
|
|
|
|
|
|
cIniFile IniFile("settings.ini");
|
2012-02-23 17:51:03 -05:00
|
|
|
if (IniFile.ReadFile())
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
|
|
|
g_bWaterPhysics = IniFile.GetValueB("Physics", "Water", false );
|
2011-12-25 08:44:22 -05:00
|
|
|
|
|
|
|
m_pState->ServerID = "-";
|
|
|
|
if (IniFile.GetValueB("Authentication", "Authenticate"))
|
|
|
|
{
|
|
|
|
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();
|
2012-01-21 17:10:05 -05:00
|
|
|
ServerID.resize(16, '0');
|
2011-12-25 08:44:22 -05:00
|
|
|
m_pState->ServerID = ServerID;
|
|
|
|
}
|
2012-02-23 17:51:03 -05:00
|
|
|
|
|
|
|
m_ClientViewDistance = IniFile.GetValueI("Server", "DefaultViewDistance", -1);
|
|
|
|
if (m_ClientViewDistance == -1)
|
|
|
|
{
|
|
|
|
m_ClientViewDistance = cClientHandle::DEFAULT_VIEW_DISTANCE;
|
|
|
|
LOG("[Server].DefaultViewDistance not set, using a default of %d", m_ClientViewDistance);
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
m_ClientViewDistance = cClientHandle::MAX_VIEW_DISTANCE;
|
|
|
|
LOGINFO("Setting default viewdistance to the maximum of %d", m_ClientViewDistance);
|
|
|
|
}
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
2012-02-25 19:36:51 -05:00
|
|
|
|
|
|
|
m_NotifyWriteThread.Start(this);
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-02-01 17:38:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
cServer::cServer()
|
|
|
|
: m_pState( new sServerState )
|
|
|
|
, m_Millisecondsf( 0 )
|
|
|
|
, m_Milliseconds( 0 )
|
|
|
|
, m_bIsConnected( false )
|
|
|
|
, m_iServerPort( 0 )
|
|
|
|
, m_bRestarting( false )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-02-01 17:38:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
cServer::~cServer()
|
|
|
|
{
|
2011-10-22 20:18:44 -04:00
|
|
|
if( m_pState->SListenClient ) m_pState->SListenClient.CloseSocket();
|
2011-10-03 14:41:19 -04:00
|
|
|
m_pState->SListenClient = 0;
|
|
|
|
|
|
|
|
m_pState->bStopListenThread = true;
|
|
|
|
delete m_pState->pListenThread; m_pState->pListenThread = 0;
|
|
|
|
m_pState->bStopTickThread = true;
|
|
|
|
delete m_pState->pTickThread; m_pState->pTickThread = 0;
|
|
|
|
|
|
|
|
delete m_pState;
|
|
|
|
}
|
|
|
|
|
2012-02-01 17:38:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-30 20:52:20 -04:00
|
|
|
// TODO - Need to modify this or something, so it broadcasts to all worlds? And move this to cWorld?
|
2012-02-13 16:47:03 -05:00
|
|
|
void cServer::Broadcast( const cPacket * a_Packet, cClientHandle* a_Exclude /* = 0 */ )
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2012-02-25 19:36:51 -05:00
|
|
|
cCSLock Lock(m_CSClients);
|
|
|
|
for( ClientList::iterator itr = m_Clients.begin(); itr != m_Clients.end(); ++itr)
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2012-02-25 19:36:51 -05:00
|
|
|
if ((*itr == a_Exclude) || !(*itr)->IsLoggedIn())
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2011-10-03 14:41:19 -04:00
|
|
|
(*itr)->Send( a_Packet );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-01 17:38:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
void cServer::StartListenClient()
|
|
|
|
{
|
2011-10-22 20:18:44 -04:00
|
|
|
cSocket SClient = m_pState->SListenClient.Accept();
|
2011-10-03 14:41:19 -04:00
|
|
|
|
2012-02-02 02:47:19 -05:00
|
|
|
if (!SClient.IsValid())
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2012-02-02 02:47:19 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const AString & ClientIP = SClient.GetIPString();
|
|
|
|
if (ClientIP.empty())
|
|
|
|
{
|
|
|
|
LOGWARN("cServer: A client connected, but didn't present its IP, disconnecting.");
|
|
|
|
SClient.CloseSocket();
|
|
|
|
return;
|
|
|
|
}
|
2011-10-03 14:41:19 -04:00
|
|
|
|
2012-02-08 05:02:46 -05:00
|
|
|
LOG("Client \"%s\" connected!", ClientIP.c_str());
|
2011-10-03 14:41:19 -04:00
|
|
|
|
2012-02-23 17:51:03 -05:00
|
|
|
cClientHandle *NewHandle = new cClientHandle(SClient, m_ClientViewDistance);
|
2012-02-08 05:02:46 -05:00
|
|
|
if (!m_SocketThreads.AddClient(&(NewHandle->GetSocket()), NewHandle))
|
|
|
|
{
|
|
|
|
// For some reason SocketThreads have rejected the handle, clean it up
|
2012-02-13 16:47:03 -05:00
|
|
|
LOGERROR("Client \"%s\" cannot be handled, server probably unstable", SClient.GetIPString().c_str());
|
2012-02-08 05:02:46 -05:00
|
|
|
SClient.CloseSocket();
|
|
|
|
delete NewHandle;
|
|
|
|
return;
|
|
|
|
}
|
2012-02-25 19:36:51 -05:00
|
|
|
|
|
|
|
cCSLock Lock(m_CSClients);
|
|
|
|
m_Clients.push_back( NewHandle );
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
|
2012-02-01 17:38:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
bool cServer::Tick(float a_Dt)
|
|
|
|
{
|
2012-01-22 15:15:11 -05:00
|
|
|
//LOG("1. Tick %0.2f", a_Dt);
|
2011-10-03 14:41:19 -04:00
|
|
|
if( a_Dt > 100.f ) a_Dt = 100.f; // Don't go over 1/10 second
|
|
|
|
|
|
|
|
m_Millisecondsf += a_Dt;
|
|
|
|
if( m_Millisecondsf > 1.f )
|
|
|
|
{
|
|
|
|
m_Milliseconds += (int)m_Millisecondsf;
|
|
|
|
m_Millisecondsf = m_Millisecondsf - (int)m_Millisecondsf;
|
|
|
|
}
|
|
|
|
|
2011-11-01 17:57:08 -04:00
|
|
|
cRoot::Get()->TickWorlds( a_Dt ); // TODO - Maybe give all worlds their own thread?
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2012-02-25 19:36:51 -05:00
|
|
|
cCSLock Lock(m_CSClients);
|
|
|
|
for (cClientHandleList::iterator itr = m_Clients.begin(); itr != m_Clients.end();)
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2012-02-25 19:36:51 -05:00
|
|
|
if ((*itr)->IsDestroyed())
|
|
|
|
{
|
|
|
|
cClientHandle* RemoveMe = *itr;
|
|
|
|
itr = m_Clients.erase(itr);
|
|
|
|
delete RemoveMe;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
(*itr)->Tick(a_Dt);
|
2011-10-03 14:41:19 -04:00
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cRoot::Get()->GetPluginManager()->Tick( a_Dt );
|
|
|
|
|
|
|
|
if( !m_bRestarting )
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_bRestarting = false;
|
|
|
|
m_pState->RestartEvent.Set();
|
|
|
|
LOG("<<<<>>>>SIGNALLED SEMAPHORE");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-01 17:38:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
void ServerTickThread( void * a_Param )
|
|
|
|
{
|
|
|
|
LOG("ServerTickThread");
|
|
|
|
cServer *CServerObj = (cServer*)a_Param;
|
|
|
|
|
|
|
|
cTimer Timer;
|
|
|
|
|
2012-01-22 15:15:11 -05:00
|
|
|
long long msPerTick = 50; // TODO - Put this in server config file
|
2011-10-03 14:41:19 -04:00
|
|
|
long long LastTime = Timer.GetNowTime();
|
|
|
|
|
|
|
|
bool bKeepGoing = true;
|
|
|
|
while( bKeepGoing )
|
|
|
|
{
|
|
|
|
long long NowTime = Timer.GetNowTime();
|
|
|
|
float DeltaTime = (float)(NowTime-LastTime);
|
|
|
|
bKeepGoing = CServerObj->Tick( DeltaTime );
|
2012-01-22 15:15:11 -05:00
|
|
|
long long TickTime = Timer.GetNowTime() - NowTime;
|
|
|
|
|
|
|
|
if( TickTime < msPerTick ) // Stretch tick time until it's at least msPerTick
|
|
|
|
{
|
|
|
|
cSleep::MilliSleep( (unsigned int)( msPerTick - TickTime ) );
|
|
|
|
}
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
LastTime = NowTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG("TICK THREAD STOPPED");
|
|
|
|
}
|
|
|
|
|
2012-02-01 04:12:54 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
void cServer::StartListenThread()
|
|
|
|
{
|
2011-12-23 09:26:29 -05:00
|
|
|
m_pState->pListenThread = new cThread( ServerListenThread, this, "cServer::ServerListenThread" );
|
|
|
|
m_pState->pTickThread = new cThread( ServerTickThread, this, "cServer::ServerTickThread" );
|
2011-10-03 14:41:19 -04:00
|
|
|
m_pState->pListenThread->Start( true );
|
|
|
|
m_pState->pTickThread->Start( true );
|
|
|
|
}
|
|
|
|
|
2012-02-01 04:12:54 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
|
|
|
|
template <class T>
|
2012-02-01 04:12:54 -05:00
|
|
|
bool from_string(
|
|
|
|
T& t,
|
|
|
|
const std::string& s,
|
|
|
|
std::ios_base& (*f)(std::ios_base&)
|
|
|
|
)
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
|
|
|
std::istringstream iss(s);
|
|
|
|
return !(iss >> f >> t).fail();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-02-01 04:12:54 -05:00
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
|
|
|
|
bool cServer::Command( cClientHandle & a_Client, const char* a_Cmd )
|
|
|
|
{
|
2012-02-23 17:51:03 -05:00
|
|
|
cPluginManager* PM = cRoot::Get()->GetPluginManager();
|
2011-10-03 14:41:19 -04:00
|
|
|
if( PM->CallHook( cPluginManager::E_PLUGIN_CHAT, 2, a_Cmd, a_Client.GetPlayer() ) )
|
2012-02-23 17:51:03 -05:00
|
|
|
{
|
2011-10-03 14:41:19 -04:00
|
|
|
return true;
|
2012-02-23 17:51:03 -05:00
|
|
|
}
|
2011-10-03 14:41:19 -04:00
|
|
|
|
|
|
|
std::string Command( a_Cmd );
|
|
|
|
if( Command.length() <= 0 ) return false;
|
|
|
|
if( Command[0] != '/' ) return false;
|
|
|
|
|
2012-02-01 04:12:54 -05:00
|
|
|
AStringVector split = StringSplit( Command, " " );
|
2011-10-03 14:41:19 -04:00
|
|
|
if( split.size() == 0 )
|
|
|
|
return false;
|
|
|
|
|
2012-02-01 04:12:54 -05:00
|
|
|
if (split[0].compare("/coords") == 0)
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2012-02-01 17:38:03 -05:00
|
|
|
AString Pos;
|
|
|
|
Printf(Pos, "[X:%0.2f] [Y:%0.2f] [Z:%0.2f]", a_Client.GetPlayer()->GetPosX(), a_Client.GetPlayer()->GetPosY(), a_Client.GetPlayer()->GetPosZ() );
|
|
|
|
a_Client.Send( cPacket_Chat(cChatColor::Green + Pos));
|
2011-10-03 14:41:19 -04:00
|
|
|
return true;
|
|
|
|
}
|
2012-02-23 17:51:03 -05:00
|
|
|
|
|
|
|
if (split[0].compare("/viewdistance") == 0)
|
|
|
|
{
|
|
|
|
if (split.size() != 2)
|
|
|
|
{
|
|
|
|
a_Client.Send(cPacket_Chat(cChatColor::Green + "Invalid syntax, expected 1 parameter, the numebr of chunks to stream"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
int dist = atol(split[1].c_str());
|
|
|
|
a_Client.SetViewDistance(dist);
|
|
|
|
return true;
|
|
|
|
}
|
2011-10-03 14:41:19 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-02-01 17:38:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-02-13 16:47:03 -05:00
|
|
|
void cServer::ServerCommand( const char * a_Cmd )
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2012-02-01 04:12:54 -05:00
|
|
|
AString Command( a_Cmd );
|
|
|
|
AStringVector split = StringSplit( Command, " " );
|
2011-10-03 14:41:19 -04:00
|
|
|
if( split.size() > 0 )
|
|
|
|
{
|
|
|
|
if( split[0].compare( "help" ) == 0 )
|
|
|
|
{
|
2012-02-08 05:02:46 -05:00
|
|
|
printf("================== ALL COMMANDS ===================\n");
|
2011-10-03 14:41:19 -04:00
|
|
|
printf("help - Shows this message\n");
|
|
|
|
printf("save-all - Saves all loaded chunks to disk\n");
|
|
|
|
printf("list - Lists all players currently in server\n");
|
2012-02-16 14:32:31 -05:00
|
|
|
printf("unload - Unloads all unused chunks\n");
|
2011-10-03 14:41:19 -04:00
|
|
|
printf("numchunks - Shows number of chunks currently loaded\n");
|
|
|
|
printf("say - Sends a chat message to all players\n");
|
|
|
|
printf("restart - Kicks all clients, and saves everything\n");
|
|
|
|
printf(" and clears memory\n");
|
|
|
|
printf("stop - Saves everything and closes server\n");
|
|
|
|
printf("===================================================\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if( split[0].compare( "stop" ) == 0 || split[0].compare( "restart" ) == 0 )
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if( split[0].compare( "save-all" ) == 0 )
|
|
|
|
{
|
2011-10-30 20:52:20 -04:00
|
|
|
cRoot::Get()->GetWorld()->SaveAllChunks(); // TODO - Force ALL worlds to save their chunks
|
2011-10-03 14:41:19 -04:00
|
|
|
return;
|
|
|
|
}
|
2012-02-16 14:32:31 -05:00
|
|
|
if (split[0].compare("unload") == 0)
|
|
|
|
{
|
|
|
|
LOG("Num loaded chunks before: %i", cRoot::Get()->GetTotalChunkCount() );
|
|
|
|
cRoot::Get()->GetDefaultWorld()->UnloadUnusedChunks(); // TODO: Iterate through ALL worlds
|
|
|
|
LOG("Num loaded chunks after: %i", cRoot::Get()->GetTotalChunkCount() );
|
|
|
|
return;
|
|
|
|
}
|
2011-10-03 14:41:19 -04:00
|
|
|
if( split[0].compare( "list" ) == 0 )
|
|
|
|
{
|
2012-02-13 16:47:03 -05:00
|
|
|
class cPlayerLogger : public cPlayerListCallback
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2012-02-13 16:47:03 -05:00
|
|
|
virtual bool Item(cPlayer * a_Player) override
|
|
|
|
{
|
2012-02-16 04:35:33 -05:00
|
|
|
LOG("\t%s @ %s", a_Player->GetName().c_str(), a_Player->GetClientHandle()->GetSocket().GetIPString().c_str());
|
2012-02-13 16:47:03 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} Logger;
|
2012-02-16 17:28:19 -05:00
|
|
|
cRoot::Get()->GetWorld()->ForEachPlayer(Logger);
|
2011-10-03 14:41:19 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if( split[0].compare( "numchunks" ) == 0 )
|
|
|
|
{
|
2012-02-17 06:40:14 -05:00
|
|
|
LOG("Num loaded chunks: %i", cRoot::Get()->GetTotalChunkCount() );
|
2011-10-03 14:41:19 -04:00
|
|
|
return;
|
|
|
|
}
|
2012-02-13 16:47:03 -05:00
|
|
|
|
|
|
|
if(split[0].compare("monsters") == 0 )
|
|
|
|
{
|
|
|
|
// TODO: cWorld::ListMonsters();
|
2011-10-03 14:41:19 -04:00
|
|
|
return;
|
|
|
|
}
|
2012-02-13 16:47:03 -05:00
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
if(split.size() > 1)
|
|
|
|
{
|
|
|
|
if( split[0].compare( "say" ) == 0 )
|
|
|
|
{
|
|
|
|
std::string Message = cChatColor::Purple + "[SERVER] " + Command.substr( Command.find_first_of("say") + 4 );
|
|
|
|
LOG("%s", Message.c_str() );
|
|
|
|
Broadcast( cPacket_Chat(Message) );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("Unknown command, type 'help' for all commands.\n");
|
|
|
|
}
|
|
|
|
//LOG("You didn't enter anything? (%s)", a_Cmd.c_str() );
|
|
|
|
}
|
|
|
|
|
2012-02-01 17:38:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
void cServer::SendMessage( const char* a_Message, cPlayer* a_Player /* = 0 */, bool a_bExclude /* = false */ )
|
|
|
|
{
|
|
|
|
cPacket_Chat Chat( a_Message );
|
|
|
|
if( a_Player && !a_bExclude )
|
|
|
|
{
|
|
|
|
cClientHandle* Client = a_Player->GetClientHandle();
|
|
|
|
if( Client ) Client->Send( Chat );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Broadcast( Chat, (a_Player)?a_Player->GetClientHandle():0 );
|
|
|
|
}
|
|
|
|
|
2012-02-01 17:38:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
void cServer::Shutdown()
|
|
|
|
{
|
|
|
|
m_bRestarting = true;
|
|
|
|
m_pState->RestartEvent.Wait();
|
|
|
|
|
|
|
|
cRoot::Get()->GetWorld()->SaveAllChunks();
|
|
|
|
|
2012-02-25 19:36:51 -05:00
|
|
|
cCSLock Lock(m_CSClients);
|
|
|
|
for( ClientList::iterator itr = m_Clients.begin(); itr != m_Clients.end(); ++itr )
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2011-11-01 17:57:08 -04:00
|
|
|
delete *itr;
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
2012-02-25 19:36:51 -05:00
|
|
|
m_Clients.clear();
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-01 17:38:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const AString & cServer::GetServerID(void) const
|
|
|
|
{
|
|
|
|
return m_pState->ServerID;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cServer::KickUser(const AString & iUserName, const AString & iReason)
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2012-02-25 19:36:51 -05:00
|
|
|
cCSLock Lock(m_CSClients);
|
|
|
|
for (ClientList::iterator itr = m_Clients.begin(); itr != m_Clients.end(); ++itr)
|
2012-02-01 17:38:03 -05:00
|
|
|
{
|
|
|
|
if ((*itr)->GetUsername() == iUserName)
|
|
|
|
{
|
|
|
|
(*itr)->Kick(iReason);
|
|
|
|
}
|
2012-02-25 19:36:51 -05:00
|
|
|
} // for itr - m_Clients[]
|
2012-02-01 17:38:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cServer::AuthenticateUser(const AString & iUserName)
|
|
|
|
{
|
2012-02-25 19:36:51 -05:00
|
|
|
cCSLock Lock(m_CSClients);
|
|
|
|
for (ClientList::iterator itr = m_Clients.begin(); itr != m_Clients.end(); ++itr)
|
2012-02-01 17:38:03 -05:00
|
|
|
{
|
|
|
|
if ((*itr)->GetUsername() == iUserName)
|
|
|
|
{
|
|
|
|
(*itr)->Authenticate();
|
|
|
|
}
|
2012-02-25 19:36:51 -05:00
|
|
|
} // for itr - m_Clients[]
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// cServer::cClientPacketThread:
|
|
|
|
|
|
|
|
cServer::cNotifyWriteThread::cNotifyWriteThread(void) :
|
|
|
|
super("ClientPacketThread"),
|
|
|
|
m_Server(NULL)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cServer::cNotifyWriteThread::~cNotifyWriteThread()
|
|
|
|
{
|
|
|
|
mShouldTerminate = 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 (!mShouldTerminate)
|
|
|
|
{
|
|
|
|
cCSLock Lock(m_CS);
|
|
|
|
while (m_Clients.size() == 0)
|
|
|
|
{
|
|
|
|
cCSUnlock Unlock(Lock);
|
|
|
|
m_Event.Wait();
|
|
|
|
if (mShouldTerminate)
|
|
|
|
{
|
|
|
|
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();
|
2012-02-01 17:38:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|