2010-08-11 15:23:10 +02:00
|
|
|
// ***************************************************************************
|
|
|
|
|
//
|
|
|
|
|
// Reality - The Matrix Online Server Emulator
|
2009-08-11 12:17:38 +00:00
|
|
|
// Copyright (C) 2006-2010 Rajko Stojadinovic
|
2010-08-11 15:23:10 +02:00
|
|
|
// http://mxoemu.info
|
2009-08-11 12:17:38 +00:00
|
|
|
//
|
2010-08-11 15:23:10 +02:00
|
|
|
// ---------------------------------------------------------------------------
|
2009-08-11 12:17:38 +00:00
|
|
|
//
|
2010-08-11 15:23:10 +02:00
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
|
// it under the terms of the GNU Affero General Public License as
|
|
|
|
|
// published by the Free Software Foundation, either version 3 of the
|
|
|
|
|
// License, or (at your option) any later version.
|
2009-08-11 12:17:38 +00:00
|
|
|
//
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2010-08-11 15:23:10 +02:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
|
//
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2009-08-11 12:17:38 +00:00
|
|
|
//
|
2010-08-11 15:23:10 +02:00
|
|
|
// ---------------------------------------------------------------------------
|
2009-08-11 12:17:38 +00:00
|
|
|
//
|
2010-08-11 15:23:10 +02:00
|
|
|
// ***************************************************************************
|
2009-08-11 12:17:38 +00:00
|
|
|
|
|
|
|
|
#include "Common.h"
|
|
|
|
|
#include "GameServer.h"
|
2010-02-13 14:32:04 +00:00
|
|
|
#include "GameClient.h"
|
|
|
|
|
#include "MarginServer.h"
|
2009-08-11 12:17:38 +00:00
|
|
|
#include "Log.h"
|
|
|
|
|
#include "Timer.h"
|
|
|
|
|
#include "Config.h"
|
2010-02-15 18:11:08 +00:00
|
|
|
#include "GameSocket.h"
|
2010-08-23 17:14:15 +02:00
|
|
|
#include "Database/DatabaseEnv.h"
|
2010-02-15 18:11:08 +00:00
|
|
|
#include <Sockets/Ipv4Address.h>
|
2009-08-11 12:17:38 +00:00
|
|
|
|
|
|
|
|
initialiseSingleton( GameServer );
|
|
|
|
|
|
|
|
|
|
bool GameServer::Start()
|
|
|
|
|
{
|
2010-09-01 06:57:33 +02:00
|
|
|
m_simtimeStart = getFloatTime();
|
|
|
|
|
m_simtimeOffset = 0;
|
|
|
|
|
|
2010-08-31 03:00:04 +02:00
|
|
|
string Interface = sConfig.GetStringDefault("GameServer.IP", "0.0.0.0");
|
2010-02-15 18:11:08 +00:00
|
|
|
int Port = sConfig.GetIntDefault("GameServer.Port", 10000);
|
2010-02-13 14:32:04 +00:00
|
|
|
INFO_LOG(format("Starting Game server on port %1%") % Port);
|
2009-08-11 12:17:38 +00:00
|
|
|
|
2010-02-15 18:11:08 +00:00
|
|
|
m_mainSocket.reset(new GameSocket(m_udpHandler));
|
|
|
|
|
port_t thePortToBind = Port;
|
2010-08-31 03:00:04 +02:00
|
|
|
if (m_mainSocket->Bind(Interface,thePortToBind) != 0)
|
2009-08-11 12:17:38 +00:00
|
|
|
{
|
2010-02-15 18:11:08 +00:00
|
|
|
ERROR_LOG(format("Error binding Game Server to port %1%") % thePortToBind);
|
2009-08-11 12:17:38 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
2010-02-15 18:11:08 +00:00
|
|
|
m_udpHandler.Add(m_mainSocket.get());
|
2010-08-31 03:00:04 +02:00
|
|
|
m_serverStartMS = getMSTime();
|
2010-08-23 17:14:15 +02:00
|
|
|
|
|
|
|
|
// Mark server as "up"
|
|
|
|
|
{
|
|
|
|
|
sDatabase.WaitExecute(format("UPDATE `worlds` SET `status`='1' WHERE `name`='%1%' LIMIT 1")
|
|
|
|
|
% this->GetName() );
|
|
|
|
|
m_serverUp=true;
|
|
|
|
|
}
|
|
|
|
|
|
2009-08-11 12:17:38 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void GameServer::Stop()
|
|
|
|
|
{
|
2010-02-15 18:11:08 +00:00
|
|
|
m_mainSocket.reset();
|
2010-08-23 17:14:15 +02:00
|
|
|
// Mark server as "down"
|
|
|
|
|
{
|
|
|
|
|
sDatabase.WaitExecute(format("UPDATE `worlds` SET `status`='0' WHERE `name`='%1%' LIMIT 1")
|
|
|
|
|
% this->GetName() );
|
|
|
|
|
m_serverUp = false;
|
|
|
|
|
}
|
2009-08-11 12:17:38 +00:00
|
|
|
INFO_LOG("Game Server shutdown");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void GameServer::Loop(void)
|
|
|
|
|
{
|
2010-02-15 18:11:08 +00:00
|
|
|
if (m_mainSocket == NULL)
|
|
|
|
|
return;
|
2009-08-11 12:17:38 +00:00
|
|
|
|
2010-02-15 18:11:08 +00:00
|
|
|
m_mainSocket->PruneDeadClients();
|
|
|
|
|
m_mainSocket->CheckAndResend();
|
|
|
|
|
m_udpHandler.Select(0,4000); //4ms
|
2009-08-11 12:17:38 +00:00
|
|
|
}
|
|
|
|
|
|
2010-09-01 06:57:33 +02:00
|
|
|
GameClient* GameServer::GetClientWithSessionId( uint32 sessionId )
|
2010-08-11 15:23:10 +02:00
|
|
|
{
|
|
|
|
|
return m_mainSocket->GetClientWithSessionId(sessionId);
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-01 06:57:33 +02:00
|
|
|
vector<GameClient*> GameServer::GetClientsWithCharacterId( uint64 charId )
|
|
|
|
|
{
|
|
|
|
|
return m_mainSocket->GetClientsWithCharacterId(charId);
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-10 14:05:42 +02:00
|
|
|
void GameServer::Broadcast( const ByteBuffer &message, bool command )
|
2009-08-13 10:58:38 +00:00
|
|
|
{
|
2010-02-15 18:11:08 +00:00
|
|
|
if (m_mainSocket != NULL)
|
2009-08-13 10:58:38 +00:00
|
|
|
{
|
2010-10-10 14:05:42 +02:00
|
|
|
m_mainSocket->Broadcast(message, command);
|
2010-02-13 14:32:04 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-15 18:11:08 +00:00
|
|
|
void GameServer::AnnounceStateUpdate( GameClient* clFrom,msgBaseClassPtr theMsg, bool immediateOnly )
|
2010-02-13 14:32:04 +00:00
|
|
|
{
|
2010-02-15 18:11:08 +00:00
|
|
|
if (m_mainSocket != NULL)
|
2010-02-13 14:32:04 +00:00
|
|
|
{
|
2010-02-15 18:11:08 +00:00
|
|
|
m_mainSocket->AnnounceStateUpdate(clFrom,theMsg,immediateOnly);
|
2010-02-13 14:32:04 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-15 18:11:08 +00:00
|
|
|
void GameServer::AnnounceCommand( GameClient* clFrom,msgBaseClassPtr theCmd )
|
2010-02-13 14:32:04 +00:00
|
|
|
{
|
2010-02-15 18:11:08 +00:00
|
|
|
if (m_mainSocket != NULL)
|
2010-02-13 14:32:04 +00:00
|
|
|
{
|
2010-02-15 18:11:08 +00:00
|
|
|
m_mainSocket->AnnounceCommand(clFrom,theCmd);
|
2009-08-13 10:58:38 +00:00
|
|
|
}
|
2010-08-23 17:14:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string GameServer::GetName() const
|
|
|
|
|
{
|
|
|
|
|
return sConfig.GetStringDefault("GameServer.WorldName", "Reality");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string GameServer::GetChatPrefix() const
|
|
|
|
|
{
|
|
|
|
|
return sConfig.GetStringDefault("GameServer.ChatPrefix", "SOE+MXO") + string("+") + GetName();
|
2009-08-13 10:58:38 +00:00
|
|
|
}
|