Skip to content

Commit

Permalink
Drop ip/port field from realmlist.
Browse files Browse the repository at this point in the history
These not needed because proxyds broadcast their address via EC.
Also if there is no proxy for a realm then mark it offline.
Finally some logging thingie.

Signed-off-by: Anubisss <anubisss210@gmail.com>
  • Loading branch information
Anubisss committed Jun 9, 2012
1 parent c9f9970 commit 851c9f7
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 88 deletions.
2 changes: 0 additions & 2 deletions main/morpheus.conf.dist
Expand Up @@ -15,8 +15,6 @@ WrongPassBan = 1
WrongPassAmnt = 3
#ban type, ip or account
WrongPassBanType = ip
#Update interval for realmd's list of realms, in minutes.
UpdateInterval = 5

[proxyd]
#this address is sent to clients, so it CANNOT BE "0.0.0.0"
Expand Down
2 changes: 1 addition & 1 deletion realmd/Realm_Database.cpp
Expand Up @@ -63,7 +63,7 @@ bool RealmDatabaseConnection::open(const std::string& driver, const std::string&
ADD_STMT(REALMD_DB_CHECK_ACCT_BAN, "SELECT UPPER(a.sha_pass_hash) , a.id, a.locked, a.last_ip, ab.active, a.failed_logins FROM account AS a LEFT OUTER JOIN account_banned AS ab ON ab.id = a.id AND ab.active = 1 WHERE a.username = ?");
ADD_STMT(REALMD_DB_SET_S_V, "UPDATE account SET v = ?, s = ? WHERE username = ?");
ADD_STMT(REALMD_DB_UPDATE_ACCOUNT, "UPDATE account SET sessionkey = ?, last_ip = ?, last_login = NOW(), locale = ?, failed_logins = 0 WHERE username = ?");
ADD_STMT(REALMD_DB_GET_REALMLIST, "SELECT id, name, address, port, icon, color, timezone, allowedSecurityLevel, population, gamebuild FROM realmlist WHERE color <>3 ORDER BY id");
ADD_STMT(REALMD_DB_GET_REALMLIST, "SELECT id, name, icon, color, timezone, allowedSecurityLevel, population, gamebuild FROM realmlist WHERE color <> 3 ORDER BY id");
ADD_STMT(REALMD_DB_GET_NUMCHAR, "SELECT realmid, numchars FROM realmcharacters WHERE acctid = ?");
ADD_STMT(REALMD_DB_UPDATE_ACCOUNT,"UPDATE account SET sessionkey = ?, last_ip = ?, last_login = NOW(), locale = ?, failed_logins = 0 WHERE username = ?");
ADD_STMT(REALMD_DB_FIX_SV, "UPDATE account set s ='', v ='' where username = ?");
Expand Down
66 changes: 29 additions & 37 deletions realmd/Realm_Service.cpp
Expand Up @@ -65,8 +65,6 @@ void Realm_Service::start()
delete acceptor;
return;
}

ACE_Time_Value tm(1 * 60 * sConfig->getInt("realmd","UpdateInterval"));

this->database = new RealmDB(sConfig->getInt("realmd", "DBThreads"));
this->database->open(sConfig->getString("realmd", "DBengine"),sConfig->getString("realmd", "DBUrl") );
Expand Down Expand Up @@ -98,7 +96,6 @@ void Realm_Service::start()
this->is_running = true;
this->activate(THR_NEW_LWP | THR_JOINABLE, sConfig->getInt("realmd", "NetThreads"));
this->database->get_realmlist();
this->reactor->schedule_timer(new Realm_Timer(), 0, tm, tm);
this->reactor->schedule_timer(new Unban_Timer(), 0, ACE_Time_Value(1), ACE_Time_Value(60));
REALM_LOG("Started\n");
ACE_Thread_Manager::instance()->wait();
Expand All @@ -116,26 +113,17 @@ void Realm_Service::update_realms(Morpheus::SQL::ResultSet* res)
Realm rlm;
uint32 id;
while (res->next()) {
std::ostringstream oss;
oss.clear();
rlm.name = res->getString(2);
oss << res->getString(3) << ":" << res->getUint16(4);
rlm.address = oss.str();
rlm.icon = res->getUint8(5);
rlm.color = res->getUint8(6);
rlm.timezone = res->getUint8(7);
rlm.allowedSecurityLevel = res->getUint8(8);
rlm.population = res->getFloat(9);
rlm.build = res->getUint16(10);
rlm.name = res->getString(2);
rlm.icon = res->getUint8(3);
rlm.color = res->getUint8(4);
rlm.timezone = res->getUint8(5);
rlm.allowedSecurityLevel = res->getUint8(6);
rlm.population = res->getFloat(7);
rlm.build = res->getUint16(8);
id = res->getUint32(1);
this->realm_map[id] = rlm;

if (!rlm.address.compare(":0")) {
if(this->proxies.find(id) == proxies.end())
this->event_channel->request_proxies_for_realm(id);
}

REALM_LOG("Added realm %s (ID: %u) %f\n",rlm.name.c_str(), res->getUint8(1), rlm.population);
REALM_LOG("Added realm %s (ID: %u) %f\n", rlm.name.c_str(), id, rlm.population);
}
}

