game2006/server/gameserver/GGListener.cc
aozhiwei 7a33c2fc1d 1
2023-11-03 19:38:31 +08:00

163 lines
4.2 KiB
C++

#include "precompile.h"
#include <google/protobuf/message.h>
#include <a8/mixedsession.h>
#include <a8/tcplistener.h>
#include "GGListener.h"
#include <f8/netmsghandler.h>
#include <f8/msgqueue.h>
#include <f8/udplog.h>
#include "app.h"
#include "jsondatamgr.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) {
f8::UdpLog::Instance()->Warning("收到client非法数据包", {});
}
}
virtual void OnRawHttpGet(const std::string& url, const std::string& querystr,
std::string& response) override
{
f8::MsgQueue::Instance()->PostMsg
(IM_ExecGM,
a8::Args
(
{
socket_handle,
a8::XValue(url).GetString(),
a8::XValue(querystr).GetString(),
saddr
}
));
}
virtual void OnDisConnect() override
{
f8::MsgQueue::Instance()->PostMsg
(IM_ClientSocketDisconnect,
a8::Args
(
{
socket_handle
}
));
}
};
static void CreateGameClientSocket(a8::TcpSession **p)
{
*p = new GCClientSession();
(*p)->SetMaxPacketLen(1024 * 1024 * 10);
}
static void GSListeneron_error(a8::TcpListener*, int type, int errorid)
{
f8::UdpLog::Instance()->Debug("GGListeneron_error %d %d", {type, errorid});
f8::MsgQueue::Instance()->PostMsg
(IM_GGListenerError,
a8::Args
(
{
errorid
}
));
abort();
}
void GGListener::Init()
{
tcp_listener_ = std::make_shared<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::Args& args)
{
int error_id = args.Get<int>(0);
GGListener::Instance()->OnListenError(error_id);
});
}
void GGListener::UnInit()
{
tcp_listener_ = nullptr;
}
void GGListener::SendText(int sockhandle, const std::string& text)
{
tcp_listener_->SendClientMsg(sockhandle, text.data(), text.size());
}
void GGListener::ForceCloseClient(int sockhandle)
{
tcp_listener_->ForceCloseClient(sockhandle);
}
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()->Terminate();
exit(1);
}