game2001/server/gameserver/GGListener.cc
2019-02-15 20:51:44 +08:00

133 lines
4.0 KiB
C++

#include "precompile.h"
#include <google/protobuf/message.h>
#include <a8/mixedsession.h>
#include <a8/tcplistener.h>
#include "framework/cpp/netmsghandler.h"
#include "app.h"
#include "GGListener.h"
#include "jsondatamgr.h"
#include "cs_proto.pb.h"
#include "cs_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,
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});
}
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();
}
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::MarkClient(int sockhandle, bool is_active)
{
tcp_listener_->MarkClient(sockhandle, is_active);
}