From 351d3c8edab0d3af9a7922b780e0a977112f40f4 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Wed, 15 May 2019 17:38:28 +0800 Subject: [PATCH] 1 --- server/tools/protobuild/ss_proto.proto | 22 +++++++++++ server/wsproxy/app.cc | 29 +++++++++----- server/wsproxy/mastersvrmgr.cc | 52 ++++++++++++++++++++++++++ server/wsproxy/mastersvrmgr.h | 15 ++++++++ third_party/a8engine | 2 +- third_party/framework | 2 +- 6 files changed, 111 insertions(+), 11 deletions(-) diff --git a/server/tools/protobuild/ss_proto.proto b/server/tools/protobuild/ss_proto.proto index 2d85631..cdc02c9 100644 --- a/server/tools/protobuild/ss_proto.proto +++ b/server/tools/protobuild/ss_proto.proto @@ -13,6 +13,28 @@ message SS_CMLogin_CMReConnect_CommonHead optional int32 server_id = 1; } +message SS_CMLogin_CMReConnect_CommonHead2 +{ + optional int32 server_id = 1; + optional string team_uuid = 2; +} + +message SS_WSP_RequestTargetServer +{ + optional int64 context_id = 1; + optional string account_id = 2; + optional string team_id = 3; +} + +message SS_MS_ResponseTargetServer +{ + optional int32 error_code = 1; + optional string error_msg = 2; + optional int64 context_id = 3; + optional string host = 4; + optional int32 port = 5; +} + message SS_SMRpcError { optional int32 error_code = 1; diff --git a/server/wsproxy/app.cc b/server/wsproxy/app.cc index f092aa1..b99a8c5 100644 --- a/server/wsproxy/app.cc +++ b/server/wsproxy/app.cc @@ -21,12 +21,11 @@ #include "ss_msgid.pb.h" #include "ss_proto.pb.h" +#include "target_conn.h" +#include "target_conn_mgr.h" #if MASTER_MODE #include "mastersvr.h" #include "mastersvrmgr.h" -#else -#include "target_conn.h" -#include "target_conn_mgr.h" #endif struct MsgNode @@ -98,11 +97,8 @@ void App::Init(int argc, char* argv[]) GCListener::Instance()->Init(); uuid.SetMachineId(instance_id); GameClientMgr::Instance()->Init(); -#if MASTER_MODE MasterSvrMgr::Instance()->Init(); -#else TargetConnMgr::Instance()->Init(); -#endif a8::UdpLog::Instance()->Info("masterserver starting instance_id:%d pid:%d", {instance_id, getpid()}); { @@ -124,11 +120,8 @@ void App::UnInit() if (terminated) { return; } -#if MASTER_MODE MasterSvrMgr::Instance()->UnInit(); -#else TargetConnMgr::Instance()->UnInit(); -#endif GameClientMgr::Instance()->UnInit(); GCListener::Instance()->UnInit(); JsonDataMgr::Instance()->UnInit(); @@ -346,6 +339,23 @@ void App::ProcessClientMsg(f8::MsgHdr& hdr) return; } #if MASTER_MODE + TargetConn* conn = nullptr; + if (hdr.msgid == ss::_SS_CMLogin || hdr.msgid == ss::_SS_CMReConnect) { + ss::SS_CMLogin_CMReConnect_CommonHead2 msg; + bool ok = msg.ParseFromArray(hdr.buf + hdr.offset, hdr.buflen - hdr.offset); + if (ok) { + MasterSvrMgr::Instance()->RequestTargetServer(hdr, msg.team_uuid()); + } + return; + } else { + GameClient* client = GameClientMgr::Instance()->GetGameClientBySocket(hdr.socket_handle); + if (client) { + conn = client->conn; + } + } + if (conn) { + conn->ForwardClientMsg(hdr); + } #else TargetConn* conn = nullptr; if (hdr.msgid == ss::_SS_CMLogin || hdr.msgid == ss::_SS_CMReConnect) { @@ -407,6 +417,7 @@ void App::ProcessIMMsg() case IM_ClientSocketDisconnect: { GameClientMgr::Instance()->OnClientDisconnect(pdelnode->params); + MasterSvrMgr::Instance()->OnClientDisconnect(pdelnode->params); } break; case IM_TargetConnDisconnect: diff --git a/server/wsproxy/mastersvrmgr.cc b/server/wsproxy/mastersvrmgr.cc index f0afe91..f1ccf6c 100644 --- a/server/wsproxy/mastersvrmgr.cc +++ b/server/wsproxy/mastersvrmgr.cc @@ -1,11 +1,18 @@ #include "precompile.h" +#include + #include "mastersvrmgr.h" #include "mastersvr.h" #include "jsondatamgr.h" +#include "ss_proto.pb.h" + +#include "framework/cpp/netmsghandler.h" void MasterSvrMgr::Init() { + curr_context_id_ = a8::MakeInt64(0, time(nullptr) + 1000 * 60 * 10); + auto master_svr_cluster_conf = JsonDataMgr::Instance()->GetMasterServerClusterConf(); for (int i = 0; i < master_svr_cluster_conf->Size(); ++i) { auto master_svr_conf = master_svr_cluster_conf->At(i); @@ -30,3 +37,48 @@ MasterSvr* MasterSvrMgr::GetConnByInstanceId(int instance_id) auto itr = mastersvr_hash_.find(instance_id); return itr != mastersvr_hash_.end() ? itr->second : nullptr; } + +void MasterSvrMgr::RequestTargetServer(f8::MsgHdr& hdr, const std::string& team_id) +{ + if (GetConextId(hdr.socket_handle) == 0) { + return; + } + unsigned int code = a8::openssl::Crc32((unsigned char*)team_id.data(), team_id.size()); + MasterSvr* svr = GetConnByInstanceId(code % mastersvr_hash_.size() + 1); + if (svr) { + ++curr_context_id_; + ss::SS_WSP_RequestTargetServer msg; + msg.set_context_id(curr_context_id_); + msg.set_team_id(team_id); + svr->SendMsg(msg); + pending_socket_hash_[hdr.socket_handle] = curr_context_id_; + } +} + +void MasterSvrMgr::OnClientDisconnect(a8::XParams& param) +{ + long long conext_id = GetConextId(param.sender); + if (conext_id != 0) { + f8::MsgHdr* hdr = GetHdr(conext_id); + if (hdr) { + if (hdr->buf) { + free((char*)hdr->buf); + } + delete hdr; + pending_request_hash_.erase(conext_id); + } + pending_socket_hash_.erase(param.sender); + } +} + +long long MasterSvrMgr::GetConextId(int socket_handle) +{ + auto itr = pending_socket_hash_.find(socket_handle); + return itr != pending_socket_hash_.end() ? itr->second : 0; +} + +f8::MsgHdr* MasterSvrMgr::GetHdr(long long conext_id) +{ + auto itr = pending_request_hash_.find(conext_id); + return itr != pending_request_hash_.end() ? itr->second : nullptr; +} diff --git a/server/wsproxy/mastersvrmgr.h b/server/wsproxy/mastersvrmgr.h index c3e08d6..9373e6a 100644 --- a/server/wsproxy/mastersvrmgr.h +++ b/server/wsproxy/mastersvrmgr.h @@ -1,5 +1,10 @@ #pragma once +namespace f8 +{ + struct MsgHdr; +} + class MasterSvr; class MasterSvrMgr : public a8::Singleton { @@ -12,8 +17,18 @@ class MasterSvrMgr : public a8::Singleton void Init(); void UnInit(); + void RequestTargetServer(f8::MsgHdr& hdr, const std::string& team_id); + void OnClientDisconnect(a8::XParams& param); + + private: + long long GetConextId(int socket_handle); + f8::MsgHdr* GetHdr(long long conext_id); MasterSvr* GetConnByInstanceId(int instance_id); private: + long long curr_context_id_ = 0; std::map mastersvr_hash_; + std::map pending_socket_hash_; + std::map pending_request_hash_; + }; diff --git a/third_party/a8engine b/third_party/a8engine index bc1e1e0..fc99a36 160000 --- a/third_party/a8engine +++ b/third_party/a8engine @@ -1 +1 @@ -Subproject commit bc1e1e002cdfbbac07abdf14151afb0bbd8025a8 +Subproject commit fc99a3615db9aabc1a77489e069a4e6af26d50d5 diff --git a/third_party/framework b/third_party/framework index 1813384..fd72ea5 160000 --- a/third_party/framework +++ b/third_party/framework @@ -1 +1 @@ -Subproject commit 18133846b6672634219c080064b7a24720d17588 +Subproject commit fd72ea56059dc8545920e33d436dad5a1d3700fb