This commit is contained in:
azw 2023-09-05 23:48:26 +00:00
parent 389b2df366
commit ae2a2bcddb
2 changed files with 66 additions and 46 deletions

109
f8/app.cc
View File

@ -13,49 +13,37 @@
#include <f8/msgqueue.h>
#include <f8/timer.h>
const int MAX_NODE_ID = 8;
const int MAX_INSTANCE_ID = 500;
static const int MAX_ZONE_ID = 100;
static const int MAX_NODE_ID = 8;
static const int MAX_INSTANCE_ID = 500;
const char* const PROJ_NAME_FMT = "game%d_wsproxy";
const char* const PROJ_ROOT_FMT = "/data/logs/%s";
const char* const PROJ_LOG_ROOT_FMT = "/data/logs/%s/logs";
const char* const PROJ_LOG_FILENAME_FMT = "log_$pid_%Y%m%d.log";
static const char* const PROJ_ROOT_FMT = "/data/logs/%s";
static const char* const PROJ_LOG_ROOT_FMT = "/data/logs/%s/logs";
static const char* const PROJ_LOG_FILENAME_FMT = "log_$pid_%Y%m%d.log";
namespace f8
{
void App::Init()
bool App::Init()
{
signal(SIGPIPE, SIG_IGN);
srand(time(nullptr));
if (!ParseOpt()) {
exit_code_ = 1;
return false;
}
uuid_ = std::make_shared<a8::uuid::SnowFlake>();
loop_mutex_ = new std::mutex();
loop_cond_ = new std::condition_variable();
if (!ParseOpt()) {
terminated_ = true;
if (node_id_ <= 0) {
a8::XPrintf("启动失败,缺少-n参数\n", {});
} else if (node_id_ > MAX_NODE_ID) {
a8::XPrintf("启动失败,-n参数不能大于%d\n", {MAX_NODE_ID});
} else if (instance_id_ <= 0) {
a8::XPrintf("启动失败,缺少-i参数\n", {});
} else if (instance_id_ > MAX_INSTANCE_ID) {
a8::XPrintf("启动失败,-i参数不能大于%d\n", {MAX_INSTANCE_ID});
}
}
a8::XPrintf("wsproxy starting node_id:%d instance_id:%d pid:%d\n",
{
node_id_,
instance_id_,
GetPid()
});
uuid_->SetMachineId((node_id_ - 1) * MAX_NODE_ID + instance_id_);
InitLog();
f8::MsgQueue::Instance()->Init();
f8::Timer::Instance()->Init();
user_app_->Init();
return true;
}
void App::UnInit()
@ -63,6 +51,7 @@ namespace f8
user_app_->UnInit();
f8::Timer::Instance()->UnInit();
f8::MsgQueue::Instance()->UnInit();
UnInitLog();
delete loop_cond_;
loop_cond_ = nullptr;
@ -75,22 +64,28 @@ namespace f8
argc_ = argc;
argv_ = argv;
user_app_ = user_app;
Init();
while (!Terminated()) {
f8::Timer::Instance()->Update();
f8::MsgQueue::Instance()->Update();
user_app->Update();
Schedule();
if (Init()) {
while (!Terminated()) {
f8::Timer::Instance()->Update();
f8::MsgQueue::Instance()->Update();
user_app->Update();
Schedule();
}
UnInit();
}
UnInit();
return 0;
return exit_code_;
}
bool App::ParseOpt()
{
int ch = 0;
while ((ch = getopt(argc_, argv_, "n:i:f:")) != -1) {
while ((ch = getopt(argc_, argv_, "z:n:i:f:")) != -1) {
switch (ch) {
case 'z':
{
zone_id_ = a8::XValue(optarg);
}
break;
case 'n':
{
node_id_ = a8::XValue(optarg);
@ -112,22 +107,49 @@ namespace f8
break;
}
}
return instance_id_ > 0 && node_id_ > 0;
if (zone_id_ <= 0) {
a8::XPrintf("启动失败,缺少-z参数\n", {});
return false;
} else if (node_id_ > MAX_ZONE_ID) {
a8::XPrintf("启动失败,-z参数不能大于%d\n", {MAX_ZONE_ID});
return false;
} else if (node_id_ <= 0) {
a8::XPrintf("启动失败,缺少-n参数\n", {});
return false;
} else if (node_id_ > MAX_NODE_ID) {
a8::XPrintf("启动失败,-n参数不能大于%d\n", {MAX_NODE_ID});
return false;
} else if (instance_id_ <= 0) {
a8::XPrintf("启动失败,缺少-i参数\n", {});
return false;
} else if (instance_id_ > MAX_INSTANCE_ID) {
a8::XPrintf("启动失败,-i参数不能大于%d\n", {MAX_INSTANCE_ID});
return false;
}
a8::XPrintf("starting zone_id:%d node_id:%d instance_id:%d pid:%d\n",
{
zone_id_,
node_id_,
instance_id_,
GetPid()
});
return true;
}
bool App::HasFlag(int flag)
{
return false;
return flags_.find(flag) != flags_.end();
}
void App::NotifyLoopCond()
{
std::unique_lock<std::mutex> lk(*loop_mutex_);
loop_cond_->notify_all();
}
long long App::NewNodeUuid()
{
return 0;
return uuid_->Generate();
}
const std::string App::NewGlobalUuid()
@ -147,14 +169,11 @@ namespace f8
a8::XValue(f8::App::Instance()->GetPid()));
std::string proj_root_dir = a8::Format
(PROJ_ROOT_FMT,
{a8::Format(PROJ_NAME_FMT,{GAME_ID})});
(PROJ_ROOT_FMT, {user_app_->GetPkgName()});
std::string proj_log_root_dir = a8::Format
(PROJ_LOG_ROOT_FMT,
{a8::Format(PROJ_NAME_FMT, {GAME_ID})});
(PROJ_LOG_ROOT_FMT, {user_app_->GetPkgName()});
std::string log_file_name = a8::Format
(PROJ_LOG_ROOT_FMT,
{a8::Format(PROJ_NAME_FMT, {GAME_ID})}) + "/" + filename_fmt;
(PROJ_LOG_ROOT_FMT, {user_app_->GetPkgName() + "/" + filename_fmt});
a8::MkDir(proj_root_dir);
a8::MkDir(proj_log_root_dir);

View File

@ -47,7 +47,7 @@ namespace f8
void Terminate() { terminated_ = true; };
private:
void Init();
bool Init();
void UnInit();
bool ParseOpt();
void InitLog();
@ -58,6 +58,7 @@ namespace f8
UserApp* user_app_ = nullptr;
int argc_ = 0;
char** argv_ = nullptr;
int exit_code_ = 0;
volatile bool terminated_ = false;
int zone_id_ = 0;