AzerothCore 3.3.5a
OpenSource WoW Emulator
Loading...
Searching...
No Matches
RealmList Class Reference

Storage object for the list of realms on the server. More...

#include "RealmList.h"

Public Types

typedef std::map< RealmHandle, RealmRealmMap
 

Public Member Functions

void Initialize (Acore::Asio::IoContext &ioContext, uint32 updateInterval)
 
void Close ()
 
RealmMap const & GetRealms () const
 
Realm const * GetRealm (RealmHandle const &id) const
 
RealmBuildInfo const * GetBuildInfo (uint32 build) const
 

Static Public Member Functions

static RealmListInstance ()
 

Private Member Functions

 RealmList ()
 
 ~RealmList ()=default
 
void LoadBuildInfo ()
 
void UpdateRealms (boost::system::error_code const &error)
 
void UpdateRealm (RealmHandle const &id, uint32 build, std::string const &name, boost::asio::ip::address &&address, boost::asio::ip::address &&localAddr, boost::asio::ip::address &&localSubmask, uint16 port, uint8 icon, RealmFlags flag, uint8 timezone, AccountTypes allowedSecurityLevel, float population)
 

Private Attributes

std::vector< RealmBuildInfo_builds
 
RealmMap _realms
 
uint32 _updateInterval {0}
 
std::unique_ptr< Acore::Asio::DeadlineTimer_updateTimer
 
std::unique_ptr< Acore::Asio::Resolver_resolver
 

Detailed Description

Storage object for the list of realms on the server.

Member Typedef Documentation

◆ RealmMap

Constructor & Destructor Documentation

◆ RealmList()

RealmList::RealmList ( )
private
27: _updateInterval(0) { }
uint32 _updateInterval
Definition: RealmList.h:72

◆ ~RealmList()

RealmList::~RealmList ( )
privatedefault

Member Function Documentation

◆ Close()

void RealmList::Close ( )
49{
50 _updateTimer->cancel();
51}
std::unique_ptr< Acore::Asio::DeadlineTimer > _updateTimer
Definition: RealmList.h:73

References _updateTimer.

◆ GetBuildInfo()

RealmBuildInfo const * RealmList::GetBuildInfo ( uint32  build) const
247{
248 for (RealmBuildInfo const& clientBuild : _builds)
249 {
250 if (clientBuild.Build == build)
251 {
252 return &clientBuild;
253 }
254 }
255
256 return nullptr;
257}
Definition: RealmList.h:29
std::vector< RealmBuildInfo > _builds
Definition: RealmList.h:70

References _builds.

◆ GetRealm()

Realm const * RealmList::GetRealm ( RealmHandle const &  id) const
236{
237 auto itr = _realms.find(id);
238 if (itr != _realms.end())
239 {
240 return &itr->second;
241 }
242
243 return nullptr;
244}
RealmMap _realms
Definition: RealmList.h:71

References _realms.

◆ GetRealms()

RealmMap const & RealmList::GetRealms ( ) const
inline
55{ return _realms; }

◆ Initialize()

void RealmList::Initialize ( Acore::Asio::IoContext ioContext,
uint32  updateInterval 
)
37{
38 _updateInterval = updateInterval;
39 _updateTimer = std::make_unique<Acore::Asio::DeadlineTimer>(ioContext);
40 _resolver = std::make_unique<Acore::Asio::Resolver>(ioContext);
41
43
44 // Get the content of the realmlist table in the database
45 UpdateRealms(boost::system::error_code());
46}
void UpdateRealms(boost::system::error_code const &error)
Definition: RealmList.cpp:127
std::unique_ptr< Acore::Asio::Resolver > _resolver
Definition: RealmList.h:74
void LoadBuildInfo()
Definition: RealmList.cpp:53

References _resolver, _updateInterval, _updateTimer, LoadBuildInfo(), and UpdateRealms().

◆ Instance()

RealmList * RealmList::Instance ( )
static
30{
31 static RealmList instance;
32 return &instance;
33}
Storage object for the list of realms on the server.
Definition: RealmList.h:46

