From af14aff44210196eee628744efac731b4d014361 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Thu, 30 May 2019 17:38:32 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0GetNodeHost?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/wsproxy/GCListener.cc | 29 ++++++++++++++++++---- server/wsproxy/app.cc | 5 ++-- server/wsproxy/jsondatamgr.cc | 46 +++++++++++++++++++++++++++++++++++ server/wsproxy/jsondatamgr.h | 4 +++ 4 files changed, 76 insertions(+), 8 deletions(-) diff --git a/server/wsproxy/GCListener.cc b/server/wsproxy/GCListener.cc index 9dd3d4f..0832aed 100644 --- a/server/wsproxy/GCListener.cc +++ b/server/wsproxy/GCListener.cc @@ -61,12 +61,31 @@ public: virtual bool HandleRedirect(const std::string& url, const std::string& querystr, std::string& location) override { - #if 1 +#if MASTER_MODE + a8::HTTPRequest request; + a8::ParserUrlQueryString(querystr.c_str(), request); + if (a8::Get(request, "c").GetString() == "Ops" && + a8::Get(request, "a").GetString() == "join") { + std::string team_uuid = a8::Get(request, "team_uuid").GetString(); + std::vector strings; + a8::Split(team_uuid, strings, '_'); + if (strings.size() > 2) { + int node_id = a8::XValue(strings[0]); + if (node_id != App::Instance()->node_id) { + std::string host; + if (JsonDataMgr::Instance()->GetNodeHost(node_id, host)) { + location = a8::Format("wss://%s/webapp/index.php?c=Ops&a=join&team_uuid=%s", + { + host, + team_uuid + }); + return true; + } + } + } + } +#endif return false; - #else - location = "ws://192.168.100.21:7101"; - return true; - #endif } virtual void OnDisConnect() override diff --git a/server/wsproxy/app.cc b/server/wsproxy/app.cc index 4d0b410..afa3b61 100644 --- a/server/wsproxy/app.cc +++ b/server/wsproxy/app.cc @@ -114,7 +114,6 @@ bool App::Init(int argc, char* argv[]) HandlerMgr::Instance()->Init(); a8::Timer::Instance()->Init(); JsonDataMgr::Instance()->Init(); - GCListener::Instance()->Init(); #if MASTER_MODE uuid.SetMachineId((node_id - 1) * MAX_NODE_ID + instance_id); #else @@ -123,6 +122,7 @@ bool App::Init(int argc, char* argv[]) GameClientMgr::Instance()->Init(); MasterSvrMgr::Instance()->Init(); TargetConnMgr::Instance()->Init(); + GCListener::Instance()->Init(); a8::UdpLog::Instance()->Info("wsproxy starting instance_id:%d pid:%d", {instance_id, getpid()}); { @@ -154,10 +154,10 @@ bool App::Init(int argc, char* argv[]) void App::UnInit() { a8::XPrintf("wsproxy terminating instance_id:%d pid:%d\n", {instance_id, getpid()}); + GCListener::Instance()->UnInit(); MasterSvrMgr::Instance()->UnInit(); TargetConnMgr::Instance()->UnInit(); GameClientMgr::Instance()->UnInit(); - GCListener::Instance()->UnInit(); JsonDataMgr::Instance()->UnInit(); a8::Timer::Instance()->UnInit(); HandlerMgr::Instance()->UnInit(); @@ -614,4 +614,3 @@ void App::FreeIMMsgQueue() } im_msg_mutex_->unlock(); } - diff --git a/server/wsproxy/jsondatamgr.cc b/server/wsproxy/jsondatamgr.cc index f8eac26..64628b1 100644 --- a/server/wsproxy/jsondatamgr.cc +++ b/server/wsproxy/jsondatamgr.cc @@ -1,13 +1,20 @@ #include "precompile.h" +#include + #include "jsondatamgr.h" #include "app.h" void JsonDataMgr::Init() { +#if MASTER_MODE + node_host_mutex_ = new std::mutex(); +#endif + std::string wsproxyserver_cluster_json_file; std::string masterserver_cluster_json_file; std::string targetserver_cluster_json_file; + std::string routing_tables_json_file; if (f8::IsOnlineEnv()) { #if MASTER_MODE wsproxyserver_cluster_json_file = a8::Format("../config/node%d/game%d.wsproxy.cluster.json", @@ -20,6 +27,9 @@ void JsonDataMgr::Init() App::Instance()->node_id, GAME_ID }); + routing_tables_json_file = a8::Format("../config/routing_tables.json", + { + }); #else wsproxyserver_cluster_json_file = a8::Format("../config/game%d.wsproxy.cluster.json", {GAME_ID}); masterserver_cluster_json_file = a8::Format("../config/game%d.masterserver.cluster.json", {GAME_ID}); @@ -38,6 +48,10 @@ void JsonDataMgr::Init() App::Instance()->node_id, GAME_ID }); + routing_tables_json_file = a8::Format("/var/data/conf_test/game%d/wsproxy/routing_tables.json", + { + GAME_ID + }); #else wsproxyserver_cluster_json_file = a8::Format("/var/data/conf_test/game%d/wsproxy/game%d.wsproxy.cluster.json", {GAME_ID, GAME_ID}); @@ -50,6 +64,17 @@ void JsonDataMgr::Init() wsproxyserver_cluster_json_.ReadFromFile(wsproxyserver_cluster_json_file); #if MASTER_MODE masterserver_cluster_json_.ReadFromFile(masterserver_cluster_json_file); + routing_tables_json_.ReadFromFile(routing_tables_json_file); + node_host_mutex_->lock(); + for (int i = 0; i < routing_tables_conf->Size(); ++i) { + int node_id = master_svr_conf->At("node_id")->AsXValue(); + std::string host = master_svr_conf->At("host")->AsXValue(); + if (node_host_hash_.find(node_id) != node_host_hash_.end()) { + abort(); + } + node_host_hash_[node_id] = host; + } + node_host_mutex_->unlock(); #else targetserver_cluster_json_.ReadFromFile(targetserver_cluster_json_file); #endif @@ -57,6 +82,10 @@ void JsonDataMgr::Init() void JsonDataMgr::UnInit() { +#if MASTER_MODE + delete node_host_mutex_; + node_host_mutex_ = nullptr; +#endif } std::shared_ptr JsonDataMgr::GetConf() @@ -73,9 +102,26 @@ std::shared_ptr JsonDataMgr::GetMasterServerClusterConf() } #if MASTER_MODE + +bool JsonDataMgr::GetNodeHost(int node_id, std::string& host) +{ + bool found = false; + node_host_mutex_->lock(); + auto itr = node_host_hash_.find(node_id); + if (itr != node_host_hash_.end()) { + //!!!因为std::string基于引用计数多线程下直接复制的话会有问题,所以要用这种方式赋值 + host = itr->second.c_str(); + found = true; + } + node_host_mutex_->unlock(); + return found; +} + #else + std::shared_ptr JsonDataMgr::GetTargetServerClusterConf() { return std::make_shared(targetserver_cluster_json_); } + #endif diff --git a/server/wsproxy/jsondatamgr.h b/server/wsproxy/jsondatamgr.h index b70f95d..b1fff4d 100644 --- a/server/wsproxy/jsondatamgr.h +++ b/server/wsproxy/jsondatamgr.h @@ -13,6 +13,7 @@ class JsonDataMgr : public a8::Singleton std::shared_ptr GetConf(); std::shared_ptr GetMasterServerClusterConf(); #if MASTER_MODE + bool GetNodeHost(int node_id, std::string& host); #else std::shared_ptr GetTargetServerClusterConf(); #endif @@ -21,6 +22,9 @@ class JsonDataMgr : public a8::Singleton a8::XObject wsproxyserver_cluster_json_; a8::XObject masterserver_cluster_json_; #if MASTER_MODE + std::mutex* node_host_mutex_ = nullptr; + a8::XObject routing_tables_json_; + std::map node_host_hash_; #else a8::XObject targetserver_cluster_json_; #endif