134 lines
4.5 KiB
C++
134 lines
4.5 KiB
C++
#include "precompile.h"
|
|
|
|
#include <a8/mutable_xobject.h>
|
|
#include <f8/msgqueue.h>
|
|
#include <f8/jsonhttprequest.h>
|
|
#include <f8/app.h>
|
|
|
|
#include "handlermgr.h"
|
|
|
|
#include "GCListener.h"
|
|
#include "mastermgr.h"
|
|
#include "app.h"
|
|
#include "jsondatamgr.h"
|
|
|
|
#include "ss_proto.pb.h"
|
|
|
|
static void _GMOpsSelfChecking(int socket_handle, std::shared_ptr<f8::JsonHttpRequest> request)
|
|
{
|
|
request->GetResp()->SetVal("errcode", 0);
|
|
request->GetResp()->SetVal("errmsg", "");
|
|
request->GetResp()->SetVal("healthy", 1);
|
|
request->GetResp()->SetVal("max_rundelay", 10);
|
|
}
|
|
|
|
static void _GMOpsGetNodeId(int socket_handle, std::shared_ptr<f8::JsonHttpRequest> request)
|
|
{
|
|
request->GetResp()->SetVal("errcode", 0);
|
|
request->GetResp()->SetVal("errmsg", "");
|
|
request->GetResp()->SetVal("node_id", f8::App::Instance()->GetNodeId());
|
|
}
|
|
|
|
static void _GMOpsSetKcpSwitch(int socket_handle, std::shared_ptr<f8::JsonHttpRequest> request)
|
|
{
|
|
request->GetResp()->SetVal("errcode", 0);
|
|
request->GetResp()->SetVal("errmsg", "");
|
|
if (request->GetParams()->HasKey("open")) {
|
|
JsonDataMgr::Instance()->SetKcpSwitch(request->GetParams()->At("open")->AsXValue().GetInt());
|
|
}
|
|
request->GetResp()->SetVal("is_open", JsonDataMgr::Instance()->GetKcpConf().open);
|
|
}
|
|
|
|
static void _GMOpsGetKcpSwitch(int socket_handle, std::shared_ptr<f8::JsonHttpRequest> request)
|
|
{
|
|
request->GetResp()->SetVal("errcode", 0);
|
|
request->GetResp()->SetVal("errmsg", "");
|
|
request->GetResp()->SetVal("is_open", JsonDataMgr::Instance()->GetKcpConf().open);
|
|
}
|
|
|
|
static void _GMOpsTerminate(int socket_handle, std::shared_ptr<f8::JsonHttpRequest> request)
|
|
{
|
|
request->GetResp()->SetVal("errcode", 0);
|
|
request->GetResp()->SetVal("errmsg", "");
|
|
f8::App::Instance()->Terminate();
|
|
}
|
|
|
|
static void _GMHttpTunnelTeamRequest(int socket_handle, std::shared_ptr<f8::JsonHttpRequest> request)
|
|
{
|
|
MasterMgr::Instance()->AddHttpTunnelRequest(socket_handle, request);
|
|
}
|
|
|
|
static void _GMHttpTunnelOnlineTeamRequest(int socket_handle, std::shared_ptr<f8::JsonHttpRequest> request)
|
|
{
|
|
MasterMgr::Instance()->AddHttpTunnelRequest(socket_handle, request);
|
|
}
|
|
|
|
void HandlerMgr::Init()
|
|
{
|
|
RegisterNetMsgHandlers();
|
|
RegisterGMMsgHandler("Ops$selfChecking", _GMOpsSelfChecking);
|
|
RegisterGMMsgHandler("Ops$getNodeId", _GMOpsGetNodeId);
|
|
RegisterGMMsgHandler("Ops$setKcpSwitch", _GMOpsSetKcpSwitch);
|
|
RegisterGMMsgHandler("Ops$getKcpSwitch", _GMOpsGetKcpSwitch);
|
|
RegisterGMMsgHandler("Ops$terminate", _GMOpsTerminate);
|
|
RegisterGMMsgHandler("HttpTunnel$teamRequest", _GMHttpTunnelTeamRequest);
|
|
RegisterGMMsgHandler("HttpTunnel$onlineTeamRequest", _GMHttpTunnelOnlineTeamRequest);
|
|
f8::MsgQueue::Instance()->RegisterCallBack
|
|
(
|
|
IM_ExecGM,
|
|
[this] (const a8::Args& args)
|
|
{
|
|
int socket_handle = args.Get<int>(0);
|
|
std::string url = args.Get<std::string>(1);
|
|
std::string query_str = args.Get<std::string>(2);
|
|
unsigned long saddr = args.Get<unsigned long>(3);
|
|
ProcGMMsg(saddr, socket_handle, url, query_str);
|
|
});
|
|
}
|
|
|
|
void HandlerMgr::UnInit()
|
|
{
|
|
}
|
|
|
|
void HandlerMgr::RegisterNetMsgHandlers()
|
|
{
|
|
RegisterNetMsgHandler(&msmsghandler, &MasterMgr::_SS_MS_ResponseTargetServer);
|
|
RegisterNetMsgHandler(&msmsghandler, &MasterMgr::_SS_MS_HttpTunnelResponse);
|
|
}
|
|
|
|
void HandlerMgr::ProcGMMsg(unsigned long saddr, int sockhandle,
|
|
const std::string& url, const std::string& querystr)
|
|
{
|
|
if (url != "/webapp/index.php") {
|
|
GCListener::Instance()->SendText(sockhandle, a8::HttpResponse(404, ""));
|
|
return;
|
|
}
|
|
|
|
a8::HTTPRequest request;
|
|
a8::ParserUrlQueryString(querystr.c_str(), request);
|
|
|
|
std::string msgname = a8::Get(request, "c").GetString() + "$" + a8::Get(request, "a").GetString();
|
|
auto itr = gmhandlers_.find(msgname);
|
|
if (itr != gmhandlers_.end()) {
|
|
auto request = std::make_shared<f8::JsonHttpRequest>
|
|
(
|
|
saddr,
|
|
url,
|
|
querystr,
|
|
[sockhandle] (const a8::Args& args)
|
|
{
|
|
std::string data = args.Get<std::string>(0);
|
|
GCListener::Instance()->SendText(sockhandle, data);
|
|
});
|
|
itr->second(sockhandle, request);
|
|
} else {
|
|
GCListener::Instance()->SendText(sockhandle, a8::HttpResponse("{}"));
|
|
}
|
|
}
|
|
|
|
void HandlerMgr::RegisterGMMsgHandler(const std::string& msgname,
|
|
void (*handler)(int, std::shared_ptr<f8::JsonHttpRequest>))
|
|
{
|
|
gmhandlers_[msgname] = handler;
|
|
}
|