This commit is contained in:
azw 2023-09-13 13:26:28 +00:00
parent bd7e0bea6b
commit 4c8d3488f4
16 changed files with 204 additions and 635 deletions

View File

@ -23,12 +23,13 @@ else()
message(LIB_DIR: ${LIB_DIR} )
endif()
set(CMAKE_CXX_FLAGS_RELEASE "-O3 -Wall -g -std=gnu++11 -DGAME_ID=${GAME_ID} -DNDEBUG")
set(CMAKE_CXX_FLAGS_DEBUG "-Wall -g -std=gnu++11 -DGAME_ID=${GAME_ID} -DDEBUG")
set(CMAKE_CXX_FLAGS_RELEASE "-O3 -Wall -g -std=gnu++1z -DGAME_ID=${GAME_ID} -DNDEBUG")
set(CMAKE_CXX_FLAGS_DEBUG "-Wall -g -std=gnu++1z -DGAME_ID=${GAME_ID} -DDEBUG")
include_directories(
AFTER
../../third_party/a8engine
../../third_party/a8
../../third_party/f8
/usr/include/mysql
/usr/include/jsoncpp
/usr/include/hiredis
@ -48,11 +49,11 @@ link_directories(
../../third_party/behaviac/lib/${LIB_DIR}
)
aux_source_directory(../../third_party/a8engine/a8
aux_source_directory(../../third_party/a8/a8
SRC_LIST
)
aux_source_directory(../../third_party/framework/cpp
aux_source_directory(../../third_party/f8/f8
SRC_LIST
)
@ -110,7 +111,7 @@ if (CMAKE_BUILD_TYPE STREQUAL "Debug")
hiredis
tinyxml2
tcmalloc
behaviac_gcc_release
behaviac_gcc_debug
)
else()
target_link_libraries(

View File

@ -4,7 +4,7 @@
#include <a8/mixedsession.h>
#include <a8/tcplistener.h>
#include "framework/cpp/netmsghandler.h"
#include <f8/netmsghandler.h>
#include "app.h"
#include "GGListener.h"
@ -43,13 +43,14 @@ public:
}
if (warning) {
a8::UdpLog::Instance()->Warning("收到client非法数据包", {});
f8::UdpLog::Instance()->Warning("收到client非法数据包", {});
}
}
virtual void OnRawHttpGet(const std::string& url, const std::string& querystr,
std::string& response) override
{
#if 0
App::Instance()->AddIMMsg(IM_ExecGM,
a8::XParams()
.SetSender(socket_handle)
@ -57,14 +58,17 @@ public:
.SetParam2(querystr)
.SetParam3(saddr)
);
#endif
}
virtual void OnDisConnect() override
{
#if 0
App::Instance()->AddIMMsg(IM_ClientSocketDisconnect,
a8::XParams()
.SetSender(socket_handle)
.SetParam1(1));
#endif
}
};
@ -76,7 +80,7 @@ static void CreateGameClientSocket(a8::TcpSession **p)
static void GSListeneron_error(a8::TcpListener*, int type, int errorid)
{
a8::UdpLog::Instance()->Debug("GGListeneron_error %d %d", {type, errorid});
f8::UdpLog::Instance()->Debug("GGListeneron_error %d %d", {type, errorid});
}
void GGListener::Init()

View File

