1
This commit is contained in:
parent
5706ff6992
commit
d8eabad407
@ -27,6 +27,9 @@ void GSMgr::UnInit()
|
||||
void GSMgr::_SS_WSP_RequestTargetServer(f8::MsgHdr& hdr, const ss::SS_WSP_RequestTargetServer& msg)
|
||||
{
|
||||
int channel = f8::ExtractChannelIdFromAccountId(msg.account_id());
|
||||
if (!App::Instance()->IsSeparateChannel(channel)) {
|
||||
channel = 0;
|
||||
}
|
||||
ss::SS_MS_ResponseTargetServer respmsg;
|
||||
respmsg.set_context_id(msg.context_id());
|
||||
if (msg.is_reconnect()) {
|
||||
@ -82,11 +85,16 @@ void GSMgr::___GSReport(f8::JsonHttpRequest* request)
|
||||
if (itr->second.online_num != online_num ||
|
||||
itr->second.room_num != room_num ||
|
||||
itr->second.alive_count != alive_count ||
|
||||
itr->second.servicing != servicing
|
||||
itr->second.servicing != servicing ||
|
||||
itr->second.channel != channel
|
||||
) {
|
||||
itr->second.online_num = online_num;
|
||||
itr->second.room_num = room_num;
|
||||
itr->second.servicing = servicing;
|
||||
if (itr->second.channel != channel) {
|
||||
itr->second.channel = channel;
|
||||
OnChannelChange(&itr->second);
|
||||
}
|
||||
RearrangeNode();
|
||||
}
|
||||
itr->second.alive_count = alive_count;
|
||||
@ -107,7 +115,7 @@ void GSMgr::___GSReport(f8::JsonHttpRequest* request)
|
||||
gs.channel = channel;
|
||||
gs.last_active_tick = a8::XGetTickCount();
|
||||
node_key_hash_[key] = gs;
|
||||
node_sorted_list_.push_back(&node_key_hash_[key]);
|
||||
AddNodeToSortedNodes(&node_key_hash_[key]);
|
||||
RearrangeNode();
|
||||
}
|
||||
|
||||
@ -130,6 +138,7 @@ void GSMgr::___GSList(f8::JsonHttpRequest* request)
|
||||
node->SetVal("ip", pair.second.ip);
|
||||
node->SetVal("port", pair.second.port);
|
||||
node->SetVal("servicing", pair.second.servicing);
|
||||
node->SetVal("channel", pair.second.channel);
|
||||
node_list->Push(*node);
|
||||
delete node;
|
||||
}
|
||||
@ -142,7 +151,8 @@ void GSMgr::___GSList(f8::JsonHttpRequest* request)
|
||||
{
|
||||
a8::MutableXObject* node_list = a8::MutableXObject::NewArray();
|
||||
|
||||
for (GSNode* gs_node : node_sorted_list_) {
|
||||
for (auto& pair : sorted_node_hash_) {
|
||||
for (GSNode* gs_node : pair.second) {
|
||||
a8::MutableXObject* node = a8::MutableXObject::NewObject();
|
||||
node->SetVal("node_id", gs_node->node_id);
|
||||
node->SetVal("instance_id", gs_node->instance_id);
|
||||
@ -151,9 +161,11 @@ void GSMgr::___GSList(f8::JsonHttpRequest* request)
|
||||
node->SetVal("ip", gs_node->ip);
|
||||
node->SetVal("port", gs_node->port);
|
||||
node->SetVal("servicing", gs_node->servicing);
|
||||
node->SetVal("channel", gs_node->channel);
|
||||
node_list->Push(*node);
|
||||
delete node;
|
||||
}
|
||||
}
|
||||
|
||||
request->resp_xobj->SetVal("errcode", 0);
|
||||
request->resp_xobj->SetVal("errmsg", "");
|
||||
@ -176,20 +188,8 @@ GSNode* GSMgr::GetNodeByNodeKey(const std::string& node_key)
|
||||
|
||||
GSNode* GSMgr::AllocNode(int channel)
|
||||
{
|
||||
std::vector<GSNode*>* sorted_nodes = &node_sorted_list_;
|
||||
std::vector<GSNode*> spec_nodes;
|
||||
if (App::Instance()->IsSeparateChannel(channel)) {
|
||||
for (GSNode* node : node_sorted_list_) {
|
||||
if (spec_nodes.size() >= 2) {
|
||||
break;
|
||||
}
|
||||
if (node->channel == channel) {
|
||||
spec_nodes.push_back(node);
|
||||
}
|
||||
}
|
||||
sorted_nodes = &spec_nodes;
|
||||
}
|
||||
if (!sorted_nodes->empty()) {
|
||||
std::vector<GSNode*>* sorted_nodes = GetSortedNodesByChannel(channel);
|
||||
if (sorted_nodes && !sorted_nodes->empty()) {
|
||||
size_t rnd = std::min((size_t)2, sorted_nodes->size());
|
||||
int idx = rand() % rnd;
|
||||
while (idx >= 0) {
|
||||
@ -200,9 +200,9 @@ GSNode* GSMgr::AllocNode(int channel)
|
||||
}
|
||||
}
|
||||
a8::UdpLog::Instance()->Warning
|
||||
("节点分配失败 node_sorted_list.size:%d node_list.size:%d sorted_node.size:%d channel:%d",
|
||||
("节点分配失败 sorted_node_hashlist.size:%d node_list.size:%d sorted_node.size:%d channel:%d",
|
||||
{
|
||||
node_sorted_list_.size(),
|
||||
sorted_node_hash_.size(),
|
||||
node_key_hash_.size(),
|
||||
sorted_nodes->size(),
|
||||
channel
|
||||
@ -212,7 +212,8 @@ GSNode* GSMgr::AllocNode(int channel)
|
||||
|
||||
void GSMgr::RearrangeNode()
|
||||
{
|
||||
std::sort(node_sorted_list_.begin(), node_sorted_list_.end(),
|
||||
for (auto& pair : sorted_node_hash_) {
|
||||
std::sort(pair.second.begin(), pair.second.end(),
|
||||
[] (const GSNode* a, const GSNode* b)
|
||||
{
|
||||
if (a->servicing && b->servicing) {
|
||||
@ -239,6 +240,7 @@ void GSMgr::RearrangeNode()
|
||||
return a->node_idx > b->node_idx;
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void GSMgr::ClearTimeOutNode()
|
||||
@ -250,14 +252,7 @@ void GSMgr::ClearTimeOutNode()
|
||||
}
|
||||
}
|
||||
for (GSNode* node : time_out_nodes) {
|
||||
{
|
||||
for (size_t i = 0; i < node_sorted_list_.size(); ++i) {
|
||||
if (node_sorted_list_[i] == node) {
|
||||
node_sorted_list_.erase(node_sorted_list_.begin() + i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
RemoveNodeRomSortedNodes(node);
|
||||
{
|
||||
std::vector<std::string> deleted_teams;
|
||||
for (auto& pair : team_hash_) {
|
||||
@ -273,3 +268,42 @@ void GSMgr::ClearTimeOutNode()
|
||||
}
|
||||
RearrangeNode();
|
||||
}
|
||||
|
||||
void GSMgr::AddNodeToSortedNodes(GSNode* node)
|
||||
{
|
||||
auto itr = sorted_node_hash_.find(node->channel);
|
||||
if (itr != sorted_node_hash_.end()) {
|
||||
itr->second.push_back(node);
|
||||
} else {
|
||||
sorted_node_hash_[node->channel] = std::vector<GSNode*>({node});
|
||||
}
|
||||
}
|
||||
|
||||
void GSMgr::RemoveNodeRomSortedNodes(GSNode* node)
|
||||
{
|
||||
for (auto& pair : sorted_node_hash_) {
|
||||
std::vector<GSNode*>& node_list = pair.second;
|
||||
for (size_t i = 0; i < node_list.size(); ++i) {
|
||||
if (node_list[i] == node) {
|
||||
node_list.erase(node_list.begin() + i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<GSNode*>* GSMgr::GetSortedNodesByChannel(int channel)
|
||||
{
|
||||
auto itr = sorted_node_hash_.find(channel);
|
||||
if (itr != sorted_node_hash_.end()) {
|
||||
return &itr->second;
|
||||
} else {
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void GSMgr::OnChannelChange(GSNode* node)
|
||||
{
|
||||
RemoveNodeRomSortedNodes(node);
|
||||
AddNodeToSortedNodes(node);
|
||||
}
|
||||
|
@ -44,10 +44,14 @@ class GSMgr : public a8::Singleton<GSMgr>
|
||||
GSNode* AllocNode(int channel);
|
||||
void RearrangeNode();
|
||||
void ClearTimeOutNode();
|
||||
void AddNodeToSortedNodes(GSNode* node);
|
||||
void RemoveNodeRomSortedNodes(GSNode* node);
|
||||
std::vector<GSNode*>* GetSortedNodesByChannel(int channel);
|
||||
void OnChannelChange(GSNode* node);
|
||||
|
||||
private:
|
||||
|
||||
std::map<std::string, GSNode*> team_hash_;
|
||||
std::map<std::string, GSNode> node_key_hash_;
|
||||
std::vector<GSNode*> node_sorted_list_;
|
||||
std::map<int, std::vector<GSNode*>> sorted_node_hash_;
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user