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 realmTimezone, 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
28: _updateInterval(0) { }
uint32 _updateInterval
Definition: RealmList.h:72

◆ ~RealmList()

RealmList::~RealmList ( )
privatedefault

Member Function Documentation

◆ Close()

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

References _updateTimer.

◆ GetBuildInfo()

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

References _builds.

◆ GetRealm()

Realm const * RealmList::GetRealm ( RealmHandle const &  id) const
237{
238 auto itr = _realms.find(id);
239 if (itr != _realms.end())
240 {
241 return &itr->second;
242 }
243
244 return nullptr;
245}
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 
)
38{
39 _updateInterval = updateInterval;
40 _updateTimer = std::make_unique<Acore::Asio::DeadlineTimer>(ioContext);
41 _resolver = std::make_unique<Acore::Asio::Resolver>(ioContext);
42
44
45 // Get the content of the realmlist table in the database
46 UpdateRealms(boost::system::error_code());
47}
void UpdateRealms(boost::system::error_code const &error)
Definition: RealmList.cpp:128
std::unique_ptr< Acore::Asio::Resolver > _resolver
Definition: RealmList.h:74
void LoadBuildInfo()
Definition: RealmList.cpp:54

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

◆ Instance()

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

◆ LoadBuildInfo()

void RealmList::LoadBuildInfo ( )
private
55{
56 // 0 1 2 3 4 5 6
57 if (auto result = LoginDatabase.Query("SELECT majorVersion, minorVersion, bugfixVersion, hotfixVersion, build, winChecksumSeed, macChecksumSeed FROM build_info ORDER BY build ASC"))
58 {
59 for (auto const& fields : *result)
60 {
61 RealmBuildInfo& build = _builds.emplace_back();
62 build.MajorVersion = fields[0].Get<uint32>();
63 build.MinorVersion = fields[1].Get<uint32>();
64 build.BugfixVersion = fields[2].Get<uint32>();
65 std::string hotfixVersion = fields[3].Get<std::string>();
66
67 if (hotfixVersion.length() < build.HotfixVersion.size())
68 {
69 std::copy(hotfixVersion.begin(), hotfixVersion.end(), build.HotfixVersion.begin());
70 }
71 else
72 {
73 std::fill(hotfixVersion.begin(), hotfixVersion.end(), '\0');
74 }
75
76 build.Build = fields[4].Get<uint32>();
77 std::string windowsHash = fields[5].Get<std::string>();
78
79 if (windowsHash.length() == build.WindowsHash.size() * 2)
80 {
81 HexStrToByteArray(windowsHash, build.WindowsHash);
82 }
83
84 std::string macHash = fields[6].Get<std::string>();
85
86 if (macHash.length() == build.MacHash.size() * 2)
87 {
88 HexStrToByteArray(macHash, build.MacHash);
89 }
90 }
91 }
92}
void HexStrToByteArray(std::string_view str, std::array< uint8, Size > &buf, bool reverse=false)
Definition: Util.h:387
std::uint32_t uint32
Definition: Define.h:107
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  realmTimezone,
AccountTypes  allowedSecurityLevel,
float  population 
)
private
97{
98 // Create new if not exist or update existed
99 Realm& realm = _realms[id];
100
101 realm.Id = id;
102 realm.Build = build;
103 realm.Name = name;
104 realm.Type = icon;
105 realm.Flags = flag;
106 realm.Timezone = realmTimezone;
107 realm.AllowedSecurityLevel = allowedSecurityLevel;
108 realm.PopulationLevel = population;
109
110 if (!realm.ExternalAddress || *realm.ExternalAddress != address)
111 {
112 realm.ExternalAddress = std::make_unique<boost::asio::ip::address>(std::move(address));
113 }
114
115 if (!realm.LocalAddress || *realm.LocalAddress != localAddr)
116 {
117 realm.LocalAddress = std::make_unique<boost::asio::ip::address>(std::move(localAddr));
118 }
119
120 if (!realm.LocalSubnetMask || *realm.LocalSubnetMask != localSubmask)
121 {
122 realm.LocalSubnetMask = std::make_unique<boost::asio::ip::address>(std::move(localSubmask));
123 }
124
125 realm.Port = port;
126}
Realm realm
Definition: World.cpp:112
Definition: Realm.h:68
uint16 Port
Definition: Realm.h:74
RealmFlags Flags
Definition: Realm.h:77
AccountTypes AllowedSecurityLevel
Definition: Realm.h:79
uint8 Timezone
Definition: Realm.h:78
std::unique_ptr< boost::asio::ip::address > LocalSubnetMask
Definition: Realm.h:73
std::unique_ptr< boost::asio::ip::address > LocalAddress
Definition: Realm.h:72
float PopulationLevel
Definition: Realm.h:80
uint32 Build
Definition: Realm.h:70
std::unique_ptr< boost::asio::ip::address > ExternalAddress
Definition: Realm.h:71
std::string Name
Definition: Realm.h:75
RealmHandle Id
Definition: Realm.h:69
uint8 Type
Definition: Realm.h:76

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

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().