AzerothCore 3.3.5a
OpenSource WoW Emulator
Loading...
Searching...
No Matches
Acore Daemon

Files

file  CliRunnable.cpp
 
file  CliRunnable.h
 
file  Main.cpp
 

Classes

class  FreezeDetector
 

Macros

#define _ACORE_CORE_CONFIG   "worldserver.conf"
 

Functions

static void PrintCliPrefix ()
 
void utf8print (void *, std::string_view str)
 
void commandFinished (void *, bool)
 
void CliThread ()
 Thread start
 
 FreezeDetector::FreezeDetector (Acore::Asio::IoContext &ioContext, uint32 maxCoreStuckTime)
 
static void FreezeDetector::Start (std::shared_ptr< FreezeDetector > const &freezeDetector)
 
static void FreezeDetector::Handler (std::weak_ptr< FreezeDetector > freezeDetectorRef, boost::system::error_code const &error)
 
void SignalHandler (boost::system::error_code const &error, int signalNumber)
 
void ClearOnlineAccounts ()
 Clear 'online' status for all accounts with characters in this realm.
 
bool StartDB ()
 Initialize connection to the databases.
 
void StopDB ()
 
bool LoadRealmInfo (Acore::Asio::IoContext &ioContext)
 
AsyncAcceptorStartRaSocketAcceptor (Acore::Asio::IoContext &ioContext)
 
void ShutdownCLIThread (std::thread *cliThread)
 
void AuctionListingRunnable ()
 
void WorldUpdateLoop ()
 
variables_map GetConsoleArguments (int argc, char **argv, fs::path &configFile, std::string &cfg_service)
 
int main (int argc, char **argv)
 Launch the Azeroth server.
 

Variables

static constexpr char CLI_PREFIX [] = "AC> "
 
char serviceName [] = "worldserver"
 
char serviceLongName [] = "AzerothCore world service"
 
char serviceDescription [] = "AzerothCore World of Warcraft emulator world service"
 
int m_ServiceStatus = -1
 
Acore::Asio::DeadlineTimer FreezeDetector::_timer
 
uint32 FreezeDetector::_worldLoopCounter
 
uint32 FreezeDetector::_lastChangeMsTime
 
uint32 FreezeDetector::_maxCoreStuckTimeInMs
 

Detailed Description

Macro Definition Documentation

◆ _ACORE_CORE_CONFIG

#define _ACORE_CORE_CONFIG   "worldserver.conf"

Function Documentation

◆ AuctionListingRunnable()

void AuctionListingRunnable ( )

#include <D:/azerothcore-wotlk/src/server/apps/worldserver/Main.cpp>

723{
724 LOG_INFO("server", "Starting up Auction House Listing thread...");
725
726 while (!World::IsStopped())
727 {
730
732 {
733 {
734 std::lock_guard<std::mutex> guard(AsyncAuctionListingMgr::GetTempLock());
735
736 for (auto const& delayEvent: AsyncAuctionListingMgr::GetTempList())
737 AsyncAuctionListingMgr::GetList().emplace_back(delayEvent);
738
740 }
741
742 for (auto& itr: AsyncAuctionListingMgr::GetList())
743 {
744 if (itr._pickupTimer <= diff)
745 {
746 itr._pickupTimer = Milliseconds::zero();
747 }
748 else
749 {
750 itr._pickupTimer -= diff;
751 }
752 }
753
754 for (auto itr = AsyncAuctionListingMgr::GetList().begin(); itr != AsyncAuctionListingMgr::GetList().end(); ++itr)
755 {
756 if ((*itr)._pickupTimer != Milliseconds::zero())
757 continue;
758
759 if ((*itr).Execute())
761
762 break;
763 }
764 }
765 std::this_thread::sleep_for(1ms);
766 }
767
768 LOG_INFO("server", "Auction House Listing thread exiting without problems.");
769}
#define LOG_INFO(filterType__,...)
Definition: Log.h:165
std::chrono::milliseconds Milliseconds
Milliseconds shorthand typedef.
Definition: Duration.h:27
static std::mutex & GetTempLock()
Definition: AsyncAuctionListing.h:73
static void ResetDiff()
Definition: AsyncAuctionListing.h:70
static Milliseconds GetDiff()
Definition: AsyncAuctionListing.h:69
static std::list< AuctionListItemsDelayEvent > & GetTempList()
Definition: AsyncAuctionListing.h:72
static std::list< AuctionListItemsDelayEvent > & GetList()
Definition: AsyncAuctionListing.h:71
static bool IsStopped()
Definition: World.h:260

References AsyncAuctionListingMgr::GetDiff(), AsyncAuctionListingMgr::GetList(), AsyncAuctionListingMgr::GetTempList(), AsyncAuctionListingMgr::GetTempLock(), World::IsStopped(), LOG_INFO, and AsyncAuctionListingMgr::ResetDiff().

Referenced by main().

◆ ClearOnlineAccounts()

void ClearOnlineAccounts ( )

#include <D:/azerothcore-wotlk/src/server/apps/worldserver/Main.cpp>

Clear 'online' status for all accounts with characters in this realm.

501{
502 // Reset online status for all accounts with characters on the current realm
503 // pussywizard: tc query would set online=0 even if logged in on another realm >_>
504 LoginDatabase.DirectExecute("UPDATE account SET online = 0 WHERE online = {}", realm.Id.Realm);
505
506 // Reset online status for all characters
507 CharacterDatabase.DirectExecute("UPDATE characters SET online = 0 WHERE online <> 0");
508}
DatabaseWorkerPool< LoginDatabaseConnection > LoginDatabase
Accessor to the realm/login database.
Definition: DatabaseEnv.cpp:22
DatabaseWorkerPool< CharacterDatabaseConnection > CharacterDatabase
Accessor to the character database.
Definition: DatabaseEnv.cpp:21
Realm realm
Definition: World.cpp:112
uint32 Realm
Definition: Realm.h:42
RealmHandle Id
Definition: Realm.h:68

References CharacterDatabase, Realm::Id, LoginDatabase, realm, and RealmHandle::Realm.

Referenced by main(), and StartDB().

◆ CliThread()

void CliThread ( )

#include <D:/azerothcore-wotlk/src/server/apps/worldserver/CommandLine/CliRunnable.cpp>

Thread start

