From 7a33c2fc1dd0a2c2792b1b9a43192967fc89a997 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Fri, 3 Nov 2023 19:38:31 +0800 Subject: [PATCH] 1 --- server/gameserver/GGListener.cc | 2 +- server/gameserver/app.cc | 38 ++++++++++++++--------------- server/gameserver/app.h | 31 +++++++++++++---------- server/gameserver/debugcmd.cc | 2 +- server/gameserver/gamelog.cc | 8 +++--- server/gameserver/handlermgr.cc | 10 ++++---- server/gameserver/jsondatamgr.cc | 6 ++--- server/gameserver/mt/MetaMgr.cc | 4 +-- server/gameserver/mt/SkillNumber.cc | 4 +-- server/gameserver/roommgr.cc | 6 ++--- 10 files changed, 58 insertions(+), 53 deletions(-) diff --git a/server/gameserver/GGListener.cc b/server/gameserver/GGListener.cc index 18d8ecaa..3d30dae4 100644 --- a/server/gameserver/GGListener.cc +++ b/server/gameserver/GGListener.cc @@ -157,6 +157,6 @@ long long GGListener::GetSentBytesNum() void GGListener::OnListenError(int errorid) { a8::XPrintf("GGListeneron_error %d\n", {errorid}); - App::Instance()->terminated = true; + App::Instance()->Terminate(); exit(1); } diff --git a/server/gameserver/app.cc b/server/gameserver/app.cc index ce57d5b1..8190c2e6 100644 --- a/server/gameserver/app.cc +++ b/server/gameserver/app.cc @@ -124,17 +124,17 @@ static void SavePerfLog() bool App::Init(int argc, char* argv[]) { signal(SIGPIPE, SIG_IGN); - this->argc = argc; - this->argv = argv; + this->argc_ = argc; + this->argv_ = argv; if (!ParseOpt()) { - if (node_id <= 0) { + if (node_id_ <= 0) { a8::XPrintf("gameserver启动失败,缺少-n参数\n", {}); - } else if (node_id > MAX_NODE_ID) { + } else if (node_id_ > MAX_NODE_ID) { a8::XPrintf("gameserver启动失败,-n参数不能大于%d\n", {MAX_NODE_ID}); - } else if (instance_id <= 0) { + } else if (instance_id_ <= 0) { a8::XPrintf("gameserver启动失败,缺少-i参数\n", {}); - } else if (instance_id > MAX_INSTANCE_ID) { + } else if (instance_id_ > MAX_INSTANCE_ID) { a8::XPrintf("gameserver启动失败,-i参数不能大于%d\n", {MAX_INSTANCE_ID}); } return false; @@ -145,8 +145,8 @@ bool App::Init(int argc, char* argv[]) #endif a8::XPrintf("gameserver starting node_id: %d instance_id:%d pid:%d game_id:%d debug_mode:%d\n", { - node_id, - instance_id, + node_id_, + instance_id_, getpid(), GAME_ID, debug_mode @@ -178,7 +178,7 @@ bool App::Init(int argc, char* argv[]) mt::MetaMgr::Instance()->Init(); SelfChecker::Init(); EntityFactory::Instance()->Init(); - uuid.SetMachineId((node_id - 1) * MAX_NODE_ID + instance_id); + uuid_.SetMachineId((node_id_ - 1) * MAX_NODE_ID + instance_id_); KillMgr::Instance()->Init(); RoomMgr::Instance()->Init(); MatchMgr::Instance()->Init(); @@ -189,7 +189,7 @@ bool App::Init(int argc, char* argv[]) f8::UdpLog::Instance()->Info("gameserver starting instance_id:%d pid:%d debug_mode:%d channel:%d", { - instance_id, + instance_id_, getpid(), debug_mode, JsonDataMgr::Instance()->channel @@ -212,7 +212,7 @@ bool App::Init(int argc, char* argv[]) [] (int event, const a8::Args* args) { if (a8::TIMER_EXEC_EVENT == event) { - App::Instance()->terminated = true; + App::Instance()->Terminate(); } } ); @@ -261,7 +261,7 @@ int App::Run() f8::UdpLog::Instance()->Info("gameserver running", {}); last_run_tick_ = a8::XGetTickCount(); int delta_time = 0; - while (!terminated) { + while (!terminated_) { a8::tick_t begin_tick = a8::XGetTickCount(); Global::g_nowtime = time(nullptr); QuickExecute(delta_time); @@ -467,22 +467,22 @@ void App::UnInitLog() bool App::ParseOpt() { int ch = 0; - while ((ch = getopt(argc, argv, "i:t:r:f:n:")) != -1) { + while ((ch = getopt(argc_, argv_, "i:t:r:f:n:")) != -1) { switch (ch) { case 'n': { - node_id = a8::XValue(optarg); + node_id_ = a8::XValue(optarg); } break; case 'i': { - instance_id = a8::XValue(optarg); + instance_id_ = a8::XValue(optarg); } break; case 't': { - is_test_mode = true; - test_param = a8::XValue(optarg); + is_test_mode_ = true; + test_param_ = a8::XValue(optarg); } break; case 'f': @@ -496,12 +496,12 @@ bool App::ParseOpt() break; } } - return instance_id > 0 && node_id > 0; + return instance_id_ > 0 && node_id_ > 0; } long long App::NewUuid() { - return uuid.Generate(); + return uuid_.Generate(); } bool App::HasFlag(int flag) diff --git a/server/gameserver/app.h b/server/gameserver/app.h index ebde2f22..06ce1faa 100644 --- a/server/gameserver/app.h +++ b/server/gameserver/app.h @@ -38,6 +38,13 @@ public: void UnSetFlag(int flag); long long AllocTempHeroUniId(); long long AllocTempWeaponUniId(); + void Terminate() { terminated_ = true; } + int GetInstanceId() const { return instance_id_; } + int GetNodeId() const { return node_id_; } + bool IsTestMode() const { return is_test_mode_; } + int GetTestParam() const { return test_param_; } + bool IsServicing() const { return servicing_; } + void SetServicing(bool open) { servicing_ = open; } private: void QuickExecute(int delta_time); @@ -55,19 +62,6 @@ private: bool ParseOpt(); void FreeSocketMsgQueue(); -public: - int argc = 0; - char** argv = nullptr; - volatile bool terminated = false; - a8::uuid::SnowFlake uuid; - -public: - int instance_id = 0; - int node_id = 0; - bool is_test_mode = false; - int test_param = 0; - bool servicing = true; - private: /* 1: 是否自动匹配机器人组队 @@ -82,6 +76,17 @@ private: std::set flags; private: + int argc_ = 0; + char** argv_ = nullptr; + volatile bool terminated_ = false; + int instance_id_ = 0; + int node_id_ = 0; + bool is_test_mode_ = false; + int test_param_ = 0; + bool servicing_ = true; + + a8::uuid::SnowFlake uuid_; + long long last_run_tick_ = 0; std::mutex *loop_mutex_ = nullptr; std::condition_variable *loop_cond_ = nullptr; diff --git a/server/gameserver/debugcmd.cc b/server/gameserver/debugcmd.cc index 6a382c15..2b42bfef 100644 --- a/server/gameserver/debugcmd.cc +++ b/server/gameserver/debugcmd.cc @@ -8,7 +8,7 @@ bool DebugCmd::Enable() { - return App::Instance()->instance_id == 6; + return App::Instance()->GetInstanceId() == 6; } void DebugCmd::CreateSphere(Creature* c, diff --git a/server/gameserver/gamelog.cc b/server/gameserver/gamelog.cc index 9c20f3b7..83a0288b 100644 --- a/server/gameserver/gamelog.cc +++ b/server/gameserver/gamelog.cc @@ -35,8 +35,8 @@ void GameLog::GameStart(Player* hum) //prop->SetVal("localuuid", ""); //prop->SetVal("start_param", ""); prop->SetVal("team_id", hum->team_id); - prop->SetVal("server_node_id", App::Instance()->node_id); - prop->SetVal("server_instance_id", App::Instance()->instance_id); + prop->SetVal("server_node_id", App::Instance()->GetNodeId()); + prop->SetVal("server_instance_id", App::Instance()->GetInstanceId()); prop->SetVal("map_id", hum->room->GetMapMeta()->map_id()); prop->SetVal("map_name", hum->room->GetMapMeta()->map_name()); @@ -75,8 +75,8 @@ void GameLog::GameEnd(Player* hum) prop->SetVal("game_time", a8::XGetTickCount() - hum->create_tick); //prop->SetVal("start_param", ""); prop->SetVal("team_id", hum->team_id); - prop->SetVal("server_node_id", App::Instance()->node_id); - prop->SetVal("server_instance_id", App::Instance()->instance_id); + prop->SetVal("server_node_id", App::Instance()->GetNodeId()); + prop->SetVal("server_instance_id", App::Instance()->GetInstanceId()); prop->SetVal("map_id", hum->room->GetMapMeta()->map_id()); prop->SetVal("map_name", hum->room->GetMapMeta()->map_name()); diff --git a/server/gameserver/handlermgr.cc b/server/gameserver/handlermgr.cc index 1dd962d5..409906e9 100644 --- a/server/gameserver/handlermgr.cc +++ b/server/gameserver/handlermgr.cc @@ -25,7 +25,7 @@ static void _GMOpsSelfChecking(std::shared_ptr request) request->resp_xobj->SetVal("errcode", 0); request->resp_xobj->SetVal("errmsg", ""); request->resp_xobj->SetVal("healthy", 1); - request->resp_xobj->SetVal("servicing", App::Instance()->servicing ? 1 : 0); + request->resp_xobj->SetVal("servicing", App::Instance()->IsServicing() ? 1 : 0); request->resp_xobj->SetVal("max_rundelay", PerfMonitor::Instance()->max_run_delay_time); request->resp_xobj->SetVal("max_timer_idle", PerfMonitor::Instance()->max_timer_idle); } @@ -42,22 +42,22 @@ static void _GMOpsStopService(std::shared_ptr request) { request->resp_xobj->SetVal("errcode", 0); request->resp_xobj->SetVal("errmsg", ""); - App::Instance()->servicing = false; + App::Instance()->SetServicing(false); } static void _GMOpsStartService(std::shared_ptr request) { request->resp_xobj->SetVal("errcode", 0); request->resp_xobj->SetVal("errmsg", ""); - App::Instance()->servicing = true; + App::Instance()->SetServicing(true); } static void _GMOpsServerInfo(std::shared_ptr request) { request->resp_xobj->SetVal("errcode", 0); request->resp_xobj->SetVal("errmsg", ""); - request->resp_xobj->SetVal("node_id", App::Instance()->node_id); - request->resp_xobj->SetVal("instance_id", App::Instance()->instance_id); + request->resp_xobj->SetVal("node_id", App::Instance()->GetNodeId()); + request->resp_xobj->SetVal("instance_id", App::Instance()->GetInstanceId()); } static void _GMStatGetRealTimeOnline(std::shared_ptr request) diff --git a/server/gameserver/jsondatamgr.cc b/server/gameserver/jsondatamgr.cc index d1681d7a..968991a5 100644 --- a/server/gameserver/jsondatamgr.cc +++ b/server/gameserver/jsondatamgr.cc @@ -29,7 +29,7 @@ void JsonDataMgr::Init() gameserver_cluster_json_file = a8::Format("%s/node%d/game%d.gameserver.cluster.json", { work_path_, - App::Instance()->node_id, + App::Instance()->GetNodeId(), GAME_ID }); setting_json_file = a8::Format("%s/setting.json", @@ -83,7 +83,7 @@ std::shared_ptr JsonDataMgr::GetConf() { for (int i = 0; i < gameserver_cluster_json_.Size(); ++i) { std::shared_ptr conf = gameserver_cluster_json_.At(i); - if (conf->At("instance_id")->AsXValue().GetInt() == App::Instance()->instance_id) { + if (conf->At("instance_id")->AsXValue().GetInt() == App::Instance()->GetInstanceId()) { return conf; } } @@ -101,7 +101,7 @@ void JsonDataMgr::Reload() masterserver_cluster_json_file = a8::Format("%s/node%d/game%d.masterserver.cluster.json", { work_path_, - App::Instance()->node_id, + App::Instance()->GetNodeId(), GAME_ID }); masterserver_cluster_json_.ReadFromFile(masterserver_cluster_json_file); diff --git a/server/gameserver/mt/MetaMgr.cc b/server/gameserver/mt/MetaMgr.cc index ab62ebff..3bff828b 100644 --- a/server/gameserver/mt/MetaMgr.cc +++ b/server/gameserver/mt/MetaMgr.cc @@ -54,13 +54,13 @@ namespace mt res_path_ = a8::Format("../../../conf_test/game%d/gameserver.test/res%d/", { GAME_ID, - App::Instance()->instance_id + App::Instance()->GetInstanceId() }); } else { res_path_ = a8::Format("../../../conf_test/game%d/gameserver.dev/res%d/", { GAME_ID, - App::Instance()->instance_id + App::Instance()->GetInstanceId() }); } } else { diff --git a/server/gameserver/mt/SkillNumber.cc b/server/gameserver/mt/SkillNumber.cc index e0b9ae57..b7f13f15 100644 --- a/server/gameserver/mt/SkillNumber.cc +++ b/server/gameserver/mt/SkillNumber.cc @@ -165,8 +165,8 @@ namespace mt abort(); } #ifdef DEBUG - if (App::Instance()->instance_id == 600 || - App::Instance()->instance_id == 100) { + if (App::Instance()->GetInstanceId() == 600 || + App::Instance()->GetInstanceId() == 100) { _float_cd = 6; } #endif diff --git a/server/gameserver/roommgr.cc b/server/gameserver/roommgr.cc index 8fce4cce..e819511f 100644 --- a/server/gameserver/roommgr.cc +++ b/server/gameserver/roommgr.cc @@ -449,15 +449,15 @@ void RoomMgr::ReportServerState(int instance_id, const std::string& host, int po port }); auto url_params = a8::MutableXObject::CreateObject(); - url_params->SetVal("node_id", App::Instance()->node_id); - url_params->SetVal("instance_id", App::Instance()->instance_id); + url_params->SetVal("node_id", App::Instance()->GetNodeId()); + url_params->SetVal("instance_id", App::Instance()->GetInstanceId()); url_params->SetVal("ip", JsonDataMgr::Instance()->ip); url_params->SetVal("port", JsonDataMgr::Instance()->listen_port); url_params->SetVal("online_num", PlayerMgr::Instance()->OnlineNum()); url_params->SetVal("room_num", RoomNum()); url_params->SetVal("channel", JsonDataMgr::Instance()->channel); url_params->SetVal("alive_count", PerfMonitor::Instance()->real_alive_count); - url_params->SetVal("servicing", App::Instance()->servicing ? 1 : 0); + url_params->SetVal("servicing", App::Instance()->IsServicing() ? 1 : 0); f8::HttpClientPool::Instance()->HttpGet ( cb,