This commit is contained in:
aozhiwei 2023-04-23 16:07:17 +08:00
parent 892af52a25
commit a968685105
5 changed files with 65 additions and 78 deletions

View File

@ -5,7 +5,6 @@
#include <google/protobuf/message.h>
#include <a8/websocketsession.h>
#include <a8/tcplistener.h>
#include <a8/udplistener.h>
#include <f8/netmsghandler.h>
#include <f8/udplog.h>
@ -16,10 +15,6 @@
#include "jsondatamgr.h"
#include "ss_proto.pb.h"
#include "handlermgr.h"
#include "ikcp.h"
#include "kcpsession.h"
#define USE_KCP 1
class GCClientSession: public a8::WebSocketSession
{
@ -128,16 +123,6 @@ static void GSListeneron_error(a8::TcpListener*, int type, int errorid)
f8::UdpLog::Instance()->Debug("GCListeneron_error %d %d", {type, errorid});
}
static void GSUdpListeneron_error(int errorid)
{
f8::UdpLog::Instance()->Debug("GCUdpListeneron_error %d", {errorid});
}
static void GSUdpListeneron_recv_packet(a8::UdpPacket* pkt)
{
App::Instance()->AddUdpMsg(pkt);
}
void GCListener::Init()
{
tcp_listener_ = new a8::TcpListener();
@ -147,14 +132,6 @@ void GCListener::Init()
tcp_listener_->bind_address = "0.0.0.0";
tcp_listener_->bind_port = JsonDataMgr::Instance()->GetConf()->At("listen_port")->AsXValue();
tcp_listener_->Open();
udp_listener_ = std::make_shared<a8::UdpListener>();
udp_listener_->on_error = GSUdpListeneron_error;
udp_listener_->on_recv_packet = GSUdpListeneron_recv_packet;
udp_listener_->bind_address = "0.0.0.0";
udp_listener_->bind_port = JsonDataMgr::Instance()->GetConf()->At("listen_udp_port")->AsXValue();
udp_listener_->Open();
}
void GCListener::UnInit()
@ -211,39 +188,3 @@ long long GCListener::GetSentBytesNum()
{
return tcp_listener_->sent_bytes_num;
}
std::shared_ptr<KcpSession> GCListener::GetKcpSessionByRemoteKey(long long key)
{
auto itr = kcp_session_addr_hash_.find(key);
return itr != kcp_session_addr_hash_.end() ? itr->second : nullptr;
}
std::shared_ptr<KcpSession> GCListener::GetKcpSessionBySocketHandle(int socket_handle)
{
auto itr = kcp_session_handle_hash_.find(socket_handle);
return itr != kcp_session_handle_hash_.end() ? itr->second : nullptr;
}
void GCListener::ProcUdpPacket(a8::UdpPacket* pkt)
{
long long key = pkt->GetRemoteKey();
auto session = GetKcpSessionByRemoteKey(key);
if (session) {
session->OnRecvPacket(pkt);
#if 0
++kcp_socket_handle_;
kcp_socket_handle_ = 123;
session = std::make_shared<KcpSession>();
session->Init(udp_listener_.get(), kcp_socket_handle_, pkt);
kcp_session_addr_hash_[key] = session;
kcp_session_handle_hash_[kcp_socket_handle_] = session;
#endif
}
}
void GCListener::Update()
{
for (auto& pair : kcp_session_addr_hash_) {
pair.second->Update();
}
}

View File

@ -4,11 +4,8 @@
namespace a8
{
class TcpListener;
class UdpListener;
struct UdpPacket;
}
class KcpSession;
class GCListener : public a8::Singleton<GCListener>
{
private:
@ -21,7 +18,6 @@ class GCListener : public a8::Singleton<GCListener>
public:
void Init();
void UnInit();
void Update();
template <typename T>
void SendMsg(unsigned short socket_handle, T& msg)
@ -38,17 +34,6 @@ class GCListener : public a8::Singleton<GCListener>
long long GetSendNodeNum();
long long GetSentBytesNum();
std::shared_ptr<KcpSession> GetKcpSessionByRemoteKey(long long key);
std::shared_ptr<KcpSession> GetKcpSessionBySocketHandle(int socket_handle);
void ProcUdpPacket(a8::UdpPacket* pkt);
private:
a8::TcpListener *tcp_listener_ = nullptr;
public:
std::shared_ptr<a8::UdpListener> udp_listener_;
unsigned short kcp_socket_handle_ = 1000;
std::map<long long, std::shared_ptr<KcpSession>> kcp_session_addr_hash_;
std::map<int, std::shared_ptr<KcpSession>> kcp_session_handle_hash_;
};

