This commit is contained in:
aozhiwei 2020-06-16 14:04:28 +08:00
parent 4ebe45d2f1
commit 68882934b5
3 changed files with 93 additions and 25 deletions

View File

@ -2,6 +2,7 @@
#include <a8/mutable_xobject.h> #include <a8/mutable_xobject.h>
#include <a8/timer.h> #include <a8/timer.h>
#include <a8/udplog.h>
#include "svrmgr.h" #include "svrmgr.h"
#include "app.h" #include "app.h"
@ -43,28 +44,40 @@ void SvrMgr::_SS_IM_ReportServerInfo(f8::MsgHdr& hdr, const ss::SS_IM_ReportServ
{ {
std::string key = msg.ip() + ":" + a8::XValue(msg.port()).GetString(); std::string key = msg.ip() + ":" + a8::XValue(msg.port()).GetString();
auto itr = node_key_hash_.find(key); SvrNode* svr = GetNodeByKey(key);
if (itr != node_key_hash_.end()) { if (svr) {
if (itr->second.online_num != msg.online_num() || if (svr->online_num != msg.online_num() ||
itr->second.servicing != msg.servicing() svr->servicing != msg.servicing()
) { ) {
itr->second.online_num = msg.online_num(); svr->online_num = msg.online_num();
itr->second.servicing = msg.servicing(); svr->servicing = msg.servicing();
RearrangeNode(); RearrangeNode();
} }
itr->second.last_active_tick = a8::XGetTickCount(); svr->last_active_tick = a8::XGetTickCount();
if (svr->instance_id != msg.instance_id()) {
a8::UdpLog::Instance()->Warning
(
"report server info %s %d %d",
{
key,
svr->instance_id,
msg.instance_id()
});
}
} else { } else {
SvrNode svr; svr = new SvrNode;
svr.key = key; svr->socket_handle = hdr.socket_handle;
svr.node_idx = App::Instance()->NewUuid(); svr->key = key;
svr.instance_id = msg.instance_id(); svr->node_idx = App::Instance()->NewUuid();
svr.online_num = msg.online_num(); svr->instance_id = msg.instance_id();
svr.ip = msg.ip(); svr->online_num = msg.online_num();
svr.port = msg.port(); svr->ip = msg.ip();
svr.servicing = msg.servicing(); svr->port = msg.port();
svr.last_active_tick = a8::XGetTickCount(); svr->servicing = msg.servicing();
svr->last_active_tick = a8::XGetTickCount();
node_key_hash_[key] = svr; node_key_hash_[key] = svr;
node_sorted_list_.push_back(&node_key_hash_[key]); socket_hash_[hdr.socket_handle] = svr;
node_sorted_list_.push_back(svr);
RearrangeNode(); RearrangeNode();
} }
@ -79,11 +92,11 @@ void SvrMgr::___GSList(f8::JsonHttpRequest* request)
for (auto& pair : node_key_hash_) { for (auto& pair : node_key_hash_) {
a8::MutableXObject* node = a8::MutableXObject::NewObject(); a8::MutableXObject* node = a8::MutableXObject::NewObject();
node->SetVal("instance_id", pair.second.instance_id); node->SetVal("instance_id", pair.second->instance_id);
node->SetVal("online_num", pair.second.online_num); node->SetVal("online_num", pair.second->online_num);
node->SetVal("ip", pair.second.ip); node->SetVal("ip", pair.second->ip);
node->SetVal("port", pair.second.port); node->SetVal("port", pair.second->port);
node->SetVal("servicing", pair.second.servicing); node->SetVal("servicing", pair.second->servicing);
node_list->Push(*node); node_list->Push(*node);
delete node; delete node;
} }
@ -167,8 +180,8 @@ void SvrMgr::ClearTimeOutNode()
{ {
std::vector<SvrNode*> time_out_nodes; std::vector<SvrNode*> time_out_nodes;
for (auto& pair : node_key_hash_) { for (auto& pair : node_key_hash_) {
if (a8::XGetTickCount() - pair.second.last_active_tick > 1000 * 3) { if (a8::XGetTickCount() - pair.second->last_active_tick > 1000 * 3) {
time_out_nodes.push_back(&pair.second); time_out_nodes.push_back(pair.second);
} }
} }
for (SvrNode* node : time_out_nodes) { for (SvrNode* node : time_out_nodes) {
@ -181,6 +194,20 @@ void SvrMgr::ClearTimeOutNode()
} }
} }
node_key_hash_.erase(node->key); node_key_hash_.erase(node->key);
socket_hash_.erase(node->socket_handle);
delete node;
} }
RearrangeNode(); RearrangeNode();
} }
SvrNode* SvrMgr::GetNodeByKey(const std::string& key)
{
auto itr = node_key_hash_.find(key);
return itr != node_key_hash_.end() ? itr->second : nullptr;
}
SvrNode* SvrMgr::GetNodeBySocket(int socket_handle)
{
auto itr = socket_hash_.find(socket_handle);
return itr != socket_hash_.end() ? itr->second : nullptr;
}

View File

@ -5,6 +5,7 @@
struct SvrNode struct SvrNode
{ {
std::string key; std::string key;
int socket_handle = 0;
long long node_idx = 0; long long node_idx = 0;
int instance_id = 0; int instance_id = 0;
int online_num = 0; int online_num = 0;
@ -12,6 +13,13 @@ struct SvrNode
int port = 0; int port = 0;
bool servicing = false; bool servicing = false;
long long last_active_tick = 0; long long last_active_tick = 0;
list_head human_list;
SvrNode()
{
INIT_LIST_HEAD(&human_list);
}
}; };
class SvrMgr : public a8::Singleton<SvrMgr> class SvrMgr : public a8::Singleton<SvrMgr>
@ -37,9 +45,12 @@ class SvrMgr : public a8::Singleton<SvrMgr>
SvrNode* AllocNode(); SvrNode* AllocNode();
void RearrangeNode(); void RearrangeNode();
void ClearTimeOutNode(); void ClearTimeOutNode();
SvrNode* GetNodeByKey(const std::string& key);
SvrNode* GetNodeBySocket(int socket_handle);
private: private:
std::map<std::string, SvrNode> node_key_hash_; std::map<std::string, SvrNode*> node_key_hash_;
std::map<int, SvrNode*> socket_hash_;
std::vector<SvrNode*> node_sorted_list_; std::vector<SvrNode*> node_sorted_list_;
}; };

View File

@ -8,3 +8,33 @@ struct PerfMonitor
long long in_data_size = 0; long long in_data_size = 0;
long long read_count = 0; long long read_count = 0;
}; };
struct BaseUserData
{
std::string account_id;
std::string nickname;
std::string avatar_url;
int sex = 0;
int online = 0;
long long group_id = 0;
long long user_value1 = 0;
long long user_value2 = 0;
long long user_value3 = 0;
long long data_version1 = 0;
};
struct Friend
{
BaseUserData base_data;
unsigned int crc32_code = 0;
list_head human_entry;
struct SvrNode* svr_node = nullptr;
Friend()
{
INIT_LIST_HEAD(&human_entry);
}
};