aozhiwei bdd60d2aa3 1
2021-12-03 18:48:16 +08:00

430 lines
11 KiB
C++
Executable File

#include "precompile.h"
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <mutex>
#include <condition_variable>
#include <a8/redis.h>
#include <a8/timer.h>
#include <a8/uuid.h>
#include "framework/cpp/netmsghandler.h"
#include "app.h"
#include "jsondatamgr.h"
#include "handlermgr.h"
#include "perfmonitor.h"
#include "metamgr.h"
#include "ss_msgid.pb.h"
#include "ss_proto.pb.h"
#include "framework/cpp/msgqueue.h"
#include "framework/cpp/tglog.h"
struct MsgNode
{
SocketFrom_e sockfrom;
int sockhandle;
unsigned short msgid;
unsigned int seqid;
long ip_saddr;
char* buf;
int buflen;
MsgNode* next;
};
struct IMMsgNode
{
unsigned short msgid;
a8::XParams params;
IMMsgNode* next = nullptr;
};
const char* const PROJ_LOG_ROOT_FMT = "/data/logs/%s/logs";
const char* const PROJ_LOG_FILENAME_FMT = "log_$pid_%Y%m%d.log";
void App::Init(int argc, char* argv[])
{
nowtime = time(nullptr);
signal(SIGPIPE, SIG_IGN);
this->argc = argc;
this->argv = argv;
if (!ParseOpt()) {
terminated = true;
if (instance_id <= 0) {
a8::XPrintf("friend_rankserver启动失败,缺少-i参数\n", {});
} else if (instance_id > MAX_INSTANCE_ID) {
a8::XPrintf("friend_rankserver启动失败,-i参数不能大于%d\n", {MAX_INSTANCE_ID});
}
return;
}
a8::XPrintf("rankserver starting instance_id:%d pid:%d\n", {instance_id, getpid()});
loop_mutex_ = new std::mutex();
loop_cond_ = new std::condition_variable();
msg_mutex_ = new std::mutex();
im_msg_mutex_ = new std::mutex();
srand(time(nullptr));
InitLog();
HandlerMgr::Instance()->Init();
a8::Timer::Instance()->Init();
PerfMonitor::Instance()->Init();
f8::MsgQueue::Instance()->Init();
f8::TGLog::Instance()->Init(a8::Format(PROJ_NAME_FMT, {}), false);
JsonDataMgr::Instance()->Init();
uuid.SetMachineId(instance_id);
MetaMgr::Instance()->Init();
a8::UdpLog::Instance()->Info("rankserver starting instance_id:%d pid:%d", {instance_id, getpid()});
}
void App::UnInit()
{
if (terminated) {
return;
}
MetaMgr::Instance()->UnInit();
JsonDataMgr::Instance()->UnInit();
f8::MsgQueue::Instance()->UnInit();
PerfMonitor::Instance()->UnInit();
a8::Timer::Instance()->UnInit();
HandlerMgr::Instance()->UnInit();
f8::TGLog::Instance()->UnInit();
UnInitLog();
A8_SAFE_DELETE(im_msg_mutex_);
A8_SAFE_DELETE(msg_mutex_);
A8_SAFE_DELETE(loop_cond_);
A8_SAFE_DELETE(loop_mutex_);
}
int App::Run()
{
if (terminated) {
return 0;
}
int ret = 0;
a8::UdpLog::Instance()->Info("rankserver running", {});
last_run_tick_ = a8::XGetTickCount();
int delta_time = 0;
while (!terminated) {
a8::tick_t begin_tick = a8::XGetTickCount();
nowtime = time(nullptr);
QuickExecute(delta_time);
SlowerExecute(delta_time);
Schedule();
a8::tick_t end_tick = a8::XGetTickCount();
if (end_tick - begin_tick > PerfMonitor::Instance()->max_run_delay_time) {
PerfMonitor::Instance()->max_run_delay_time = end_tick - begin_tick;
}
delta_time = end_tick - begin_tick;
}
return ret;
}
void App::AddSocketMsg(SocketFrom_e sockfrom,
int sockhandle,
long ip_saddr,
unsigned short msgid,
unsigned int seqid,
const char *msgbody,
int bodylen)
{
MsgNode *p = (MsgNode*) malloc(sizeof(MsgNode));
memset(p, 0, sizeof(MsgNode));
p->sockfrom = sockfrom;
p->ip_saddr = ip_saddr;
p->sockhandle = sockhandle;
p->msgid = msgid;
p->seqid = seqid;
p->buf = nullptr;
p->buflen = bodylen;
if (bodylen > 0) {
p->buf = (char*)malloc(bodylen);
memmove(p->buf, msgbody, bodylen);
}
msg_mutex_->lock();
if (bot_node_) {
bot_node_->next = p;
bot_node_ = p;
} else {
top_node_ = p;
bot_node_ = p;
}
++msgnode_size_;
msg_mutex_->unlock();
NotifyLoopCond();
}
void App::AddIMMsg(unsigned short imcmd, a8::XParams params)
{
IMMsgNode *p = new IMMsgNode;
p->msgid = imcmd;
p->params = params;
p->next = nullptr;
im_msg_mutex_->lock();
if (im_bot_node_) {
im_bot_node_->next = p;
im_bot_node_ = p;
} else {
im_top_node_ = p;
im_bot_node_ = p;
}
im_msg_mutex_->unlock();
NotifyLoopCond();
}
void App::QuickExecute(int delta_time)
{
ProcessIMMsg();
DispatchMsg();
a8::Timer::Instance()->Update();
}
void App::SlowerExecute(int delta_time)
{
}
void App::NotifyLoopCond()
{
std::unique_lock<std::mutex> lk(*loop_mutex_);
loop_cond_->notify_all();
}
void App::Schedule()
{
#if 1
{
std::unique_lock<std::mutex> lk(*loop_mutex_);
loop_cond_->wait_for(lk, std::chrono::milliseconds(1));
}
#else
std::unique_lock<std::mutex> lk(*loop_mutex_);
if (!HasTask()) {
int sleep_time = a8::Timer::Instance()->GetIdleableMillSeconds();
loop_cond_->wait_for(lk, std::chrono::milliseconds(sleep_time));
if (sleep_time > perf.max_timer_idle) {
perf.max_timer_idle = sleep_time;
}
}
#endif
}
bool App::HasTask()
{
{
if (!im_work_node_) {
im_msg_mutex_->lock();
if (!im_work_node_ && im_top_node_) {
im_work_node_ = im_top_node_;
im_top_node_ = nullptr;
im_bot_node_ = nullptr;
}
im_msg_mutex_->unlock();
}
if (im_work_node_) {
return true;
}
}
{
if (!work_node_) {
msg_mutex_->lock();
if (!work_node_ && top_node_) {
work_node_ = top_node_;
top_node_ = nullptr;
bot_node_ = nullptr;
}
msg_mutex_->unlock();
}
if (work_node_) {
return true;
}
}
return false;
}
void App::DispatchMsg()
{
long long starttick = a8::XGetTickCount();
if (!work_node_ && top_node_) {
msg_mutex_->lock();
work_node_ = top_node_;
top_node_ = nullptr;
bot_node_ = nullptr;
working_msgnode_size_ = msgnode_size_;
msg_mutex_->unlock();
}
f8::MsgHdr hdr;
while (work_node_) {
MsgNode *pdelnode = work_node_;
work_node_ = pdelnode->next;
hdr.msgid = pdelnode->msgid;
hdr.seqid = pdelnode->seqid;
hdr.socket_handle = pdelnode->sockhandle;
hdr.buf = pdelnode->buf;
hdr.buflen = pdelnode->buflen;
hdr.offset = 0;
hdr.ip_saddr = pdelnode->ip_saddr;
switch (pdelnode->sockfrom) {
case SF_IMServer:
{
ProcessIMServerMsg(hdr);
}
break;
}
if (pdelnode->buf) {
free(pdelnode->buf);
}
free(pdelnode);
working_msgnode_size_--;
if (a8::XGetTickCount() - starttick > 200) {
break;
}
}//end while
if (!work_node_) {
working_msgnode_size_ = 0;
}
}
void App::ProcessIMServerMsg(f8::MsgHdr& hdr)
{
f8::NetMsgHandler* handler = f8::GetNetMsgHandler(&HandlerMgr::Instance()->immsghandler,
hdr.msgid);
if (handler) {
#ifdef DEBUG
f8::DumpMsgToLog(hdr, handler, ">>>>>>IMS ");
#endif
}
}
void App::ProcessIMMsg()
{
if (!im_work_node_ && im_top_node_) {
im_msg_mutex_->lock();
im_work_node_ = im_top_node_;
im_top_node_ = nullptr;
im_bot_node_ = nullptr;
im_msg_mutex_->unlock();
}
while (im_work_node_) {
IMMsgNode *pdelnode = im_work_node_;
switch (im_work_node_->msgid) {
case f8::IM_SysMsgQueue:
{
const a8::XParams* param = (const a8::XParams*)pdelnode->params.param1.GetUserData();
f8::MsgQueue::Instance()->ProcessMsg(pdelnode->params.sender.GetInt(),
*param
);
delete param;
}
break;
case IM_ClientSocketDisconnect:
{
#if 0
PlayerMgr::Instance()->OnClientDisconnect(pdelnode->params);
#endif
}
break;
case IM_IMSSocketDisconnect:
{
}
break;
case IM_ExecGM:
{
HandlerMgr::Instance()->ProcGMMsg(pdelnode->params.param3,
pdelnode->params.sender,
pdelnode->params.param1.GetString(),
pdelnode->params.param2.GetString()
);
}
break;
}
im_work_node_ = im_work_node_->next;
delete pdelnode;
}
}
void App::InitLog()
{
std::string filename_fmt = PROJ_LOG_FILENAME_FMT;
a8::ReplaceString(filename_fmt, "$pid", a8::XValue(getpid()));
std::string proj_root_dir = a8::Format(PROJ_ROOT_FMT, {a8::Format(PROJ_NAME_FMT,{})});
std::string proj_log_root_dir = a8::Format(PROJ_LOG_ROOT_FMT, {a8::Format(PROJ_NAME_FMT, {})});
std::string log_file_name = a8::Format(PROJ_LOG_ROOT_FMT,
{a8::Format(PROJ_NAME_FMT, {})}) + "/" + filename_fmt;
a8::MkDir(proj_root_dir);
a8::MkDir(proj_log_root_dir);
a8::UdpLog::Instance()->SetLogFileName(log_file_name);
a8::UdpLog::Instance()->Init();
a8::UdpLog::Instance()->Info("proj_root_dir:%s", {proj_root_dir});
a8::UdpLog::Instance()->Info("proj_log_root_dir:%s", {proj_log_root_dir});
a8::UdpLog::Instance()->Info("log_file_name:%s", {log_file_name});
}
void App::UnInitLog()
{
a8::UdpLog::Instance()->UnInit();
}
bool App::ParseOpt()
{
int ch = 0;
while ((ch = getopt(argc, argv, "n:i:t:r:f:")) != -1) {
switch (ch) {
case 'i':
{
instance_id = a8::XValue(optarg);
}
break;
case 't':
{
is_test_mode = true;
test_param = 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;
}
}
return instance_id > 0;
}
long long App::NewUuid()
{
return uuid.Generate();
}
a8::XParams* App::AddContext(long long context_id)
{
context_hash_[context_id] = a8::XParams();
return GetContext(context_id);
}
void App::DelContext(long long context_id)
{
context_hash_.erase(context_id);
}
a8::XParams* App::GetContext(long long context_id)
{
auto itr = context_hash_.find(context_id);
return itr != context_hash_.end() ? &(itr->second) : nullptr;
}