mirror of
https://github.com/SapphireServer/Sapphire.git
synced 2025-05-07 11:17:46 +00:00
Cleanup of the server mainloop
This commit is contained in:
parent
1472041262
commit
775b1e5641
2 changed files with 62 additions and 78 deletions
|
@ -293,7 +293,7 @@ void Sapphire::World::WorldServer::mainLoop()
|
||||||
{
|
{
|
||||||
auto& terriMgr = Common::Service< TerritoryMgr >::ref();
|
auto& terriMgr = Common::Service< TerritoryMgr >::ref();
|
||||||
auto& scriptMgr = Common::Service< Scripting::ScriptMgr >::ref();
|
auto& scriptMgr = Common::Service< Scripting::ScriptMgr >::ref();
|
||||||
auto& db = Common::Service< Db::DbWorkerPool< Db::ZoneDbConnection > >::ref();
|
|
||||||
auto& contentFinder = Common::Service< ContentFinder >::ref();
|
auto& contentFinder = Common::Service< ContentFinder >::ref();
|
||||||
|
|
||||||
while( isRunning() )
|
while( isRunning() )
|
||||||
|
@ -309,93 +309,80 @@ void Sapphire::World::WorldServer::mainLoop()
|
||||||
|
|
||||||
contentFinder.update();
|
contentFinder.update();
|
||||||
|
|
||||||
std::lock_guard< std::mutex > lock( m_sessionMutex );
|
updateSessions( currTime );
|
||||||
for( auto sessionIt : m_sessionMapById )
|
|
||||||
{
|
|
||||||
auto session = sessionIt.second;
|
|
||||||
if( session && session->getPlayer() )
|
|
||||||
{
|
|
||||||
|
|
||||||
// if the player is in a zone, let the zone handler take care of his updates
|
|
||||||
// else do it here.
|
|
||||||
if( !session->getPlayer()->isConnected() )
|
|
||||||
session->update();
|
|
||||||
|
|
||||||
|
DbKeepAlive( currTime );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Sapphire::World::WorldServer::DbKeepAlive( uint32_t currTime )
|
||||||
|
{
|
||||||
|
auto& db = Common::Service< Db::DbWorkerPool< Db::ZoneDbConnection > >::ref();
|
||||||
if( currTime - m_lastDBPingTime > 3 )
|
if( currTime - m_lastDBPingTime > 3 )
|
||||||
{
|
{
|
||||||
db.keepAlive();
|
db.keepAlive();
|
||||||
m_lastDBPingTime = currTime;
|
m_lastDBPingTime = currTime;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
auto it = m_sessionMapById.begin();
|
void Sapphire::World::WorldServer::updateSessions( uint32_t currTime )
|
||||||
for( ; it != m_sessionMapById.end(); )
|
|
||||||
{
|
{
|
||||||
auto diff = std::difftime( currTime, it->second->getLastDataTime() );
|
std::queue< uint32_t > sessionRemovalQueue;
|
||||||
|
std::lock_guard< std::mutex > lock( m_sessionMutex );
|
||||||
|
for( const auto& [ id, session ] : m_sessionMapById )
|
||||||
|
{
|
||||||
|
if( !session || !session->getPlayer() )
|
||||||
|
continue;
|
||||||
|
|
||||||
auto pPlayer = it->second->getPlayer();
|
// if the player is in a zone, let the zone handler take care of his updates, else do it here.
|
||||||
|
if( !session->getPlayer()->isConnected() )
|
||||||
|
session->update();
|
||||||
|
|
||||||
|
auto diff = difftime( currTime, session->getLastDataTime() );
|
||||||
|
auto& player = *session->getPlayer();
|
||||||
|
|
||||||
// remove session of players marked for removel ( logoff / kick )
|
// remove session of players marked for removel ( logoff / kick )
|
||||||
if( pPlayer->isMarkedForRemoval() && diff > 5 )
|
if( ( player.isMarkedForRemoval() && diff > 5 ) || diff > 20 )
|
||||||
{
|
{
|
||||||
it->second->close();
|
Logger::info( "[{0}] Session removal", session->getId() );
|
||||||
// if( it->second.unique() )
|
session->close();
|
||||||
{
|
sessionRemovalQueue.push( session->getId() );
|
||||||
Logger::info( "[{0}] Session removal", it->second->getId() );
|
|
||||||
it = m_sessionMapById.erase( it );
|
|
||||||
removeSession( pPlayer->getCharacterId() );
|
|
||||||
removeSession( pPlayer->getName() );
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// remove sessions that simply timed out
|
while( !sessionRemovalQueue.empty() )
|
||||||
if( diff > 20 )
|
|
||||||
{
|
{
|
||||||
Logger::info( "[{0}] Session time out", it->second->getId() );
|
auto removalId = sessionRemovalQueue.front();
|
||||||
|
sessionRemovalQueue.pop();
|
||||||
it->second->close();
|
auto session = getSession( removalId );
|
||||||
// if( it->second.unique() )
|
if( session )
|
||||||
{
|
{
|
||||||
it = m_sessionMapById.erase( it );
|
m_sessionMapById.erase( removalId );
|
||||||
removeSession( pPlayer->getCharacterId() );
|
removeSession( *session->getPlayer() );
|
||||||
removeSession( pPlayer->getName() );
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
++it;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Sapphire::World::WorldServer::createSession( uint32_t sessionId )
|
bool Sapphire::World::WorldServer::createSession( uint32_t sessionId )
|
||||||
{
|
{
|
||||||
std::lock_guard< std::mutex > lock( m_sessionMutex );
|
std::lock_guard< std::mutex > lock( m_sessionMutex );
|
||||||
|
|
||||||
const auto session_id_str = std::to_string( sessionId );
|
const auto sessionIdStr = std::to_string( sessionId );
|
||||||
|
|
||||||
auto it = m_sessionMapById.find( sessionId );
|
auto pSession = getSession( sessionId );
|
||||||
|
|
||||||
if( it != m_sessionMapById.end() )
|
if( pSession )
|
||||||
{
|
{
|
||||||
Logger::error( "[{0}] Error creating session", session_id_str );
|
Logger::error( "[{0}] Error creating session, already in list", sessionIdStr );
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Logger::info( "[{0}] Creating new session", session_id_str );
|
Logger::info( "[{0}] Creating new session", sessionIdStr );
|
||||||
|
|
||||||
std::shared_ptr< Session > newSession( new Session( sessionId ) );
|
|
||||||
|
|
||||||
|
|
||||||
|
auto newSession = std::make_shared< Session >( sessionId );
|
||||||
if( !newSession->loadPlayer() )
|
if( !newSession->loadPlayer() )
|
||||||
{
|
{
|
||||||
Logger::error( "[{0}] Error loading player {0}", session_id_str );
|
Logger::error( "[{0}] Error loading player {0}", sessionIdStr );
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -404,7 +391,6 @@ bool Sapphire::World::WorldServer::createSession( uint32_t sessionId )
|
||||||
m_sessionMapByCharacterId[ newSession->getPlayer()->getCharacterId() ] = newSession;
|
m_sessionMapByCharacterId[ newSession->getPlayer()->getCharacterId() ] = newSession;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Sapphire::World::WorldServer::removeSession( uint32_t sessionId )
|
void Sapphire::World::WorldServer::removeSession( uint32_t sessionId )
|
||||||
|
@ -453,14 +439,10 @@ Sapphire::World::SessionPtr Sapphire::World::WorldServer::getSession( const std:
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Sapphire::World::WorldServer::removeSession( const std::string& playerName )
|
void Sapphire::World::WorldServer::removeSession( const Entity::Player& player )
|
||||||
{
|
{
|
||||||
m_sessionMapByName.erase( playerName );
|
m_sessionMapByName.erase( player.getName() );
|
||||||
}
|
m_sessionMapByCharacterId.erase( player.getCharacterId() );
|
||||||
|
|
||||||
void Sapphire::World::WorldServer::removeSession( uint64_t characterId )
|
|
||||||
{
|
|
||||||
m_sessionMapByCharacterId.erase( characterId );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Sapphire::World::WorldServer::isRunning() const
|
bool Sapphire::World::WorldServer::isRunning() const
|
||||||
|
|
|
@ -24,8 +24,7 @@ namespace Sapphire::World
|
||||||
bool createSession( uint32_t sessionId );
|
bool createSession( uint32_t sessionId );
|
||||||
|
|
||||||
void removeSession( uint32_t sessionId );
|
void removeSession( uint32_t sessionId );
|
||||||
void removeSession( uint64_t characterId );
|
void removeSession( const Entity::Player& player );
|
||||||
void removeSession( const std::string& playerName );
|
|
||||||
|
|
||||||
World::SessionPtr getSession( uint32_t id );
|
World::SessionPtr getSession( uint32_t id );
|
||||||
World::SessionPtr getSession( uint64_t characterId );
|
World::SessionPtr getSession( uint64_t characterId );
|
||||||
|
@ -94,6 +93,9 @@ namespace Sapphire::World
|
||||||
public:
|
public:
|
||||||
std::map< int32_t, BNPCMap >& getBNpcTeriMap();
|
std::map< int32_t, BNPCMap >& getBNpcTeriMap();
|
||||||
|
|
||||||
|
void updateSessions( uint32_t currTime );
|
||||||
|
|
||||||
|
void DbKeepAlive( uint32_t currTime );
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue