292 lines
8.7 KiB
C++
292 lines
8.7 KiB
C++
#include <signal.h>
|
|
#include <unistd.h>
|
|
#include <string.h>
|
|
|
|
#include <mutex>
|
|
#include <condition_variable>
|
|
|
|
#include <a8/a8.h>
|
|
#include <a8/uuid.h>
|
|
#include <a8/perfmonitor.h>
|
|
|
|
#include <f8/f8.h>
|
|
#include <f8/udplog.h>
|
|
#include <f8/app.h>
|
|
#include <f8/msgqueue.h>
|
|
#include <f8/timer.h>
|
|
#include <f8/protoutils.h>
|
|
#include <f8/tglog.h>
|
|
#include <f8/httpclientpool.h>
|
|
|
|
static const int MAX_ZONE_ID = 100;
|
|
static const int MAX_NODE_ID = 8;
|
|
static const int MAX_INSTANCE_ID = 500;
|
|
|
|
static const int MAX_SYS_HTTP_NUM = 2;
|
|
static const int MAX_USER_HTTP_NUM = 8;
|
|
static const int MAX_ALL_HTTP_NUM = MAX_SYS_HTTP_NUM + MAX_USER_HTTP_NUM;
|
|
|
|
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
|
|
{
|
|
|
|
bool App::Init()
|
|
{
|
|
signal(SIGPIPE, SIG_IGN);
|
|
srand(time(nullptr));
|
|
nowtime_ = 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();
|
|
|
|
uuid_->SetMachineId((node_id_ - 1) * MAX_NODE_ID + instance_id_);
|
|
a8::PerfMonitor::Instance()->Init();
|
|
InitLog();
|
|
f8::MsgQueue::Instance()->Init();
|
|
f8::Timer::Instance()->Init();
|
|
f8::TGLog::Instance()->Init(user_app_->GetPkgName(), false, 0);
|
|
f8::HttpClientPool::Instance()->Init(MAX_ALL_HTTP_NUM, MAX_SYS_HTTP_NUM, MAX_USER_HTTP_NUM);
|
|
user_app_->Init();
|
|
return true;
|
|
}
|
|
|
|
void App::UnInit()
|
|
{
|
|
user_app_->UnInit();
|
|
f8::TGLog::Instance()->UnInit();
|
|
f8::HttpClientPool::Instance()->UnInit();
|
|
f8::Timer::Instance()->UnInit();
|
|
f8::MsgQueue::Instance()->UnInit();
|
|
UnInitLog();
|
|
a8::PerfMonitor::Instance()->Init();
|
|
|
|
delete loop_cond_;
|
|
loop_cond_ = nullptr;
|
|
delete loop_mutex_;
|
|
loop_mutex_ = nullptr;
|
|
}
|
|
|
|
int App::Run(int argc, char* argv[], UserApp* user_app)
|
|
{
|
|
argc_ = argc;
|
|
argv_ = argv;
|
|
user_app_ = user_app;
|
|
int delta_time = 0;
|
|
if (Init()) {
|
|
a8::tick_t last_stat_tick = a8::XGetTickCount();
|
|
while (!Terminated()) {
|
|
a8::tick_t begin_tick = a8::XGetTickCount();
|
|
nowtime_ = time(nullptr);
|
|
f8::Timer::Instance()->Update();
|
|
f8::MsgQueue::Instance()->Update();
|
|
DispatchNetMsg();
|
|
user_app->Update(delta_time);
|
|
a8::tick_t end_tick = a8::XGetTickCount();
|
|
if (end_tick - begin_tick > max_run_delay_time_) {
|
|
max_run_delay_time_ = end_tick - begin_tick;
|
|
}
|
|
Schedule();
|
|
end_tick = a8::XGetTickCount();
|
|
if (end_tick - last_stat_tick > 0) {
|
|
delta_time = end_tick - last_stat_tick;
|
|
last_stat_tick = end_tick;
|
|
} else {
|
|
delta_time = 0;
|
|
}
|
|
}
|
|
UnInit();
|
|
}
|
|
return exit_code_;
|
|
}
|
|
|
|
bool App::ParseOpt()
|
|
{
|
|
int ch = 0;
|
|
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);
|
|
}
|
|
break;
|
|
case 'i':
|
|
{
|
|
instance_id_ = a8::XValue(optarg);
|
|
}
|
|
break;
|
|
case 'f':
|
|
{
|
|
std::vector<std::string> strings;
|
|
a8::Split(optarg, strings, ',');
|
|
for (auto& str : strings) {
|
|
flags_.insert(a8::XValue(str).GetInt());
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
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 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 uuid_->Generate();
|
|
}
|
|
|
|
const std::string App::NewGlobalUuid()
|
|
{
|
|
std::string id = a8::Format("%d%d", {100 + zone_id_, uuid_->Generate()});
|
|
return id;
|
|
}
|
|
|
|
int App::GetPid()
|
|
{
|
|
return getpid();
|
|
}
|
|
|
|
void App::InitLog()
|
|
{
|
|
std::string filename_fmt = PROJ_LOG_FILENAME_FMT;
|
|
a8::ReplaceString(filename_fmt, "$pid",
|
|
a8::XValue(f8::App::Instance()->GetPid()));
|
|
|
|
std::string proj_root_dir = a8::Format
|
|
(PROJ_ROOT_FMT, {user_app_->GetPkgName()});
|
|
std::string proj_log_root_dir = a8::Format
|
|
(PROJ_LOG_ROOT_FMT, {user_app_->GetPkgName()});
|
|
std::string log_file_name = a8::Format
|
|
(PROJ_LOG_ROOT_FMT, {user_app_->GetPkgName()}) + "/" + filename_fmt;
|
|
|
|
a8::MkDir(proj_root_dir);
|
|
a8::MkDir(proj_log_root_dir);
|
|
a8::XPrintf("log_file_name:%s\n", {log_file_name});
|
|
f8::UdpLog::Instance()->SetLogFileName(log_file_name);
|
|
f8::UdpLog::Instance()->Init();
|
|
f8::UdpLog::Instance()->Info("proj_root_dir:%s", {proj_root_dir});
|
|
f8::UdpLog::Instance()->Info("proj_log_root_dir:%s", {proj_log_root_dir});
|
|
f8::UdpLog::Instance()->Info("log_file_name:%s", {log_file_name});
|
|
}
|
|
|
|
void App::UnInitLog()
|
|
{
|
|
f8::UdpLog::Instance()->UnInit();
|
|
}
|
|
|
|
void App::Schedule()
|
|
{
|
|
std::unique_lock<std::mutex> lk(*loop_mutex_);
|
|
bool has_task = false;
|
|
{
|
|
queue_.Fetch();
|
|
if (!list_empty(queue_.GetWorkList())) {
|
|
has_task = true;
|
|
}
|
|
}
|
|
if (!has_task) {
|
|
has_task = user_app_->HasTask();
|
|
}
|
|
if (!has_task) {
|
|
int sleep_time = f8::Timer::Instance()->GetIdleTime();
|
|
loop_cond_->wait_for(lk, std::chrono::milliseconds(sleep_time));
|
|
} else {
|
|
int sleep_time = 1;
|
|
loop_cond_->wait_for(lk, std::chrono::milliseconds(sleep_time));
|
|
}
|
|
}
|
|
|
|
void App::DispatchNetMsg()
|
|
{
|
|
queue_.Fetch();
|
|
list_head* work_list = queue_.GetWorkList();
|
|
while (!list_empty(work_list)){
|
|
MsgHdr* hdr = list_first_entry(work_list, MsgHdr, entry);
|
|
list_del_init(&hdr->entry);
|
|
user_app_->DispatchSocketMsg(hdr);
|
|
--msgnode_size_;
|
|
MsgHdr::Destroy(hdr);
|
|
}
|
|
}
|
|
|
|
void App::AddSocketMsg(int sockfrom,
|
|
int sockhandle,
|
|
long ip_saddr,
|
|
unsigned short msgid,
|
|
unsigned int seqid,
|
|
const char *msgbody,
|
|
int bodylen,
|
|
int tag)
|
|
{
|
|
char *p = (char*)malloc(sizeof(MsgHdr) + bodylen);
|
|
MsgHdr* hdr = (MsgHdr*)p;
|
|
hdr->sockfrom = sockfrom;
|
|
hdr->seqid = seqid;
|
|
hdr->msgid = msgid;
|
|
hdr->socket_handle = sockhandle;
|
|
hdr->ip_saddr = ip_saddr;
|
|
hdr->buf = p + sizeof(MsgHdr);
|
|
hdr->buflen = bodylen;
|
|
hdr->offset = 0;
|
|
hdr->hum = nullptr;
|
|
hdr->user_data = nullptr;
|
|
hdr->tag = tag;
|
|
if (bodylen > 0) {
|
|
memmove((void*)hdr->buf, msgbody, bodylen);
|
|
}
|
|
++msgnode_size_;
|
|
queue_.Push(&hdr->entry);
|
|
NotifyLoopCond();
|
|
}
|
|
|
|
}
|