449 lines
11 KiB
C++
449 lines
11 KiB
C++
#include "precompile.h"
|
|
|
|
#include <signal.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
#include <mutex>
|
|
#include <condition_variable>
|
|
#include <regex>
|
|
|
|
#include <a8/uuid.h>
|
|
#include <a8/collision.h>
|
|
#include <a8/magicenum.h>
|
|
#include <a8/lisp.h>
|
|
|
|
#include <f8/udplog.h>
|
|
#include <f8/netmsghandler.h>
|
|
#include <f8/comgr.h>
|
|
|
|
#include "app.h"
|
|
#include "handlermgr.h"
|
|
#include "httpproxy.h"
|
|
#include "playermgr.h"
|
|
#include "iomgr.h"
|
|
|
|
#include "ss_msgid.pb.h"
|
|
#include "ss_proto.pb.h"
|
|
|
|
#include "f8/msgqueue.h"
|
|
#include "f8/tglog.h"
|
|
#include "f8/httpclientpool.h"
|
|
#include "f8/btmgr.h"
|
|
#include <f8/timer.h>
|
|
|
|
struct MsgNode
|
|
{
|
|
SocketFrom_e sockfrom;
|
|
int sockhandle;
|
|
unsigned short msgid;
|
|
unsigned int seqid;
|
|
long ip_saddr;
|
|
char* buf;
|
|
int buflen;
|
|
MsgNode* next;
|
|
};
|
|
|
|
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 void SavePerfLog()
|
|
{
|
|
}
|
|
|
|
bool App::Init(int argc, char* argv[])
|
|
{
|
|
signal(SIGPIPE, SIG_IGN);
|
|
this->argc = argc;
|
|
this->argv = argv;
|
|
|
|
if (!ParseOpt()) {
|
|
if (node_id <= 0) {
|
|
a8::XPrintf("robotserver启动失败,缺少-n参数\n", {});
|
|
} else if (node_id > MAX_NODE_ID) {
|
|
a8::XPrintf("robotserver启动失败,-n参数不能大于%d\n", {MAX_NODE_ID});
|
|
} else if (instance_id <= 0) {
|
|
a8::XPrintf("robotserver启动失败,缺少-i参数\n", {});
|
|
} else if (instance_id > MAX_INSTANCE_ID) {
|
|
a8::XPrintf("robotserver启动失败,-i参数不能大于%d\n", {MAX_INSTANCE_ID});
|
|
}
|
|
return false;
|
|
}
|
|
int debug_mode = 0;
|
|
#ifdef DEBUG
|
|
debug_mode = 1;
|
|
#endif
|
|
a8::XPrintf("robotserver starting node_id: %d instance_id:%d pid:%d game_id:%d debug_mode:%d\n",
|
|
{
|
|
node_id,
|
|
instance_id,
|
|
getpid(),
|
|
GAME_ID,
|
|
debug_mode
|
|
});
|
|
|
|
loop_mutex_ = new std::mutex();
|
|
loop_cond_ = new std::condition_variable();
|
|
msg_mutex_ = new std::mutex();
|
|
|
|
srand(time(nullptr));
|
|
InitLog();
|
|
f8::MsgQueue::Instance()->Init();
|
|
HandlerMgr::Instance()->Init();
|
|
f8::Timer::Instance()->Init();
|
|
f8::TGLog::Instance()->Init(a8::Format(PROJ_NAME_FMT, {GAME_ID}), false, 0);
|
|
f8::CoMgr::Instance()->Init();
|
|
f8::HttpClientPool::Instance()->Init(MAX_ALL_HTTP_NUM, MAX_SYS_HTTP_NUM, MAX_USER_HTTP_NUM);
|
|
f8::BtMgr::Instance()->Init("exported");
|
|
#ifdef DEBUG
|
|
f8::BtMgr::Instance()->SetLogging(true);
|
|
#endif
|
|
uuid.SetMachineId((node_id - 1) * MAX_NODE_ID + instance_id);
|
|
IoMgr::Instance()->Init();
|
|
HttpProxy::Instance()->Init();
|
|
PlayerMgr::Instance()->Init();
|
|
{
|
|
int perf_log_time = 1000 * 30;
|
|
f8::Timer::Instance()->SetInterval
|
|
(perf_log_time,
|
|
[] (int event, const a8::Args* args)
|
|
{
|
|
if (a8::TIMER_EXEC_EVENT == event) {
|
|
SavePerfLog();
|
|
}
|
|
});
|
|
}
|
|
if (HasFlag(7)) {
|
|
f8::Timer::Instance()->SetTimeout
|
|
(
|
|
1000 * 60 * 1,
|
|
[] (int event, const a8::Args* args)
|
|
{
|
|
if (a8::TIMER_EXEC_EVENT == event) {
|
|
App::Instance()->terminated = true;
|
|
}
|
|
}
|
|
);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
void App::UnInit()
|
|
{
|
|
//const char* s2 = GetEnumString<int>();
|
|
//int i = static_cast<int>(Test_e::kFlyBuffId);
|
|
PlayerMgr::Instance()->UnInit();
|
|
IoMgr::Instance()->UnInit();
|
|
HttpProxy::Instance()->UnInit();
|
|
f8::BtMgr::Instance()->UnInit();
|
|
f8::HttpClientPool::Instance()->UnInit();
|
|
HandlerMgr::Instance()->UnInit();
|
|
f8::CoMgr::Instance()->UnInit();
|
|
f8::MsgQueue::Instance()->UnInit();
|
|
f8::Timer::Instance()->UnInit();
|
|
f8::TGLog::Instance()->UnInit();
|
|
UnInitLog();
|
|
|
|
FreeSocketMsgQueue();
|
|
A8_SAFE_DELETE(msg_mutex_);
|
|
A8_SAFE_DELETE(loop_cond_);
|
|
A8_SAFE_DELETE(loop_mutex_);
|
|
}
|
|
|
|
int App::Run()
|
|
{
|
|
int ret = 0;
|
|
f8::UdpLog::Instance()->Info("robotserver running", {});
|
|
last_run_tick_ = a8::XGetTickCount();
|
|
int delta_time = 0;
|
|
while (!terminated) {
|
|
a8::tick_t begin_tick = a8::XGetTickCount();
|
|
QuickExecute(delta_time);
|
|
SlowerExecute(delta_time);
|
|
Schedule();
|
|
a8::tick_t end_tick = a8::XGetTickCount();
|
|
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::QuickExecute(int delta_time)
|
|
{
|
|
f8::MsgQueue::Instance()->Update();
|
|
IoMgr::Instance()->Update();
|
|
DispatchMsg();
|
|
f8::Timer::Instance()->Update();
|
|
}
|
|
|
|
void App::SlowerExecute(int delta_time)
|
|
{
|
|
f8::CoMgr::Instance()->Update();
|
|
}
|
|
|
|
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_);
|
|
loop_cond_->wait_for(lk, std::chrono::milliseconds(1));
|
|
}
|
|
|
|
bool App::HasTask()
|
|
{
|
|
{
|
|
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 begin_tick = 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:
|
|
{
|
|
ProcessGameServerMsg(hdr);
|
|
}
|
|
break;
|
|
}
|
|
if (pdelnode->buf) {
|
|
free(pdelnode->buf);
|
|
}
|
|
free(pdelnode);
|
|
working_msgnode_size_--;
|
|
if (a8::XGetTickCount() - begin_tick > 200) {
|
|
break;
|
|
}
|
|
}//end while
|
|
|
|
if (!work_node_) {
|
|
working_msgnode_size_ = 0;
|
|
}
|
|
a8::tick_t end_tick = a8::XGetTickCount();
|
|
}
|
|
|
|
void App::ProcessGameServerMsg(f8::MsgHdr& hdr)
|
|
{
|
|
f8::NetMsgHandler* handler = f8::GetNetMsgHandler(&HandlerMgr::Instance()->gsmsghandler,
|
|
hdr.msgid);
|
|
if (handler) {
|
|
switch (handler->handlerid) {
|
|
case HID_Player:
|
|
{
|
|
auto player = PlayerMgr::Instance()->GetPlayerBySocketHandle(hdr.socket_handle);
|
|
if (player) {
|
|
ProcessNetMsg(handler, player.get(), hdr);
|
|
}
|
|
}
|
|
break;
|
|
default:
|
|
{
|
|
abort();
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
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,{GAME_ID})});
|
|
std::string proj_log_root_dir = a8::Format(PROJ_LOG_ROOT_FMT, {a8::Format(PROJ_NAME_FMT, {GAME_ID})});
|
|
std::string log_file_name = a8::Format(PROJ_LOG_ROOT_FMT,
|
|
{a8::Format(PROJ_NAME_FMT, {GAME_ID})}) + "/" + filename_fmt;
|
|
|
|
a8::MkDir(proj_root_dir);
|
|
a8::MkDir(proj_log_root_dir);
|
|
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();
|
|
}
|
|
|
|
bool App::ParseOpt()
|
|
{
|
|
int ch = 0;
|
|
while ((ch = getopt(argc, argv, "i:t:r:f:n:r:")) != -1) {
|
|
switch (ch) {
|
|
case 'n':
|
|
{
|
|
node_id = a8::XValue(optarg);
|
|
}
|
|
break;
|
|
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;
|
|
case 'r':
|
|
{
|
|
robot_num = a8::XValue(optarg);
|
|
robot_num = std::min(robot_num, 100);
|
|
robot_num = std::max(robot_num, 1);
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
return instance_id > 0 && node_id > 0;
|
|
}
|
|
|
|
long long App::NewUuid()
|
|
{
|
|
return uuid.Generate();
|
|
}
|
|
|
|
bool App::HasFlag(int flag)
|
|
{
|
|
return flags.find(flag) != flags.end();
|
|
}
|
|
|
|
void App::SetFlag(int flag)
|
|
{
|
|
flags.insert(flag);
|
|
}
|
|
|
|
void App::UnSetFlag(int flag)
|
|
{
|
|
flags.erase(flag);
|
|
}
|
|
|
|
void App::FreeSocketMsgQueue()
|
|
{
|
|
msg_mutex_->lock();
|
|
if (!work_node_) {
|
|
work_node_ = top_node_;
|
|
top_node_ = nullptr;
|
|
bot_node_ = nullptr;
|
|
}
|
|
while (work_node_) {
|
|
MsgNode* pdelnode = work_node_;
|
|
work_node_ = work_node_->next;
|
|
if (pdelnode->buf) {
|
|
free(pdelnode->buf);
|
|
}
|
|
free(pdelnode);
|
|
if (!work_node_) {
|
|
work_node_ = top_node_;
|
|
top_node_ = nullptr;
|
|
bot_node_ = nullptr;
|
|
}
|
|
}
|
|
msg_mutex_->unlock();
|
|
}
|
|
|
|
long long App::AllocTempHeroUniId()
|
|
{
|
|
if (curr_uniid_ < 1000) {
|
|
curr_uniid_ = 1000;
|
|
}
|
|
if (curr_uniid_ > 100000000) {
|
|
curr_uniid_ = 1001;
|
|
}
|
|
++curr_uniid_;
|
|
return -curr_uniid_;
|
|
}
|
|
|
|
long long App::AllocTempWeaponUniId()
|
|
{
|
|
if (curr_uniid_ < 1000) {
|
|
curr_uniid_ = 1000;
|
|
}
|
|
if (curr_uniid_ > 100000000) {
|
|
curr_uniid_ = 1000;
|
|
}
|
|
++curr_uniid_;
|
|
return -curr_uniid_;
|
|
}
|