2019-01-12 18:48:10 +08:00

402 lines
10 KiB
C++

#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 "GSListener.h"
#include "jsondatamgr.h"
#include "handlermgr.h"
#include "ss_msgid.pb.h"
#include "ss_proto.pb.h"
#include "dbpool.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 = "/data/logs/%s/logs";
const char* const PROJ_LOG_FILENAME = "log_$pid_%Y%m%d.log";
static void SavePerfLog()
{
a8::UdpLog::Instance()->Info(" max_run_delay_time:%d max_timer_idle:%d "
"in_data_size:%d out_data_size:%d msgnode_size:%d read_count:%d",
{
App::Instance()->perf.max_run_delay_time,
App::Instance()->perf.max_timer_idle,
App::Instance()->perf.in_data_size,
App::Instance()->perf.out_data_size,
App::Instance()->msgnode_size_,
App::Instance()->perf.read_count,
});
#if 1
App::Instance()->perf.max_run_delay_time = 0;
App::Instance()->perf.max_timer_idle = 0;
#else
App::Instance()->perf = PerfMonitor();
#endif
}
void App::Init(int argc, char* argv[])
{
signal(SIGPIPE, SIG_IGN);
this->argc = argc;
this->argv = argv;
if (!ParseOpt()) {
terminated = true;
a8::XPrintf("masterserver启动失败,缺少-i参数\n", {});
return;
}
a8::XPrintf("masterserver 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();
JsonDataMgr::Instance()->Init();
DBPool::Instance()->Init();
GSListener::Instance()->Init();
uuid.SetMachineId(instance_id);
a8::UdpLog::Instance()->Info("masterserver starting instance_id:%d pid:%d", {instance_id, getpid()});
{
int perf_log_time = 1000 * 60 * 5;
if (getenv("is_dev_env")) {
perf_log_time = 1000 * 10;
}
a8::Timer::Instance()->AddRepeatTimer(perf_log_time,
a8::XParams(),
[] (const a8::XParams& param)
{
SavePerfLog();
});
}
}
void App::UnInit()
{
if (terminated) {
return;
}
GSListener::Instance()->UnInit();
DBPool::Instance()->UnInit();
JsonDataMgr::Instance()->UnInit();
a8::Timer::Instance()->UnInit();
HandlerMgr::Instance()->UnInit();
UnInitLog();
delete im_msg_mutex_;
im_msg_mutex_ = nullptr;
delete msg_mutex_;
msg_mutex_ = nullptr;
delete loop_cond_;
loop_cond_ = nullptr;
delete loop_mutex_;
loop_mutex_ = nullptr;
}
int App::Run()
{
if (terminated) {
return 0;
}
int ret = 0;
a8::UdpLog::Instance()->Info("masterserver running", {});
while (!terminated) {
a8::tick_t begin_tick = a8::XGetTickCount();
QuickExecute();
SlowerExecute();
a8::tick_t end_tick = a8::XGetTickCount();
if (end_tick - begin_tick > perf.max_run_delay_time) {
perf.max_run_delay_time = end_tick - begin_tick;
}
Schedule();
}
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()
{
ProcessIMMsg();
DispatchMsg();
a8::Timer::Instance()->Update();
}
void App::SlowerExecute()
{
}
void App::NotifyLoopCond()
{
std::unique_lock<std::mutex> lk(*loop_mutex_);
loop_cond_->notify_all();
}
void App::Schedule()
{
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;
}
}
}
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_GameServer:
{
ProcessGSMsg(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::ProcessGSMsg(f8::MsgHdr& hdr)
{
f8::NetMsgHandler* handler = GetNetMsgHandler(&HandlerMgr::Instance()->gsmsghandler,
hdr.msgid);
if (handler) {
switch (handler->handlerid) {
case HID_DBPool:
{
ProcessNetMsg(handler, DBPool::Instance(), hdr);
}
break;
}
}
}
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 IM_ExecGM:
{
HandlerMgr::Instance()->ProcGMMsg(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;
if (getenv("is_dev_env")) {
a8::ReplaceString(filename_fmt, "$pid", a8::XValue(0));
} else {
a8::ReplaceString(filename_fmt, "$pid", a8::XValue(getpid()));
}
a8::MkDir(a8::Format(PROJ_ROOT, {PROJ_NAME}));
a8::MkDir(a8::Format(PROJ_LOG_ROOT, {PROJ_NAME}));
a8::UdpLog::Instance()->SetLogFileName(a8::Format(PROJ_LOG_ROOT, {PROJ_NAME}) + "/" + filename_fmt);
a8::UdpLog::Instance()->Init();
}
void App::UnInitLog()
{
a8::UdpLog::Instance()->UnInit();
}
bool App::ParseOpt()
{
int ch = 0;
while ((ch = getopt(argc, argv, "i:")) != -1) {
switch (ch) {
case 'i':
{
instance_id = a8::XValue(optarg);
}
break;
}
}
return instance_id > 0;
}
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;
}