◆ LoadBuildInfo()

void RealmList::LoadBuildInfo ( )
private
54{
55 // 0 1 2 3 4 5 6
56 if (auto result = LoginDatabase.Query("SELECT majorVersion, minorVersion, bugfixVersion, hotfixVersion, build, winChecksumSeed, macChecksumSeed FROM build_info ORDER BY build ASC"))
57 {
58 for (auto const& fields : *result)
59 {
60 RealmBuildInfo& build = _builds.emplace_back();
61 build.MajorVersion = fields[0].Get<uint32>();
62 build.MinorVersion = fields[1].Get<uint32>();
63 build.BugfixVersion = fields[2].Get<uint32>();
64 std::string hotfixVersion = fields[3].Get<std::string>();
65
66 if (hotfixVersion.length() < build.HotfixVersion.size())
67 {
68 std::copy(hotfixVersion.begin(), hotfixVersion.end(), build.HotfixVersion.begin());
69 }
70 else
71 {
72 std::fill(hotfixVersion.begin(), hotfixVersion.end(), '\0');
73 }
74
75 build.Build = fields[4].Get<uint32>();
76 std::string windowsHash = fields[5].Get<std::string>();
77
78 if (windowsHash.length() == build.WindowsHash.size() * 2)
79 {
80 HexStrToByteArray(windowsHash, build.WindowsHash);
81 }
82
83 std::string macHash = fields[6].Get<std::string>();
84
85 if (macHash.length() == build.MacHash.size() * 2)
86 {
87 HexStrToByteArray(macHash, build.MacHash);
88 }
89 }
90 }
91}
std::uint32_t uint32
Definition: Define.h:108
void HexStrToByteArray(std::string_view str, std::array< uint8, Size > &buf, bool reverse=false)
Definition: Util.h:387
DatabaseWorkerPool< LoginDatabaseConnection > LoginDatabase
Accessor to the realm/login database.
Definition: DatabaseEnv.cpp:22
std::array< char, 4 > HotfixVersion
Definition: RealmList.h:34
uint32 MajorVersion
Definition: RealmList.h:31
uint32 BugfixVersion
Definition: RealmList.h:33
uint32 Build
Definition: RealmList.h:30
uint32 MinorVersion
Definition: RealmList.h:32
std::array< uint8, 20 > WindowsHash
Definition: RealmList.h:35
std::array< uint8, 20 > MacHash
Definition: RealmList.h:36

References _builds, RealmBuildInfo::BugfixVersion, RealmBuildInfo::Build, HexStrToByteArray(), RealmBuildInfo::HotfixVersion, LoginDatabase, RealmBuildInfo::MacHash, RealmBuildInfo::MajorVersion, RealmBuildInfo::MinorVersion, and RealmBuildInfo::WindowsHash.

Referenced by Initialize().

◆ UpdateRealm()

void RealmList::UpdateRealm ( RealmHandle const &  id,
uint32  build,
std::string const &  name,
boost::asio::ip::address &&  address,
boost::asio::ip::address &&  localAddr,
boost::asio::ip::address &&  localSubmask,
uint16  port,
uint8  icon,
RealmFlags  flag,
uint8  timezone,
AccountTypes  allowedSecurityLevel,
float  population 
)
private
96{
97 // Create new if not exist or update existed
98 Realm& realm = _realms[id];
99
100 realm.Id = id;
101 realm.Build = build;
102 realm.Name = name;
103 realm.Type = icon;
104 realm.Flags = flag;
105 realm.Timezone = timezone;
106 realm.AllowedSecurityLevel = allowedSecurityLevel;
107 realm.PopulationLevel = population;
108
109 if (!realm.ExternalAddress || *realm.ExternalAddress != address)
110 {
111 realm.ExternalAddress = std::make_unique<boost::asio::ip::address>(std::move(address));
112 }
113
114 if (!realm.LocalAddress || *realm.LocalAddress != localAddr)
115 {
116 realm.LocalAddress = std::make_unique<boost::asio::ip::address>(std::move(localAddr));
117 }
118
119 if (!realm.LocalSubnetMask || *realm.LocalSubnetMask != localSubmask)
120 {
121 realm.LocalSubnetMask = std::make_unique<boost::asio::ip::address>(std::move(localSubmask));
122 }
123
124 realm.Port = port;
125}
Realm realm
Definition: World.cpp:112
Definition: Realm.h:67
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
RealmHandle Id
Definition: Realm.h:68
uint8 Type
Definition: Realm.h:75

