1
Fork 0
mirror of https://github.com/SapphireServer/Sapphire.git synced 2025-04-30 16:17:46 +00:00

refactoring of server zone mainloop

This commit is contained in:
Mordred 2017-10-01 17:58:11 +02:00
parent 303f8090d0
commit 598d038aea
3 changed files with 41 additions and 21 deletions

View file

@ -638,9 +638,9 @@ namespace Core {
StatusAffliction1, StatusAffliction1,
Occupied, Occupied,
Occupied1, Occupied1,
Occupied2, Occupied2,
Occupied3, Occupied3,
BoundByDuty, BoundByDuty,
Occupied4, Occupied4,
DuelingArea, DuelingArea,
@ -651,6 +651,7 @@ namespace Core {
PreparingToCraft, PreparingToCraft,
Gathering, Gathering,
Fishing, Fishing,
BeingRaised, BeingRaised,
BetweenAreas, BetweenAreas,
Stealthed, Stealthed,
@ -661,9 +662,9 @@ namespace Core {
BetweenAreas1, BetweenAreas1,
SystemError, SystemError,
LoggingOut, LoggingOut,
InvalidLocation, InvalidLocation,
WaitingForDuty, WaitingForDuty,
BoundByDuty1, BoundByDuty1,
Mounting, Mounting,
WatchingCutscene, WatchingCutscene,
@ -681,9 +682,9 @@ namespace Core {
FreeTrail, FreeTrail,
BeingMoved, BeingMoved,
Mounting2, Mounting2,
StatusAffliction3, StatusAffliction3,
StatusAffliction4, StatusAffliction4,
RegisteringRaceOrMatch, RegisteringRaceOrMatch,
WaitingForRaceOrMatch, WaitingForRaceOrMatch,
WaitingForTripleTriadMatch, WaitingForTripleTriadMatch,

View file

@ -41,7 +41,8 @@ Core::LinkshellMgr g_linkshellMgr;
Core::ServerZone::ServerZone( const std::string& configPath, uint16_t serverId ) Core::ServerZone::ServerZone( const std::string& configPath, uint16_t serverId )
: m_configPath( configPath ) : m_configPath( configPath ),
m_bRunning( true )
{ {
m_pConfig = XMLConfigPtr( new XMLConfig ); m_pConfig = XMLConfigPtr( new XMLConfig );
} }
@ -224,18 +225,32 @@ void Core::ServerZone::run( int32_t argc, char* argv[] )
g_zoneMgr.createZones(); g_zoneMgr.createZones();
std::vector< std::thread > thread_list; std::vector< std::thread > thread_list;
thread_list.push_back( std::thread( std::bind( &Network::Hive::Run, hive.get() ) ) ); thread_list.emplace_back( std::thread( std::bind( &Network::Hive::Run, hive.get() ) ) );
g_log.info( "Server listening on port: " + std::to_string( m_port ) ); g_log.info( "Server listening on port: " + std::to_string( m_port ) );
g_log.info( "Ready for connections..." ); g_log.info( "Ready for connections..." );
while( true ) mainLoop();
for( auto& thread_entry : thread_list )
{ {
std::this_thread::sleep_for( std::chrono::milliseconds( 50 ) ); thread_entry.join();
}
}
void Core::ServerZone::mainLoop()
{
while( isRunning() )
{
this_thread::sleep_for( chrono::milliseconds( 50 ) );
g_zoneMgr.updateZones(); g_zoneMgr.updateZones();
std::lock_guard<std::mutex> lock( m_sessionMutex );
for( auto sessionIt : m_sessionMap ) auto currTime = static_cast< uint32_t >( time( nullptr ) );
lock_guard< std::mutex > lock( this->m_sessionMutex );
for( auto sessionIt : this->m_sessionMap )
{ {
auto session = sessionIt.second; auto session = sessionIt.second;
if( session && session->getPlayer() ) if( session && session->getPlayer() )
@ -247,9 +262,9 @@ void Core::ServerZone::run( int32_t argc, char* argv[] )
} }
} }
uint32_t currTime = static_cast< uint32_t >( time( nullptr ) );
auto it = m_sessionMap.begin(); auto it = this->m_sessionMap.begin();
for( ; it != m_sessionMap.end(); ) for( ; it != this->m_sessionMap.end(); )
{ {
uint32_t diff = currTime - it->second->getLastDataTime(); uint32_t diff = currTime - it->second->getLastDataTime();
@ -257,11 +272,11 @@ void Core::ServerZone::run( int32_t argc, char* argv[] )
if( diff > 20 ) if( diff > 20 )
{ {
g_log.info( "[" + std::to_string( it->second->getId() ) + "] Session time out" ); g_log.info("[" + std::to_string(it->second->getId() ) + "] Session time out" );
it->second->close(); it->second->close();
// if( it->second.unique() ) // if( it->second.unique() )
{ {
it = m_sessionMap.erase( it ); it = this->m_sessionMap.erase(it );
} }
} }
else else
@ -272,13 +287,6 @@ void Core::ServerZone::run( int32_t argc, char* argv[] )
} }
} }
// currently never reached, need a "stopServer" variable to break out of the above while loop
/*for( auto& thread_entry : thread_list )
{
thread_entry.join();
}*/
} }
bool Core::ServerZone::createSession( uint32_t sessionId ) bool Core::ServerZone::createSession( uint32_t sessionId )
@ -364,3 +372,8 @@ void Core::ServerZone::updateSession( std::string playerName )
it->second->loadPlayer(); it->second->loadPlayer();
} }
bool Core::ServerZone::isRunning() const
{
return m_bRunning;
}

View file

@ -40,12 +40,18 @@ namespace Core {
Entity::BattleNpcTemplatePtr getBnpcTemplate( std::string templateName ); Entity::BattleNpcTemplatePtr getBnpcTemplate( std::string templateName );
void mainLoop();
bool isRunning() const;
private: private:
uint16_t m_port; uint16_t m_port;
std::string m_ip; std::string m_ip;
bool m_bRunning;
std::string m_configPath; std::string m_configPath;
XMLConfigPtr m_pConfig; XMLConfigPtr m_pConfig;