2023-09-06 00:31:57 +00:00

499 lines
14 KiB
C++

#include "precompile.h"
#include <string.h>
#include <a8/xtimer.h>
#include <a8/uuid.h>
#include <a8/udplistener.h>
#include <f8/netmsghandler.h>
#include <f8/udplog.h>
#include <f8/app.h>
#include <f8/msgqueue.h>
#include "app.h"
#include "GCListener.h"
#include "jsondatamgr.h"
#include "handlermgr.h"
#include "downstream.h"
#include "downstreammgr.h"
#include "upstream.h"
#include "upstreammgr.h"
#include "master.h"
#include "mastermgr.h"
#include "longsessionmgr.h"
#include "ss_msgid.pb.h"
#include "ss_proto.pb.h"
struct MsgNode
{
SocketFrom_e sockfrom;
int sockhandle;
unsigned short msgid;
unsigned int seqid;
long ip_saddr;
char* buf;
int buflen;
int tag;
MsgNode* next;
};
struct UdpMsgNode
{
a8::UdpPacket* pkt;
UdpMsgNode* next;
};
static void SavePerfLog()
{
f8::UdpLog::Instance()->Info("max_run_delay_time:%d max_timer_idle:%d "
"in_data_size:%d out_data_size:%d msgnode_size:%d udp_msgnode_size:%d "
"read_count:%d max_login_time:%d "
"max_join_time:%d tcp_count:%d udp_count:%d down_stream_count:%d",
{
App::Instance()->GetPerf().max_run_delay_time,
App::Instance()->GetPerf().max_timer_idle,
App::Instance()->GetPerf().in_data_size,
App::Instance()->GetPerf().out_data_size,
App::Instance()->GetMsgNodeSize(),
App::Instance()->GetUdpMsgNodeSize(),
App::Instance()->GetPerf().read_count,
App::Instance()->GetPerf().max_login_time,
App::Instance()->GetPerf().max_join_time,
GCListener::Instance()->GetSocketCount(),
LongSessionMgr::Instance()->GetLongSessionCount(),
DownStreamMgr::Instance()->GetDownStreamCount()
});
if (f8::App::Instance()->HasFlag(2)) {
a8::XPrintf("mainloop_time:%d netmsg_time:%d send_node_num:%d sent_bytes_num:%d\n",
{
App::Instance()->GetPerf().max_run_delay_time,
App::Instance()->GetPerf().max_dispatchmsg_time,
GCListener::Instance()->GetSendNodeNum(),
GCListener::Instance()->GetSentBytesNum()
});
}
App::Instance()->GetPerf().max_run_delay_time = 0;
App::Instance()->GetPerf().max_timer_idle = 0;
App::Instance()->GetPerf().max_login_time = 0;
App::Instance()->GetPerf().max_join_time = 0;
}
const std::string App::GetPkgName()
{
return a8::Format("game%d_wsproxy", {GAME_ID});
}
void App::Init()
{
msg_mutex_ = new std::mutex();
udp_msg_mutex_ = new std::mutex();
HandlerMgr::Instance()->Init();
JsonDataMgr::Instance()->Init();
DownStreamMgr::Instance()->Init();
MasterMgr::Instance()->Init();
UpStreamMgr::Instance()->Init();
LongSessionMgr::Instance()->Init();
GCListener::Instance()->Init();
f8::UdpLog::Instance()->Info("wsproxy starting instance_id:%d pid:%d",
{
f8::App::Instance()->GetInstanceId(),
f8::App::Instance()->GetPid(),
});
{
int perf_log_time = 1000 * 60 * 5;
if (getenv("is_dev_env")) {
perf_log_time = 1000 * 10;
}
f8::Timer::Instance()->SetInterval
(perf_log_time,
[] (int event, const a8::Args* args)
{
if (a8::TIMER_EXEC_EVENT == event) {
SavePerfLog();
}
});
}
if (f8::App::Instance()->HasFlag(1)) {
f8::Timer::Instance()->SetTimeout
(
1000 * 60,
[] (int event, const a8::Args* args)
{
if (a8::TIMER_EXEC_EVENT == event) {
f8::App::Instance()->Terminated();
f8::App::Instance()->NotifyLoopCond();
}
}
);
}
}
void App::UnInit()
{
a8::XPrintf("wsproxy terminating instance_id:%d pid:%d\n",
{f8::App::Instance()->GetInstanceId(),
f8::App::Instance()->GetPid()});
GCListener::Instance()->UnInit();
LongSessionMgr::Instance()->UnInit();
MasterMgr::Instance()->UnInit();
UpStreamMgr::Instance()->UnInit();
DownStreamMgr::Instance()->UnInit();
JsonDataMgr::Instance()->UnInit();
HandlerMgr::Instance()->UnInit();
FreeSocketMsgQueue();
FreeUdpMsgQueue();
delete msg_mutex_;
msg_mutex_ = nullptr;
delete udp_msg_mutex_;
udp_msg_mutex_ = nullptr;
a8::XPrintf("wsproxy terminated instance_id:%d pid:%d\n",
{f8::App::Instance()->GetInstanceId(),
f8::App::Instance()->GetPid()});
}
void App::Update()
{
QuickExecute();
SlowerExecute();
}
void App::AddSocketMsg(SocketFrom_e sockfrom,
int sockhandle,
long ip_saddr,
unsigned short msgid,
unsigned int seqid,
const char *msgbody,
int bodylen,
int tag)
{
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;
p->tag = tag;
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();
f8::App::Instance()->NotifyLoopCond();
}
void App::QuickExecute()
{
f8::Timer::Instance()->Update();
f8::MsgQueue::Instance()->Update();
DispatchMsg();
DispatchUdpMsg();
LongSessionMgr::Instance()->Update();
}
void App::SlowerExecute()
{
}
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;
}
}
{
if (!udp_work_node_) {
udp_msg_mutex_->lock();
if (!udp_work_node_ && udp_top_node_) {
udp_work_node_ = udp_top_node_;
udp_top_node_ = nullptr;
udp_bot_node_ = nullptr;
}
udp_msg_mutex_->unlock();
}
if (udp_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_Client:
{
ProcessClientMsg(hdr, pdelnode->tag);
}
break;
case SF_TargetServer:
{
ProcessTargetServerMsg(hdr, pdelnode->tag);
}
break;
case SF_MasterServer:
{
ProcessMasterServerMsg(hdr, pdelnode->tag);
}
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::ProcessClientMsg(f8::MsgHdr& hdr, int tag)
{
if (hdr.msgid == ss::_SS_CMLogin ||
hdr.msgid == ss::_SS_CMReconnect ||
hdr.msgid == ss::_SS_CMKcpHandshake) {
auto down_wp = DownStreamMgr::Instance()->GetDownStream(hdr.socket_handle);
if (down_wp.expired()) {
switch (hdr.msgid) {
case ss::_SS_CMLogin:
{
ss::SS_CMLogin msg;
bool ok = msg.ParseFromArray(hdr.buf + hdr.offset, hdr.buflen - hdr.offset);
if (ok) {
MasterMgr::Instance()->RequestTargetServer
(hdr,
msg.team_uuid(),
msg.account_id(),
msg.session_id(),
"",
0,
msg.proto_version());
}
}
break;
case ss::_SS_CMReconnect:
{
ss::SS_CMReconnect msg;
bool ok = msg.ParseFromArray(hdr.buf + hdr.offset, hdr.buflen - hdr.offset);
if (ok) {
MasterMgr::Instance()->RequestTargetServer
(hdr,
msg.team_uuid(),
msg.account_id(),
msg.session_id(),
msg.server_info(),
1,
0);
}
}
break;
case ss::_SS_CMKcpHandshake:
{
ss::SS_CMKcpHandshake msg;
bool ok = msg.ParseFromArray(hdr.buf + hdr.offset, hdr.buflen - hdr.offset);
if (ok) {
LongSessionMgr::Instance()->_SS_CMKcpHandshake(hdr, msg);
}
}
break;
default:
{
abort();
}
break;
}
}
} else {
auto down_wp = DownStreamMgr::Instance()->GetDownStream(hdr.socket_handle);
if (auto down = down_wp.lock(); !down_wp.expired()) {
down->ProcCMMsg(hdr, tag);
}
}
}
void App::ProcessMasterServerMsg(f8::MsgHdr& hdr, int tag)
{
f8::NetMsgHandler* handler = f8::GetNetMsgHandler(&HandlerMgr::Instance()->msmsghandler,
hdr.msgid);
if (handler) {
switch (handler->handlerid) {
case HID_MasterMgr:
ProcessNetMsg(handler, MasterMgr::Instance(), hdr);
break;
}
}
}
void App::ProcessTargetServerMsg(f8::MsgHdr& hdr, int tag)
{
if (hdr.msgid == ss::_SS_ForceCloseSocket) {
GCListener::Instance()->ForceCloseClient(hdr.socket_handle);
return;
}
if (hdr.msgid == ss::_SS_CMLogin || hdr.msgid == ss::_SS_CMReconnect) {
DownStreamMgr::Instance()->BindUpStream(hdr.socket_handle, hdr.ip_saddr);
GCListener::Instance()->MarkClient(hdr.socket_handle, true);
}
auto down_wp = DownStreamMgr::Instance()->GetDownStream(hdr.socket_handle);
if (!down_wp.expired()) {
down_wp.lock()->ForwardUpStreamMsg(hdr);
}
}
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();
}
void App::FreeUdpMsgQueue()
{
udp_msg_mutex_->lock();
if (!udp_work_node_) {
udp_work_node_ = udp_top_node_;
udp_top_node_ = nullptr;
udp_bot_node_ = nullptr;
}
while (udp_work_node_) {
UdpMsgNode* pdelnode = udp_work_node_;
udp_work_node_ = udp_work_node_->next;
{
if (pdelnode->pkt->buf) {
free((void*)pdelnode->pkt->buf);
}
delete pdelnode->pkt;
free(pdelnode);
}
if (!udp_work_node_) {
udp_work_node_ = udp_top_node_;
udp_top_node_ = nullptr;
udp_bot_node_ = nullptr;
}
}
udp_msg_mutex_->unlock();
}
void App::AddUdpMsg(a8::UdpPacket* pkt)
{
UdpMsgNode *p = (UdpMsgNode*) malloc(sizeof(UdpMsgNode));
memset(p, 0, sizeof(UdpMsgNode));
p->pkt = pkt;
udp_msg_mutex_->lock();
if (udp_bot_node_) {
udp_bot_node_->next = p;
udp_bot_node_ = p;
} else {
udp_top_node_ = p;
udp_bot_node_ = p;
}
++udp_msgnode_size_;
udp_msg_mutex_->unlock();
f8::App::Instance()->NotifyLoopCond();
}
void App::DispatchUdpMsg()
{
long long starttick = a8::XGetTickCount();
if (!udp_work_node_ && udp_top_node_) {
udp_msg_mutex_->lock();
udp_work_node_ = udp_top_node_;
udp_top_node_ = nullptr;
udp_bot_node_ = nullptr;
udp_working_msgnode_size_ = udp_msgnode_size_;
udp_msg_mutex_->unlock();
}
while (udp_work_node_) {
UdpMsgNode *pdelnode = udp_work_node_;
LongSessionMgr::Instance()->ProcUdpPacket(pdelnode->pkt);
udp_work_node_ = pdelnode->next;
{
if (pdelnode->pkt->buf) {
free((void*)pdelnode->pkt->buf);
}
delete pdelnode->pkt;
free(pdelnode);
}
udp_working_msgnode_size_--;
if (a8::XGetTickCount() - starttick > 200) {
break;
}
}//end while
if (!udp_work_node_) {
udp_working_msgnode_size_ = 0;
}
}