Command Line Interface handling thread.

  • As long as the World is running (no World::m_stopEvent), get the command line and handle it
109{
110#if AC_PLATFORM == AC_PLATFORM_WINDOWS
111 // print this here the first time
112 // later it will be printed after command queue updates
114#else
115 ::rl_attempted_completion_function = &Acore::Impl::Readline::cli_completion;
116 {
117 static char BLANK = '\0';
118 ::rl_completer_word_break_characters = &BLANK;
119 }
120 ::rl_event_hook = &Acore::Impl::Readline::cli_hook_func;
121#endif
122
123 if (sConfigMgr->GetOption<bool>("BeepAtStart", true))
124 printf("\a"); // \a = Alert
125
126#if AC_PLATFORM == AC_PLATFORM_WINDOWS
127 if (sConfigMgr->GetOption<bool>("FlashAtStart", true))
128 {
129 FLASHWINFO fInfo;
130 fInfo.cbSize = sizeof(FLASHWINFO);
131 fInfo.dwFlags = FLASHW_TRAY | FLASHW_TIMERNOFG;
132 fInfo.hwnd = GetConsoleWindow();
133 fInfo.uCount = 0;
134 fInfo.dwTimeout = 0;
135 FlashWindowEx(&fInfo);
136 }
137#endif
138
140 while (!World::IsStopped())
141 {
142 fflush(stdout);
143
144 std::string command;
145
146#if AC_PLATFORM == AC_PLATFORM_WINDOWS
147 wchar_t commandbuf[256];
148 if (fgetws(commandbuf, sizeof(commandbuf), stdin))
149 {
150 if (!WStrToUtf8(commandbuf, wcslen(commandbuf), command))
151 {
153 continue;
154 }
155 }
156#else
157 char* command_str = readline(CLI_PREFIX);
158 ::rl_bind_key('\t', ::rl_complete);
159 if (command_str != nullptr)
160 {
161 command = command_str;
162 free(command_str);
163 }
164#endif
165
166 if (!command.empty())
167 {
168 std::size_t nextLineIndex = command.find_first_of("\r\n");
169 if (nextLineIndex != std::string::npos)
170 {
171 if (nextLineIndex == 0)
172 {
173#if AC_PLATFORM == AC_PLATFORM_WINDOWS
175#endif
176 continue;
177 }
178
179 command.erase(nextLineIndex);
180 }
181
182 fflush(stdout);
183 sWorld->QueueCliCommand(new CliCommandHolder(nullptr, command.c_str(), &utf8print, &commandFinished));
184#if AC_PLATFORM != AC_PLATFORM_WINDOWS
185 add_history(command.c_str());
186#endif
187 }
188 else if (feof(stdin))
189 {
191 }
192 }
193}
#define sConfigMgr
Definition: Config.h:95
bool WStrToUtf8(wchar_t const *wstr, size_t size, std::string &utf8str)
Definition: Util.cpp:333
static void PrintCliPrefix()
Definition: CliRunnable.cpp:38
static constexpr char CLI_PREFIX[]
Definition: CliRunnable.cpp:36
void utf8print(void *, std::string_view str)
Definition: CliRunnable.cpp:74
void commandFinished(void *, bool)
Definition: CliRunnable.cpp:86
#define sWorld
Definition: World.h:447
@ SHUTDOWN_EXIT_CODE
Definition: World.h:54
Storage class for commands issued for delayed execution.
Definition: IWorld.h:39
static void StopNow(uint8 exitcode)
Definition: World.h:259

References CLI_PREFIX, commandFinished(), World::IsStopped(), PrintCliPrefix(), sConfigMgr, SHUTDOWN_EXIT_CODE, World::StopNow(), sWorld, utf8print(), and WStrToUtf8().

Referenced by main().

◆ commandFinished()

void commandFinished ( void *  ,
bool   
)

◆ FreezeDetector()

FreezeDetector::FreezeDetector ( Acore::Asio::IoContext ioContext,
uint32  maxCoreStuckTime 
)
inline

#include <D:/azerothcore-wotlk/src/server/apps/worldserver/Main.cpp>

91 : _timer(ioContext), _worldLoopCounter(0), _lastChangeMsTime(getMSTime()), _maxCoreStuckTimeInMs(maxCoreStuckTime) { }
uint32 getMSTime()
Definition: Timer.h:103
uint32 _lastChangeMsTime
Definition: Main.cpp:104
uint32 _worldLoopCounter
Definition: Main.cpp:103
Acore::Asio::DeadlineTimer _timer
Definition: Main.cpp:102
uint32 _maxCoreStuckTimeInMs
Definition: Main.cpp:105

◆ GetConsoleArguments()

variables_map GetConsoleArguments ( int  argc,
char **  argv,
fs::path &  configFile,
std::string &  cfg_service 
)

#include <D:/azerothcore-wotlk/src/server/apps/worldserver/Main.cpp>

772{
773 options_description all("Allowed options");
774 all.add_options()
775 ("help,h", "print usage message")
776 ("version,v", "print version build info")
777 ("dry-run,d", "Dry run")
778 ("config,c", value<fs::path>(&configFile)->default_value(fs::path(sConfigMgr->GetConfigPath() + std::string(_ACORE_CORE_CONFIG))), "use <arg> as configuration file");
779
780#if AC_PLATFORM == AC_PLATFORM_WINDOWS
781 options_description win("Windows platform specific options");
782 win.add_options()
783 ("service,s", value<std::string>(&configService)->default_value(""), "Windows service options: [install | uninstall]");
784
785 all.add(win);
786#endif
787
788 variables_map vm;
789
790 try
791 {
792 store(command_line_parser(argc, argv).options(all).allow_unregistered().run(), vm);
793 notify(vm);
794 }
795 catch (std::exception const& e)
796 {
797 std::cerr << e.what() << "\n";
798 }
799
800 if (vm.count("help"))
801 {
802 std::cout << all << "\n";
803 }
804 else if (vm.count("dry-run"))
805 {
806 sConfigMgr->setDryRun(true);
807 }
808
809 return vm;
810}
#define _ACORE_CORE_CONFIG
Definition: Main.cpp:81

References _ACORE_CORE_CONFIG, and sConfigMgr.

Referenced by main().

◆ Handler()

void FreezeDetector::Handler ( std::weak_ptr< FreezeDetector freezeDetectorRef,
boost::system::error_code const &  error 
)
static

