relation/server/imserver/WSListener.cc
aozhiwei 1b73729a60 1
2020-05-04 21:56:12 +08:00

118 lines
3.5 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 "WSListener.h"
#include "jsondatamgr.h"
#include "ss_proto.pb.h"
#include "handlermgr.h"
class WSProxySession: public a8::MixedSession
{
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::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_WSProxy,
(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("收到wsproxy非法数据包", {});
}
}
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_WSProxyDisconnect,
a8::XParams()
.SetSender(socket_handle)
.SetParam1(1));
}
};
static void CreateWSProxySocket(a8::TcpSession **p)
{
*p = new WSProxySession();
}
static void GSListeneron_error(a8::TcpListener*, int type, int errorid)
{
a8::UdpLog::Instance()->Debug("WSListeneron_error %d %d", {type, errorid});
}
void WSListener::Init()
{
tcp_listener_ = new a8::TcpListener();
tcp_listener_->on_create_client_socket = CreateWSProxySocket;
tcp_listener_->on_error = GSListeneron_error;
tcp_listener_->bind_address = "0.0.0.0";
tcp_listener_->bind_port = JsonDataMgr::Instance()->GetConf()->At("wsproxy_port")->AsXValue();
tcp_listener_->Open();
}
void WSListener::UnInit()
{
delete tcp_listener_;
tcp_listener_ = nullptr;
}
void WSListener::SendText(unsigned short sockhandle, const std::string& text)
{
tcp_listener_->SendClientMsg(sockhandle, text.data(), text.size());
}
void WSListener::ForceCloseClient(unsigned short sockhandle)
{
tcp_listener_->ForceCloseClient(sockhandle);
}
void WSListener::MarkClient(unsigned short sockhandle, bool is_active)
{
tcp_listener_->MarkClient(sockhandle, is_active);
}
void WSListener::_SS_Ping(f8::MsgHdr& hdr, const ss::SS_Ping& msg)
{
ss::SS_Pong pongmsg;
SendProxyMsg(hdr.socket_handle, pongmsg);
}