View File

@ -258,7 +258,7 @@ void App::QuickExecute()
f8::MsgQueue::Instance()->Update();
DispatchMsg();
DispatchUdpMsg();
GCListener::Instance()->Update();
LongSessionMgr::Instance()->Update();
f8::Timer::Instance()->Update();
}
@ -588,7 +588,7 @@ void App::DispatchUdpMsg()
while (udp_work_node_) {
UdpMsgNode *pdelnode = udp_work_node_;
GCListener::Instance()->ProcUdpPacket(pdelnode->pkt);
LongSessionMgr::Instance()->ProcUdpPacket(pdelnode->pkt);
udp_work_node_ = pdelnode->next;
udp_working_msgnode_size_--;
if (a8::XGetTickCount() - starttick > 200) {

View File

@ -1,13 +1,40 @@
#include "precompile.h"
#include <a8/udplistener.h>
#include <f8/udplog.h>
#include "longsessionmgr.h"
#include "app.h"
#include "jsondatamgr.h"
static void GSUdpListeneron_error(int errorid)
{
f8::UdpLog::Instance()->Debug("GCUdpListeneron_error %d", {errorid});
}
static void GSUdpListeneron_recv_packet(a8::UdpPacket* pkt)
{
App::Instance()->AddUdpMsg(pkt);
}
void LongSessionMgr::Init()
{
udp_listener_ = std::make_shared<a8::UdpListener>();
udp_listener_->on_error = GSUdpListeneron_error;
udp_listener_->on_recv_packet = GSUdpListeneron_recv_packet;
udp_listener_->bind_address = "0.0.0.0";
udp_listener_->bind_port = JsonDataMgr::Instance()->GetConf()->At("listen_udp_port")->AsXValue();
udp_listener_->Open();
}
void LongSessionMgr::UnInit()
{
}
void LongSessionMgr::UnInit()
void LongSessionMgr::Update()
{
}
@ -16,3 +43,29 @@ std::shared_ptr<LongSession> GetSessionBySocketHandle(int socket_handle)
{
return nullptr;
}
#if 0
std::shared_ptr<KcpSession> GCListener::GetKcpSessionByRemoteKey(long long key)
{
auto itr = kcp_session_addr_hash_.find(key);
return itr != kcp_session_addr_hash_.end() ? itr->second : nullptr;
}
std::shared_ptr<KcpSession> GCListener::GetKcpSessionBySocketHandle(int socket_handle)
{
auto itr = kcp_session_handle_hash_.find(socket_handle);
return itr != kcp_session_handle_hash_.end() ? itr->second : nullptr;
}
#endif
void LongSessionMgr::ProcUdpPacket(a8::UdpPacket* pkt)
{
#if 0
long long key = pkt->GetRemoteKey();
auto session = GetKcpSessionByRemoteKey(key);
if (session) {
session->OnRecvPacket(pkt);
}
#endif
}

View File

@ -2,6 +2,12 @@
#include <a8/singleton.h>
namespace a8
{
class UdpListener;
struct UdpPacket;
}
class LongSession;
class LongSessionMgr : public a8::Singleton<LongSessionMgr>
{
@ -13,10 +19,12 @@ class LongSessionMgr : public a8::Singleton<LongSessionMgr>
void Init();
void UnInit();
void Update();
void ProcUdpPacket(a8::UdpPacket* pkt);
std::shared_ptr<LongSession> GetSession(int socket_handle);
private:
std::shared_ptr<a8::UdpListener> udp_listener_;
std::map<int, std::shared_ptr<LongSession>> socket_handle_hash_;
};