#include <D:/azerothcore-wotlk/src/server/apps/worldserver/Main.cpp>

627{
628 if (!error)
629 {
630 if (std::shared_ptr<FreezeDetector> freezeDetector = freezeDetectorRef.lock())
631 {
632 uint32 curtime = getMSTime();
633
634 uint32 worldLoopCounter = World::m_worldLoopCounter;
635 if (freezeDetector->_worldLoopCounter != worldLoopCounter)
636 {
637 freezeDetector->_lastChangeMsTime = curtime;
638 freezeDetector->_worldLoopCounter = worldLoopCounter;
639 }
640 // possible freeze
641 else
642 {
643 uint32 msTimeDiff = getMSTimeDiff(freezeDetector->_lastChangeMsTime, curtime);
644 if (msTimeDiff > freezeDetector->_maxCoreStuckTimeInMs)
645 {
646 LOG_ERROR("server.worldserver", "World Thread hangs for {} ms, forcing a crash!", msTimeDiff);
647 ABORT("World Thread hangs for {} ms, forcing a crash!", msTimeDiff);
648 }
649 }
650
651 freezeDetector->_timer.expires_from_now(boost::posix_time::seconds(1));
652 freezeDetector->_timer.async_wait(std::bind(&FreezeDetector::Handler, freezeDetectorRef, std::placeholders::_1));
653 }
654 }
655}
#define ABORT
Definition: Errors.h:76
std::uint32_t uint32
Definition: Define.h:108
#define LOG_ERROR(filterType__,...)
Definition: Log.h:157
uint32 getMSTimeDiff(uint32 oldMSTime, uint32 newMSTime)
Definition: Timer.h:110
static void Handler(std::weak_ptr< FreezeDetector > freezeDetectorRef, boost::system::error_code const &error)
Definition: Main.cpp:626
static uint32 m_worldLoopCounter
Definition: World.h:158

References ABORT, getMSTime(), getMSTimeDiff(), FreezeDetector::Handler(), LOG_ERROR, and World::m_worldLoopCounter.

Referenced by FreezeDetector::Handler(), and FreezeDetector::Start().

◆ LoadRealmInfo()

bool LoadRealmInfo ( Acore::Asio::IoContext ioContext)

#include <D:/azerothcore-wotlk/src/server/apps/worldserver/Main.cpp>

675{
676 QueryResult result = LoginDatabase.Query("SELECT id, name, address, localAddress, localSubnetMask, port, icon, flag, timezone, allowedSecurityLevel, population, gamebuild FROM realmlist WHERE id = {}", realm.Id.Realm);
677 if (!result)
678 return false;
679
680 Acore::Asio::Resolver resolver(ioContext);
681
682 Field* fields = result->Fetch();
683 realm.Name = fields[1].Get<std::string>();
684
685 Optional<boost::asio::ip::tcp::endpoint> externalAddress = resolver.Resolve(boost::asio::ip::tcp::v4(), fields[2].Get<std::string>(), "");
686 if (!externalAddress)
687 {
688 LOG_ERROR("server.worldserver", "Could not resolve address {}", fields[2].Get<std::string>());
689 return false;
690 }
691
692 realm.ExternalAddress = std::make_unique<boost::asio::ip::address>(externalAddress->address());
693
694 Optional<boost::asio::ip::tcp::endpoint> localAddress = resolver.Resolve(boost::asio::ip::tcp::v4(), fields[3].Get<std::string>(), "");
695 if (!localAddress)
696 {
697 LOG_ERROR("server.worldserver", "Could not resolve address {}", fields[3].Get<std::string>());
698 return false;
699 }
700
701 realm.LocalAddress = std::make_unique<boost::asio::ip::address>(localAddress->address());
702
703 Optional<boost::asio::ip::tcp::endpoint> localSubmask = resolver.Resolve(boost::asio::ip::tcp::v4(), fields[4].Get<std::string>(), "");
704 if (!localSubmask)
705 {
706 LOG_ERROR("server.worldserver", "Could not resolve address {}", fields[4].Get<std::string>());
707 return false;
708 }
709
710 realm.LocalSubnetMask = std::make_unique<boost::asio::ip::address>(localSubmask->address());
711
712 realm.Port = fields[5].Get<uint16>();
713 realm.Type = fields[6].Get<uint8>();
714 realm.Flags = RealmFlags(fields[7].Get<uint8>());
715 realm.Timezone = fields[8].Get<uint8>();
716 realm.AllowedSecurityLevel = AccountTypes(fields[9].Get<uint8>());
717 realm.PopulationLevel = fields[10].Get<float>();
718 realm.Build = fields[11].Get<uint32>();
719 return true;
720}
AccountTypes
Definition: Common.h:58
std::uint8_t uint8
Definition: Define.h:110
std::uint16_t uint16
Definition: Define.h:109
std::optional< T > Optional
Optional helper class to wrap optional values within.
Definition: Optional.h:24
std::shared_ptr< ResultSet > QueryResult
Definition: DatabaseEnvFwd.h:28
RealmFlags
Definition: Realm.h:25
Definition: Resolver.h:32
Class used to access individual fields of database query result.
Definition: Field.h:99
std::enable_if_t< std::is_arithmetic_v< T >, T > Get() const
Definition: Field.h:113
uint16 Port
Definition: Realm.h:73
RealmFlags Flags
Definition: Realm.h:76
AccountTypes AllowedSecurityLevel
Definition: Realm.h:78
uint8 Timezone
Definition: Realm.h:77
std::unique_ptr< boost::asio::ip::address > LocalSubnetMask
Definition: Realm.h:72
std::unique_ptr< boost::asio::ip::address > LocalAddress
Definition: Realm.h:71
float PopulationLevel
Definition: Realm.h:79
uint32 Build
Definition: Realm.h:69
std::unique_ptr< boost::asio::ip::address > ExternalAddress
Definition: Realm.h:70
std::string Name
Definition: Realm.h:74
uint8 Type
Definition: Realm.h:75

References Realm::AllowedSecurityLevel, Realm::Build, Realm::ExternalAddress, Realm::Flags, Field::Get(), Realm::Id, Realm::LocalAddress, Realm::LocalSubnetMask, LOG_ERROR, LoginDatabase, Realm::Name, Realm::PopulationLevel, Realm::Port, realm, RealmHandle::Realm, Acore::Asio::Resolver::Resolve(), Realm::Timezone, and Realm::Type.

