game2006/server/gameserver/jsondatamgr.cc
aozhiwei 7bd0372f4d 1
2022-12-16 18:45:27 +08:00

124 lines
3.9 KiB
C++

#include "precompile.h"
#include "jsondatamgr.h"
#include "app.h"
#include <f8/utils.h>
#include <f8/udplog.h>
void JsonDataMgr::Init()
{
if (!f8::IsOnlineEnv()) {
if (f8::IsTestEnv()) {
work_path_ = a8::Format("../../../conf_test/game%d/gameserver.test",
{
GAME_ID
});
} else {
work_path_ = a8::Format("../../../conf_test/game%d/gameserver.dev",
{
GAME_ID
});
}
}
std::string gameserver_cluster_json_file;
std::string setting_json_file;
std::string httpproxy_json_file;
gameserver_cluster_json_file = a8::Format("%s/node%d/game%d.gameserver.cluster.json",
{
work_path_,
App::Instance()->node_id,
GAME_ID
});
setting_json_file = a8::Format("%s/setting.json",
{
work_path_,
});
httpproxy_json_file = a8::Format("%s/httpproxy.cluster.json",
{
work_path_,
});
gameserver_cluster_json_.ReadFromFile(gameserver_cluster_json_file);
setting_json_.ReadFromFile(setting_json_file);
httpproxy_cluster_json_.ReadFromFile(httpproxy_json_file);
if (setting_json_.GetType() == a8::XOT_OBJECT &&
setting_json_.HasKey("api_url")) {
api_url_ = setting_json_.Get("api_url").GetString();
}
ip = GetConf()->At("ip")->AsXValue().GetString();
listen_port = GetConf()->At("listen_port")->AsXValue();
if (GetConf()->HasKey("channel")) {
channel = GetConf()->At("channel")->AsXValue();
}
#ifdef DEBUG
if (!f8::IsOnlineEnv()) {
if (!f8::IsTestEnv()) {
bool ok = a8::GetLocalIp(ip);
if (getenv("HOST_IP")) {
ip = getenv("HOST_IP");
}
if (!ok) {
A8_ABORT();
}
}
}
#endif
server_info = a8::Format("%s:%d", {ip, listen_port});
Reload();
f8::UdpLog::Instance()->Info("api_url:%s",
{
api_url_
});
}
void JsonDataMgr::UnInit()
{
}
std::shared_ptr<a8::XObject> JsonDataMgr::GetConf()
{
for (int i = 0; i < gameserver_cluster_json_.Size(); ++i) {
std::shared_ptr<a8::XObject> conf = gameserver_cluster_json_.At(i);
if (conf->At("instance_id")->AsXValue().GetInt() == App::Instance()->instance_id) {
return conf;
}
}
A8_ABORT();
}
std::shared_ptr<a8::XObject> JsonDataMgr::GetMasterServerClusterConf()
{
return std::make_shared<a8::XObject>(masterserver_cluster_json_);
}
void JsonDataMgr::Reload()
{
std::string masterserver_cluster_json_file;
masterserver_cluster_json_file = a8::Format("%s/node%d/game%d.masterserver.cluster.json",
{
work_path_,
App::Instance()->node_id,
GAME_ID
});
masterserver_cluster_json_.ReadFromFile(masterserver_cluster_json_file);
}
void JsonDataMgr::GetApiUrl(std::string& url)
{
if (!api_url_.empty()) {
url = api_url_;
}
}
void JsonDataMgr::GetHttpProxyUrl(std::string& url)
{
if (httpproxy_cluster_json_.Size() > 0) {
std::shared_ptr<a8::XObject> conf = httpproxy_cluster_json_.At(0);
url = conf->At("url")->AsXValue().GetString();
}
}