From 598d038aea8259d475fa80758f77d2a3bd8fd895 Mon Sep 17 00:00:00 2001 From: Mordred Date: Sun, 1 Oct 2017 17:58:11 +0200 Subject: [PATCH] refactoring of server zone mainloop --- src/servers/Server_Common/Common.h | 7 ++-- src/servers/Server_Zone/ServerZone.cpp | 49 ++++++++++++++++---------- src/servers/Server_Zone/ServerZone.h | 6 ++++ 3 files changed, 41 insertions(+), 21 deletions(-) diff --git a/src/servers/Server_Common/Common.h b/src/servers/Server_Common/Common.h index f573dfc6..609496a9 100644 --- a/src/servers/Server_Common/Common.h +++ b/src/servers/Server_Common/Common.h @@ -638,9 +638,9 @@ namespace Core { StatusAffliction1, Occupied, Occupied1, - Occupied2, Occupied3, + BoundByDuty, Occupied4, DuelingArea, @@ -651,6 +651,7 @@ namespace Core { PreparingToCraft, Gathering, Fishing, + BeingRaised, BetweenAreas, Stealthed, @@ -661,9 +662,9 @@ namespace Core { BetweenAreas1, SystemError, LoggingOut, + InvalidLocation, WaitingForDuty, - BoundByDuty1, Mounting, WatchingCutscene, @@ -681,9 +682,9 @@ namespace Core { FreeTrail, BeingMoved, Mounting2, - StatusAffliction3, StatusAffliction4, + RegisteringRaceOrMatch, WaitingForRaceOrMatch, WaitingForTripleTriadMatch, diff --git a/src/servers/Server_Zone/ServerZone.cpp b/src/servers/Server_Zone/ServerZone.cpp index 16042e03..32d6b9e1 100644 --- a/src/servers/Server_Zone/ServerZone.cpp +++ b/src/servers/Server_Zone/ServerZone.cpp @@ -41,7 +41,8 @@ Core::LinkshellMgr g_linkshellMgr; Core::ServerZone::ServerZone( const std::string& configPath, uint16_t serverId ) - : m_configPath( configPath ) + : m_configPath( configPath ), + m_bRunning( true ) { m_pConfig = XMLConfigPtr( new XMLConfig ); } @@ -224,18 +225,32 @@ void Core::ServerZone::run( int32_t argc, char* argv[] ) g_zoneMgr.createZones(); 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( "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(); - std::lock_guard 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; 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(); - for( ; it != m_sessionMap.end(); ) + + auto it = this->m_sessionMap.begin(); + for( ; it != this->m_sessionMap.end(); ) { uint32_t diff = currTime - it->second->getLastDataTime(); @@ -257,11 +272,11 @@ void Core::ServerZone::run( int32_t argc, char* argv[] ) 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(); // if( it->second.unique() ) { - it = m_sessionMap.erase( it ); + it = this->m_sessionMap.erase(it ); } } 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 ) @@ -364,3 +372,8 @@ void Core::ServerZone::updateSession( std::string playerName ) it->second->loadPlayer(); } +bool Core::ServerZone::isRunning() const +{ + return m_bRunning; +} + diff --git a/src/servers/Server_Zone/ServerZone.h b/src/servers/Server_Zone/ServerZone.h index 2472c487..130d2f22 100644 --- a/src/servers/Server_Zone/ServerZone.h +++ b/src/servers/Server_Zone/ServerZone.h @@ -40,12 +40,18 @@ namespace Core { Entity::BattleNpcTemplatePtr getBnpcTemplate( std::string templateName ); + void mainLoop(); + + bool isRunning() const; + private: uint16_t m_port; std::string m_ip; + bool m_bRunning; + std::string m_configPath; XMLConfigPtr m_pConfig;