Referenced by main().

◆ main()

int main ( int  argc,
char **  argv 
)

#include <D:/azerothcore-wotlk/src/server/apps/worldserver/Main.cpp>

Launch the Azeroth server.

worldserver PID file creation

  • Initialize the World
  • Clean database before leaving
121{
123 signal(SIGABRT, &Acore::AbortHandler);
124
125 // Command line parsing
126 auto configFile = fs::path(sConfigMgr->GetConfigPath() + std::string(_ACORE_CORE_CONFIG));
127 std::string configService;
128 auto vm = GetConsoleArguments(argc, argv, configFile, configService);
129
130 // exit if help or version is enabled
131 if (vm.count("help"))
132 return 0;
133
134#if AC_PLATFORM == AC_PLATFORM_WINDOWS
135 if (configService.compare("install") == 0)
136 return WinServiceInstall() == true ? 0 : 1;
137 else if (configService.compare("uninstall") == 0)
138 return WinServiceUninstall() == true ? 0 : 1;
139 else if (configService.compare("run") == 0)
140 WinServiceRun();
141
142 Optional<UINT> newTimerResolution;
143 boost::system::error_code dllError;
144 std::shared_ptr<boost::dll::shared_library> winmm(new boost::dll::shared_library("winmm.dll", dllError, boost::dll::load_mode::search_system_folders), [&](boost::dll::shared_library* lib)
145 {
146 try
147 {
148 if (newTimerResolution)
149 lib->get<decltype(timeEndPeriod)>("timeEndPeriod")(*newTimerResolution);
150 }
151 catch (std::exception const&)
152 {
153 // ignore
154 }
155
156 delete lib;
157 });
158
159 if (winmm->is_loaded())
160 {
161 try
162 {
163 auto timeGetDevCapsPtr = winmm->get<decltype(timeGetDevCaps)>("timeGetDevCaps");
164 // setup timer resolution
165 TIMECAPS timeResolutionLimits;
166 if (timeGetDevCapsPtr(&timeResolutionLimits, sizeof(TIMECAPS)) == TIMERR_NOERROR)
167 {
168 auto timeBeginPeriodPtr = winmm->get<decltype(timeBeginPeriod)>("timeBeginPeriod");
169 newTimerResolution = std::min(std::max(timeResolutionLimits.wPeriodMin, 1u), timeResolutionLimits.wPeriodMax);
170 timeBeginPeriodPtr(*newTimerResolution);
171 }
172 }
173 catch (std::exception const& e)
174 {
175 printf("Failed to initialize timer resolution: %s\n", e.what());
176 }
177 }
178
179#endif
180
181 // Add file and args in config
182 sConfigMgr->Configure(configFile.generic_string(), {argv, argv + argc}, CONFIG_FILE_LIST);
183
184 if (!sConfigMgr->LoadAppConfigs())
185 return 1;
186
187 std::shared_ptr<Acore::Asio::IoContext> ioContext = std::make_shared<Acore::Asio::IoContext>();
188
189 // Init all logs
190 sLog->RegisterAppender<AppenderDB>();
191 // If logs are supposed to be handled async then we need to pass the IoContext into the Log singleton
192 sLog->Initialize(sConfigMgr->GetOption<bool>("Log.Async.Enable", false) ? ioContext.get() : nullptr);
193
194 Acore::Banner::Show("worldserver-daemon",
195 [](std::string_view text)
196 {
197 LOG_INFO("server.worldserver", text);
198 },
199 []()
200 {
201 LOG_INFO("server.worldserver", "> Using configuration file {}", sConfigMgr->GetFilename());
202 LOG_INFO("server.worldserver", "> Using SSL version: {} (library: {})", OPENSSL_VERSION_TEXT, OpenSSL_version(OPENSSL_VERSION));
203 LOG_INFO("server.worldserver", "> Using Boost version: {}.{}.{}", BOOST_VERSION / 100000, BOOST_VERSION / 100 % 1000, BOOST_VERSION % 100);
204 });
205
207
208 std::shared_ptr<void> opensslHandle(nullptr, [](void*) { OpenSSLCrypto::threadsCleanup(); });
209
210 // Seed the OpenSSL's PRNG here.
211 // That way it won't auto-seed when calling BigNumber::SetRand and slow down the first world login
212 BigNumber seed;
213 seed.SetRand(16 * 8);
214
216 std::string pidFile = sConfigMgr->GetOption<std::string>("PidFile", "");
217 if (!pidFile.empty())
218 {
219 if (uint32 pid = CreatePIDFile(pidFile))
220 LOG_ERROR("server", "Daemon PID: {}\n", pid); // outError for red color in console
221 else
222 {
223 LOG_ERROR("server", "Cannot create PID file {} (possible error: permission)\n", pidFile);
224 return 1;
225 }
226 }
227
228 // Set signal handlers (this must be done before starting IoContext threads, because otherwise they would unblock and exit)
229 boost::asio::signal_set signals(*ioContext, SIGINT, SIGTERM);
230#if AC_PLATFORM == AC_PLATFORM_WINDOWS
231 signals.add(SIGBREAK);
232#endif
233 signals.async_wait(SignalHandler);
234
235 // Start the Boost based thread pool
236 int numThreads = sConfigMgr->GetOption<int32>("ThreadPool", 1);
237 std::shared_ptr<std::vector<std::thread>> threadPool(new std::vector<std::thread>(), [ioContext](std::vector<std::thread>* del)
238 {
239 ioContext->stop();
240 for (std::thread& thr : *del)
241 thr.join();
242
243 delete del;
244 });
245
246 if (numThreads < 1)
247 {
248 numThreads = 1;
249 }
250
251 for (int i = 0; i < numThreads; ++i)
252 {
253 threadPool->push_back(std::thread([ioContext]()
254 {
255 ioContext->run();
256 }));
257 }
258
259 // Set process priority according to configuration settings
260 SetProcessPriority("server.worldserver", sConfigMgr->GetOption<int32>(CONFIG_PROCESSOR_AFFINITY, 0), sConfigMgr->GetOption<bool>(CONFIG_HIGH_PRIORITY, false));
261
262 // Loading modules configs before scripts
263 sConfigMgr->LoadModulesConfigs();
264
265 sScriptMgr->SetScriptLoader(AddScripts);
266 sScriptMgr->SetModulesLoader(AddModulesScripts);
267
268 std::shared_ptr<void> sScriptMgrHandle(nullptr, [](void*)
269 {
270 sScriptMgr->Unload();
271 //sScriptReloadMgr->Unload();
272 });
273
274 LOG_INFO("server.loading", "Initializing Scripts...");
275 sScriptMgr->Initialize();
276
277 // Start the databases
278 if (!StartDB())
279 return 1;
280
281 std::shared_ptr<void> dbHandle(nullptr, [](void*) { StopDB(); });
282
283 // set server offline (not connectable)
284 LoginDatabase.DirectExecute("UPDATE realmlist SET flag = (flag & ~{}) | {} WHERE id = '{}'", REALM_FLAG_OFFLINE, REALM_FLAG_VERSION_MISMATCH, realm.Id.Realm);
285
286 LoadRealmInfo(*ioContext);
287
288 sMetric->Initialize(realm.Name, *ioContext, []()
289 {
290 METRIC_VALUE("online_players", sWorld->GetPlayerCount());
291 METRIC_VALUE("db_queue_login", uint64(LoginDatabase.QueueSize()));
292 METRIC_VALUE("db_queue_character", uint64(CharacterDatabase.QueueSize()));
293 METRIC_VALUE("db_queue_world", uint64(WorldDatabase.QueueSize()));
294 });
295
296 METRIC_EVENT("events", "Worldserver started", "");
297
298 std::shared_ptr<void> sMetricHandle(nullptr, [](void*)
299 {
300 METRIC_EVENT("events", "Worldserver shutdown", "");
301 sMetric->Unload();
302 });
303
305
307 sSecretMgr->Initialize();
308 sWorld->SetInitialWorldSettings();
309
310 std::shared_ptr<void> mapManagementHandle(nullptr, [](void*)
311 {
312 // unload battleground templates before different singletons destroyed
313 sBattlegroundMgr->DeleteAllBattlegrounds();
314
315 sOutdoorPvPMgr->Die(); // unload it before MapMgr
316 sMapMgr->UnloadAll(); // unload all grids (including locked in memory)
317
318 sScriptMgr->OnAfterUnloadAllMaps();
319 });
320
321 // Start the Remote Access port (acceptor) if enabled
322 std::unique_ptr<AsyncAcceptor> raAcceptor;
323 if (sConfigMgr->GetOption<bool>("Ra.Enable", false))
324 {
325 raAcceptor.reset(StartRaSocketAcceptor(*ioContext));
326 }
327
328 // Start soap serving thread if enabled
329 std::shared_ptr<std::thread> soapThread;
330 if (sConfigMgr->GetOption<bool>("SOAP.Enabled", false))
331 {
332 soapThread.reset(new std::thread(ACSoapThread, sConfigMgr->GetOption<std::string>("SOAP.IP", "127.0.0.1"), uint16(sConfigMgr->GetOption<int32>("SOAP.Port", 7878))),
333 [](std::thread* thr)
334 {
335 thr->join();
336 delete thr;
337 });
338 }
339
340 // Launch the worldserver listener socket
341 uint16 worldPort = uint16(sWorld->getIntConfig(CONFIG_PORT_WORLD));
342 std::string worldListener = sConfigMgr->GetOption<std::string>("BindIP", "0.0.0.0");
343
344 int networkThreads = sConfigMgr->GetOption<int32>("Network.Threads", 1);
345
346 if (networkThreads <= 0)
347 {
348 LOG_ERROR("server.worldserver", "Network.Threads must be greater than 0");
350 return 1;
351 }
352
353 if (!sWorldSocketMgr.StartWorldNetwork(*ioContext, worldListener, worldPort, networkThreads))
354 {
355 LOG_ERROR("server.worldserver", "Failed to initialize network");
357 return 1;
358 }
359
360 std::shared_ptr<void> sWorldSocketMgrHandle(nullptr, [](void*)
361 {
362 sWorld->KickAll(); // save and kick all players
363 sWorld->UpdateSessions(1); // real players unload required UpdateSessions call
364
365 sWorldSocketMgr.StopNetwork();
366
369 });
370
371 // Set server online (allow connecting now)
372 LoginDatabase.DirectExecute("UPDATE realmlist SET flag = flag & ~{}, population = 0 WHERE id = '{}'", REALM_FLAG_VERSION_MISMATCH, realm.Id.Realm);
373 realm.PopulationLevel = 0.0f;
375
376 // Start the freeze check callback cycle in 5 seconds (cycle itself is 1 sec)
377 std::shared_ptr<FreezeDetector> freezeDetector;
378 if (int32 coreStuckTime = sConfigMgr->GetOption<int32>("MaxCoreStuckTime", 60))
379 {
380 freezeDetector = std::make_shared<FreezeDetector>(*ioContext, coreStuckTime * 1000);
381 FreezeDetector::Start(freezeDetector);
382 LOG_INFO("server.worldserver", "Starting up anti-freeze thread ({} seconds max stuck time)...", coreStuckTime);
383 }
384
385 LOG_INFO("server.worldserver", "{} (worldserver-daemon) ready...", GitRevision::GetFullVersion());
386
387 sScriptMgr->OnStartup();
388
389// Be kind and warn people of EOL deprecation :)
390#if !defined(MARIADB_VERSION_ID)
391 if (MySQL::GetLibraryVersion() < 80000)
392 LOG_WARN("server", "WARNING: You are using MySQL version 5.7 which is soon EOL!\nThis version will be deprecated. Consider upgrading to MySQL 8.0 or 8.1!");
393#endif
394#if OPENSSL_VERSION_NUMBER < 0x30000000L
395 LOG_WARN("server", "WARNING: You are using OpenSSL version 1.1 which is soon EOL!\nThis version will be deprecated. Consider upgrading to OpenSSL 3.0 or 3.1!");
396#endif
397
398 // Launch CliRunnable thread
399 std::shared_ptr<std::thread> cliThread;
400#if AC_PLATFORM == AC_PLATFORM_WINDOWS
401 if (sConfigMgr->GetOption<bool>("Console.Enable", true) && (m_ServiceStatus == -1)/* need disable console in service mode*/)
402#else
403 if (sConfigMgr->GetOption<bool>("Console.Enable", true))
404#endif
405 {
406 cliThread.reset(new std::thread(CliThread), &ShutdownCLIThread);
407 }
408
409 // Launch auction listing thread
410 std::shared_ptr<std::thread> auctionListingThread;
411 auctionListingThread.reset(new std::thread(AuctionListingRunnable),
412 [](std::thread* thr)
413 {
414 thr->join();
415 delete thr;
416 });
417
419
420 // Shutdown starts here
421 threadPool.reset();
422
423 sLog->SetSynchronous();
424
425 sScriptMgr->OnShutdown();
426
427 // set server offline
428 LoginDatabase.DirectExecute("UPDATE realmlist SET flag = flag | {} WHERE id = '{}'", REALM_FLAG_OFFLINE, realm.Id.Realm);
429
430 LOG_INFO("server.worldserver", "Halting process...");
431
432 // 0 - normal shutdown
433 // 1 - shutdown at error
434 // 2 - restart command used, this code can be used by restarter for restart AzerothCore
435
436 return World::GetExitCode();
437}
std::int32_t int32
Definition: Define.h:104
#define sLog
Definition: Log.h:126
#define LOG_WARN(filterType__,...)
Definition: Log.h:161
#define sMetric
Definition: Metric.h:132
#define METRIC_EVENT(category, title, description)
Definition: Metric.h:187
void SetProcessPriority(std::string const &logChannel, uint32 affinity, bool highPriority)
Definition: ProcessPriority.cpp:29
#define CONFIG_HIGH_PRIORITY
Definition: ProcessPriority.h:25
#define CONFIG_PROCESSOR_AFFINITY
Definition: ProcessPriority.h:24
uint32 CreatePIDFile(std::string const &filename)
create PID file
Definition: Util.cpp:218
void ACSoapThread(const std::string &host, uint16 port)
Definition: ACSoap.cpp:24
#define sBattlegroundMgr
Definition: BattlegroundMgr.h:188
#define sMapMgr
Definition: MapMgr.h:221
#define sOutdoorPvPMgr
Definition: OutdoorPvPMgr.h:103
#define sScriptMgr
Definition: ScriptMgr.h:698
@ CONFIG_PORT_WORLD
Definition: IWorld.h:215
void AddScripts()
Definition: WorldMock.h:29
@ REALM_FLAG_OFFLINE
Definition: Realm.h:28
@ REALM_FLAG_VERSION_MISMATCH
Definition: Realm.h:27
#define sSecretMgr
Definition: SecretMgr.h:73
@ SERVER_PROCESS_WORLDSERVER
Definition: SharedDefines.h:3739
AsyncAcceptor * StartRaSocketAcceptor(Acore::Asio::IoContext &ioContext)
Definition: Main.cpp:657
bool StartDB()
Initialize connection to the databases.
Definition: Main.cpp:440
void ClearOnlineAccounts()
Clear 'online' status for all accounts with characters in this realm.
Definition: Main.cpp:500
void CliThread()
Thread start
Definition: CliRunnable.cpp:108
void WorldUpdateLoop()
Definition: Main.cpp:571
static void Start(std::shared_ptr< FreezeDetector > const &freezeDetector)
Definition: Main.cpp:93
bool LoadRealmInfo(Acore::Asio::IoContext &ioContext)
Definition: Main.cpp:674
void StopDB()
Definition: Main.cpp:490
void ShutdownCLIThread(std::thread *cliThread)
Definition: Main.cpp:510
variables_map GetConsoleArguments(int argc, char **argv, fs::path &configFile, std::string &cfg_service)
Definition: Main.cpp:771
int m_ServiceStatus
Definition: Main.cpp:74
void SignalHandler(boost::system::error_code const &error, int signalNumber)
Definition: Main.cpp:620
void AuctionListingRunnable()
Definition: Main.cpp:722
#define sWorldSocketMgr
Definition: WorldSocketMgr.h:64
@ ERROR_EXIT_CODE
Definition: World.h:55
AC_COMMON_API void AbortHandler(int sigval)
Definition: Errors.cpp:148
AC_COMMON_API void Show(std::string_view applicationName, void(*log)(std::string_view text), void(*logExtraInfo)())
Definition: Banner.cpp:22
AC_COMMON_API void threadsSetup()
Needs to be called before threads using openssl are spawned.
Definition: OpenSSLCrypto.cpp:64
AC_COMMON_API void threadsCleanup()
Needs to be called after threads using openssl are despawned.
Definition: OpenSSLCrypto.cpp:88
AC_COMMON_API char const * GetFullVersion()
Definition: GitRevision.cpp:82
AC_DATABASE_API uint32 GetLibraryVersion()
Definition: MySQLThreading.cpp:31
AC_COMMON_API void SetEnableModulesList(std::string_view modulesList)
Definition: ModuleMgr.cpp:26
Definition: BigNumber.h:29
void SetRand(int32 numbits)
Definition: BigNumber.cpp:84
Definition: AppenderDB.h:24
static uint8 GetExitCode()
Definition: World.h:258
static ServerProcessTypes _type
Definition: SharedDefines.h:3762

