aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShauren <shauren.trinity@gmail.com>2016-03-26 13:39:46 +0100
committerShauren <shauren.trinity@gmail.com>2016-04-09 12:30:00 +0200
commit778f1725f8b085161d28a48806b1d2ec56a53e6b (patch)
tree41219161647fee7bb7c63089a3b91c7e3fc688fe
parent3f7e6af49ede1a352b20bc02f59db1ac42a393ad (diff)
Core/Misc: Moved RealmList to shared
(cherry picked from commit cfe8a6a58b37280e4ed922e4d70878579dbc8bee)
-rw-r--r--src/server/authserver/Server/AuthSession.cpp2
-rw-r--r--src/server/shared/Realm/Realm.cpp35
-rw-r--r--src/server/shared/Realm/Realm.h24
-rw-r--r--src/server/shared/Realm/RealmList.cpp (renamed from src/server/authserver/Realms/RealmList.cpp)52
-rw-r--r--src/server/shared/Realm/RealmList.h (renamed from src/server/authserver/Realms/RealmList.h)12
5 files changed, 88 insertions, 37 deletions
diff --git a/src/server/authserver/Server/AuthSession.cpp b/src/server/authserver/Server/AuthSession.cpp
index 982aca58eee..f044e0cea94 100644
--- a/src/server/authserver/Server/AuthSession.cpp
+++ b/src/server/authserver/Server/AuthSession.cpp
@@ -924,7 +924,7 @@ void AuthSession::RealmListCallback(PreparedQueryResult result)
pkt << uint8(lock); // if 1, then realm locked
pkt << uint8(flag); // RealmFlags
pkt << name;
- pkt << boost::lexical_cast<std::string>(GetAddressForClient(realm, GetRemoteIpAddress()));
+ pkt << boost::lexical_cast<std::string>(realm.GetAddressForClient(GetRemoteIpAddress()));
pkt << float(realm.PopulationLevel);
pkt << uint8(characterCounts[realm.Id.Realm]);
pkt << uint8(realm.Timezone); // realm category
diff --git a/src/server/shared/Realm/Realm.cpp b/src/server/shared/Realm/Realm.cpp
index 0c8f4d1d492..11c52f281a9 100644
--- a/src/server/shared/Realm/Realm.cpp
+++ b/src/server/shared/Realm/Realm.cpp
@@ -16,3 +16,38 @@
*/
#include "Realm.h"
+
+ip::tcp::endpoint Realm::GetAddressForClient(ip::address const& clientAddr) const
+{
+ ip::address realmIp;
+
+ // Attempt to send best address for client
+ if (clientAddr.is_loopback())
+ {
+ // Try guessing if realm is also connected locally
+ if (LocalAddress.is_loopback() || ExternalAddress.is_loopback())
+ realmIp = clientAddr;
+ else
+ {
+ // Assume that user connecting from the machine that bnetserver is located on
+ // has all realms available in his local network
+ realmIp = LocalAddress;
+ }
+ }
+ else
+ {
+ if (clientAddr.is_v4() &&
+ (clientAddr.to_v4().to_ulong() & LocalSubnetMask.to_v4().to_ulong()) ==
+ (LocalAddress.to_v4().to_ulong() & LocalSubnetMask.to_v4().to_ulong()))
+ {
+ realmIp = LocalAddress;
+ }
+ else
+ realmIp = ExternalAddress;
+ }
+
+ ip::tcp::endpoint endpoint(realmIp, Port);
+
+ // Return external IP
+ return endpoint;
+}
diff --git a/src/server/shared/Realm/Realm.h b/src/server/shared/Realm/Realm.h
index 83a344dd817..241ccd2bca8 100644
--- a/src/server/shared/Realm/Realm.h
+++ b/src/server/shared/Realm/Realm.h
@@ -37,7 +37,7 @@ enum RealmFlags
REALM_FLAG_FULL = 0x80
};
-struct RealmHandle
+struct TC_SHARED_API RealmHandle
{
RealmHandle() : Realm(0) { }
RealmHandle(uint32 index) : Realm(index) { }
@@ -53,20 +53,20 @@ struct RealmHandle
/// Type of server, this is values from second column of Cfg_Configs.dbc
enum RealmType
{
- REALM_TYPE_NORMAL = 0,
- REALM_TYPE_PVP = 1,
- REALM_TYPE_NORMAL2 = 4,
- REALM_TYPE_RP = 6,
- REALM_TYPE_RPPVP = 8,
+ REALM_TYPE_NORMAL = 0,
+ REALM_TYPE_PVP = 1,
+ REALM_TYPE_NORMAL2 = 4,
+ REALM_TYPE_RP = 6,
+ REALM_TYPE_RPPVP = 8,
- MAX_CLIENT_REALM_TYPE = 14,
+ MAX_CLIENT_REALM_TYPE = 14,
- REALM_TYPE_FFA_PVP = 16 // custom, free for all pvp mode like arena PvP in all zones except rest activated places and sanctuaries
- // replaced by REALM_PVP in realm list
+ REALM_TYPE_FFA_PVP = 16 // custom, free for all pvp mode like arena PvP in all zones except rest activated places and sanctuaries
+ // replaced by REALM_PVP in realm list
};
// Storage object for a realm
-struct Realm
+struct TC_SHARED_API Realm
{
RealmHandle Id;
uint32 Build;
@@ -75,11 +75,13 @@ struct Realm
ip::address LocalSubnetMask;
uint16 Port;
std::string Name;
- uint8 Type; // icon
+ uint8 Type;
RealmFlags Flags;
uint8 Timezone;
AccountTypes AllowedSecurityLevel;
float PopulationLevel;
+
+ ip::tcp::endpoint GetAddressForClient(ip::address const& clientAddr) const;
};
#endif // Realm_h__
diff --git a/src/server/authserver/Realms/RealmList.cpp b/src/server/shared/Realm/RealmList.cpp
index f1b25d8554d..e941800cd76 100644
--- a/src/server/authserver/Realms/RealmList.cpp
+++ b/src/server/shared/Realm/RealmList.cpp
@@ -16,20 +16,25 @@
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-#include "Common.h"
-#include "Database/DatabaseEnv.h"
#include "RealmList.h"
-#include <boost/asio/ip/tcp.hpp>
+#include "Database/DatabaseEnv.h"
+#include "Util.h"
-namespace boost { namespace asio { namespace ip { class address; } } }
+RealmList::RealmList() : _updateInterval(0), _updateTimer(nullptr), _resolver(nullptr)
+{
+}
-RealmList::RealmList() : _updateInterval(0), _updateTimer(nullptr), _resolver(nullptr) { }
RealmList::~RealmList()
{
- delete _resolver;
delete _updateTimer;
}
+RealmList* RealmList::Instance()
+{
+ static RealmList instance;
+ return &instance;
+}
+
// Load the realm list from the database
void RealmList::Initialize(boost::asio::io_service& ioService, uint32 updateInterval)
{
@@ -38,7 +43,7 @@ void RealmList::Initialize(boost::asio::io_service& ioService, uint32 updateInte
_resolver = new boost::asio::ip::tcp::resolver(ioService);
// Get the content of the realmlist table in the database
- UpdateRealms(true, boost::system::error_code());
+ UpdateRealms(boost::system::error_code());
}
void RealmList::Close()
@@ -67,16 +72,22 @@ void RealmList::UpdateRealm(RealmHandle const& id, uint32 build, const std::stri
realm.Port = port;
}
-void RealmList::UpdateRealms(bool init, boost::system::error_code const& error)
+void RealmList::UpdateRealms(boost::system::error_code const& error)
{
if (error)
return;
- TC_LOG_INFO("server.authserver", "Updating Realm List...");
+ TC_LOG_DEBUG("server.authserver", "Updating Realm List...");
PreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_SEL_REALMLIST);
PreparedQueryResult result = LoginDatabase.Query(stmt);
+ std::map<RealmHandle, std::string> existingRealms;
+ for (auto const& p : _realms)
+ existingRealms[p.first] = p.second.Name;
+
+ _realms.clear();
+
// Circle through results and add them to the realm map
if (result)
{
@@ -95,8 +106,8 @@ void RealmList::UpdateRealms(bool init, boost::system::error_code const& error)
boost::asio::ip::tcp::resolver::iterator endPoint = _resolver->resolve(externalAddressQuery, ec);
if (endPoint == end || ec)
{
- TC_LOG_ERROR("server.authserver", "Could not resolve address %s", fields[2].GetString().c_str());
- return;
+ TC_LOG_ERROR("server.authserver", "Could not resolve address %s for realm \"%s\" id %u", fields[2].GetString().c_str(), name.c_str(), realmId);
+ continue;
}
ip::address externalAddress = (*endPoint).endpoint().address();
@@ -105,8 +116,8 @@ void RealmList::UpdateRealms(bool init, boost::system::error_code const& error)
endPoint = _resolver->resolve(localAddressQuery, ec);
if (endPoint == end || ec)
{
- TC_LOG_ERROR("server.authserver", "Could not resolve address %s", fields[3].GetString().c_str());
- return;
+ TC_LOG_ERROR("server.authserver", "Could not resolve localAddress %s for realm \"%s\" id %u", fields[3].GetString().c_str(), name.c_str(), realmId);
+ continue;
}
ip::address localAddress = (*endPoint).endpoint().address();
@@ -115,8 +126,8 @@ void RealmList::UpdateRealms(bool init, boost::system::error_code const& error)
endPoint = _resolver->resolve(localSubmaskQuery, ec);
if (endPoint == end || ec)
{
- TC_LOG_ERROR("server.authserver", "Could not resolve address %s", fields[4].GetString().c_str());
- return;
+ TC_LOG_ERROR("server.authserver", "Could not resolve localSubnetMask %s for realm \"%s\" id %u", fields[4].GetString().c_str(), name.c_str(), realmId);
+ continue;
}
ip::address localSubmask = (*endPoint).endpoint().address();
@@ -138,8 +149,12 @@ void RealmList::UpdateRealms(bool init, boost::system::error_code const& error)
UpdateRealm(id, build, name, externalAddress, localAddress, localSubmask, port, icon, flag,
timezone, (allowedSecurityLevel <= SEC_ADMINISTRATOR ? AccountTypes(allowedSecurityLevel) : SEC_ADMINISTRATOR), pop);
- if (init)
+ if (!existingRealms.count(id))
TC_LOG_INFO("server.authserver", "Added realm \"%s\" at %s:%u.", name.c_str(), externalAddress.to_string().c_str(), port);
+ else
+ TC_LOG_DEBUG("server.authserver", "Updating realm \"%s\" at %s:%u.", name.c_str(), externalAddress.to_string().c_str(), port);
+
+ existingRealms.erase(id);
}
catch (std::exception& ex)
{
@@ -150,10 +165,13 @@ void RealmList::UpdateRealms(bool init, boost::system::error_code const& error)
while (result->NextRow());
}
+ for (auto itr = existingRealms.begin(); itr != existingRealms.end(); ++itr)
+ TC_LOG_INFO("server.authserver", "Removed realm \"%s\".", itr->second.c_str());
+
if (_updateInterval)
{
_updateTimer->expires_from_now(boost::posix_time::seconds(_updateInterval));
- _updateTimer->async_wait(std::bind(&RealmList::UpdateRealms, this, false, std::placeholders::_1));
+ _updateTimer->async_wait(std::bind(&RealmList::UpdateRealms, this, std::placeholders::_1));
}
}
diff --git a/src/server/authserver/Realms/RealmList.h b/src/server/shared/Realm/RealmList.h
index e35975b215a..3b81337e762 100644
--- a/src/server/authserver/Realms/RealmList.h
+++ b/src/server/shared/Realm/RealmList.h
@@ -29,16 +29,12 @@
using namespace boost::asio;
/// Storage object for the list of realms on the server
-class RealmList
+class TC_SHARED_API RealmList
{
public:
typedef std::map<RealmHandle, Realm> RealmMap;
- static RealmList* instance()
- {
- static RealmList instance;
- return &instance;
- }
+ static RealmList* Instance();
~RealmList();
@@ -51,7 +47,7 @@ public:
private:
RealmList();
- void UpdateRealms(bool init, boost::system::error_code const& error);
+ 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);
@@ -61,5 +57,5 @@ private:
boost::asio::ip::tcp::resolver* _resolver;
};
-#define sRealmList RealmList::instance()
+#define sRealmList RealmList::Instance()
#endif