@ -1,5 +1,7 @@
#pragma once
#include <f8/protoutils.h>
//game client listener
namespace a8
{

View File

@ -6,11 +6,12 @@
#include <mutex>
#include <condition_variable>
#include <a8/redis.h>
#include <a8/timer.h>
#include <a8/uuid.h>
#include "framework/cpp/netmsghandler.h"
#include <f8/netmsghandler.h>
#include <f8/msgqueue.h>
#include <f8/tglog.h>
#include <f8/timer.h>
#include "app.h"
#include "jsondatamgr.h"
@ -21,9 +22,6 @@
#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;
@ -36,20 +34,9 @@ struct MsgNode
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";
static void SavePerfLog()
{
a8::UdpLog::Instance()->Info("max_mainloop_rundelay:%d",
f8::UdpLog::Instance()->Info("max_mainloop_rundelay:%d",
{
App::Instance()->perf.max_run_delay_time,
});
@ -57,119 +44,51 @@ static void SavePerfLog()
App::Instance()->perf.max_timer_idle = 0;
}
void App::Init(int argc, char* argv[])
const std::string App::GetPkgName()
{
#if 0
{
Vector2D dir;
dir.Normalize();
}
#endif
signal(SIGPIPE, SIG_IGN);
this->argc = argc;
this->argv = argv;
return a8::Format("game%d_masterserver", {GAME_ID});
}
if (!ParseOpt()) {
terminated = true;
if (node_id <= 0) {
a8::XPrintf("gameserver启动失败,缺少-n参数\n", {});
} else if (node_id > MAX_NODE_ID) {
a8::XPrintf("gameserver启动失败,-n参数不能大于%d\n", {MAX_NODE_ID});
} else if (instance_id <= 0) {
a8::XPrintf("gameserver启动失败,缺少-i参数\n", {});
} else if (instance_id > MAX_INSTANCE_ID) {
a8::XPrintf("gameserver启动失败,-i参数不能大于%d\n", {MAX_INSTANCE_ID});
}
return;
}
a8::XPrintf("masterserver starting instance_id:%d pid:%d game_id:%d\n", {instance_id, getpid(), GAME_ID});
loop_mutex_ = new std::mutex();
loop_cond_ = new std::condition_variable();
void App::Init()
{
msg_mutex_ = new std::mutex();
im_msg_mutex_ = new std::mutex();
srand(time(nullptr));
InitLog();
HandlerMgr::Instance()->Init();
a8::Timer::Instance()->Init();
f8::MsgQueue::Instance()->Init();
f8::TGLog::Instance()->Init(a8::Format(PROJ_NAME_FMT, {GAME_ID}), false);
JsonDataMgr::Instance()->Init();
LoadSeparateChannelConfig();
uuid.SetMachineId((node_id - 1) * MAX_NODE_ID + instance_id);
GGListener::Instance()->Init();
GSMgr::Instance()->Init();
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();
});
}
{
a8::Timer::Instance()->AddRepeatTimer(1000 * 60 * 5,
a8::XParams(),
[] (const a8::XParams& param)
{
App::Instance()->LoadSeparateChannelConfig();
});
f8::Timer::Instance()->SetInterval
(perf_log_time,
[] (int e, const a8::Args* args)
{
if (a8::TIMER_EXEC_EVENT == e) {
SavePerfLog();
}
});
}
}
void App::UnInit()
{
if (terminated) {
return;
}
GSMgr::Instance()->UnInit();
GGListener::Instance()->UnInit();
JsonDataMgr::Instance()->UnInit();
f8::MsgQueue::Instance()->UnInit();
a8::Timer::Instance()->UnInit();
HandlerMgr::Instance()->UnInit();
f8::TGLog::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()
void App::Update()
{
if (terminated) {
return 0;
}
int ret = 0;
a8::UdpLog::Instance()->Info("masterserver running", {});
last_run_tick_ = a8::XGetTickCount();
int delta_time = 0;
while (!terminated) {
a8::tick_t begin_tick = a8::XGetTickCount();
Global::g_nowtime = time(nullptr);
QuickExecute(delta_time);
SlowerExecute(delta_time);
Schedule();
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;
}
delta_time = end_tick - begin_tick;
}
return ret;
QuickExecute();
SlowerExecute();
}
void App::AddSocketMsg(SocketFrom_e sockfrom,
@ -203,79 +122,20 @@ void App::AddSocketMsg(SocketFrom_e sockfrom,
}
++msgnode_size_;
msg_mutex_->unlock();
NotifyLoopCond();
f8::App::Instance()->NotifyLoopCond();
}
void App::AddIMMsg(unsigned short imcmd, a8::XParams params)
void App::QuickExecute()
{
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::SlowerExecute()
{
}
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();
@ -340,8 +200,9 @@ void App::DispatchMsg()
void App::ProcessGameGateMsg(f8::MsgHdr& hdr)
{
f8::NetMsgHandler* handler = f8::GetNetMsgHandler(&HandlerMgr::Instance()->ggmsghandler,
hdr.msgid);
f8::NetMsgHandler* handler = f8::GetNetMsgHandler
(&HandlerMgr::Instance()->ggmsghandler,
hdr.msgid);
if (handler) {
switch (handler->handlerid) {
case HID_GSMgr:
@ -350,160 +211,3 @@ void App::ProcessGameGateMsg(f8::MsgHdr& hdr)
}
}
}
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_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,{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);
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 '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;
}
}
return instance_id > 0 && node_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;
}
bool App::IsSeparateChannel(int channel)
{
return separate_channel_hash_.find(channel) != separate_channel_hash_.end();
}
void App::LoadSeparateChannelConfig()
{
std::string data;
a8::ReadStringFromFile("../config/separate_channels.csv", data);
std::vector<std::string> strings;
a8::Split(data, strings, ',');
separate_channel_hash_.clear();
for (std::string& str : strings) {
int channel = a8::XValue(str);
if (channel != 0) {
separate_channel_hash_.insert(channel);
}
}
a8::UdpLog::Instance()->Info("LoadSeparateChannelConfig channelids:%s",
{
GetSeparateChannelConfigData()
});
}
std::string App::GetSeparateChannelConfigData()
{
std::string data;
for (int channel : separate_channel_hash_) {
data += a8::XValue(channel).GetString() + ",";
}
return data;
}