References _ACORE_CORE_CONFIG, Acore::Impl::CurrentServerProcessHolder::_type, Acore::AbortHandler(), ACSoapThread(), AddScripts(), AuctionListingRunnable(), ClearOnlineAccounts(), CliThread(), CONFIG_HIGH_PRIORITY, CONFIG_PORT_WORLD, CONFIG_PROCESSOR_AFFINITY, CreatePIDFile(), ERROR_EXIT_CODE, Realm::Flags, GetConsoleArguments(), World::GetExitCode(), GitRevision::GetFullVersion(), MySQL::GetLibraryVersion(), Realm::Id, LoadRealmInfo(), LOG_ERROR, LOG_INFO, LOG_WARN, LoginDatabase, m_ServiceStatus, METRIC_EVENT, Realm::Name, Realm::PopulationLevel, realm, RealmHandle::Realm, REALM_FLAG_OFFLINE, REALM_FLAG_VERSION_MISMATCH, sBattlegroundMgr, sConfigMgr, SERVER_PROCESS_WORLDSERVER, Acore::Module::SetEnableModulesList(), SetProcessPriority(), BigNumber::SetRand(), Acore::Banner::Show(), ShutdownCLIThread(), SignalHandler(), sLog, sMapMgr, sMetric, sOutdoorPvPMgr, sScriptMgr, sSecretMgr, FreezeDetector::Start(), StartDB(), StartRaSocketAcceptor(), StopDB(), World::StopNow(), sWorld, sWorldSocketMgr, OpenSSLCrypto::threadsCleanup(), OpenSSLCrypto::threadsSetup(), and WorldUpdateLoop().