Expand Down Expand Up @@ -168,10 +156,6 @@ void Realm_Service::add_proxy(uint8 realm, std::string ip, float load)
REALM_TRACE;
if (realm_map.find(realm) == realm_map.end())
return;
if (!realm_map[realm].address.compare(":0")) {
REALM_LOG("Received incorrect Proxy Server (%s) for realm with TC1/TC2 gameserver!\n", ip.c_str());
return;
}

std::pair<std::multimap<uint8, Proxy_Info>::iterator,
std::multimap<uint8, Proxy_Info>::iterator> ret; //Fuck you stl.
Expand Down Expand Up @@ -210,19 +194,27 @@ void Realm_Service::add_proxy_load_report(std::string ip, float load)

std::string Realm_Service::get_proxy_for_realm(uint8 id)
{
REALM_TRACE;
std::multimap<uint8, Proxy_Info>::iterator ret, itr;
std::pair< std::multimap<uint8, Proxy_Info>::iterator,
std::multimap<uint8, Proxy_Info>::iterator > proxies_for_realm;

proxies_for_realm = this->proxies.equal_range(id);
ret = proxies_for_realm.first;
for (itr = ret; itr != proxies_for_realm.second; itr++) {
if (itr->second.load < ret->second.load)
ret = itr;
}

return ret->second.ip;
// get valid proxys for the realm id
std::list<Proxy_Info> proxies_for_realm;
for (std::multimap<uint8, Proxy_Info>::const_iterator itr = proxies.begin();
itr != proxies.end();
++itr)
if (itr->first == id)
proxies_for_realm.push_back(itr->second);

// there's no proxy for that realm
if (proxies_for_realm.empty())
return "";

// select the most lowest load
std::list<Proxy_Info>::const_iterator lowestLoad = proxies_for_realm.begin();
for (std::list<Proxy_Info>::const_iterator itr = lowestLoad;
itr != proxies_for_realm.end();
++itr)
if (itr->load < lowestLoad->load)
lowestLoad = itr;

return lowestLoad->ip;
}

};
Expand Down
7 changes: 6 additions & 1 deletion realmd/Realm_Service.h
Expand Up @@ -64,7 +64,6 @@ class EC_Communicator;
struct Realm
{
std::string name;
std::string address;
uint8 icon;
uint8 color;
uint8 timezone;
Expand Down Expand Up @@ -126,6 +125,12 @@ class Realm_Service : public ACE_Task_Base

void add_proxy_load_report(std::string ip, float load);

/**
* @brief Get a proxy ip for a specific realm.
* @details Select the most lowest loaded proxy and
* return with its address.
* Return empty string if there is no proxy for that realm.
*/
std::string get_proxy_for_realm(uint8 id);

private:
Expand Down
46 changes: 28 additions & 18 deletions realmd/Realm_Socket.cpp
Expand Up @@ -574,14 +574,13 @@ ByteBuffer* Realm_Socket::build_realm_packet()
if (i->second.build != this->client_build)
continue;

std::string address = sRealm->get_proxy_for_realm(i->first);

*pkt << (uint8) i->second.icon;
*pkt << (uint8) i->second.color;
*pkt << uint8(address == "" ? 0x02 : i->second.color); // if no proxy then mark the realm is offline
*pkt << i->second.name;

if (i->second.address == ":")
*pkt << sRealm->get_proxy_for_realm(i->first);
else
*pkt << i->second.address;

*pkt << address;

*pkt << (float)i->second.population;

Expand All @@ -592,6 +591,14 @@ ByteBuffer* Realm_Socket::build_realm_packet()

*pkt << (uint8) i->second.timezone;
*pkt << (uint8) 0x00;

#ifdef _MORPHEUS_DEBUG
if (address == "")
REALM_LOG("No proxy for realm: %s (%u)\n", i->second.name.c_str(), i->first);
else
REALM_LOG("Selected proxy for realm: %s (%u) | %s\n", i->second.name.c_str(), i->first, address.c_str());
#endif

}
}