View File

@ -1,10 +1,11 @@
#pragma once
#include <a8/uuid.h>
#include <a8/singleton.h>
#include <f8/app.h>
struct MsgNode;
struct IMMsgNode;
class App : public a8::Singleton<App>
class App : public a8::Singleton<App>, public f8::UserApp
{
private:
App() {};
@ -12,10 +13,11 @@ private:
public:
void Init(int argc, char* argv[]);
void UnInit();
int Run();
virtual const std::string GetPkgName() override;
virtual void Init() override;
virtual void UnInit() override;
virtual void Update() override;
virtual bool HasTask() override;
void AddSocketMsg(SocketFrom_e sockfrom,
int sockhandle,
@ -24,68 +26,25 @@ public:
unsigned int seqid,
const char *msgbody,
int bodylen);
void AddIMMsg(unsigned short imcmd, a8::XParams params);
void NotifyLoopCond();
long long NewUuid();
a8::XParams* AddContext(long long context_id);
void DelContext(long long context_id);
a8::XParams* GetContext(long long context_id);
bool IsSeparateChannel(int channel);
void LoadSeparateChannelConfig();
std::string GetSeparateChannelConfigData();
private:
void QuickExecute(int delta_time);
void SlowerExecute(int delta_time);
void Schedule();
bool HasTask();
void QuickExecute();
void SlowerExecute();
void DispatchMsg();
void ProcessIMMsg();
void ProcessGameGateMsg(f8::MsgHdr& hdr);
void InitLog();
void UnInitLog();
bool ParseOpt();
public:
int argc = 0;
char** argv = nullptr;
volatile bool terminated = false;
PerfMonitor perf;
a8::uuid::SnowFlake uuid;
public:
int node_id = 0;
int instance_id = 0;
bool is_test_mode = false;
int test_param = 0;
std::set<int> flags;
private:
long long last_run_tick_ = 0;
std::mutex *loop_mutex_ = nullptr;
std::condition_variable *loop_cond_ = nullptr;
std::mutex *msg_mutex_ = nullptr;
MsgNode* top_node_ = nullptr;
MsgNode* bot_node_ = nullptr;
MsgNode* work_node_ = nullptr;
std::mutex* im_msg_mutex_ = nullptr;
IMMsgNode* im_top_node_ = nullptr;
IMMsgNode* im_bot_node_ = nullptr;
IMMsgNode* im_work_node_ = nullptr;
std::set<int> separate_channel_hash_;
std::map<long long, a8::XParams> context_hash_;
public:
int msgnode_size_ = 0 ;
int working_msgnode_size_ = 0;
};

View File

@ -1,13 +0,0 @@
#pragma once
/*
http的错误码设计
0
<400:
>500:
*/
enum ErrorCode_e
{
ERRNO_OK = 0,
};

View File

@ -1,37 +0,0 @@
#include "precompile.h"
#include "global.h"
int g_hint_flags = 0;
bool Global::IsVirtualItem(int itemid)
{
return (itemid == VID_Item_Exp ||
itemid == VID_Item_Gold ||
itemid == VID_Soul_Stone ||
itemid == VID_Pickaxe);
}
time_t Global::BetweenDays(time_t time1, time_t time2)
{
return (time1 + g_time_zone*3600)/3600/24 - (time2 + g_time_zone*3600)/3600/24;
}
time_t Global::GetDaySeconds(time_t time, int incdays)
{
return time_t((time + g_time_zone * 3600)/3600/24 + incdays) * 3600 * 24 - 3600 * g_time_zone;
}
bool Global::IsTimeToReset(int time)
{
return BetweenDays(g_nowtime - 60 * SYS_RESET_TIME, time - 60 * SYS_RESET_TIME) > 0;
}
int Global::g_nowtime = time(nullptr);
int Global::g_time_zone = 8;
bool Global::g_shutdown = false;
bool IsValidSlotId(int slot_id)
{
return slot_id >= 0 && slot_id < IS_END;
}

View File