◆ PrintCliPrefix()

static void PrintCliPrefix ( )
inlinestatic

◆ ShutdownCLIThread()

void ShutdownCLIThread ( std::thread *  cliThread)

#include <D:/azerothcore-wotlk/src/server/apps/worldserver/Main.cpp>

511{
512 if (cliThread)
513 {
514#ifdef _WIN32
515 // First try to cancel any I/O in the CLI thread
516 if (!CancelSynchronousIo(cliThread->native_handle()))
517 {
518 // if CancelSynchronousIo() fails, print the error and try with old way
519 DWORD errorCode = GetLastError();
520 LPCSTR errorBuffer;
521
522 DWORD formatReturnCode = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS,
523 nullptr, errorCode, 0, (LPTSTR)&errorBuffer, 0, nullptr);
524 if (!formatReturnCode)
525 errorBuffer = "Unknown error";
526
527 LOG_DEBUG("server.worldserver", "Error cancelling I/O of CliThread, error code {}, detail: {}", uint32(errorCode), errorBuffer);
528
529 if (!formatReturnCode)
530 LocalFree((LPSTR)errorBuffer);
531
532 // send keyboard input to safely unblock the CLI thread
533 INPUT_RECORD b[4];
534 HANDLE hStdIn = GetStdHandle(STD_INPUT_HANDLE);
535 b[0].EventType = KEY_EVENT;
536 b[0].Event.KeyEvent.bKeyDown = TRUE;
537 b[0].Event.KeyEvent.uChar.AsciiChar = 'X';
538 b[0].Event.KeyEvent.wVirtualKeyCode = 'X';
539 b[0].Event.KeyEvent.wRepeatCount = 1;
540
541 b[1].EventType = KEY_EVENT;
542 b[1].Event.KeyEvent.bKeyDown = FALSE;
543 b[1].Event.KeyEvent.uChar.AsciiChar = 'X';
544 b[1].Event.KeyEvent.wVirtualKeyCode = 'X';
545 b[1].Event.KeyEvent.wRepeatCount = 1;
546
547 b[2].EventType = KEY_EVENT;
548 b[2].Event.KeyEvent.bKeyDown = TRUE;
549 b[2].Event.KeyEvent.dwControlKeyState = 0;
550 b[2].Event.KeyEvent.uChar.AsciiChar = '\r';
551 b[2].Event.KeyEvent.wVirtualKeyCode = VK_RETURN;
552 b[2].Event.KeyEvent.wRepeatCount = 1;
553 b[2].Event.KeyEvent.wVirtualScanCode = 0x1c;
554
555 b[3].EventType = KEY_EVENT;
556 b[3].Event.KeyEvent.bKeyDown = FALSE;
557 b[3].Event.KeyEvent.dwControlKeyState = 0;
558 b[3].Event.KeyEvent.uChar.AsciiChar = '\r';
559 b[3].Event.KeyEvent.wVirtualKeyCode = VK_RETURN;
560 b[3].Event.KeyEvent.wVirtualScanCode = 0x1c;
561 b[3].Event.KeyEvent.wRepeatCount = 1;
562 DWORD numb;
563 WriteConsoleInput(hStdIn, b, 4, &numb);
564 }
565#endif
566 cliThread->join();
567 delete cliThread;
568 }
569}
#define LOG_DEBUG(filterType__,...)
Definition: Log.h:169

