From 68882934b5bea82a76d6c73877c36f5d5a8ff31e Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Tue, 16 Jun 2020 14:04:28 +0800 Subject: [PATCH] 1 --- server/masterserver/svrmgr.cc | 75 ++++++++++++++++++++++++----------- server/masterserver/svrmgr.h | 13 +++++- server/masterserver/types.h | 30 ++++++++++++++ 3 files changed, 93 insertions(+), 25 deletions(-) diff --git a/server/masterserver/svrmgr.cc b/server/masterserver/svrmgr.cc index cc4e9a6..b1fbbc7 100644 --- a/server/masterserver/svrmgr.cc +++ b/server/masterserver/svrmgr.cc @@ -2,6 +2,7 @@ #include #include +#include #include "svrmgr.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(); - auto itr = node_key_hash_.find(key); - if (itr != node_key_hash_.end()) { - if (itr->second.online_num != msg.online_num() || - itr->second.servicing != msg.servicing() + SvrNode* svr = GetNodeByKey(key); + if (svr) { + if (svr->online_num != msg.online_num() || + svr->servicing != msg.servicing() ) { - itr->second.online_num = msg.online_num(); - itr->second.servicing = msg.servicing(); + svr->online_num = msg.online_num(); + svr->servicing = msg.servicing(); 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 { - SvrNode svr; - svr.key = key; - svr.node_idx = App::Instance()->NewUuid(); - svr.instance_id = msg.instance_id(); - svr.online_num = msg.online_num(); - svr.ip = msg.ip(); - svr.port = msg.port(); - svr.servicing = msg.servicing(); - svr.last_active_tick = a8::XGetTickCount(); + svr = new SvrNode; + svr->socket_handle = hdr.socket_handle; + svr->key = key; + svr->node_idx = App::Instance()->NewUuid(); + svr->instance_id = msg.instance_id(); + svr->online_num = msg.online_num(); + svr->ip = msg.ip(); + svr->port = msg.port(); + svr->servicing = msg.servicing(); + svr->last_active_tick = a8::XGetTickCount(); 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(); } @@ -79,11 +92,11 @@ void SvrMgr::___GSList(f8::JsonHttpRequest* request) for (auto& pair : node_key_hash_) { a8::MutableXObject* node = a8::MutableXObject::NewObject(); - node->SetVal("instance_id", pair.second.instance_id); - node->SetVal("online_num", pair.second.online_num); - node->SetVal("ip", pair.second.ip); - node->SetVal("port", pair.second.port); - node->SetVal("servicing", pair.second.servicing); + node->SetVal("instance_id", pair.second->instance_id); + node->SetVal("online_num", pair.second->online_num); + node->SetVal("ip", pair.second->ip); + node->SetVal("port", pair.second->port); + node->SetVal("servicing", pair.second->servicing); node_list->Push(*node); delete node; } @@ -167,8 +180,8 @@ void SvrMgr::ClearTimeOutNode() { std::vector time_out_nodes; for (auto& pair : node_key_hash_) { - if (a8::XGetTickCount() - pair.second.last_active_tick > 1000 * 3) { - time_out_nodes.push_back(&pair.second); + if (a8::XGetTickCount() - pair.second->last_active_tick > 1000 * 3) { + time_out_nodes.push_back(pair.second); } } for (SvrNode* node : time_out_nodes) { @@ -181,6 +194,20 @@ void SvrMgr::ClearTimeOutNode() } } node_key_hash_.erase(node->key); + socket_hash_.erase(node->socket_handle); + delete node; } 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; +} diff --git a/server/masterserver/svrmgr.h b/server/masterserver/svrmgr.h index 031402b..0cd685d 100644 --- a/server/masterserver/svrmgr.h +++ b/server/masterserver/svrmgr.h @@ -5,6 +5,7 @@ struct SvrNode { std::string key; + int socket_handle = 0; long long node_idx = 0; int instance_id = 0; int online_num = 0; @@ -12,6 +13,13 @@ struct SvrNode int port = 0; bool servicing = false; long long last_active_tick = 0; + + list_head human_list; + + SvrNode() + { + INIT_LIST_HEAD(&human_list); + } }; class SvrMgr : public a8::Singleton @@ -37,9 +45,12 @@ class SvrMgr : public a8::Singleton SvrNode* AllocNode(); void RearrangeNode(); void ClearTimeOutNode(); + SvrNode* GetNodeByKey(const std::string& key); + SvrNode* GetNodeBySocket(int socket_handle); private: - std::map node_key_hash_; + std::map node_key_hash_; + std::map socket_hash_; std::vector node_sorted_list_; }; diff --git a/server/masterserver/types.h b/server/masterserver/types.h index 1467ba7..e94c7b6 100755 --- a/server/masterserver/types.h +++ b/server/masterserver/types.h @@ -8,3 +8,33 @@ struct PerfMonitor long long in_data_size = 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); + } +};