95 lines
3.3 KiB
C++
95 lines
3.3 KiB
C++
#include "precompile.h"
|
||
|
||
#include <mutex>
|
||
|
||
#include <f8/utils.h>
|
||
|
||
#include "jsondatamgr.h"
|
||
#include "app.h"
|
||
|
||
void JsonDataMgr::Init()
|
||
{
|
||
node_host_mutex_ = new std::mutex();
|
||
|
||
if (!f8::IsOnlineEnv()) {
|
||
if (f8::IsTestEnv()) {
|
||
work_path_ = a8::Format("../../../conf_test/game%d/wsproxy.test",
|
||
{
|
||
GAME_ID,
|
||
});
|
||
} else {
|
||
work_path_ = a8::Format("../../../conf_test/game%d/wsproxy.dev",
|
||
{
|
||
GAME_ID
|
||
});
|
||
}
|
||
}
|
||
|
||
std::string wsproxyserver_cluster_json_file;
|
||
std::string masterserver_cluster_json_file;
|
||
std::string routing_tables_json_file;
|
||
wsproxyserver_cluster_json_file = a8::Format("%s/node%d/game%d.wsproxy.cluster.json",
|
||
{
|
||
work_path_,
|
||
App::Instance()->node_id,
|
||
GAME_ID
|
||
});
|
||
masterserver_cluster_json_file = a8::Format("%s/node%d/game%d.masterserver.cluster.json",
|
||
{
|
||
work_path_,
|
||
App::Instance()->node_id,
|
||
GAME_ID
|
||
});
|
||
routing_tables_json_file = a8::Format("%s/routing_tables.json",
|
||
{
|
||
work_path_
|
||
});
|
||
|
||
wsproxyserver_cluster_json_.ReadFromFile(wsproxyserver_cluster_json_file);
|
||
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_json_.Size(); ++i) {
|
||
auto node_conf = routing_tables_json_.At(i);
|
||
int node_id = node_conf->At("node_id")->AsXValue();
|
||
std::string host = node_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();
|
||
}
|
||
|
||
void JsonDataMgr::UnInit()
|
||
{
|
||
delete node_host_mutex_;
|
||
node_host_mutex_ = nullptr;
|
||
}
|
||
|
||
std::shared_ptr<a8::XObject> JsonDataMgr::GetConf()
|
||
{
|
||
if (App::Instance()->instance_id < 1 || App::Instance()->instance_id > wsproxyserver_cluster_json_.Size()) {
|
||
abort();
|
||
}
|
||
return wsproxyserver_cluster_json_[App::Instance()->instance_id - 1];
|
||
}
|
||
|
||
std::shared_ptr<a8::XObject> JsonDataMgr::GetMasterServerClusterConf()
|
||
{
|
||
return std::make_shared<a8::XObject>(masterserver_cluster_json_);
|
||
}
|
||
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;
|
||
}
|