References LOG_DEBUG.

Referenced by main().

◆ SignalHandler()

void SignalHandler ( boost::system::error_code const &  error,
int  signalNumber 
)

◆ Start()

static void FreezeDetector::Start ( std::shared_ptr< FreezeDetector > const &  freezeDetector)
inlinestatic

#include <D:/azerothcore-wotlk/src/server/apps/worldserver/Main.cpp>

94 {
95 freezeDetector->_timer.expires_from_now(boost::posix_time::seconds(5));
96 freezeDetector->_timer.async_wait(std::bind(&FreezeDetector::Handler, std::weak_ptr<FreezeDetector>(freezeDetector), std::placeholders::_1));
97 }

References FreezeDetector::Handler().

Referenced by main().

◆ StartDB()

bool StartDB ( )

#include <D:/azerothcore-wotlk/src/server/apps/worldserver/Main.cpp>

Initialize connection to the databases.

  • Get the realm Id from the configuration file
  • Clean the database before starting
  • Insert version info into DB
441{
443
444 // Load databases
445 DatabaseLoader loader("server.worldserver", DatabaseLoader::DATABASE_NONE, AC_MODULES_LIST);
446 loader
447 .AddDatabase(LoginDatabase, "Login")
448 .AddDatabase(CharacterDatabase, "Character")
449 .AddDatabase(WorldDatabase, "World");
450
451 if (!loader.Load())
452 return false;
453
455 realm.Id.Realm = sConfigMgr->GetOption<uint32>("RealmID", 0);
456 if (!realm.Id.Realm)
457 {
458 LOG_ERROR("server.worldserver", "Realm ID not defined in configuration file");
459 return false;
460 }
461 else if (realm.Id.Realm > 255)
462 {
463 /*
464 * Due to the client only being able to read a realm.Id.Realm
465 * with a size of uint8 we can "only" store up to 255 realms
466 * anything further the client will behave anormaly
467 */
468 LOG_ERROR("server.worldserver", "Realm ID must range from 1 to 255");
469 return false;
470 }
471
472 LOG_INFO("server.loading", "Loading World Information...");
473 LOG_INFO("server.loading", "> RealmID: {}", realm.Id.Realm);
474
477
479 WorldDatabase.Execute("UPDATE version SET core_version = '{}', core_revision = '{}'", GitRevision::GetFullVersion(), GitRevision::GetHash()); // One-time query
480
481 sWorld->LoadDBVersion();
482
483 LOG_INFO("server.loading", "> Version DB world: {}", sWorld->GetDBVersion());
484
485 sScriptMgr->OnAfterDatabasesLoaded(loader.GetUpdateFlags());
486
487 return true;
488}
DatabaseWorkerPool< WorldDatabaseConnection > WorldDatabase
Accessor to the world database.
Definition: DatabaseEnv.cpp:20
AC_COMMON_API char const * GetHash()
Definition: GitRevision.cpp:21
AC_DATABASE_API void Library_Init()
Definition: MySQLThreading.cpp:21
Definition: DatabaseLoader.h:33
@ DATABASE_NONE
Definition: DatabaseLoader.h:46