References _realms, Realm::AllowedSecurityLevel, Realm::Build, Realm::ExternalAddress, Realm::Flags, Realm::Id, Realm::LocalAddress, Realm::LocalSubnetMask, Realm::Name, Realm::PopulationLevel, Realm::Port, realm, Realm::Timezone, and Realm::Type.

Referenced by UpdateRealms().

◆ UpdateRealms()

void RealmList::UpdateRealms ( boost::system::error_code const &  error)
private
128{
129 if (error)
130 {
131 // Skip update if have errors
132 return;
133 }
134
135 LOG_DEBUG("server.authserver", "Updating Realm List...");
136
138 PreparedQueryResult result = LoginDatabase.Query(stmt);
139
140 std::map<RealmHandle, std::string> existingRealms;
141 for (auto const& [handle, realm] : _realms)
142 {
143 existingRealms[handle] = realm.Name;
144 }
145
146 _realms.clear();
147
148 // Circle through results and add them to the realm map
149 if (result)
150 {
151 for (auto const& fields : *result)
152 {
153 try
154 {
155 uint32 realmId = fields[0].Get<uint32>();
156 std::string name = fields[1].Get<std::string>();
157 std::string externalAddressString = fields[2].Get<std::string>();
158 std::string localAddressString = fields[3].Get<std::string>();
159 std::string localSubmaskString = fields[4].Get<std::string>();
160 uint16 port = fields[5].Get<uint16>();
161
162 Optional<boost::asio::ip::tcp::endpoint> externalAddress = _resolver->Resolve(boost::asio::ip::tcp::v4(), externalAddressString, "");
163 if (!externalAddress)
164 {
165 LOG_ERROR("server.authserver", "Could not resolve address {} for realm \"{}\" id {}", externalAddressString, name, realmId);
166 continue;
167 }
168
169 Optional<boost::asio::ip::tcp::endpoint> localAddress = _resolver->Resolve(boost::asio::ip::tcp::v4(), localAddressString, "");
170 if (!localAddress)
171 {
172 LOG_ERROR("server.authserver", "Could not resolve localAddress {} for realm \"{}\" id {}", localAddressString, name, realmId);
173 continue;
174 }
175
176 Optional<boost::asio::ip::tcp::endpoint> localSubmask = _resolver->Resolve(boost::asio::ip::tcp::v4(), localSubmaskString, "");
177 if (!localSubmask)
178 {
179 LOG_ERROR("server.authserver", "Could not resolve localSubnetMask {} for realm \"{}\" id {}", localSubmaskString, name, realmId);
180 continue;
181 }
182
183 uint8 icon = fields[6].Get<uint8>();
184
185 if (icon == REALM_TYPE_FFA_PVP)
186 {
187 icon = REALM_TYPE_PVP;
188 }
189
190 if (icon >= MAX_CLIENT_REALM_TYPE)
191 {
192 icon = REALM_TYPE_NORMAL;
193 }
194
195 RealmFlags flag = RealmFlags(fields[7].Get<uint8>());
196 uint8 timezone = fields[8].Get<uint8>();
197 uint8 allowedSecurityLevel = fields[9].Get<uint8>();
198 float pop = fields[10].Get<float>();
199 uint32 build = fields[11].Get<uint32>();
200
201 RealmHandle id{ realmId };
202
203 UpdateRealm(id, build, name, externalAddress->address(), localAddress->address(), localSubmask->address(), port, icon, flag,
204 timezone, (allowedSecurityLevel <= SEC_ADMINISTRATOR ? AccountTypes(allowedSecurityLevel) : SEC_ADMINISTRATOR), pop);
205
206 if (!existingRealms.count(id))
207 {
208 LOG_INFO("server.authserver", "Added realm \"{}\" at {}:{}.", name, externalAddressString, port);
209 }
210 else
211 {
212 LOG_DEBUG("server.authserver", "Updating realm \"{}\" at {}:{}.", name, externalAddressString, port);
213 }
214
215 existingRealms.erase(id);
216 }
217 catch (std::exception const& ex)
218 {
219 LOG_ERROR("server.authserver", "Realmlist::UpdateRealms has thrown an exception: {}", ex.what());
220 ABORT();
221 }
222 }
223 }
224
225 for (auto itr = existingRealms.begin(); itr != existingRealms.end(); ++itr)
226 LOG_INFO("server.authserver", "Removed realm \"{}\".", itr->second);
227
228 if (_updateInterval)
229 {
230 _updateTimer->expires_from_now(boost::posix_time::seconds(_updateInterval));
231 _updateTimer->async_wait(std::bind(&RealmList::UpdateRealms, this, std::placeholders::_1));
232 }
233}
AccountTypes
Definition: Common.h:58
@ SEC_ADMINISTRATOR
Definition: Common.h:62
#define ABORT
Definition: Errors.h:76
std::uint8_t uint8
Definition: Define.h:110
std::uint16_t uint16
Definition: Define.h:109
#define LOG_INFO(filterType__,...)
Definition: Log.h:165
#define LOG_ERROR(filterType__,...)
Definition: Log.h:157
#define LOG_DEBUG(filterType__,...)
Definition: Log.h:169
std::optional< T > Optional
Optional helper class to wrap optional values within.
Definition: Optional.h:24
std::shared_ptr< PreparedResultSet > PreparedQueryResult
Definition: DatabaseEnvFwd.h:46
@ LOGIN_SEL_REALMLIST
Definition: LoginDatabase.h:31
RealmFlags
Definition: Realm.h:25
@ REALM_TYPE_FFA_PVP
Definition: Realm.h:61
@ MAX_CLIENT_REALM_TYPE
Definition: Realm.h:59
@ REALM_TYPE_PVP
Definition: Realm.h:54
@ REALM_TYPE_NORMAL
Definition: Realm.h:53
Definition: PreparedStatement.h:158
Definition: Realm.h:38
void UpdateRealm(RealmHandle const &id, uint32 build, std::string const &name, boost::asio::ip::address &&address, boost::asio::ip::address &&localAddr, boost::asio::ip::address &&localSubmask, uint16 port, uint8 icon, RealmFlags flag, uint8 timezone, AccountTypes allowedSecurityLevel, float population)
Definition: RealmList.cpp:93

