From 89816f516a27e3f875715205e0208d6ebc500716 Mon Sep 17 00:00:00 2001 From: azw Date: Sun, 9 Apr 2023 08:43:13 +0000 Subject: [PATCH] 1 --- server/wsproxy/app.cc | 12 ++++++------ server/wsproxy/downstreammgr.cc | 28 ++++++++++++---------------- server/wsproxy/downstreammgr.h | 4 ++-- 3 files changed, 20 insertions(+), 24 deletions(-) diff --git a/server/wsproxy/app.cc b/server/wsproxy/app.cc index a88b4e8..97add9d 100644 --- a/server/wsproxy/app.cc +++ b/server/wsproxy/app.cc @@ -344,8 +344,8 @@ void App::ProcessClientMsg(f8::MsgHdr& hdr) return; } if (hdr.msgid == ss::_SS_CMLogin || hdr.msgid == ss::_SS_CMReconnect) { - DownStream* client = DownStreamMgr::Instance()->GetGameClientBySocket(hdr.socket_handle); - if (!client) { + auto down_wp = DownStreamMgr::Instance()->GetGameClientBySocket(hdr.socket_handle); + if (auto down = down_wp.lock(); !down_wp.expired()) { switch (hdr.msgid) { case ss::_SS_CMLogin: { @@ -385,10 +385,10 @@ void App::ProcessClientMsg(f8::MsgHdr& hdr) } } } else { - DownStream* client = DownStreamMgr::Instance()->GetGameClientBySocket(hdr.socket_handle); - if (client && client->conn) { - if (client->conn) { - client->conn->ForwardClientMsg(hdr); + auto down_wp = DownStreamMgr::Instance()->GetGameClientBySocket(hdr.socket_handle); + if (auto down = down_wp.lock(); !down_wp.expired()) { + if (down->conn) { + down->conn->ForwardClientMsg(hdr); } } } diff --git a/server/wsproxy/downstreammgr.cc b/server/wsproxy/downstreammgr.cc index 984e5a4..b5ae978 100644 --- a/server/wsproxy/downstreammgr.cc +++ b/server/wsproxy/downstreammgr.cc @@ -26,9 +26,6 @@ void DownStreamMgr::Init() void DownStreamMgr::UnInit() { - for (auto& pair : socket_hash_) { - delete pair.second; - } socket_hash_.clear(); #if 0 pending_account_hash_.clear(); @@ -37,14 +34,13 @@ void DownStreamMgr::UnInit() void DownStreamMgr::OnClientDisconnect(int socket_handle) { - DownStream* client = GetGameClientBySocket(socket_handle); - if (client) { - if (client->conn) { + auto down_wp = GetGameClientBySocket(socket_handle); + if (auto down = down_wp.lock(); !down_wp.expired()) { + if (down->conn) { ss::SS_WSP_SocketDisconnect msg; - client->conn->SendMsg(socket_handle, msg); + down->conn->SendMsg(socket_handle, msg); } socket_hash_.erase(socket_handle); - delete client; } RemovePendingAccount(socket_handle); } @@ -74,7 +70,7 @@ void DownStreamMgr::OnTargetServerConnect(a8::XParams& param) } #endif -DownStream* DownStreamMgr::GetGameClientBySocket(int sockhandle) +std::weak_ptr DownStreamMgr::GetGameClientBySocket(int sockhandle) { auto itr = socket_hash_.find(sockhandle); return itr != socket_hash_.end() ? itr->second : nullptr; @@ -84,14 +80,14 @@ void DownStreamMgr::BindUpStream(int socket_handle, int conn_instance_id) { UpStream* conn = UpStreamMgr::Instance()->GetConnById(conn_instance_id); if (conn) { - DownStream* client = GetGameClientBySocket(socket_handle); - if (client) { - client->conn = conn; + auto down_wp = GetGameClientBySocket(socket_handle); + if (auto down = down_wp.lock(); !down_wp.expired()) { + down->conn = conn; } else { - client = new DownStream(); - client->socket_handle = socket_handle; - client->conn = conn; - socket_hash_[client->socket_handle] = client; + down = std::make_shared(); + down->socket_handle = socket_handle; + down->conn = conn; + socket_hash_[down->socket_handle] = down; f8::UdpLog::Instance()->Info("BindUpStream socket_handle:%d", {socket_handle}); { #if 0 diff --git a/server/wsproxy/downstreammgr.h b/server/wsproxy/downstreammgr.h index 992d770..94f3b46 100644 --- a/server/wsproxy/downstreammgr.h +++ b/server/wsproxy/downstreammgr.h @@ -18,7 +18,7 @@ class DownStreamMgr : public a8::Singleton void OnTargetServerDisconnect(a8::XParams& param); void OnTargetServerConnect(a8::XParams& param); #endif - DownStream* GetGameClientBySocket(int sockhande); + std::weak_ptr GetGameClientBySocket(int sockhande); void BindUpStream(int socket_handle, int conn_instance_id); void AddPendingAccount(const std::string& account_id, int socket_handle, long long req_tick); @@ -29,7 +29,7 @@ private: private: a8::Attacher timer_attacher_; - std::map socket_hash_; + std::map> socket_hash_; #if 0 std::map> pending_account_hash_; #endif