61 lines
1.7 KiB
C++
61 lines
1.7 KiB
C++
#include "precompile.h"
|
|
|
|
#include "target_conn_mgr.h"
|
|
#include "target_conn.h"
|
|
#include "jsondatamgr.h"
|
|
|
|
void TargetConnMgr::Init()
|
|
{
|
|
#if MASTER_MODE
|
|
#else
|
|
auto target_server_cluster_conf = JsonDataMgr::Instance()->GetTargetServerClusterConf();
|
|
for (int i = 0; i < target_server_cluster_conf->Size(); ++i) {
|
|
auto target_server_conf = target_server_cluster_conf->At(i);
|
|
int instance_id = target_server_conf->At("instance_id")->AsXValue();
|
|
std::string remote_ip = target_server_conf->At("ip")->AsXValue();
|
|
int remote_port = target_server_conf->At("port")->AsXValue();
|
|
{
|
|
TargetConn* conn = new TargetConn();
|
|
conn->Init(instance_id, remote_ip, remote_port);
|
|
id_hash_[conn->instance_id] = conn;
|
|
conn->Open();
|
|
}
|
|
}
|
|
#endif
|
|
}
|
|
|
|
void TargetConnMgr::UnInit()
|
|
{
|
|
}
|
|
|
|
TargetConn* TargetConnMgr::GetConnByKey(const std::string& key)
|
|
{
|
|
auto itr = key_hash_.find(key);
|
|
return itr != key_hash_.end() ? itr->second : nullptr;
|
|
}
|
|
|
|
TargetConn* TargetConnMgr::GetConnById(int instance_id)
|
|
{
|
|
auto itr = id_hash_.find(instance_id);
|
|
return itr != id_hash_.end() ? itr->second : nullptr;
|
|
}
|
|
|
|
TargetConn* TargetConnMgr::RecreateTargetConn(const std::string& host, int port)
|
|
{
|
|
std::string key = host + ":" + a8::XValue(port).GetString();
|
|
TargetConn* 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->Init(instance_id, remote_ip, remote_port);
|
|
id_hash_[conn->instance_id] = conn;
|
|
key_hash_[key] = conn;
|
|
conn->Open();
|
|
return conn;
|
|
}
|