add ApiListener

This commit is contained in:
aozhiwei 2019-03-26 11:18:39 +08:00
parent 13d3e87cd0
commit d49b207a37
3 changed files with 164 additions and 14 deletions

View File

@ -0,0 +1,129 @@
#include "precompile.h"
#include <google/protobuf/message.h>
#include <a8/websocketsession.h>
#include <a8/tcplistener.h>
#include "framework/cpp/netmsghandler.h"
#include "app.h"
#include "ApiListener.h"
#include "jsondatamgr.h"
#include "handlermgr.h"
class GCClientSession: public a8::WebSocketSession
{
public:
virtual void DecodeUserPacket(char* buf, int& offset, unsigned int buflen) override
{
//packagelen + msgid + magiccode + msgbody
//2 + 2 + 4+ xx + \0 + xx
bool warning = false;
while (buflen - offset >= sizeof(f8::PackHead)) {
f8::PackHead* p = (f8::PackHead*)&buf[offset];
if (p->magic_code == f8::MAGIC_CODE) {
if (buflen - offset < sizeof(f8::PackHead) + p->packlen) {
break;
}
App::Instance()->AddSocketMsg(SF_Client,
socket_handle,
saddr,
p->msgid,
p->seqid,
&buf[offset + sizeof(f8::PackHead)],
p->packlen);
offset += sizeof(f8::PackHead) + p->packlen;
} else {
warning = true;
offset++;
continue;
}
}
if (warning) {
a8::UdpLog::Instance()->Warning("收到client非法数据包", {});
}
}
virtual void OnRawHttpGet(const std::string& url, const std::string& querystr,
std::string& response) override
{
App::Instance()->AddIMMsg(IM_ExecGM,
a8::XParams()
.SetSender(socket_handle)
.SetParam1(url)
.SetParam2(querystr)
.SetParam3(saddr));
}
virtual void OnDisConnect() override
{
#if 0
App::Instance()->AddIMMsg(IM_ClientSocketDisconnect,
a8::XParams()
.SetSender(socket_handle)
.SetParam1(1));
#endif
}
};
static void CreateGameClientSocket(a8::TcpSession **p)
{
*p = new GCClientSession();
}
static void GSListeneron_error(a8::TcpListener*, int type, int errorid)
{
a8::UdpLog::Instance()->Debug("ApiListeneron_error %d %d", {type, errorid});
}
void ApiListener::Init()
{
tcp_listener_ = new a8::TcpListener();
tcp_listener_->on_create_client_socket = CreateGameClientSocket;
tcp_listener_->on_error = GSListeneron_error;
tcp_listener_->bind_address = "0.0.0.0";
tcp_listener_->bind_port = JsonDataMgr::Instance()->GetConf()->At("listen_port")->AsXValue();
tcp_listener_->Open();
}
void ApiListener::UnInit()
{
delete tcp_listener_;
tcp_listener_ = nullptr;
}
void ApiListener::ForwardTargetConnMsg(f8::MsgHdr& hdr)
{
char* buff = (char*)malloc(sizeof(f8::PackHead) + hdr.buflen);
f8::PackHead* head = (f8::PackHead*)buff;
head->packlen = hdr.buflen;
head->msgid = hdr.msgid;
head->seqid = hdr.seqid;
head->magic_code = f8::MAGIC_CODE;
head->rpc_error_code = 0;
if (hdr.buflen > 0) {
memmove(buff + sizeof(f8::PackHead), hdr.buf, hdr.buflen);
}
tcp_listener_->SendClientMsg(hdr.socket_handle, buff, sizeof(f8::PackHead) + head->packlen);
free(buff);
}
void ApiListener::SendText(unsigned short sockhandle, const std::string& text)
{
tcp_listener_->SendClientMsg(sockhandle, text.data(), text.size());
}
void ApiListener::ForceCloseClient(unsigned short sockhandle)
{
tcp_listener_->ForceCloseClient(sockhandle);
}
void ApiListener::MarkClient(unsigned short sockhandle, bool is_active)
{
tcp_listener_->MarkClient(sockhandle, is_active);
}

View File

@ -0,0 +1,34 @@
#pragma once
//game client listener
namespace a8
{
class TcpListener;
}
class ApiListener : public a8::Singleton<ApiListener>
{
private:
ApiListener() {};
friend class a8::Singleton<ApiListener>;
public:
void Init();
void UnInit();
template <typename T>
void SendMsg(unsigned short socket_handle, T& msg)
{
static int msgid = f8::Net_GetMessageId(msg);
f8::Net_SendMsg(tcp_listener_, socket_handle, 0, msgid, msg);
}
void ForwardTargetConnMsg(f8::MsgHdr& hdr);
void SendText(unsigned short sockhandle, const std::string& text);
void ForceCloseClient(unsigned short sockhandle);
void MarkClient(unsigned short sockhandle, bool is_active);
private:
a8::TcpListener *tcp_listener_ = nullptr;
};

View File

@ -8,8 +8,6 @@ enum SocketFrom_e
enum InnerMesssage_e
{
IM_ClientSocketDisconnect = 100,
IM_PlayerOffline,
IM_ExecGM,
IM_TargetConnDisconnect
};
@ -19,19 +17,8 @@ enum NetHandler_e
{
HID_Player,
HID_PlayerMgr,
HID_RoomSvrMgr,
HID_GCListener,
};
enum PlayerState_e
{
PS_None,
PS_InRoom,
PS_Matching,
PS_WaitingMatch
};
const char* const PROJ_NAME_FMT = "game%d_wsproxy";
const char* const PROJ_NAME_FMT = "game%d_payserver";
const char* const PROJ_ROOT_FMT = "/data/logs/%s";
const int POSTFIX_LEN = 7;