game2004/server/gameserver/GGListener.cc
aozhiwei 3a7638a790 1
2020-02-11 11:38:28 +08:00

174 lines
5.2 KiB
C++

#include "precompile.h"
#include <google/protobuf/message.h>
#include <a8/mixedsession.h>
#include <a8/tcplistener.h>
#include "framework/cpp/netmsghandler.h"
#include "framework/cpp/msgqueue.h"
#include "app.h"
#include "GGListener.h"
#include "jsondatamgr.h"
#include "cs_proto.pb.h"
#include "cs_msgid.pb.h"
#include "ss_proto.pb.h"
#include "ss_msgid.pb.h"
#include "handlermgr.h"
class GCClientSession: public a8::MixedSession
{
public:
virtual void DecodeUserPacket(char* buf, int& offset, unsigned int buflen) override
{
#if 1
is_activite = true;
#endif
bool warning = false;
while (buflen - offset >= sizeof(f8::WSProxyPackHead_C)) {
f8::WSProxyPackHead_C* p = (f8::WSProxyPackHead_C*)&buf[offset];
if (p->magic_code == f8::MAGIC_CODE) {
if (buflen - offset < sizeof(f8::WSProxyPackHead_C) + p->packlen) {
break;
}
App::Instance()->AddSocketMsg(SF_GameGate,
(socket_handle << 16) + p->socket_handle,
p->ip_saddr,
p->msgid,
p->seqid,
&buf[offset + sizeof(f8::WSProxyPackHead_C)],
p->packlen);
offset += sizeof(f8::WSProxyPackHead_C) + 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
{
App::Instance()->AddIMMsg(IM_ClientSocketDisconnect,
a8::XParams()
.SetSender(socket_handle)
.SetParam1(1));
}
};
static void CreateGameClientSocket(a8::TcpSession **p)
{
*p = new GCClientSession();
}
static void GSListeneron_error(a8::TcpListener*, int type, int errorid)
{
a8::UdpLog::Instance()->Debug("GGListeneron_error %d %d", {type, errorid});
f8::MsgQueue::Instance()->PostMsg_r(IM_GGListenerError,
a8::XParams()
.SetParam1(errorid)
);
}
void GGListener::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();
f8::MsgQueue::Instance()->RegisterCallBack(IM_GGListenerError,
[] (const a8::XParams& param)
{
GGListener::Instance()->OnListenError(param.param1);
});
}
void GGListener::UnInit()
{
delete tcp_listener_;
tcp_listener_ = nullptr;
}
void GGListener::SendText(int sockhandle, const std::string& text)
{
tcp_listener_->SendClientMsg(sockhandle, text.data(), text.size());
}
void GGListener::SendError(int sockhandle, unsigned int seqid,
int error_code, const std::string& error_msg,
const char* file, int lineno, int error_param)
{
cs::SMRpcError msg;
msg.set_error_code(error_code);
msg.set_error_msg(error_msg);
msg.set_debug_msg("");
if (file) {
msg.set_file(file);
} else {
msg.set_file("");
}
msg.set_lineno(lineno);
msg.set_error_param(error_param);
f8::Net_SendProxyMsg(tcp_listener_, sockhandle, seqid, error_code, cs::_SMRpcError, msg);
}
void GGListener::ForceCloseClient(int sockhandle)
{
tcp_listener_->ForceCloseClient(sockhandle);
}
void GGListener::ForceCloseChildSocket(int sockhandle)
{
ss::SS_ForceCloseSocket msg;
SendProxyMsg(sockhandle, msg);
}
void GGListener::MarkClient(int sockhandle, bool is_active)
{
tcp_listener_->MarkClient(sockhandle, is_active);
}
long long GGListener::GetSendNodeNum()
{
return tcp_listener_->send_node_num;
}
long long GGListener::GetSentNodeNum()
{
return tcp_listener_->sent_node_num;
}
long long GGListener::GetSentBytesNum()
{
return tcp_listener_->sent_bytes_num;
}
void GGListener::OnListenError(int errorid)
{
a8::XPrintf("GGListeneron_error %d\n", {errorid});
App::Instance()->terminated = true;
exit(1);
}