References _realms, _resolver, _updateInterval, _updateTimer, ABORT, LOG_DEBUG, LOG_ERROR, LOG_INFO, LOGIN_SEL_REALMLIST, LoginDatabase, MAX_CLIENT_REALM_TYPE, Realm::Name, realm, REALM_TYPE_FFA_PVP, REALM_TYPE_NORMAL, REALM_TYPE_PVP, SEC_ADMINISTRATOR, UpdateRealm(), and UpdateRealms().

Referenced by Initialize(), and UpdateRealms().

Member Data Documentation

◆ _builds

std::vector<RealmBuildInfo> RealmList::_builds
private

Referenced by GetBuildInfo(), and LoadBuildInfo().

◆ _realms

RealmMap RealmList::_realms
private

Referenced by GetRealm(), UpdateRealm(), and UpdateRealms().

◆ _resolver

std::unique_ptr<Acore::Asio::Resolver> RealmList::_resolver
private

Referenced by Initialize(), and UpdateRealms().

◆ _updateInterval

uint32 RealmList::_updateInterval {0}
private

Referenced by Initialize(), and UpdateRealms().

◆ _updateTimer

std::unique_ptr<Acore::Asio::DeadlineTimer> RealmList::_updateTimer
private

Referenced by Close(), Initialize(), and UpdateRealms().