1
This commit is contained in:
parent
389b2df366
commit
ae2a2bcddb
109
f8/app.cc
109
f8/app.cc
@ -13,49 +13,37 @@
|
|||||||
#include <f8/msgqueue.h>
|
#include <f8/msgqueue.h>
|
||||||
#include <f8/timer.h>
|
#include <f8/timer.h>
|
||||||
|
|
||||||
const int MAX_NODE_ID = 8;
|
static const int MAX_ZONE_ID = 100;
|
||||||
const int MAX_INSTANCE_ID = 500;
|
static const int MAX_NODE_ID = 8;
|
||||||
|
static const int MAX_INSTANCE_ID = 500;
|
||||||
|
|
||||||
const char* const PROJ_NAME_FMT = "game%d_wsproxy";
|
static const char* const PROJ_ROOT_FMT = "/data/logs/%s";
|
||||||
const char* const PROJ_ROOT_FMT = "/data/logs/%s";
|
static const char* const PROJ_LOG_ROOT_FMT = "/data/logs/%s/logs";
|
||||||
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";
|
||||||
const char* const PROJ_LOG_FILENAME_FMT = "log_$pid_%Y%m%d.log";
|
|
||||||
|
|
||||||
namespace f8
|
namespace f8
|
||||||
{
|
{
|
||||||
|
|
||||||
void App::Init()
|
bool App::Init()
|
||||||
{
|
{
|
||||||
signal(SIGPIPE, SIG_IGN);
|
signal(SIGPIPE, SIG_IGN);
|
||||||
srand(time(nullptr));
|
srand(time(nullptr));
|
||||||
|
|
||||||
|
if (!ParseOpt()) {
|
||||||
|
exit_code_ = 1;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
uuid_ = std::make_shared<a8::uuid::SnowFlake>();
|
uuid_ = std::make_shared<a8::uuid::SnowFlake>();
|
||||||
loop_mutex_ = new std::mutex();
|
loop_mutex_ = new std::mutex();
|
||||||
loop_cond_ = new std::condition_variable();
|
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_);
|
uuid_->SetMachineId((node_id_ - 1) * MAX_NODE_ID + instance_id_);
|
||||||
|
InitLog();
|
||||||
f8::MsgQueue::Instance()->Init();
|
f8::MsgQueue::Instance()->Init();
|
||||||
f8::Timer::Instance()->Init();
|
f8::Timer::Instance()->Init();
|
||||||
user_app_->Init();
|
user_app_->Init();
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void App::UnInit()
|
void App::UnInit()
|
||||||
@ -63,6 +51,7 @@ namespace f8
|
|||||||
user_app_->UnInit();
|
user_app_->UnInit();
|
||||||
f8::Timer::Instance()->UnInit();
|
f8::Timer::Instance()->UnInit();
|
||||||
f8::MsgQueue::Instance()->UnInit();
|
f8::MsgQueue::Instance()->UnInit();
|
||||||
|
UnInitLog();
|
||||||
|
|
||||||
delete loop_cond_;
|
delete loop_cond_;
|
||||||
loop_cond_ = nullptr;
|
loop_cond_ = nullptr;
|
||||||
@ -75,22 +64,28 @@ namespace f8
|
|||||||
argc_ = argc;
|
argc_ = argc;
|
||||||
argv_ = argv;
|
argv_ = argv;
|
||||||
user_app_ = user_app;
|
user_app_ = user_app;
|
||||||
Init();
|
if (Init()) {
|
||||||
while (!Terminated()) {
|
while (!Terminated()) {
|
||||||
f8::Timer::Instance()->Update();
|
f8::Timer::Instance()->Update();
|
||||||
f8::MsgQueue::Instance()->Update();
|
f8::MsgQueue::Instance()->Update();
|
||||||
user_app->Update();
|
user_app->Update();
|
||||||
Schedule();
|
Schedule();
|
||||||
|
}
|
||||||
|
UnInit();
|
||||||
}
|
}
|
||||||
UnInit();
|
return exit_code_;
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool App::ParseOpt()
|
bool App::ParseOpt()
|
||||||
{
|
{
|
||||||
int ch = 0;
|
int ch = 0;
|
||||||
while ((ch = getopt(argc_, argv_, "n:i:f:")) != -1) {
|
while ((ch = getopt(argc_, argv_, "z:n:i:f:")) != -1) {
|
||||||
switch (ch) {
|
switch (ch) {
|
||||||
|
case 'z':
|
||||||
|
{
|
||||||
|
zone_id_ = a8::XValue(optarg);
|
||||||
|
}
|
||||||
|
break;
|
||||||
case 'n':
|
case 'n':
|
||||||
{
|
{
|
||||||
node_id_ = a8::XValue(optarg);
|
node_id_ = a8::XValue(optarg);
|
||||||
@ -112,22 +107,49 @@ namespace f8
|
|||||||
break;
|
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)
|
bool App::HasFlag(int flag)
|
||||||
{
|
{
|
||||||
return false;
|
return flags_.find(flag) != flags_.end();
|
||||||
}
|
}
|
||||||
|
|
||||||
void App::NotifyLoopCond()
|
void App::NotifyLoopCond()
|
||||||
{
|
{
|
||||||
|
std::unique_lock<std::mutex> lk(*loop_mutex_);
|
||||||
|
loop_cond_->notify_all();
|
||||||
}
|
}
|
||||||
|
|
||||||
long long App::NewNodeUuid()
|
long long App::NewNodeUuid()
|
||||||
{
|
{
|
||||||
return 0;
|
return uuid_->Generate();
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string App::NewGlobalUuid()
|
const std::string App::NewGlobalUuid()
|
||||||
@ -147,14 +169,11 @@ namespace f8
|
|||||||
a8::XValue(f8::App::Instance()->GetPid()));
|
a8::XValue(f8::App::Instance()->GetPid()));
|
||||||
|
|
||||||
std::string proj_root_dir = a8::Format
|
std::string proj_root_dir = a8::Format
|
||||||
(PROJ_ROOT_FMT,
|
(PROJ_ROOT_FMT, {user_app_->GetPkgName()});
|
||||||
{a8::Format(PROJ_NAME_FMT,{GAME_ID})});
|
|
||||||
std::string proj_log_root_dir = a8::Format
|
std::string proj_log_root_dir = a8::Format
|
||||||
(PROJ_LOG_ROOT_FMT,
|
(PROJ_LOG_ROOT_FMT, {user_app_->GetPkgName()});
|
||||||
{a8::Format(PROJ_NAME_FMT, {GAME_ID})});
|
|
||||||
std::string log_file_name = a8::Format
|
std::string log_file_name = a8::Format
|
||||||
(PROJ_LOG_ROOT_FMT,
|
(PROJ_LOG_ROOT_FMT, {user_app_->GetPkgName() + "/" + filename_fmt});
|
||||||
{a8::Format(PROJ_NAME_FMT, {GAME_ID})}) + "/" + filename_fmt;
|
|
||||||
|
|
||||||
a8::MkDir(proj_root_dir);
|
a8::MkDir(proj_root_dir);
|
||||||
a8::MkDir(proj_log_root_dir);
|
a8::MkDir(proj_log_root_dir);
|
||||||
|
3
f8/app.h
3
f8/app.h
@ -47,7 +47,7 @@ namespace f8
|
|||||||
void Terminate() { terminated_ = true; };
|
void Terminate() { terminated_ = true; };
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void Init();
|
bool Init();
|
||||||
void UnInit();
|
void UnInit();
|
||||||
bool ParseOpt();
|
bool ParseOpt();
|
||||||
void InitLog();
|
void InitLog();
|
||||||
@ -58,6 +58,7 @@ namespace f8
|
|||||||
UserApp* user_app_ = nullptr;
|
UserApp* user_app_ = nullptr;
|
||||||
int argc_ = 0;
|
int argc_ = 0;
|
||||||
char** argv_ = nullptr;
|
char** argv_ = nullptr;
|
||||||
|
int exit_code_ = 0;
|
||||||
volatile bool terminated_ = false;
|
volatile bool terminated_ = false;
|
||||||
|
|
||||||
int zone_id_ = 0;
|
int zone_id_ = 0;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user