1
This commit is contained in:
parent
c07764f578
commit
877ff708c8
158
server/robotserver/GGListener.cc
Normal file
158
server/robotserver/GGListener.cc
Normal file
@ -0,0 +1,158 @@
|
||||
#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;
|
||||
}
|
||||
f8::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,
|
||||
0);
|
||||
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 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_->RegisterSessionClass<GCClientSession>(1024 * 1024 * 10);
|
||||
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});
|
||||
f8::App::Instance()->Terminate();
|
||||
exit(1);
|
||||
}
|
64
server/robotserver/GGListener.h
Normal file
64
server/robotserver/GGListener.h
Normal file
@ -0,0 +1,64 @@
|
||||
#pragma once
|
||||
|
||||
#include <a8/singleton.h>
|
||||
|
||||
#include <f8/types.h>
|
||||
#include <f8/protoutils.h>
|
||||
|
||||
//game client listener
|
||||
namespace a8
|
||||
{
|
||||
class TcpListener;
|
||||
}
|
||||
|
||||
class GGListener : public a8::Singleton<GGListener>
|
||||
{
|
||||
private:
|
||||
GGListener() {};
|
||||
friend class a8::Singleton<GGListener>;
|
||||
|
||||
public:
|
||||
enum { HID = HID_GGListener };
|
||||
|
||||
public:
|
||||
void Init();
|
||||
void UnInit();
|
||||
|
||||
int max_packet_size = 0;
|
||||
int his_max_packet_size = 0;
|
||||
|
||||
template <typename T>
|
||||
void SendProxyMsg(int sockhandle, T& msg)
|
||||
{
|
||||
static int msgid = f8::Net_GetMessageId(msg);
|
||||
f8::Net_SendProxyMsg(tcp_listener_.get(), sockhandle, 0, 0, msgid, msg);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void SendToClient(int sockhandle, unsigned int seqid, T& msg)
|
||||
{
|
||||
static int msgid = f8::Net_GetMessageId(msg);
|
||||
int packet_size = f8::Net_SendProxyMsg(tcp_listener_.get(), sockhandle, seqid, 0, msgid, msg);
|
||||
if (packet_size > max_packet_size) {
|
||||
max_packet_size = packet_size;
|
||||
}
|
||||
}
|
||||
void SendText(int sockhandle, const std::string& text);
|
||||
|
||||
void SendError(int sockhandle, unsigned int seqid,
|
||||
int error_code, const std::string& error_msg,
|
||||
const char* file = nullptr, int lineno = 0, int error_param = 0);
|
||||
|
||||
void ForceCloseClient(int sockhandle);
|
||||
void ForceCloseChildSocket(int sockhandle);
|
||||
void MarkClient(int sockhandle, bool is_active);
|
||||
long long GetSendNodeNum();
|
||||
long long GetSentNodeNum();
|
||||
long long GetSentBytesNum();
|
||||
|
||||
private:
|
||||
void OnListenError(int errorid);
|
||||
|
||||
private:
|
||||
std::shared_ptr<a8::TcpListener> tcp_listener_;
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user