This commit is contained in:
aozhiwei 2020-05-03 16:51:56 +08:00
parent 498863e362
commit 21699e1524
4 changed files with 12 additions and 44 deletions

View File

@ -7,7 +7,6 @@
void MSConnMgr::Init()
{
#if MASTER_MODE
auto master_svr_cluster_conf = JsonDataMgr::Instance()->GetMasterServerClusterConf();
for (int i = 0; i < master_svr_cluster_conf->Size(); ++i) {
auto master_svr_conf = master_svr_cluster_conf->At(i);
@ -15,13 +14,12 @@ void MSConnMgr::Init()
std::string remote_ip = master_svr_conf->At("ip")->AsXValue();
int remote_port = master_svr_conf->At("port")->AsXValue();
{
MasterSvr* conn = new MasterSvr();
MSConn* conn = new MSConn();
conn->Init(instance_id, remote_ip, remote_port);
mastersvr_hash_[conn->instance_id] = conn;
conn->Open();
id_hash_[conn->instance_id] = conn;
}
}
#endif
}
void MSConnMgr::UnInit()
@ -32,34 +30,8 @@ void MSConnMgr::UnInit()
}
}
MSConn* MSConnMgr::GetConnByKey(const std::string& key)
{
auto itr = key_hash_.find(key);
return itr != key_hash_.end() ? itr->second : nullptr;
}
MSConn* MSConnMgr::GetConnById(int instance_id)
{
auto itr = id_hash_.find(instance_id);
return itr != id_hash_.end() ? itr->second : nullptr;
}
MSConn* MSConnMgr::RecreateMSConn(const std::string& host, int port)
{
std::string key = host + ":" + a8::XValue(port).GetString();
MSConn* conn = GetConnByKey(key);
if (conn) {
return conn;
}
while (GetConnById(++curr_id_)) {};
int instance_id = curr_id_;
std::string remote_ip = host;
int remote_port = port;
conn = new MSConn();
conn->Init(instance_id, remote_ip, remote_port);
id_hash_[conn->instance_id] = conn;
key_hash_[key] = conn;
conn->Open();
return conn;
}

View File

@ -12,12 +12,8 @@ class MSConnMgr : public a8::Singleton<MSConnMgr>
void Init();
void UnInit();
MSConn* GetConnByKey(const std::string& key);
MSConn* GetConnById(int instance_id);
MSConn* RecreateMSConn(const std::string& host, int port);
private:
unsigned short curr_id_ = 1000;
std::map<std::string, MSConn*> key_hash_;
std::map<int, MSConn*> id_hash_;
};

View File

@ -21,18 +21,17 @@ void JsonDataMgr::Init()
}
}
std::string wsproxyserver_cluster_json_file;
std::string imserver_cluster_json_file;
std::string masterserver_cluster_json_file;
std::string routing_tables_json_file;
wsproxyserver_cluster_json_file = a8::Format("%s/friend.imserver.cluster.json",
{
work_path_,
});
imserver_cluster_json_file = a8::Format("%s/friend.imserver.cluster.json",
{
work_path_,
});
masterserver_cluster_json_file = a8::Format("%s/friend.masterserver.cluster.json",
{
work_path_,
});
wsproxyserver_cluster_json_.ReadFromFile(wsproxyserver_cluster_json_file);
imserver_cluster_json_.ReadFromFile(imserver_cluster_json_file);
masterserver_cluster_json_.ReadFromFile(masterserver_cluster_json_file);
}
@ -42,10 +41,11 @@ void JsonDataMgr::UnInit()
std::shared_ptr<a8::XObject> JsonDataMgr::GetConf()
{
if (App::Instance()->instance_id < 1 || App::Instance()->instance_id > wsproxyserver_cluster_json_.Size()) {
if (App::Instance()->instance_id < 1 ||
App::Instance()->instance_id > imserver_cluster_json_.Size()) {
abort();
}
return wsproxyserver_cluster_json_[App::Instance()->instance_id - 1];
return imserver_cluster_json_[App::Instance()->instance_id - 1];
}
std::shared_ptr<a8::XObject> JsonDataMgr::GetMasterServerClusterConf()

View File

@ -15,6 +15,6 @@ class JsonDataMgr : public a8::Singleton<JsonDataMgr>
private:
std::string work_path_ = "../config";
a8::XObject wsproxyserver_cluster_json_;
a8::XObject imserver_cluster_json_;
a8::XObject masterserver_cluster_json_;
};