Expand Down Expand Up @@ -624,27 +631,30 @@ ByteBuffer* Realm_Socket::build_expansion_realm_packet()
*pkt << (uint16) listSize;

if (listSize > 0) {
for (i = realmlist->begin(); i != realmlist->end(); i++) {
for (i = realmlist->begin(); i != realmlist->end(); i++)
{
if (i->second.build != this->client_build)
continue;


std::string address = sRealm->get_proxy_for_realm(i->first);

*pkt << uint8(i->second.icon);
*pkt << uint8(i->second.allowedSecurityLevel > this->acct.gmlevel ? 1 : 0);
*pkt << uint8(i->second.color);
*pkt << uint8(address == "" ? 0x02 : i->second.color); // if no proxy then mark the realm is offline
*pkt << i->second.name;

if(!i->second.address.compare(":0"))
*pkt << sRealm->get_proxy_for_realm(i->first);

else {
REALM_LOG("proxy: %s\n", i->second.address.c_str());
*pkt << i->second.address;
}

*pkt << address;
*pkt << float(i->second.population);
*pkt << uint8(this->realm_char_amount[i->first]);
*pkt << uint8(i->second.timezone);
*pkt << uint8(0x00);

#ifdef _MORPHEUS_DEBUG
if (address == "")
REALM_LOG("No proxy for realm: %s (%u)\n", i->second.name.c_str(), i->first);
else
REALM_LOG("Selected proxy for realm: %s (%u) | %s\n", i->second.name.c_str(), i->first, address.c_str());
#endif

}
}

Expand Down
36 changes: 9 additions & 27 deletions realmd/Realm_Timer.h
Expand Up @@ -20,8 +20,8 @@
/**
* @file
* @brief Holds implementation of
* Realm_Timer class that performs
* timed update to realmd.
* Unban_Timer class that performs
* timed update to prune bans.
* @author raczman <raczman@gmail.com>
* @date 2009-11-02
* @ingroup Realmd
Expand All @@ -36,35 +36,17 @@ namespace Realmd
{

/**
* @brief Timer that performs tasks for realmd
* @details This class, being an inherit from ACE_Event_Handler
* contains only one function, which goes off at a time
* set in configuration file.
*/
class Realm_Timer : public ACE_Event_Handler
{

private:

/**
* @brief Inherited from ACE_Event_Handler
* @details If there are any tasks that need to go at scheduled time,
* put them in here.
*/
int handle_timeout(const ACE_Time_Value &, const void *act)
{
sRealm->get_db()->get_realmlist();
return 0;
}
};

/**
* @brief Prunes bans every minute.
* @brief Prunes bans every minute.
* @details This class, being an inherit from ACE_Event_Handler
* contains only one function, which goes off at a time
* set in configuration file.
*/
class Unban_Timer : public ACE_Event_Handler
{
/**
* @see Realm_Timer::handle_timeout
* @brief Inherited from ACE_Event_Handler
* @details If there are any tasks that need to go at scheduled time,
* put them in here.
*/
int handle_timeout(const ACE_Time_Value &, const void* act)
{
Expand Down
2 changes: 0 additions & 2 deletions sql/create_realm.sql
Expand Up @@ -75,8 +75,6 @@ DROP TABLE IF EXISTS `realmlist`;
CREATE TABLE `realmlist` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(32) NOT NULL DEFAULT '',
`address` varchar(32) NOT NULL DEFAULT '127.0.0.1',
`port` int(11) NOT NULL DEFAULT '8085',
`icon` tinyint(3) unsigned NOT NULL DEFAULT '0',
`color` tinyint(3) unsigned NOT NULL DEFAULT '2',
`timezone` tinyint(3) unsigned NOT NULL DEFAULT '0',
Expand Down

0 comments on commit 851c9f7

Please sign in to comment.