Skip to content

Commit

Permalink
Core/Authserver: Partial port of 56cf7ff
Browse files Browse the repository at this point in the history
Change the "UpdateIfNeed" logic to a deadline_timer
  • Loading branch information
DDuarte committed Mar 6, 2016
1 parent 2ea3102 commit 6cd63ca
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 29 deletions.
2 changes: 2 additions & 0 deletions src/server/authserver/Main.cpp
Expand Up @@ -194,6 +194,8 @@ int main(int argc, char** argv)

sAuthSocketMgr.StopNetwork();

sRealmList->Close();

// Close the Database Pool and library
StopDB();

Expand Down
38 changes: 19 additions & 19 deletions src/server/authserver/Realms/RealmList.cpp
Expand Up @@ -23,20 +23,27 @@

namespace boost { namespace asio { namespace ip { class address; } } }

RealmList::RealmList() : _updateInterval(0), _nextUpdateTime(time(NULL)), _resolver(nullptr) { }
RealmList::RealmList() : _updateInterval(0), _updateTimer(nullptr), _resolver(nullptr) { }
RealmList::~RealmList()
{
delete _resolver;
delete _updateTimer;
}

// Load the realm list from the database
void RealmList::Initialize(boost::asio::io_service& ioService, uint32 updateInterval)
{
_resolver = new boost::asio::ip::tcp::resolver(ioService);
_updateInterval = updateInterval;
_updateTimer = new boost::asio::deadline_timer(ioService);
_resolver = new boost::asio::ip::tcp::resolver(ioService);

// Get the content of the realmlist table in the database
UpdateRealms(true);
UpdateRealms(boost::system::error_code());
}

void RealmList::Close()
{
_updateTimer->cancel();
}

void RealmList::UpdateRealm(RealmHandle const& id, uint32 build, const std::string& name, ip::address const& address, ip::address const& localAddr,
Expand All @@ -60,23 +67,11 @@ void RealmList::UpdateRealm(RealmHandle const& id, uint32 build, const std::stri
realm.Port = port;
}

void RealmList::UpdateIfNeed()
void RealmList::UpdateRealms(boost::system::error_code const& error)
{
// maybe disabled or updated recently
if (!_updateInterval || _nextUpdateTime > time(NULL))
if (error)
return;

_nextUpdateTime = time(NULL) + _updateInterval;

// Clears Realm list
_realms.clear();

// Get the content of the realmlist table in the database
UpdateRealms();
}

void RealmList::UpdateRealms(bool init)
{
TC_LOG_INFO("server.authserver", "Updating Realm List...");

PreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_SEL_REALMLIST);
Expand Down Expand Up @@ -143,8 +138,7 @@ void RealmList::UpdateRealms(bool init)
UpdateRealm(id, build, name, externalAddress, localAddress, localSubmask, port, icon, flag,
timezone, (allowedSecurityLevel <= SEC_ADMINISTRATOR ? AccountTypes(allowedSecurityLevel) : SEC_ADMINISTRATOR), pop);

if (init)
TC_LOG_INFO("server.authserver", "Added realm \"%s\" at %s:%u.", name.c_str(), externalAddress.to_string().c_str(), port);
TC_LOG_INFO("server.authserver", "Added realm \"%s\" at %s:%u.", name.c_str(), externalAddress.to_string().c_str(), port);
}
catch (std::exception& ex)
{
Expand All @@ -154,6 +148,12 @@ void RealmList::UpdateRealms(bool init)
}
while (result->NextRow());
}

if (_updateInterval)
{
_updateTimer->expires_from_now(boost::posix_time::seconds(_updateInterval));
_updateTimer->async_wait(std::bind(&RealmList::UpdateRealms, this, std::placeholders::_1));
}
}

Realm const* RealmList::GetRealm(RealmHandle const& id) const
Expand Down
12 changes: 5 additions & 7 deletions src/server/authserver/Realms/RealmList.h
Expand Up @@ -24,6 +24,7 @@
#include <boost/asio/ip/address.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/io_service.hpp>
#include <boost/asio/deadline_timer.hpp>

using namespace boost::asio;

Expand All @@ -42,24 +43,21 @@ class RealmList
~RealmList();

void Initialize(boost::asio::io_service& ioService, uint32 updateInterval);

void UpdateIfNeed();

void AddRealm(const Realm& NewRealm) { _realms[NewRealm.Id] = NewRealm; }
void Close();

RealmMap const& GetRealms() const { return _realms; }
Realm const* GetRealm(RealmHandle const& id) const;

private:
RealmList();

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

RealmMap _realms;
uint32 _updateInterval;
time_t _nextUpdateTime;
uint32 _updateInterval;
boost::asio::deadline_timer* _updateTimer;
boost::asio::ip::tcp::resolver* _resolver;
};

Expand Down
3 changes: 0 additions & 3 deletions src/server/authserver/Server/AuthSession.cpp
Expand Up @@ -880,9 +880,6 @@ void AuthSession::RealmListCallback(PreparedQueryResult result)
} while (result->NextRow());
}

// Update realm list if need
sRealmList->UpdateIfNeed();

// Circle through realms in the RealmList and construct the return packet (including # of user characters in each realm)
ByteBuffer pkt;

Expand Down

0 comments on commit 6cd63ca

Please sign in to comment.