@ -1,22 +0,0 @@
#pragma once
extern int g_hint_flags;
class Global : public a8::Singleton<Global>
{
private:
Global() {};
friend class a8::Singleton<Global>;
public:
static bool IsVirtualItem(int itemid);
static bool IsTimeToReset(int time);
static time_t BetweenDays(time_t time1, time_t time2);
static time_t GetDaySeconds(time_t time, int incdays = 0);
static int g_nowtime;
static int g_time_zone; // 默认东八区
static bool g_shutdown;
};
bool IsValidSlotId(int slot_id);

View File

@ -1,22 +1,43 @@
#include "precompile.h"
#include <a8/mutable_xobject.h>
#include <a8/timer.h>
#include <f8/utils.h>
#include <f8/jsonhttprequest.h>
#include "gsmgr.h"
#include "app.h"
#include "GGListener.h"
#include "framework/cpp/utils.h"
#include "ss_proto.pb.h"
struct GSNode
{
std::string key;
long long node_idx = 0;
int node_id = 0;
int instance_id = 0;
int room_num = 0;
int alive_count = 0;
int online_num = 0;
std::string ip;
int port = 0;
bool servicing = false;
int version = 0;
long long last_active_tick = 0;
};
void GSMgr::Init()
{
a8::Timer::Instance()->AddRepeatTimer(1000 * 2,
a8::XParams(),
[] (const a8::XParams& param)
{
GSMgr::Instance()->ClearTimeOutNode();
});
f8::Timer::Instance()->SetIntervalEx
(1000 * 2,
[] (int e, const a8::Args* args)
{
if (e == a8::TIMER_EXEC_EVENT) {
GSMgr::Instance()->ClearTimeOutNode();
}
},
&timer_attacher_);
}
void GSMgr::UnInit()
@ -26,43 +47,31 @@ void GSMgr::UnInit()
void GSMgr::_SS_WSP_RequestTargetServer(f8::MsgHdr& hdr, const ss::SS_WSP_RequestTargetServer& msg)
{
int channel = f8::ExtractChannelIdFromAccountId(msg.account_id());
if (!App::Instance()->IsSeparateChannel(channel)) {
channel = 0;
}
#if 1
int src_channel = f8::ExtractChannelIdFromAccountId(msg.account_id());
if (channel == 0 &&
src_channel == kTouTiaoChannelId &&
msg.proto_version() == 2019071502) {
channel = src_channel;
}
#endif
ss::SS_MS_ResponseTargetServer respmsg;
respmsg.set_context_id(msg.context_id());
if (msg.is_reconnect()) {
GSNode* node = GetNodeByNodeKey(msg.server_info());
std::shared_ptr<GSNode> node = GetNodeByNodeKey(msg.server_info());
if (node) {
respmsg.set_host(node->ip);
respmsg.set_port(node->port);
} else {
respmsg.set_error_code(1);
respmsg.set_error_msg("无法分配节点");
respmsg.set_error_msg("cant alloc node");
}
} else {
GSNode* node = GetNodeByTeamId(msg.team_id());
if (node && node->channel == channel) {
std::shared_ptr<GSNode> node = GetNodeByTeamId(msg.team_id());
if (node) {
respmsg.set_host(node->ip);
respmsg.set_port(node->port);
} else {
node = AllocNode(channel);
node = AllocNode();
if (node) {
respmsg.set_host(node->ip);
respmsg.set_port(node->port);
team_hash_[msg.team_id()] = node;
} else {
respmsg.set_error_code(1);
respmsg.set_error_msg("无法分配节点");
respmsg.set_error_msg("cant alloc node");
}
}
}
@ -77,52 +86,52 @@ void GSMgr::_SS_Ping(f8::MsgHdr& hdr, const ss::SS_Ping& msg)
void GSMgr::___GSReport(f8::JsonHttpRequest* request)
{
std::string ip = request->request.Get("ip");
int port = request->request.Get("port");
int alive_count = request->request.Get("alive_count");
int online_num = request->request.Get("online_num");
int room_num = request->request.Get("room_num");
int instance_id = request->request.Get("instance_id");
int node_id = request->request.Get("node_id");
int channel = request->request.Get("channel");
bool servicing = request->request.Get("servicing");
std::string ip = request->params->Get("ip");
int port = request->params->Get("port");
int alive_count = request->params->Get("alive_count");
int online_num = request->params->Get("online_num");
int room_num = request->params->Get("room_num");
int instance_id = request->params->Get("instance_id");
int node_id = request->params->Get("node_id");
int version = request->params->Get("version");
bool servicing = request->params->Get("servicing");
std::string key = ip + ":" + a8::XValue(port).GetString();
auto itr = node_key_hash_.find(key);
if (itr != node_key_hash_.end()) {
if (itr->second.online_num != online_num ||
itr->second.room_num != room_num ||
itr->second.alive_count != alive_count ||
itr->second.servicing != servicing ||
itr->second.channel != channel
if (itr->second->online_num != online_num ||
itr->second->room_num != room_num ||
itr->second->alive_count != alive_count ||
itr->second->servicing != servicing ||
itr->second->version != version
) {
itr->second.online_num = online_num;
itr->second.room_num = room_num;
itr->second.servicing = servicing;
if (itr->second.channel != channel) {
itr->second.channel = channel;
OnChannelChange(&itr->second);
itr->second->online_num = online_num;
itr->second->room_num = room_num;
itr->second->servicing = servicing;
if (itr->second->version != version) {
itr->second->version = version;
OnVersionChange(itr->second);
}
RearrangeNode();
}
itr->second.alive_count = alive_count;
itr->second.last_active_tick = a8::XGetTickCount();
itr->second->alive_count = alive_count;
itr->second->last_active_tick = a8::XGetTickCount();
} else {
GSNode gs;
gs.key = key;
gs.node_id = node_id;
gs.node_idx = App::Instance()->NewUuid();
gs.instance_id = instance_id;
gs.alive_count = alive_count;
gs.online_num = online_num;
gs.room_num = room_num;
gs.ip = ip;
gs.port = port;
gs.servicing = servicing;
gs.channel = channel;
gs.last_active_tick = a8::XGetTickCount();
auto gs = std::make_shared<GSNode>();
gs->key = key;
gs->node_id = node_id;
gs->node_idx = f8::App::Instance()->NewNodeUuid();
gs->instance_id = instance_id;
gs->alive_count = alive_count;
gs->online_num = online_num;
gs->room_num = room_num;
gs->ip = ip;
gs->port = port;
gs->servicing = servicing;
gs->version = version;
gs->last_active_tick = a8::XGetTickCount();
node_key_hash_[key] = gs;
AddNodeToSortedNodes(&node_key_hash_[key]);
AddNodeToSortedNodes(gs);
RearrangeNode();
}
@ -133,34 +142,32 @@ void GSMgr::___GSReport(f8::JsonHttpRequest* request)
void GSMgr::___GSList(f8::JsonHttpRequest* request)
{
{
a8::MutableXObject* node_list = a8::MutableXObject::NewArray();
auto node_list = a8::MutableXObject::CreateArray();
for (auto& pair : node_key_hash_) {
a8::MutableXObject* node = a8::MutableXObject::NewObject();
node->SetVal("node_id", pair.second.node_id);
node->SetVal("instance_id", pair.second.instance_id);
node->SetVal("room_num", pair.second.room_num);
node->SetVal("alive_count", pair.second.alive_count);
node->SetVal("online_num", pair.second.online_num);
node->SetVal("ip", pair.second.ip);
node->SetVal("port", pair.second.port);
node->SetVal("servicing", pair.second.servicing);
node->SetVal("channel", pair.second.channel);
auto node = a8::MutableXObject::CreateObject();
node->SetVal("node_id", pair.second->node_id);
node->SetVal("instance_id", pair.second->instance_id);
node->SetVal("room_num", pair.second->room_num);
node->SetVal("alive_count", pair.second->alive_count);
node->SetVal("online_num", pair.second->online_num);
node->SetVal("ip", pair.second->ip);
node->SetVal("port", pair.second->port);
node->SetVal("servicing", pair.second->servicing);
node->SetVal("version", pair.second->version);
node_list->Push(*node);
delete node;
}
request->resp_xobj->SetVal("errcode", 0);
request->resp_xobj->SetVal("errmsg", "");
request->resp_xobj->SetVal("node_list", *node_list);
delete node_list;
}
{
a8::MutableXObject* node_list = a8::MutableXObject::NewArray();
auto node_list = a8::MutableXObject::CreateArray();
for (auto& pair : sorted_node_hash_) {
for (GSNode* gs_node : pair.second) {
a8::MutableXObject* node = a8::MutableXObject::NewObject();
for (std::shared_ptr<GSNode> gs_node : pair.second) {
auto node = a8::MutableXObject::CreateObject();
node->SetVal("node_id", gs_node->node_id);
node->SetVal("instance_id", gs_node->instance_id);
node->SetVal("room_num", gs_node->room_num);
@ -168,39 +175,37 @@ void GSMgr::___GSList(f8::JsonHttpRequest* request)
node->SetVal("ip", gs_node->ip);
node->SetVal("port", gs_node->port);
node->SetVal("servicing", gs_node->servicing);
node->SetVal("channel", gs_node->channel);
node->SetVal("version", gs_node->version);
node_list->Push(*node);
delete node;
}
}
request->resp_xobj->SetVal("errcode", 0);
request->resp_xobj->SetVal("errmsg", "");
request->resp_xobj->SetVal("sorted_node_list", *node_list);
delete node_list;
}
}
GSNode* GSMgr::GetNodeByTeamId(const std::string& team_id)
std::shared_ptr<GSNode> GSMgr::GetNodeByTeamId(const std::string& team_id)
{
auto itr = team_hash_.find(team_id);
return itr != team_hash_.end() ? itr->second : nullptr;
}
GSNode* GSMgr::GetNodeByNodeKey(const std::string& node_key)
std::shared_ptr<GSNode> GSMgr::GetNodeByNodeKey(const std::string& node_key)
{
auto itr = node_key_hash_.find(node_key);
return itr != node_key_hash_.end() ? &itr->second : nullptr;
return itr != node_key_hash_.end() ? itr->second : nullptr;
}
GSNode* GSMgr::AllocNode(int channel)
std::shared_ptr<GSNode> GSMgr::AllocNode()
{
std::vector<GSNode*>* sorted_nodes = GetSortedNodesByChannel(channel);
std::vector<std::shared_ptr<GSNode>>* sorted_nodes = GetSortedNodes();
if (sorted_nodes && !sorted_nodes->empty()) {
size_t rnd = std::min((size_t)2, sorted_nodes->size());
if (rnd >= 2) {
GSNode* n1 = sorted_nodes->at(sorted_nodes->size() - 1);
GSNode* n2 = sorted_nodes->at(sorted_nodes->size() - 2);
std::shared_ptr<GSNode> n1 = sorted_nodes->at(sorted_nodes->size() - 1);
std::shared_ptr<GSNode> n2 = sorted_nodes->at(sorted_nodes->size() - 2);
if (n1->online_num < 100 && n2->online_num < 100) {
if (n1->instance_id < n2->instance_id) {
if (n1->servicing) {
@ -221,13 +226,12 @@ GSNode* GSMgr::AllocNode(int channel)
--idx;
}
}
a8::UdpLog::Instance()->Warning
("节点分配失败 sorted_node_hashlist.size:%d node_list.size:%d sorted_node.size:%d channel:%d",
f8::UdpLog::Instance()->Warning
("节点分配失败 sorted_node_hashlist.size:%d node_list.size:%d sorted_node.size:%d",
{
sorted_node_hash_.size(),
node_key_hash_.size(),
sorted_nodes ? sorted_nodes->size() : 0,
channel
});
return nullptr;
}
@ -236,7 +240,7 @@ void GSMgr::RearrangeNode()
{
for (auto& pair : sorted_node_hash_) {
std::sort(pair.second.begin(), pair.second.end(),
[] (const GSNode* a, const GSNode* b)
[] (const std::shared_ptr<GSNode> a, const std::shared_ptr<GSNode> b)
{
if (a->servicing && b->servicing) {
if (a->online_num < b->online_num) {
@ -267,13 +271,13 @@ void GSMgr::RearrangeNode()
void GSMgr::ClearTimeOutNode()
{
std::vector<GSNode*> time_out_nodes;
std::vector<std::shared_ptr<GSNode>> time_out_nodes;
for (auto& pair : node_key_hash_) {
if (a8::XGetTickCount() - pair.second.last_active_tick > 1000 * 5) {
time_out_nodes.push_back(&pair.second);
if (a8::XGetTickCount() - pair.second->last_active_tick > 1000 * 5) {
time_out_nodes.push_back(pair.second);
}
}
for (GSNode* node : time_out_nodes) {
for (std::shared_ptr<GSNode> node : time_out_nodes) {
RemoveNodeFromSortedNodes(node);
{
std::vector<std::string> deleted_teams;
@ -291,20 +295,20 @@ void GSMgr::ClearTimeOutNode()
RearrangeNode();
}
void GSMgr::AddNodeToSortedNodes(GSNode* node)
void GSMgr::AddNodeToSortedNodes(std::shared_ptr<GSNode> node)
{
auto itr = sorted_node_hash_.find(node->channel);
auto itr = sorted_node_hash_.find(node->version);
if (itr != sorted_node_hash_.end()) {
itr->second.push_back(node);
} else {
sorted_node_hash_[node->channel] = std::vector<GSNode*>({node});
sorted_node_hash_[node->version] = std::vector<std::shared_ptr<GSNode>>({node});
}
}
void GSMgr::RemoveNodeFromSortedNodes(GSNode* node)
void GSMgr::RemoveNodeFromSortedNodes(std::shared_ptr<GSNode> node)
{
for (auto& pair : sorted_node_hash_) {
std::vector<GSNode*>& node_list = pair.second;
std::vector<std::shared_ptr<GSNode>>& node_list = pair.second;
for (size_t i = 0; i < node_list.size(); ++i) {
if (node_list[i] == node) {
node_list.erase(node_list.begin() + i);
@ -314,17 +318,19 @@ void GSMgr::RemoveNodeFromSortedNodes(GSNode* node)
}
}
std::vector<GSNode*>* GSMgr::GetSortedNodesByChannel(int channel)
std::vector<std::shared_ptr<GSNode>>* GSMgr::GetSortedNodes()
{
auto itr = sorted_node_hash_.find(channel);
if (itr != sorted_node_hash_.end()) {
return &itr->second;
} else {
if (sorted_node_hash_.empty()) {
return nullptr;
}
std::vector<std::shared_ptr<GSNode>>* result = nullptr;
for (auto& pair : sorted_node_hash_) {
result = &pair.second;
}
return result;
}
void GSMgr::OnChannelChange(GSNode* node)
void GSMgr::OnVersionChange(std::shared_ptr<GSNode> node)
{
RemoveNodeFromSortedNodes(node);
AddNodeToSortedNodes(node);

View File

@ -1,23 +1,14 @@
#pragma once
#include "ss_proto.pb.h"
#include <f8/timer.h>
struct GSNode
namespace ss
{
std::string key;
long long node_idx = 0;
int node_id = 0;
int instance_id = 0;
int room_num = 0;
int alive_count = 0;
int online_num = 0;
std::string ip;
int port = 0;
bool servicing = false;
int channel = 0;
long long last_active_tick = 0;
};
class SS_WSP_RequestTargetServer;
class SS_Ping;
}
struct GSNode;
class GSMgr : public a8::Singleton<GSMgr>
{
public:
@ -39,19 +30,19 @@ class GSMgr : public a8::Singleton<GSMgr>
void ___GSList(f8::JsonHttpRequest* request);
private:
GSNode* GetNodeByTeamId(const std::string& team_id);
GSNode* GetNodeByNodeKey(const std::string& node_key);
GSNode* AllocNode(int channel);
std::shared_ptr<GSNode> GetNodeByTeamId(const std::string& team_id);
std::shared_ptr<GSNode> GetNodeByNodeKey(const std::string& node_key);
std::shared_ptr<GSNode> AllocNode();
void RearrangeNode();
void ClearTimeOutNode();
void AddNodeToSortedNodes(GSNode* node);
void RemoveNodeFromSortedNodes(GSNode* node);
std::vector<GSNode*>* GetSortedNodesByChannel(int channel);
void OnChannelChange(GSNode* node);
void AddNodeToSortedNodes(std::shared_ptr<GSNode> node);
void RemoveNodeFromSortedNodes(std::shared_ptr<GSNode> node);
std::vector<std::shared_ptr<GSNode>>* GetSortedNodes();
void OnVersionChange(std::shared_ptr<GSNode> node);
private:
std::map<std::string, GSNode*> team_hash_;
std::map<std::string, GSNode> node_key_hash_;
std::map<int, std::vector<GSNode*>> sorted_node_hash_;
f8::Attacher timer_attacher_;
std::map<std::string, std::shared_ptr<GSNode>> team_hash_;
std::map<std::string, std::shared_ptr<GSNode>> node_key_hash_;
std::map<int, std::vector<std::shared_ptr<GSNode>>> sorted_node_hash_;
};

View File

@ -1,6 +1,7 @@
#include "precompile.h"
#include <a8/mutable_xobject.h>
#include <f8/jsonhttprequest.h>
#include "handlermgr.h"
@ -8,6 +9,8 @@
#include "app.h"
#include "gsmgr.h"
#include "ss_proto.pb.h"
static void _GMOpsSelfChecking(f8::JsonHttpRequest* request)
{
request->resp_xobj->SetVal("errcode", 0);
@ -17,27 +20,10 @@ static void _GMOpsSelfChecking(f8::JsonHttpRequest* request)
request->resp_xobj->SetVal("max_timer_idle", App::Instance()->perf.max_timer_idle);
}
static void _GMOpsReload(f8::JsonHttpRequest* request)
{
App::Instance()->LoadSeparateChannelConfig();
request->resp_xobj->SetVal("errcode", 0);
request->resp_xobj->SetVal("errmsg", "");
a8::UdpLog::Instance()->Warning("reload config files", {});
}
static void _GMOpsShowConfig(f8::JsonHttpRequest* request)
{
request->resp_xobj->SetVal("errcode", 0);
request->resp_xobj->SetVal("errmsg", "");
request->resp_xobj->SetVal("separate_channels", App::Instance()->GetSeparateChannelConfigData());
}
void HandlerMgr::Init()
{
RegisterNetMsgHandlers();
RegisterGMMsgHandler("Ops@selfChecking", _GMOpsSelfChecking);
RegisterGMMsgHandler("Ops@reload", _GMOpsReload);
RegisterGMMsgHandler("Ops@showConfig", _GMOpsShowConfig);
RegisterGMMsgHandler("GS@report", [] (f8::JsonHttpRequest* request)
{
GSMgr::Instance()->___GSReport(request);
@ -76,7 +62,7 @@ void HandlerMgr::ProcGMMsg(unsigned long saddr, int sockhandle,
request->saddr = saddr;
request->socket_handle = sockhandle;
request->query_str = querystr;
request->request.ReadFromUrlQueryString(querystr);
request->params->ReadFromUrlQueryString(querystr);
request->resp_xobj->SetVal("errcode", 0);
request->resp_xobj->SetVal("errmsg", "");
itr->second(request);

View File

@ -2,7 +2,7 @@
#include <a8/basehttpsession.h>
#include "framework/cpp/netmsghandler.h"
#include <f8/netmsghandler.h>
namespace a8
{

View File

@ -1,10 +1,10 @@
#include "precompile.h"
#include <f8/utils.h>
#include "jsondatamgr.h"
#include "app.h"
#include "framework/cpp/utils.h"
void JsonDataMgr::Init()
{
std::string masterserver_cluster_json_file;
@ -13,14 +13,14 @@ void JsonDataMgr::Init()
work_path_ = a8::Format("/root/pub/%d/%d/conf_test/game%d/masterserver.test",
{
GAME_ID,
App::Instance()->instance_id,
f8::App::Instance()->GetInstanceId(),
GAME_ID
});
} else {
work_path_ = a8::Format("/root/pub/%d/%d/conf_test/game%d/masterserver.dev",
{
GAME_ID,
App::Instance()->instance_id,
f8::App::Instance()->GetInstanceId(),
GAME_ID
});
}
@ -28,7 +28,7 @@ void JsonDataMgr::Init()
masterserver_cluster_json_file = a8::Format("%s/node%d/game%d.masterserver.cluster.json",
{
work_path_,
App::Instance()->node_id,
f8::App::Instance()->GetNodeId(),
GAME_ID
});
masterserver_cluster_json_.ReadFromFile(masterserver_cluster_json_file);
@ -40,8 +40,9 @@ void JsonDataMgr::UnInit()
std::shared_ptr<a8::XObject> JsonDataMgr::GetConf()
{
if (App::Instance()->instance_id < 1 || App::Instance()->instance_id > masterserver_cluster_json_.Size()) {
if (f8::App::Instance()->GetInstanceId() < 1 ||
f8::App::Instance()->GetInstanceId() > masterserver_cluster_json_.Size()) {
abort();
}
return masterserver_cluster_json_[App::Instance()->instance_id - 1];
return masterserver_cluster_json_[f8::App::Instance()->GetInstanceId() - 1];
}

View File

@ -1,11 +1,10 @@
#include "precompile.h"
#include <f8/app.h>
#include "app.h"
int main(int argc, char* argv[])
{
int exitcode = 0;
App::Instance()->Init(argc, argv);
exitcode = App::Instance()->Run();
App::Instance()->UnInit();
return exitcode;
return f8::App::Instance()->Run(argc, argv, App::Instance());
}

View File

@ -1,21 +1,9 @@
#pragma once
#include <a8/a8.h>
#include <a8/udplog.h>
#include <f8/f8.h>
#include <f8/udplog.h>
#include "constant.h"
#include "types.h"
#include "error_code.h"
#include "global.h"
namespace google
{
namespace protobuf
{
class Message;
}
}
#include "framework/cpp/types.h"
#include "framework/cpp/protoutils.h"

2
third_party/f8 vendored

@ -1 +1 @@
Subproject commit 0b0158805fcdfaaf336f10a99f8f4f7421f28aa4
Subproject commit ae2a2bcddbacf26d0ba2f1be7c95ee9e20ce3023