References DatabaseLoader::AddDatabase(), CharacterDatabase, ClearOnlineAccounts(), DatabaseLoader::DATABASE_NONE, GitRevision::GetFullVersion(), GitRevision::GetHash(), DatabaseLoader::GetUpdateFlags(), Realm::Id, MySQL::Library_Init(), DatabaseLoader::Load(), LOG_ERROR, LOG_INFO, LoginDatabase, realm, RealmHandle::Realm, sConfigMgr, sScriptMgr, sWorld, and WorldDatabase.

Referenced by main().

◆ StartRaSocketAcceptor()

AsyncAcceptor * StartRaSocketAcceptor ( Acore::Asio::IoContext ioContext)

#include <D:/azerothcore-wotlk/src/server/apps/worldserver/Main.cpp>

658{
659 uint16 raPort = uint16(sConfigMgr->GetOption<int32>("Ra.Port", 3443));
660 std::string raListener = sConfigMgr->GetOption<std::string>("Ra.IP", "0.0.0.0");
661
662 AsyncAcceptor* acceptor = new AsyncAcceptor(ioContext, raListener, raPort);
663 if (!acceptor->Bind())
664 {
665 LOG_ERROR("server.worldserver", "Failed to bind RA socket acceptor");
666 delete acceptor;
667 return nullptr;
668 }
669
670 acceptor->AsyncAccept<RASession>();
671 return acceptor;
672}
Definition: RASession.h:32
Definition: AsyncAcceptor.h:37

References LOG_ERROR, and sConfigMgr.

Referenced by main().

◆ StopDB()

void StopDB ( )

#include <D:/azerothcore-wotlk/src/server/apps/worldserver/Main.cpp>

491{
492 CharacterDatabase.Close();
493 WorldDatabase.Close();
494 LoginDatabase.Close();
495
497}
AC_DATABASE_API void Library_End()
Definition: MySQLThreading.cpp:26

References CharacterDatabase, MySQL::Library_End(), LoginDatabase, and WorldDatabase.

Referenced by main().

◆ utf8print()

void utf8print ( void *  ,
std::string_view  str 
)

#include <D:/azerothcore-wotlk/src/server/apps/worldserver/CommandLine/CliRunnable.cpp>

75{
76#if AC_PLATFORM == AC_PLATFORM_WINDOWS
77 fmt::print(str);
78#else
79{
80 fmt::print(str);
81 fflush(stdout);
82}
83#endif
84}

Referenced by CliThread().

◆ WorldUpdateLoop()

void WorldUpdateLoop ( )

#include <D:/azerothcore-wotlk/src/server/apps/worldserver/Main.cpp>

  • While we have not World::m_stopEvent, update the world
572{
573 uint32 minUpdateDiff = uint32(sConfigMgr->GetOption<int32>("MinWorldUpdateTime", 1));
574 uint32 realCurrTime = 0;
575 uint32 realPrevTime = getMSTime();
576
577 uint32 maxCoreStuckTime = uint32(sConfigMgr->GetOption<int32>("MaxCoreStuckTime", 60)) * 1000;
578 uint32 halfMaxCoreStuckTime = maxCoreStuckTime / 2;
579 if (!halfMaxCoreStuckTime)
580 halfMaxCoreStuckTime = std::numeric_limits<uint32>::max();
581
582 LoginDatabase.WarnAboutSyncQueries(true);
583 CharacterDatabase.WarnAboutSyncQueries(true);
584 WorldDatabase.WarnAboutSyncQueries(true);
585
587 while (!World::IsStopped())
588 {
590 realCurrTime = getMSTime();
591
592 uint32 diff = getMSTimeDiff(realPrevTime, realCurrTime);
593 if (diff < minUpdateDiff)
594 {
595 uint32 sleepTime = minUpdateDiff - diff;
596 if (sleepTime >= halfMaxCoreStuckTime)
597 LOG_ERROR("server.worldserver", "WorldUpdateLoop() waiting for {} ms with MaxCoreStuckTime set to {} ms", sleepTime, maxCoreStuckTime);
598 // sleep until enough time passes that we can update all timers
599 std::this_thread::sleep_for(Milliseconds(sleepTime));
600 continue;
601 }
602
603 sWorld->Update(diff);
604 realPrevTime = realCurrTime;
605
606#ifdef _WIN32
607 if (m_ServiceStatus == 0)
609
610 while (m_ServiceStatus == 2)
611 Sleep(1000);
612#endif
613 }
614
615 LoginDatabase.WarnAboutSyncQueries(false);
616 CharacterDatabase.WarnAboutSyncQueries(false);
617 WorldDatabase.WarnAboutSyncQueries(false);
618}

References CharacterDatabase, getMSTime(), getMSTimeDiff(), World::IsStopped(), LOG_ERROR, LoginDatabase, m_ServiceStatus, World::m_worldLoopCounter, sConfigMgr, SHUTDOWN_EXIT_CODE, World::StopNow(), sWorld, and WorldDatabase.

Referenced by main().

Variable Documentation

◆ _lastChangeMsTime

uint32 FreezeDetector::_lastChangeMsTime
private

◆ _maxCoreStuckTimeInMs

uint32 FreezeDetector::_maxCoreStuckTimeInMs
private

◆ _timer

Acore::Asio::DeadlineTimer FreezeDetector::_timer
private

◆ _worldLoopCounter

uint32 FreezeDetector::_worldLoopCounter
private

◆ CLI_PREFIX

constexpr char CLI_PREFIX[] = "AC> "
staticconstexpr

◆ m_ServiceStatus

int m_ServiceStatus = -1

◆ serviceDescription

char serviceDescription[] = "AzerothCore World of Warcraft emulator world service"

◆ serviceLongName

char serviceLongName[] = "AzerothCore world service"

◆ serviceName

char serviceName[] = "worldserver"