From bd46875b2f237ed2e5738ba820f96f1b7b4c3187 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Wed, 15 May 2019 11:22:51 +0800 Subject: [PATCH 01/15] add master_mode --- server/wsproxy/CMakeLists.txt | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/server/wsproxy/CMakeLists.txt b/server/wsproxy/CMakeLists.txt index 1a88675..bf8ac61 100644 --- a/server/wsproxy/CMakeLists.txt +++ b/server/wsproxy/CMakeLists.txt @@ -8,10 +8,17 @@ else() message(GAME_ID: ${GAME_ID}) endif() +if (${MASTER_MODE}) + message(MASTER_MODE: 1) +else() + set(MASTER_MODE 0) + message(MASTER_MODE: 0) +endif() + set(CMAKE_BUILD_TYPE "Debug") set(CMAKE_CXX_FLAGS_RELEASE "-std=gnu++11 -fsanitize=address -fno-omit-frame-pointer") set(CMAKE_CXX_FLAGS_DEBUG "-Wall -g -std=gnu++11") -set(CMAKE_CXX_FLAGS_DEBUG "-Wall -g -std=gnu++11 -DGAME_ID=${GAME_ID}") +set(CMAKE_CXX_FLAGS_DEBUG "-Wall -g -std=gnu++11 -DGAME_ID=${GAME_ID} -DMASTER_MODE=${MASTER_MODE}") include_directories( AFTER @@ -53,10 +60,7 @@ add_executable( add_custom_target(script_pb_protocol ALL) add_custom_command(TARGET script_pb_protocol PRE_BUILD -# COMMAND python ../../tools/script/construct/build_script.py COMMAND python ../tools/scripts/construct/build_pb.py -# COMMAND python ../../tools/script/construct/build_protocol.py -# COMMAND python ../../tools/script/construct/build_version_file.py ) add_dependencies(wsproxy script_pb_protocol) From 268f62cff44d2481f7f112aeb8bdcb20319fbd86 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Wed, 15 May 2019 11:29:02 +0800 Subject: [PATCH 02/15] add master* --- server/wsproxy/constant.h | 3 +- server/wsproxy/mastersvr.cc | 172 +++++++++++++++++++++++++++++++++ server/wsproxy/mastersvr.h | 54 +++++++++++ server/wsproxy/mastersvrmgr.cc | 0 server/wsproxy/mastersvrmgr.h | 19 ++++ 5 files changed, 247 insertions(+), 1 deletion(-) create mode 100644 server/wsproxy/mastersvr.cc create mode 100644 server/wsproxy/mastersvr.h create mode 100644 server/wsproxy/mastersvrmgr.cc create mode 100644 server/wsproxy/mastersvrmgr.h diff --git a/server/wsproxy/constant.h b/server/wsproxy/constant.h index 7e0454f..4a5a9c8 100644 --- a/server/wsproxy/constant.h +++ b/server/wsproxy/constant.h @@ -11,7 +11,8 @@ enum InnerMesssage_e IM_ClientSocketDisconnect = 100, IM_PlayerOffline, IM_ExecGM, - IM_TargetConnDisconnect + IM_TargetConnDisconnect, + IM_MasterSvrDisconnect }; //网络处理对象 diff --git a/server/wsproxy/mastersvr.cc b/server/wsproxy/mastersvr.cc new file mode 100644 index 0000000..f6a7b31 --- /dev/null +++ b/server/wsproxy/mastersvr.cc @@ -0,0 +1,172 @@ +#include "precompile.h" + +#include + +#include "ss_proto.pb.h" +#include "ss_msgid.pb.h" +#include "mastersvr.h" +#include +#include +#include +#include "app.h" + +const int PACK_MAX = 1024 * 64; + +void MasterSvr::Init(int instance_id, const std::string& remote_ip, int remote_port) +{ + this->instance_id = instance_id; + this->remote_ip = remote_ip; + this->remote_port = remote_port; + + recv_bufflen_ = 0; + last_pong_tick = a8::XGetTickCount(); + recv_buff_ = (char*) malloc(PACK_MAX * 2); + tcp_client_ = new a8::TcpClient(); + tcp_client_->remote_address = remote_ip; + tcp_client_->remote_port = remote_port; + tcp_client_->on_error = std::bind(&MasterSvr::on_error, this, std::placeholders::_1, std::placeholders::_2); + tcp_client_->on_connect = std::bind(&MasterSvr::on_connect, this, std::placeholders::_1); + tcp_client_->on_disconnect = std::bind(&MasterSvr::on_disconnect, this, std::placeholders::_1); + tcp_client_->on_socketread = std::bind(&MasterSvr::on_socketread, this ,std::placeholders::_1, std::placeholders::_2, std::placeholders::_3); + timer_ = a8::Timer::Instance()->AddRepeatTimer(1000 * 9 + a8::RandEx(500, 150), + a8::XParams().SetSender(this), + [] (const a8::XParams& param) + { + MasterSvr* conn = (MasterSvr*)param.sender.GetUserData(); + conn->CheckAlive(); + }); +} + +void MasterSvr::UnInit() +{ + a8::Timer::Instance()->DeleteTimer(timer_); + timer_ = nullptr; + tcp_client_->Close(); + delete tcp_client_; + tcp_client_ = nullptr; + recv_bufflen_ = 0; + free(recv_buff_); + recv_buff_ = nullptr; +} + +void MasterSvr::Open() +{ + tcp_client_->Open(); +} + +void MasterSvr::Close() +{ + tcp_client_->Close(); +} + +bool MasterSvr::Connected() +{ + return tcp_client_->Connected(); +} + +void MasterSvr::ForwardClientMsg(f8::MsgHdr& hdr) +{ + char* buff = (char*)malloc(sizeof(f8::WSProxyPackHead_C) + hdr.buflen); + f8::WSProxyPackHead_C* head = (f8::WSProxyPackHead_C*)buff; + head->packlen = hdr.buflen; + head->msgid = hdr.msgid; + head->seqid = hdr.seqid; + head->magic_code = f8::MAGIC_CODE; + #if 0 + head->rpc_error_code = 0; + #endif + head->socket_handle = hdr.socket_handle; + head->ip_saddr = hdr.ip_saddr; + + if (hdr.buflen > 0) { + memmove(buff + sizeof(f8::WSProxyPackHead_C), hdr.buf, hdr.buflen); + } + + tcp_client_->SendBuff(buff, sizeof(f8::WSProxyPackHead_C) + head->packlen); + free(buff); +} + +void MasterSvr::on_error(a8::TcpClient* sender, int errorId) +{ + a8::UdpLog::Instance()->Error("MasterSvr errorid=%d", {errorId}); +} + +void MasterSvr::on_connect(a8::TcpClient* sender) +{ + recv_bufflen_ = 0; + a8::UdpLog::Instance()->Info("target server connected", {}); +} + +void MasterSvr::on_disconnect(a8::TcpClient* sender) +{ + recv_bufflen_ = 0; + a8::UdpLog::Instance()->Info("target server %d disconnected after 10s later reconnect", {instance_id}); + App::Instance()->AddIMMsg(IM_MasterSvrDisconnect, + a8::XParams() + .SetSender(instance_id) + ); +} + +void MasterSvr::on_socketread(a8::TcpClient* sender, char* buf, unsigned int len) +{ + #if 0 + ++App::Instance()->perf.read_count; + #endif + if (recv_bufflen_ + len > 2 * PACK_MAX) { + recv_bufflen_ = 0; + a8::UdpLog::Instance()->Debug("recvied target server too long message", {}); + return; + } else { + memmove(&recv_buff_[recv_bufflen_], buf, len); + recv_bufflen_ += len; + } + + bool warning = false; + unsigned int offset = 0; + while (recv_bufflen_ - offset >= sizeof(f8::WSProxyPackHead_S)) { + f8::WSProxyPackHead_S* p = (f8::WSProxyPackHead_S*) &recv_buff_[offset]; + if (p->magic_code == f8::MAGIC_CODE) { + if (recv_bufflen_ - offset < sizeof(f8::WSProxyPackHead_S) + p->packlen) { + break; + } + App::Instance()->AddSocketMsg(SF_TargetServer, + p->socket_handle, + instance_id, + p->msgid, + p->seqid, + &recv_buff_[offset + sizeof(f8::WSProxyPackHead_S)], + p->packlen); + offset += sizeof(f8::WSProxyPackHead_S) + p->packlen; + } else { + warning = true; + offset++; + continue; + } + } + + if (warning) { + a8::UdpLog::Instance()->Debug("recvied bad package", {}); + } + if (offset > 0 && offset < recv_bufflen_) { + memmove(recv_buff_, recv_buff_ + offset, recv_bufflen_ - offset); + } + recv_bufflen_ -= offset; + #if 1 + last_pong_tick = a8::XGetTickCount(); + #endif +} + +void MasterSvr::CheckAlive() +{ + if (!Connected()) { + Open(); + } else { + if (a8::XGetTickCount() - last_pong_tick > 60 * 10 * 1000) { + last_pong_tick = a8::XGetTickCount(); + Open(); + } else { + ss::SS_Ping msg; + SendMsg(msg); + } + } +} diff --git a/server/wsproxy/mastersvr.h b/server/wsproxy/mastersvr.h new file mode 100644 index 0000000..e79d435 --- /dev/null +++ b/server/wsproxy/mastersvr.h @@ -0,0 +1,54 @@ +#pragma once + +#include "framework/cpp/protoutils.h" + +namespace a8 +{ + class TcpClient; +} + +struct timer_list; +class MasterSvr +{ + public: + int instance_id = 0; + std::string remote_ip; + int remote_port = 0; + int matching_player_num = 0; + a8::tick_t last_pong_tick = 0; + + public: + void Init(int instance_id, const std::string& remote_ip, int remote_port); + void UnInit(); + + void Open(); + void Close(); + bool Connected(); + + template + void SendMsg(T& msg) + { + static int msgid = f8::Net_GetMessageId(msg); + #if 1 + f8::Net_SendProxyCMsg(tcp_client_, msgid, msg); + #else + f8::Net_SendMsg(tcp_client_, 0, msgid, msg); + #endif + } + + void ForwardClientMsg(f8::MsgHdr& hdr); + + private: + void on_error(a8::TcpClient* sender, int errorId); + void on_connect(a8::TcpClient* sender); + void on_disconnect(a8::TcpClient* sender); + void on_socketread(a8::TcpClient* sender, char* buf, unsigned int len); + + void CheckAlive(); + + private: + char *recv_buff_ = nullptr; + unsigned int recv_bufflen_ = 0; + a8::TcpClient* tcp_client_ = nullptr; + timer_list* timer_ = nullptr; +}; diff --git a/server/wsproxy/mastersvrmgr.cc b/server/wsproxy/mastersvrmgr.cc new file mode 100644 index 0000000..e69de29 diff --git a/server/wsproxy/mastersvrmgr.h b/server/wsproxy/mastersvrmgr.h new file mode 100644 index 0000000..1fda6ad --- /dev/null +++ b/server/wsproxy/mastersvrmgr.h @@ -0,0 +1,19 @@ +#pragma once + +class MasterSvr; +class MasterSvrMgr : public a8::Singleton +{ + private: + MasterSvrMgr() {}; + friend class a8::Singleton; + + public: + + void Init(); + void UnInit(); + + MasterSvr* GetConnByInstanceId(int instance_id); + + private: + std::map target_conn_hash_; +}; From 37d7c529f47567314ea251781df447956fc07dbd Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Wed, 15 May 2019 11:40:00 +0800 Subject: [PATCH 03/15] 1 --- server/wsproxy/app.cc | 14 ++++++++++++++ server/wsproxy/app.h | 1 + server/wsproxy/constant.h | 1 + server/wsproxy/mastersvr.cc | 24 +----------------------- server/wsproxy/mastersvr.h | 7 ------- server/wsproxy/mastersvrmgr.cc | 32 ++++++++++++++++++++++++++++++++ server/wsproxy/mastersvrmgr.h | 2 +- 7 files changed, 50 insertions(+), 31 deletions(-) diff --git a/server/wsproxy/app.cc b/server/wsproxy/app.cc index 630bdc8..cb8715b 100644 --- a/server/wsproxy/app.cc +++ b/server/wsproxy/app.cc @@ -22,6 +22,8 @@ #include "gameclientmgr.h" #include "ss_msgid.pb.h" #include "ss_proto.pb.h" +#include "mastersvr.h" +#include "mastersvrmgr.h" struct MsgNode { @@ -93,6 +95,7 @@ void App::Init(int argc, char* argv[]) uuid.SetMachineId(instance_id); GameClientMgr::Instance()->Init(); TargetConnMgr::Instance()->Init(); + MasterSvrMgr::Instance()->Init(); a8::UdpLog::Instance()->Info("masterserver starting instance_id:%d pid:%d", {instance_id, getpid()}); { @@ -114,6 +117,7 @@ void App::UnInit() if (terminated) { return; } + MasterSvrMgr::Instance()->UnInit(); TargetConnMgr::Instance()->UnInit(); GameClientMgr::Instance()->UnInit(); GCListener::Instance()->UnInit(); @@ -300,6 +304,11 @@ void App::DispatchMsg() ProcessTargetServerMsg(hdr); } break; + case SF_MasterServer: + { + ProcessMasterServerMsg(hdr); + } + break; } if (pdelnode->buf) { free(pdelnode->buf); @@ -358,6 +367,11 @@ void App::ProcessTargetServerMsg(f8::MsgHdr& hdr) GCListener::Instance()->ForwardTargetConnMsg(hdr); } +void App::ProcessMasterServerMsg(f8::MsgHdr& hdr) +{ + +} + void App::ProcessIMMsg() { if (!im_work_node_ && im_top_node_) { diff --git a/server/wsproxy/app.h b/server/wsproxy/app.h index e8c4563..3308881 100644 --- a/server/wsproxy/app.h +++ b/server/wsproxy/app.h @@ -43,6 +43,7 @@ private: void ProcessClientMsg(f8::MsgHdr& hdr); void ProcessTargetServerMsg(f8::MsgHdr& hdr); + void ProcessMasterServerMsg(f8::MsgHdr& hdr); void InitLog(); void UnInitLog(); diff --git a/server/wsproxy/constant.h b/server/wsproxy/constant.h index 4a5a9c8..cb07ea9 100644 --- a/server/wsproxy/constant.h +++ b/server/wsproxy/constant.h @@ -4,6 +4,7 @@ enum SocketFrom_e { SF_Client, SF_TargetServer, + SF_MasterServer, }; enum InnerMesssage_e diff --git a/server/wsproxy/mastersvr.cc b/server/wsproxy/mastersvr.cc index f6a7b31..d39dc5a 100644 --- a/server/wsproxy/mastersvr.cc +++ b/server/wsproxy/mastersvr.cc @@ -64,28 +64,6 @@ bool MasterSvr::Connected() return tcp_client_->Connected(); } -void MasterSvr::ForwardClientMsg(f8::MsgHdr& hdr) -{ - char* buff = (char*)malloc(sizeof(f8::WSProxyPackHead_C) + hdr.buflen); - f8::WSProxyPackHead_C* head = (f8::WSProxyPackHead_C*)buff; - head->packlen = hdr.buflen; - head->msgid = hdr.msgid; - head->seqid = hdr.seqid; - head->magic_code = f8::MAGIC_CODE; - #if 0 - head->rpc_error_code = 0; - #endif - head->socket_handle = hdr.socket_handle; - head->ip_saddr = hdr.ip_saddr; - - if (hdr.buflen > 0) { - memmove(buff + sizeof(f8::WSProxyPackHead_C), hdr.buf, hdr.buflen); - } - - tcp_client_->SendBuff(buff, sizeof(f8::WSProxyPackHead_C) + head->packlen); - free(buff); -} - void MasterSvr::on_error(a8::TcpClient* sender, int errorId) { a8::UdpLog::Instance()->Error("MasterSvr errorid=%d", {errorId}); @@ -129,7 +107,7 @@ void MasterSvr::on_socketread(a8::TcpClient* sender, char* buf, unsigned int len if (recv_bufflen_ - offset < sizeof(f8::WSProxyPackHead_S) + p->packlen) { break; } - App::Instance()->AddSocketMsg(SF_TargetServer, + App::Instance()->AddSocketMsg(SF_MasterServer, p->socket_handle, instance_id, p->msgid, diff --git a/server/wsproxy/mastersvr.h b/server/wsproxy/mastersvr.h index e79d435..86698ce 100644 --- a/server/wsproxy/mastersvr.h +++ b/server/wsproxy/mastersvr.h @@ -14,7 +14,6 @@ class MasterSvr int instance_id = 0; std::string remote_ip; int remote_port = 0; - int matching_player_num = 0; a8::tick_t last_pong_tick = 0; public: @@ -29,15 +28,9 @@ class MasterSvr void SendMsg(T& msg) { static int msgid = f8::Net_GetMessageId(msg); - #if 1 f8::Net_SendProxyCMsg(tcp_client_, msgid, msg); - #else - f8::Net_SendMsg(tcp_client_, 0, msgid, msg); - #endif } - void ForwardClientMsg(f8::MsgHdr& hdr); - private: void on_error(a8::TcpClient* sender, int errorId); void on_connect(a8::TcpClient* sender); diff --git a/server/wsproxy/mastersvrmgr.cc b/server/wsproxy/mastersvrmgr.cc index e69de29..bc66b85 100644 --- a/server/wsproxy/mastersvrmgr.cc +++ b/server/wsproxy/mastersvrmgr.cc @@ -0,0 +1,32 @@ +#include "precompile.h" + +#include "mastersvrmgr.h" +#include "mastersvr.h" +#include "jsondatamgr.h" + +void MasterSvrMgr::Init() +{ + auto master_svr_cluster_conf = JsonDataMgr::Instance()->GetTargetServerClusterConf(); + for (int i = 0; i < master_svr_cluster_conf->Size(); ++i) { + auto master_svr_conf = master_svr_cluster_conf->At(i); + int instance_id = master_svr_conf->At("instance_id")->AsXValue(); + std::string remote_ip = master_svr_conf->At("ip")->AsXValue(); + int remote_port = master_svr_conf->At("port")->AsXValue(); + { + MasterSvr* conn = new MasterSvr(); + conn->Init(instance_id, remote_ip, remote_port); + mastersvr_hash_[conn->instance_id] = conn; + conn->Open(); + } + } +} + +void MasterSvrMgr::UnInit() +{ +} + +MasterSvr* MasterSvrMgr::GetConnByInstanceId(int instance_id) +{ + auto itr = mastersvr_hash_.find(instance_id); + return itr != mastersvr_hash_.end() ? itr->second : nullptr; +} diff --git a/server/wsproxy/mastersvrmgr.h b/server/wsproxy/mastersvrmgr.h index 1fda6ad..c3e08d6 100644 --- a/server/wsproxy/mastersvrmgr.h +++ b/server/wsproxy/mastersvrmgr.h @@ -15,5 +15,5 @@ class MasterSvrMgr : public a8::Singleton MasterSvr* GetConnByInstanceId(int instance_id); private: - std::map target_conn_hash_; + std::map mastersvr_hash_; }; From fc5ed7f5779dc2a6d73d227c29abe36d02882e06 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Wed, 15 May 2019 14:18:55 +0800 Subject: [PATCH 04/15] 1 --- server/wsproxy/app.cc | 36 ++++++++++++++++++++++++------- server/wsproxy/app.h | 5 ++++- server/wsproxy/gameclientmgr.cc | 3 +++ server/wsproxy/gameclientmgr.h | 3 +++ server/wsproxy/jsondatamgr.cc | 24 +++++++++++++++++++++ server/wsproxy/jsondatamgr.h | 9 ++++++++ server/wsproxy/mastersvrmgr.cc | 2 +- server/wsproxy/target_conn.h | 4 ---- server/wsproxy/target_conn_mgr.cc | 6 ++++++ server/wsproxy/target_conn_mgr.h | 7 ++++++ 10 files changed, 85 insertions(+), 14 deletions(-) diff --git a/server/wsproxy/app.cc b/server/wsproxy/app.cc index cb8715b..f092aa1 100644 --- a/server/wsproxy/app.cc +++ b/server/wsproxy/app.cc @@ -16,14 +16,18 @@ #include "GCListener.h" #include "jsondatamgr.h" #include "handlermgr.h" -#include "target_conn.h" -#include "target_conn_mgr.h" #include "gameclient.h" #include "gameclientmgr.h" #include "ss_msgid.pb.h" #include "ss_proto.pb.h" + +#if MASTER_MODE #include "mastersvr.h" #include "mastersvrmgr.h" +#else +#include "target_conn.h" +#include "target_conn_mgr.h" +#endif struct MsgNode { @@ -94,8 +98,11 @@ void App::Init(int argc, char* argv[]) GCListener::Instance()->Init(); uuid.SetMachineId(instance_id); GameClientMgr::Instance()->Init(); - TargetConnMgr::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()}); { @@ -117,8 +124,11 @@ 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(); @@ -301,12 +311,17 @@ void App::DispatchMsg() break; case SF_TargetServer: { +#if MASTER_MODE +#else ProcessTargetServerMsg(hdr); +#endif } break; case SF_MasterServer: { +#if MASTER_MODE ProcessMasterServerMsg(hdr); +#endif } break; } @@ -330,6 +345,8 @@ void App::ProcessClientMsg(f8::MsgHdr& hdr) if (hdr.msgid < 100) { return; } +#if MASTER_MODE +#else TargetConn* conn = nullptr; if (hdr.msgid == ss::_SS_CMLogin || hdr.msgid == ss::_SS_CMReConnect) { ss::SS_CMLogin_CMReConnect_CommonHead msg; @@ -353,8 +370,15 @@ void App::ProcessClientMsg(f8::MsgHdr& hdr) if (conn) { conn->ForwardClientMsg(hdr); } +#endif } +#if MASTER_MODE +void App::ProcessMasterServerMsg(f8::MsgHdr& hdr) +{ + +} +#else void App::ProcessTargetServerMsg(f8::MsgHdr& hdr) { if (hdr.msgid < 100) { @@ -366,11 +390,7 @@ void App::ProcessTargetServerMsg(f8::MsgHdr& hdr) } GCListener::Instance()->ForwardTargetConnMsg(hdr); } - -void App::ProcessMasterServerMsg(f8::MsgHdr& hdr) -{ - -} +#endif void App::ProcessIMMsg() { diff --git a/server/wsproxy/app.h b/server/wsproxy/app.h index 3308881..c8af816 100644 --- a/server/wsproxy/app.h +++ b/server/wsproxy/app.h @@ -42,8 +42,11 @@ private: void ProcessIMMsg(); void ProcessClientMsg(f8::MsgHdr& hdr); - void ProcessTargetServerMsg(f8::MsgHdr& hdr); +#if MASTER_MODE void ProcessMasterServerMsg(f8::MsgHdr& hdr); +#else + void ProcessTargetServerMsg(f8::MsgHdr& hdr); +#endif void InitLog(); void UnInitLog(); diff --git a/server/wsproxy/gameclientmgr.cc b/server/wsproxy/gameclientmgr.cc index 7a22c83..7ec1a84 100644 --- a/server/wsproxy/gameclientmgr.cc +++ b/server/wsproxy/gameclientmgr.cc @@ -50,6 +50,8 @@ GameClient* GameClientMgr::GetGameClientBySocket(int sockhandle) return itr != socket_hash_.end() ? itr->second : nullptr; } +#if MASTER_MODE +#else void GameClientMgr::BindTargetConn(int socket_handle, int conn_instance_id) { TargetConn* conn = TargetConnMgr::Instance()->GetConnByInstanceId(conn_instance_id); @@ -65,3 +67,4 @@ void GameClientMgr::BindTargetConn(int socket_handle, int conn_instance_id) } } } +#endif diff --git a/server/wsproxy/gameclientmgr.h b/server/wsproxy/gameclientmgr.h index aeeeeb0..0436687 100644 --- a/server/wsproxy/gameclientmgr.h +++ b/server/wsproxy/gameclientmgr.h @@ -15,7 +15,10 @@ class GameClientMgr : public a8::Singleton void OnClientDisconnect(a8::XParams& param); void OnTargetServerDisconnect(a8::XParams& param); GameClient* GetGameClientBySocket(int sockhande); +#if MASTER_MODE +#else void BindTargetConn(int socket_handle, int conn_instance_id); +#endif private: std::map socket_hash_; diff --git a/server/wsproxy/jsondatamgr.cc b/server/wsproxy/jsondatamgr.cc index 29eadf3..aec73a2 100644 --- a/server/wsproxy/jsondatamgr.cc +++ b/server/wsproxy/jsondatamgr.cc @@ -6,18 +6,35 @@ void JsonDataMgr::Init() { std::string wsproxyserver_cluster_json_file; +#if MASTER_MODE + std::string masterserver_cluster_json_file; +#else std::string targetserver_cluster_json_file; +#endif if (f8::IsOnlineEnv()) { wsproxyserver_cluster_json_file = a8::Format("../config/game%d.wsproxy.cluster.json", {GAME_ID}); +#if MASTER_MODE + masterserver_cluster_json_file = a8::Format("../config/game%d.masterserver.cluster.json", {GAME_ID}); +#else targetserver_cluster_json_file = a8::Format("../config/game%d.gameserver.cluster.json", {GAME_ID}); +#endif } else { wsproxyserver_cluster_json_file = a8::Format("/var/data/conf_test/game%d/wsproxy/game%d.wsproxy.cluster.json", {GAME_ID, GAME_ID}); +#if MASTER_MODE + masterserver_cluster_json_file = a8::Format("/var/data/conf_test/game%d/wsproxy/game%d.masterserver.cluster.json", + {GAME_ID, GAME_ID}); +#else targetserver_cluster_json_file = a8::Format("/var/data/conf_test/game%d/wsproxy/game%d.gameserver.cluster.json", {GAME_ID, GAME_ID}); +#endif } wsproxyserver_cluster_json_.ReadFromFile(wsproxyserver_cluster_json_file); +#if MASTER_MODE + masterserver_cluster_json_.ReadFromFile(masterserver_cluster_json_file); +#else targetserver_cluster_json_.ReadFromFile(targetserver_cluster_json_file); +#endif } void JsonDataMgr::UnInit() @@ -32,7 +49,14 @@ std::shared_ptr JsonDataMgr::GetConf() return wsproxyserver_cluster_json_[App::Instance()->instance_id - 1]; } +#if MASTER_MODE +std::shared_ptr JsonDataMgr::GetMasterServerClusterConf() +{ + return std::make_shared(masterserver_cluster_json_); +} +#else std::shared_ptr JsonDataMgr::GetTargetServerClusterConf() { return std::make_shared(targetserver_cluster_json_); } +#endif diff --git a/server/wsproxy/jsondatamgr.h b/server/wsproxy/jsondatamgr.h index d99fe56..8f69d59 100644 --- a/server/wsproxy/jsondatamgr.h +++ b/server/wsproxy/jsondatamgr.h @@ -11,10 +11,19 @@ class JsonDataMgr : public a8::Singleton void UnInit(); std::shared_ptr GetConf(); +#if MASTER_MODE + std::shared_ptr GetMasterServerClusterConf(); +#else std::shared_ptr GetTargetServerClusterConf(); +#endif + private: a8::XObject wsproxyserver_cluster_json_; +#if MASTER_MODE + a8::XObject masterserver_cluster_json_; +#else a8::XObject targetserver_cluster_json_; +#endif }; diff --git a/server/wsproxy/mastersvrmgr.cc b/server/wsproxy/mastersvrmgr.cc index bc66b85..f0afe91 100644 --- a/server/wsproxy/mastersvrmgr.cc +++ b/server/wsproxy/mastersvrmgr.cc @@ -6,7 +6,7 @@ void MasterSvrMgr::Init() { - auto master_svr_cluster_conf = JsonDataMgr::Instance()->GetTargetServerClusterConf(); + 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); int instance_id = master_svr_conf->At("instance_id")->AsXValue(); diff --git a/server/wsproxy/target_conn.h b/server/wsproxy/target_conn.h index 3660e38..4da2af3 100644 --- a/server/wsproxy/target_conn.h +++ b/server/wsproxy/target_conn.h @@ -29,11 +29,7 @@ class TargetConn void SendMsg(T& msg) { static int msgid = f8::Net_GetMessageId(msg); - #if 1 f8::Net_SendProxyCMsg(tcp_client_, msgid, msg); - #else - f8::Net_SendMsg(tcp_client_, 0, msgid, msg); - #endif } void ForwardClientMsg(f8::MsgHdr& hdr); diff --git a/server/wsproxy/target_conn_mgr.cc b/server/wsproxy/target_conn_mgr.cc index 0e19e51..3fbbf00 100644 --- a/server/wsproxy/target_conn_mgr.cc +++ b/server/wsproxy/target_conn_mgr.cc @@ -6,6 +6,8 @@ void TargetConnMgr::Init() { +#if MASTER_MODE +#else auto target_server_cluster_conf = JsonDataMgr::Instance()->GetTargetServerClusterConf(); for (int i = 0; i < target_server_cluster_conf->Size(); ++i) { auto target_server_conf = target_server_cluster_conf->At(i); @@ -19,14 +21,18 @@ void TargetConnMgr::Init() conn->Open(); } } +#endif } void TargetConnMgr::UnInit() { } +#if MASTER_MODE +#else TargetConn* TargetConnMgr::GetConnByInstanceId(int instance_id) { auto itr = target_conn_hash_.find(instance_id); return itr != target_conn_hash_.end() ? itr->second : nullptr; } +#endif diff --git a/server/wsproxy/target_conn_mgr.h b/server/wsproxy/target_conn_mgr.h index 9519bdb..0f7aa9c 100644 --- a/server/wsproxy/target_conn_mgr.h +++ b/server/wsproxy/target_conn_mgr.h @@ -12,8 +12,15 @@ class TargetConnMgr : public a8::Singleton void Init(); void UnInit(); +#if MASTER_MODE +#else TargetConn* GetConnByInstanceId(int instance_id); +#endif private: + +#if MASTER_MODE +#else std::map target_conn_hash_; +#endif }; From 351d3c8edab0d3af9a7922b780e0a977112f40f4 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Wed, 15 May 2019 17:38:28 +0800 Subject: [PATCH 05/15] 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 From ce5d5b7f6e296189f020819a7dccb39f97c782fc Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Wed, 15 May 2019 17:40:03 +0800 Subject: [PATCH 06/15] 1 --- server/wsproxy/mastersvrmgr.cc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/server/wsproxy/mastersvrmgr.cc b/server/wsproxy/mastersvrmgr.cc index f1ccf6c..710cea6 100644 --- a/server/wsproxy/mastersvrmgr.cc +++ b/server/wsproxy/mastersvrmgr.cc @@ -52,6 +52,8 @@ void MasterSvrMgr::RequestTargetServer(f8::MsgHdr& hdr, const std::string& team_ msg.set_team_id(team_id); svr->SendMsg(msg); pending_socket_hash_[hdr.socket_handle] = curr_context_id_; + assert(pending_request_hash_.find(curr_context_id_) == pending_request_hash_.end()); + pending_request_hash_[curr_context_id_] = hdr.Clone(); } } From 0442ea3ecefa5dbc7242d8dc64ab9e3ed876fea8 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Wed, 15 May 2019 17:55:47 +0800 Subject: [PATCH 07/15] 1 --- server/wsproxy/constant.h | 4 +--- server/wsproxy/handlermgr.cc | 4 ++++ server/wsproxy/handlermgr.h | 3 +-- server/wsproxy/mastersvrmgr.cc | 19 ++++++++++++++++++- server/wsproxy/mastersvrmgr.h | 9 +++++++++ server/wsproxy/target_conn_mgr.cc | 5 +++++ server/wsproxy/target_conn_mgr.h | 2 ++ 7 files changed, 40 insertions(+), 6 deletions(-) diff --git a/server/wsproxy/constant.h b/server/wsproxy/constant.h index cb07ea9..3c28b59 100644 --- a/server/wsproxy/constant.h +++ b/server/wsproxy/constant.h @@ -19,10 +19,8 @@ enum InnerMesssage_e //网络处理对象 enum NetHandler_e { - HID_Player, - HID_PlayerMgr, - HID_RoomSvrMgr, HID_GCListener, + HID_MasterSvrMgr, }; enum PlayerState_e diff --git a/server/wsproxy/handlermgr.cc b/server/wsproxy/handlermgr.cc index 7608c93..02f368c 100644 --- a/server/wsproxy/handlermgr.cc +++ b/server/wsproxy/handlermgr.cc @@ -5,6 +5,9 @@ #include "handlermgr.h" #include "GCListener.h" +#include "mastersvrmgr.h" + +#include "ss_proto.pb.h" static void _GMOpsSelfChecking(f8::JsonHttpRequest* request) { @@ -26,6 +29,7 @@ void HandlerMgr::UnInit() void HandlerMgr::RegisterNetMsgHandlers() { + RegisterNetMsgHandler(&msmsghandler, &MasterSvrMgr::_SS_MS_ResponseTargetServer); } void HandlerMgr::ProcGMMsg(unsigned long saddr, int sockhandle, diff --git a/server/wsproxy/handlermgr.h b/server/wsproxy/handlermgr.h index 4e92981..6f5fab6 100644 --- a/server/wsproxy/handlermgr.h +++ b/server/wsproxy/handlermgr.h @@ -22,8 +22,7 @@ class HandlerMgr : public a8::Singleton void UnInit(); f8::NetMsgHandlerObject gcmsghandler; - f8::NetMsgHandlerObject rsmsghandler; - f8::NetMsgHandlerObject gsmsghandler; + f8::NetMsgHandlerObject msmsghandler; void ProcGMMsg(unsigned long saddr, int sockhandle, const std::string& url, const std::string& querystr); diff --git a/server/wsproxy/mastersvrmgr.cc b/server/wsproxy/mastersvrmgr.cc index 710cea6..5ca2d08 100644 --- a/server/wsproxy/mastersvrmgr.cc +++ b/server/wsproxy/mastersvrmgr.cc @@ -32,6 +32,23 @@ void MasterSvrMgr::UnInit() { } +void MasterSvrMgr::_SS_MS_ResponseTargetServer(f8::MsgHdr& hdr, const ss::SS_MS_ResponseTargetServer& msg) +{ + f8::MsgHdr* context_hdr = GetHdr(msg.context_id()); + if (context_hdr) { + if (msg.error_code() == 0) { + + } + + pending_request_hash_.erase(msg.context_id()); + pending_socket_hash_.erase(hdr.socket_handle); + if (context_hdr->buf) { + free((char*)context_hdr->buf); + } + free(context_hdr); + } +} + MasterSvr* MasterSvrMgr::GetConnByInstanceId(int instance_id) { auto itr = mastersvr_hash_.find(instance_id); @@ -66,7 +83,7 @@ void MasterSvrMgr::OnClientDisconnect(a8::XParams& param) if (hdr->buf) { free((char*)hdr->buf); } - delete hdr; + free(hdr); pending_request_hash_.erase(conext_id); } pending_socket_hash_.erase(param.sender); diff --git a/server/wsproxy/mastersvrmgr.h b/server/wsproxy/mastersvrmgr.h index 9373e6a..8c657c1 100644 --- a/server/wsproxy/mastersvrmgr.h +++ b/server/wsproxy/mastersvrmgr.h @@ -5,9 +5,17 @@ namespace f8 struct MsgHdr; } +namespace ss +{ + class SS_MS_ResponseTargetServer; +} + class MasterSvr; class MasterSvrMgr : public a8::Singleton { + public: + enum { HID = HID_MasterSvrMgr }; + private: MasterSvrMgr() {}; friend class a8::Singleton; @@ -17,6 +25,7 @@ class MasterSvrMgr : public a8::Singleton void Init(); void UnInit(); + void _SS_MS_ResponseTargetServer(f8::MsgHdr& hdr, const ss::SS_MS_ResponseTargetServer& msg); void RequestTargetServer(f8::MsgHdr& hdr, const std::string& team_id); void OnClientDisconnect(a8::XParams& param); diff --git a/server/wsproxy/target_conn_mgr.cc b/server/wsproxy/target_conn_mgr.cc index 3fbbf00..50ae376 100644 --- a/server/wsproxy/target_conn_mgr.cc +++ b/server/wsproxy/target_conn_mgr.cc @@ -29,6 +29,11 @@ void TargetConnMgr::UnInit() } #if MASTER_MODE +TargetConn* TargetConnMgr::GetConnByKey(const std::string& key) +{ + auto itr = target_conn_hash_.find(key); + return itr != target_conn_hash_.end() ? itr->second : nullptr; +} #else TargetConn* TargetConnMgr::GetConnByInstanceId(int instance_id) { diff --git a/server/wsproxy/target_conn_mgr.h b/server/wsproxy/target_conn_mgr.h index 0f7aa9c..f2c96d2 100644 --- a/server/wsproxy/target_conn_mgr.h +++ b/server/wsproxy/target_conn_mgr.h @@ -13,6 +13,7 @@ class TargetConnMgr : public a8::Singleton void UnInit(); #if MASTER_MODE + TargetConn* GetConnByKey(const std::string& key); #else TargetConn* GetConnByInstanceId(int instance_id); #endif @@ -20,6 +21,7 @@ class TargetConnMgr : public a8::Singleton private: #if MASTER_MODE + std::map target_conn_hash_; #else std::map target_conn_hash_; #endif From cacc618c2c4f445c67da580e2055c9549563d916 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Fri, 17 May 2019 15:01:42 +0800 Subject: [PATCH 08/15] add SendStockMsg --- server/wsproxy/app.cc | 11 +++++++- server/wsproxy/constant.h | 3 ++- server/wsproxy/gameclientmgr.cc | 7 ++++- server/wsproxy/gameclientmgr.h | 1 + server/wsproxy/mastersvrmgr.cc | 10 +++++-- server/wsproxy/mastersvrmgr.h | 2 +- server/wsproxy/target_conn.cc | 45 +++++++++++++++++++++++++++++++ server/wsproxy/target_conn.h | 25 +++++++++++++++-- server/wsproxy/target_conn_mgr.cc | 15 +++++------ server/wsproxy/target_conn_mgr.h | 12 +++------ 10 files changed, 106 insertions(+), 25 deletions(-) diff --git a/server/wsproxy/app.cc b/server/wsproxy/app.cc index b99a8c5..3d0668d 100644 --- a/server/wsproxy/app.cc +++ b/server/wsproxy/app.cc @@ -362,7 +362,7 @@ void App::ProcessClientMsg(f8::MsgHdr& hdr) ss::SS_CMLogin_CMReConnect_CommonHead msg; bool ok = msg.ParseFromArray(hdr.buf + hdr.offset, hdr.buflen - hdr.offset); if (ok) { - conn = TargetConnMgr::Instance()->GetConnByInstanceId(msg.server_id()); + conn = TargetConnMgr::Instance()->GetConnById(msg.server_id()); if (!conn) { ss::SS_SMRpcError respmsg; respmsg.set_error_code(10); @@ -420,6 +420,15 @@ void App::ProcessIMMsg() MasterSvrMgr::Instance()->OnClientDisconnect(pdelnode->params); } break; + case IM_TargetConnConnect: + { + GameClientMgr::Instance()->OnTargetServerDisconnect(pdelnode->params); + TargetConn* conn = TargetConnMgr::Instance()->GetConnById(pdelnode->params.sender); + if (conn && conn->Connected()) { + conn->SendStockMsg(); + } + } + break; case IM_TargetConnDisconnect: { GameClientMgr::Instance()->OnTargetServerDisconnect(pdelnode->params); diff --git a/server/wsproxy/constant.h b/server/wsproxy/constant.h index 3c28b59..b35c230 100644 --- a/server/wsproxy/constant.h +++ b/server/wsproxy/constant.h @@ -13,7 +13,8 @@ enum InnerMesssage_e IM_PlayerOffline, IM_ExecGM, IM_TargetConnDisconnect, - IM_MasterSvrDisconnect + IM_MasterSvrDisconnect, + IM_TargetConnConnect, }; //网络处理对象 diff --git a/server/wsproxy/gameclientmgr.cc b/server/wsproxy/gameclientmgr.cc index 7ec1a84..4941b67 100644 --- a/server/wsproxy/gameclientmgr.cc +++ b/server/wsproxy/gameclientmgr.cc @@ -44,6 +44,11 @@ void GameClientMgr::OnTargetServerDisconnect(a8::XParams& param) } } +void GameClientMgr::OnTargetServerConnect(a8::XParams& param) +{ + +} + GameClient* GameClientMgr::GetGameClientBySocket(int sockhandle) { auto itr = socket_hash_.find(sockhandle); @@ -54,7 +59,7 @@ GameClient* GameClientMgr::GetGameClientBySocket(int sockhandle) #else void GameClientMgr::BindTargetConn(int socket_handle, int conn_instance_id) { - TargetConn* conn = TargetConnMgr::Instance()->GetConnByInstanceId(conn_instance_id); + TargetConn* conn = TargetConnMgr::Instance()->GetConnById(conn_instance_id); if (conn) { GameClient* client = GetGameClientBySocket(socket_handle); if (client) { diff --git a/server/wsproxy/gameclientmgr.h b/server/wsproxy/gameclientmgr.h index 0436687..4d13800 100644 --- a/server/wsproxy/gameclientmgr.h +++ b/server/wsproxy/gameclientmgr.h @@ -14,6 +14,7 @@ class GameClientMgr : public a8::Singleton void OnClientDisconnect(a8::XParams& param); void OnTargetServerDisconnect(a8::XParams& param); + void OnTargetServerConnect(a8::XParams& param); GameClient* GetGameClientBySocket(int sockhande); #if MASTER_MODE #else diff --git a/server/wsproxy/mastersvrmgr.cc b/server/wsproxy/mastersvrmgr.cc index 5ca2d08..2b6f605 100644 --- a/server/wsproxy/mastersvrmgr.cc +++ b/server/wsproxy/mastersvrmgr.cc @@ -6,6 +6,8 @@ #include "mastersvr.h" #include "jsondatamgr.h" #include "ss_proto.pb.h" +#include "target_conn.h" +#include "target_conn_mgr.h" #include "framework/cpp/netmsghandler.h" @@ -37,7 +39,11 @@ void MasterSvrMgr::_SS_MS_ResponseTargetServer(f8::MsgHdr& hdr, const ss::SS_MS_ f8::MsgHdr* context_hdr = GetHdr(msg.context_id()); if (context_hdr) { if (msg.error_code() == 0) { + std::string key = msg.host() + ":" + a8::XValue(msg.port()).GetString(); + TargetConn* conn = TargetConnMgr::Instance()->GetConnByKey(key); + if (conn) { + } } pending_request_hash_.erase(msg.context_id()); @@ -49,7 +55,7 @@ void MasterSvrMgr::_SS_MS_ResponseTargetServer(f8::MsgHdr& hdr, const ss::SS_MS_ } } -MasterSvr* MasterSvrMgr::GetConnByInstanceId(int instance_id) +MasterSvr* MasterSvrMgr::GetConnById(int instance_id) { auto itr = mastersvr_hash_.find(instance_id); return itr != mastersvr_hash_.end() ? itr->second : nullptr; @@ -61,7 +67,7 @@ void MasterSvrMgr::RequestTargetServer(f8::MsgHdr& hdr, const std::string& team_ return; } unsigned int code = a8::openssl::Crc32((unsigned char*)team_id.data(), team_id.size()); - MasterSvr* svr = GetConnByInstanceId(code % mastersvr_hash_.size() + 1); + MasterSvr* svr = GetConnById(code % mastersvr_hash_.size() + 1); if (svr) { ++curr_context_id_; ss::SS_WSP_RequestTargetServer msg; diff --git a/server/wsproxy/mastersvrmgr.h b/server/wsproxy/mastersvrmgr.h index 8c657c1..19555d1 100644 --- a/server/wsproxy/mastersvrmgr.h +++ b/server/wsproxy/mastersvrmgr.h @@ -32,7 +32,7 @@ class MasterSvrMgr : public a8::Singleton private: long long GetConextId(int socket_handle); f8::MsgHdr* GetHdr(long long conext_id); - MasterSvr* GetConnByInstanceId(int instance_id); + MasterSvr* GetConnById(int instance_id); private: long long curr_context_id_ = 0; diff --git a/server/wsproxy/target_conn.cc b/server/wsproxy/target_conn.cc index 41a63bc..dc670e4 100644 --- a/server/wsproxy/target_conn.cc +++ b/server/wsproxy/target_conn.cc @@ -39,6 +39,17 @@ void TargetConn::Init(int instance_id, const std::string& remote_ip, int remote_ void TargetConn::UnInit() { + TargetConnMsgNode* work_node; + work_node = top_node_; + top_node_ = nullptr; + bot_node_ = nullptr; + while (work_node) { + TargetConnMsgNode* pdelnode = work_node; + work_node = work_node->next_node; + delete pdelnode->msg; + delete pdelnode; + } + a8::Timer::Instance()->DeleteTimer(timer_); timer_ = nullptr; tcp_client_->Close(); @@ -64,6 +75,22 @@ bool TargetConn::Connected() return tcp_client_->Connected(); } +void TargetConn::SendStockMsg() +{ + TargetConnMsgNode* work_node; + work_node = top_node_; + top_node_ = nullptr; + bot_node_ = nullptr; + while (work_node) { + TargetConnMsgNode* pdelnode = work_node; + work_node = work_node->next_node; + + f8::Net_SendProxyCMsg(tcp_client_, pdelnode->msgid, *pdelnode->msg); + delete pdelnode->msg; + delete pdelnode; + } +} + void TargetConn::ForwardClientMsg(f8::MsgHdr& hdr) { char* buff = (char*)malloc(sizeof(f8::WSProxyPackHead_C) + hdr.buflen); @@ -95,6 +122,10 @@ void TargetConn::on_connect(a8::TcpClient* sender) { recv_bufflen_ = 0; a8::UdpLog::Instance()->Info("target server connected", {}); + App::Instance()->AddIMMsg(IM_TargetConnDisconnect, + a8::XParams() + .SetSender(instance_id) + ); } void TargetConn::on_disconnect(a8::TcpClient* sender) @@ -170,3 +201,17 @@ void TargetConn::CheckAlive() } } } + +void TargetConn::AddStockMsg(int msgid, ::google::protobuf::Message* msg) +{ + TargetConnMsgNode* node = new TargetConnMsgNode(); + node->msgid = msgid; + node->msg = msg; + if (bot_node_) { + bot_node_->next_node = node; + bot_node_ = node; + } else { + top_node_ = node; + bot_node_ = node; + } +} diff --git a/server/wsproxy/target_conn.h b/server/wsproxy/target_conn.h index 4da2af3..bec71cd 100644 --- a/server/wsproxy/target_conn.h +++ b/server/wsproxy/target_conn.h @@ -7,6 +7,14 @@ namespace a8 class TcpClient; } +struct TargetConnMsgNode +{ + int msgid = 0; + ::google::protobuf::Message* msg = nullptr; + + TargetConnMsgNode* next_node = nullptr; +}; + struct timer_list; class TargetConn { @@ -14,7 +22,6 @@ class TargetConn int instance_id = 0; std::string remote_ip; int remote_port = 0; - int matching_player_num = 0; a8::tick_t last_pong_tick = 0; public: @@ -29,9 +36,19 @@ class TargetConn void SendMsg(T& msg) { static int msgid = f8::Net_GetMessageId(msg); - f8::Net_SendProxyCMsg(tcp_client_, msgid, msg); + if (Connected()) { + if (top_node_) { + SendStockMsg(); + } + f8::Net_SendProxyCMsg(tcp_client_, msgid, msg); + } else { + T* new_msg = new T(); + *new_msg = msg; + AddStockMsg(msgid, new_msg); + } } + void SendStockMsg(); void ForwardClientMsg(f8::MsgHdr& hdr); private: @@ -41,10 +58,14 @@ class TargetConn void on_socketread(a8::TcpClient* sender, char* buf, unsigned int len); void CheckAlive(); + void AddStockMsg(int msgid, ::google::protobuf::Message* msg); private: char *recv_buff_ = nullptr; unsigned int recv_bufflen_ = 0; a8::TcpClient* tcp_client_ = nullptr; timer_list* timer_ = nullptr; + + TargetConnMsgNode* top_node_ = nullptr; + TargetConnMsgNode* bot_node_ = nullptr; }; diff --git a/server/wsproxy/target_conn_mgr.cc b/server/wsproxy/target_conn_mgr.cc index 50ae376..0b0e300 100644 --- a/server/wsproxy/target_conn_mgr.cc +++ b/server/wsproxy/target_conn_mgr.cc @@ -28,16 +28,15 @@ void TargetConnMgr::UnInit() { } -#if MASTER_MODE TargetConn* TargetConnMgr::GetConnByKey(const std::string& key) { - auto itr = target_conn_hash_.find(key); - return itr != target_conn_hash_.end() ? itr->second : nullptr; + auto itr = key_hash_.find(key); + return itr != key_hash_.end() ? itr->second : nullptr; } -#else -TargetConn* TargetConnMgr::GetConnByInstanceId(int instance_id) + +TargetConn* TargetConnMgr::GetConnById(int instance_id) { - auto itr = target_conn_hash_.find(instance_id); - return itr != target_conn_hash_.end() ? itr->second : nullptr; + auto itr = id_hash_.find(instance_id); + return itr != id_hash_.end() ? itr->second : nullptr; } -#endif + diff --git a/server/wsproxy/target_conn_mgr.h b/server/wsproxy/target_conn_mgr.h index f2c96d2..ef8d742 100644 --- a/server/wsproxy/target_conn_mgr.h +++ b/server/wsproxy/target_conn_mgr.h @@ -12,17 +12,11 @@ class TargetConnMgr : public a8::Singleton void Init(); void UnInit(); -#if MASTER_MODE TargetConn* GetConnByKey(const std::string& key); -#else - TargetConn* GetConnByInstanceId(int instance_id); -#endif + TargetConn* GetConnById(int instance_id); private: -#if MASTER_MODE - std::map target_conn_hash_; -#else - std::map target_conn_hash_; -#endif + std::map key_hash_; + std::map id_hash_; }; From d16e00654ddae44689dd1a20d2656cdeb0f955a9 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Fri, 17 May 2019 16:04:44 +0800 Subject: [PATCH 09/15] 1 --- server/wsproxy/app.cc | 7 +------ server/wsproxy/app.h | 3 +-- server/wsproxy/gameclientmgr.cc | 3 --- server/wsproxy/gameclientmgr.h | 3 --- server/wsproxy/mastersvrmgr.h | 1 + 5 files changed, 3 insertions(+), 14 deletions(-) diff --git a/server/wsproxy/app.cc b/server/wsproxy/app.cc index 3d0668d..1e7ef82 100644 --- a/server/wsproxy/app.cc +++ b/server/wsproxy/app.cc @@ -304,10 +304,7 @@ void App::DispatchMsg() break; case SF_TargetServer: { -#if MASTER_MODE -#else ProcessTargetServerMsg(hdr); -#endif } break; case SF_MasterServer: @@ -383,12 +380,11 @@ void App::ProcessClientMsg(f8::MsgHdr& hdr) #endif } -#if MASTER_MODE void App::ProcessMasterServerMsg(f8::MsgHdr& hdr) { } -#else + void App::ProcessTargetServerMsg(f8::MsgHdr& hdr) { if (hdr.msgid < 100) { @@ -400,7 +396,6 @@ void App::ProcessTargetServerMsg(f8::MsgHdr& hdr) } GCListener::Instance()->ForwardTargetConnMsg(hdr); } -#endif void App::ProcessIMMsg() { diff --git a/server/wsproxy/app.h b/server/wsproxy/app.h index c8af816..6b8c333 100644 --- a/server/wsproxy/app.h +++ b/server/wsproxy/app.h @@ -44,9 +44,8 @@ private: void ProcessClientMsg(f8::MsgHdr& hdr); #if MASTER_MODE void ProcessMasterServerMsg(f8::MsgHdr& hdr); -#else - void ProcessTargetServerMsg(f8::MsgHdr& hdr); #endif + void ProcessTargetServerMsg(f8::MsgHdr& hdr); void InitLog(); void UnInitLog(); diff --git a/server/wsproxy/gameclientmgr.cc b/server/wsproxy/gameclientmgr.cc index 4941b67..295b658 100644 --- a/server/wsproxy/gameclientmgr.cc +++ b/server/wsproxy/gameclientmgr.cc @@ -55,8 +55,6 @@ GameClient* GameClientMgr::GetGameClientBySocket(int sockhandle) return itr != socket_hash_.end() ? itr->second : nullptr; } -#if MASTER_MODE -#else void GameClientMgr::BindTargetConn(int socket_handle, int conn_instance_id) { TargetConn* conn = TargetConnMgr::Instance()->GetConnById(conn_instance_id); @@ -72,4 +70,3 @@ void GameClientMgr::BindTargetConn(int socket_handle, int conn_instance_id) } } } -#endif diff --git a/server/wsproxy/gameclientmgr.h b/server/wsproxy/gameclientmgr.h index 4d13800..28bbb71 100644 --- a/server/wsproxy/gameclientmgr.h +++ b/server/wsproxy/gameclientmgr.h @@ -16,10 +16,7 @@ class GameClientMgr : public a8::Singleton void OnTargetServerDisconnect(a8::XParams& param); void OnTargetServerConnect(a8::XParams& param); GameClient* GetGameClientBySocket(int sockhande); -#if MASTER_MODE -#else void BindTargetConn(int socket_handle, int conn_instance_id); -#endif private: std::map socket_hash_; diff --git a/server/wsproxy/mastersvrmgr.h b/server/wsproxy/mastersvrmgr.h index 19555d1..639760a 100644 --- a/server/wsproxy/mastersvrmgr.h +++ b/server/wsproxy/mastersvrmgr.h @@ -35,6 +35,7 @@ class MasterSvrMgr : public a8::Singleton MasterSvr* GetConnById(int instance_id); private: + int target_conn_id_ = 100; long long curr_context_id_ = 0; std::map mastersvr_hash_; std::map pending_socket_hash_; From b82a5e2d02a254a0e0ce1f01ab71735da53a6056 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Fri, 17 May 2019 16:14:41 +0800 Subject: [PATCH 10/15] 1 --- server/wsproxy/app.cc | 11 ++++------- server/wsproxy/mastersvrmgr.cc | 2 +- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/server/wsproxy/app.cc b/server/wsproxy/app.cc index 1e7ef82..266aa55 100644 --- a/server/wsproxy/app.cc +++ b/server/wsproxy/app.cc @@ -336,23 +336,20 @@ 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 (client && client->conn) { + if (client->conn) { + client->conn->ForwardClientMsg(hdr); + } } } - if (conn) { - conn->ForwardClientMsg(hdr); - } #else TargetConn* conn = nullptr; if (hdr.msgid == ss::_SS_CMLogin || hdr.msgid == ss::_SS_CMReConnect) { diff --git a/server/wsproxy/mastersvrmgr.cc b/server/wsproxy/mastersvrmgr.cc index 2b6f605..4092e12 100644 --- a/server/wsproxy/mastersvrmgr.cc +++ b/server/wsproxy/mastersvrmgr.cc @@ -42,7 +42,7 @@ void MasterSvrMgr::_SS_MS_ResponseTargetServer(f8::MsgHdr& hdr, const ss::SS_MS_ std::string key = msg.host() + ":" + a8::XValue(msg.port()).GetString(); TargetConn* conn = TargetConnMgr::Instance()->GetConnByKey(key); if (conn) { - + conn->ForwardClientMsg(hdr); } } From 1e555756fa8564c6868be20c9a01d862238e922e Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Fri, 17 May 2019 16:22:50 +0800 Subject: [PATCH 11/15] 1 --- server/wsproxy/app.cc | 14 +++++++++++--- server/wsproxy/app.h | 2 -- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/server/wsproxy/app.cc b/server/wsproxy/app.cc index 266aa55..f6a5f80 100644 --- a/server/wsproxy/app.cc +++ b/server/wsproxy/app.cc @@ -309,9 +309,7 @@ void App::DispatchMsg() break; case SF_MasterServer: { -#if MASTER_MODE ProcessMasterServerMsg(hdr); -#endif } break; } @@ -379,7 +377,17 @@ void App::ProcessClientMsg(f8::MsgHdr& hdr) void App::ProcessMasterServerMsg(f8::MsgHdr& hdr) { - +#if MASTER_MODE + f8::NetMsgHandler* handler = f8::GetNetMsgHandler(&HandlerMgr::Instance()->msmsghandler, + hdr.msgid); + if (handler) { + switch (handler->handlerid) { + case HID_MasterSvrMgr: + ProcessNetMsg(handler, MasterSvrMgr::Instance(), hdr); + break; + } + } +#endif } void App::ProcessTargetServerMsg(f8::MsgHdr& hdr) diff --git a/server/wsproxy/app.h b/server/wsproxy/app.h index 6b8c333..8fdb272 100644 --- a/server/wsproxy/app.h +++ b/server/wsproxy/app.h @@ -42,9 +42,7 @@ private: void ProcessIMMsg(); void ProcessClientMsg(f8::MsgHdr& hdr); -#if MASTER_MODE void ProcessMasterServerMsg(f8::MsgHdr& hdr); -#endif void ProcessTargetServerMsg(f8::MsgHdr& hdr); void InitLog(); From 559a50f82c3487d18ad41b344f36e5b82d431215 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Sat, 18 May 2019 10:55:58 +0800 Subject: [PATCH 12/15] 1 --- server/wsproxy/app.cc | 2 +- server/wsproxy/constant.h | 1 + server/wsproxy/mastersvrmgr.cc | 52 +++++++++++++++++++++------------- server/wsproxy/mastersvrmgr.h | 6 ++-- 4 files changed, 38 insertions(+), 23 deletions(-) diff --git a/server/wsproxy/app.cc b/server/wsproxy/app.cc index f6a5f80..25248b8 100644 --- a/server/wsproxy/app.cc +++ b/server/wsproxy/app.cc @@ -417,7 +417,7 @@ void App::ProcessIMMsg() case IM_ClientSocketDisconnect: { GameClientMgr::Instance()->OnClientDisconnect(pdelnode->params); - MasterSvrMgr::Instance()->OnClientDisconnect(pdelnode->params); + MasterSvrMgr::Instance()->RemoveRequest(pdelnode->params.param1, pdelnode->params.sender); } break; case IM_TargetConnConnect: diff --git a/server/wsproxy/constant.h b/server/wsproxy/constant.h index b35c230..3673a5a 100644 --- a/server/wsproxy/constant.h +++ b/server/wsproxy/constant.h @@ -15,6 +15,7 @@ enum InnerMesssage_e IM_TargetConnDisconnect, IM_MasterSvrDisconnect, IM_TargetConnConnect, + IM_RequestTargetServerTimeout }; //网络处理对象 diff --git a/server/wsproxy/mastersvrmgr.cc b/server/wsproxy/mastersvrmgr.cc index 4092e12..c57ed44 100644 --- a/server/wsproxy/mastersvrmgr.cc +++ b/server/wsproxy/mastersvrmgr.cc @@ -1,6 +1,7 @@ #include "precompile.h" #include +#include #include "mastersvrmgr.h" #include "mastersvr.h" @@ -8,6 +9,7 @@ #include "ss_proto.pb.h" #include "target_conn.h" #include "target_conn_mgr.h" +#include "app.h" #include "framework/cpp/netmsghandler.h" @@ -36,7 +38,7 @@ void MasterSvrMgr::UnInit() void MasterSvrMgr::_SS_MS_ResponseTargetServer(f8::MsgHdr& hdr, const ss::SS_MS_ResponseTargetServer& msg) { - f8::MsgHdr* context_hdr = GetHdr(msg.context_id()); + f8::MsgHdr* context_hdr = GetHdrByContextId(msg.context_id()); if (context_hdr) { if (msg.error_code() == 0) { std::string key = msg.host() + ":" + a8::XValue(msg.port()).GetString(); @@ -45,13 +47,7 @@ void MasterSvrMgr::_SS_MS_ResponseTargetServer(f8::MsgHdr& hdr, const ss::SS_MS_ conn->ForwardClientMsg(hdr); } } - - pending_request_hash_.erase(msg.context_id()); - pending_socket_hash_.erase(hdr.socket_handle); - if (context_hdr->buf) { - free((char*)context_hdr->buf); - } - free(context_hdr); + RemoveRequest(context_hdr->socket_handle, msg.context_id()); } } @@ -63,47 +59,65 @@ MasterSvr* MasterSvrMgr::GetConnById(int instance_id) void MasterSvrMgr::RequestTargetServer(f8::MsgHdr& hdr, const std::string& team_id) { - if (GetConextId(hdr.socket_handle) == 0) { + if (GetContextIdBySocket(hdr.socket_handle) == 0) { return; } unsigned int code = a8::openssl::Crc32((unsigned char*)team_id.data(), team_id.size()); MasterSvr* svr = GetConnById(code % mastersvr_hash_.size() + 1); if (svr) { ++curr_context_id_; + a8::TimerAttacher* timer_attacher = new a8::TimerAttacher(); + f8::MsgHdr* new_hdr = hdr.Clone(); + new_hdr->user_data = timer_attacher; + 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_; assert(pending_request_hash_.find(curr_context_id_) == pending_request_hash_.end()); - pending_request_hash_[curr_context_id_] = hdr.Clone(); + pending_request_hash_[curr_context_id_] = new_hdr; + a8::Timer::Instance()->AddDeadLineTimerAndAttach(1000 * 10, + a8::XParams() + .SetSender(curr_context_id_) + .SetParam1(hdr.socket_handle), + [] (const a8::XParams& param) + { + App::Instance()->AddIMMsg(IM_RequestTargetServerTimeout, + a8::XParams() + .SetSender(param.sender) + .SetParam1(param.param1)); + }, + &timer_attacher->timer_list_); } } -void MasterSvrMgr::OnClientDisconnect(a8::XParams& param) +void MasterSvrMgr::RemoveRequest(int socket_handle, long long context_id) { - long long conext_id = GetConextId(param.sender); - if (conext_id != 0) { - f8::MsgHdr* hdr = GetHdr(conext_id); + if (context_id == GetContextIdBySocket(socket_handle)) { + f8::MsgHdr* hdr = GetHdrByContextId(context_id); if (hdr) { + a8::TimerAttacher* timer_attacher = (a8::TimerAttacher*)hdr->user_data; + delete timer_attacher; if (hdr->buf) { free((char*)hdr->buf); } free(hdr); - pending_request_hash_.erase(conext_id); + pending_request_hash_.erase(context_id); } - pending_socket_hash_.erase(param.sender); + pending_socket_hash_.erase(socket_handle); } } -long long MasterSvrMgr::GetConextId(int socket_handle) +long long MasterSvrMgr::GetContextIdBySocket(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) +f8::MsgHdr* MasterSvrMgr::GetHdrByContextId(long long context_id) { - auto itr = pending_request_hash_.find(conext_id); + auto itr = pending_request_hash_.find(context_id); return itr != pending_request_hash_.end() ? itr->second : nullptr; } diff --git a/server/wsproxy/mastersvrmgr.h b/server/wsproxy/mastersvrmgr.h index 639760a..67833b5 100644 --- a/server/wsproxy/mastersvrmgr.h +++ b/server/wsproxy/mastersvrmgr.h @@ -27,11 +27,11 @@ class MasterSvrMgr : public a8::Singleton void _SS_MS_ResponseTargetServer(f8::MsgHdr& hdr, const ss::SS_MS_ResponseTargetServer& msg); void RequestTargetServer(f8::MsgHdr& hdr, const std::string& team_id); - void OnClientDisconnect(a8::XParams& param); + void RemoveRequest(int socket_handle, long long context_id); private: - long long GetConextId(int socket_handle); - f8::MsgHdr* GetHdr(long long conext_id); + long long GetContextIdBySocket(int socket_handle); + f8::MsgHdr* GetHdrByContextId(long long context_id); MasterSvr* GetConnById(int instance_id); private: From f8afa41c280bd05a9f03e1e25a8a4caac0c76250 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Sat, 18 May 2019 11:11:56 +0800 Subject: [PATCH 13/15] RecreateTargetConn --- server/wsproxy/mastersvrmgr.cc | 7 +++++-- server/wsproxy/target_conn_mgr.cc | 18 ++++++++++++++++++ server/wsproxy/target_conn_mgr.h | 3 ++- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/server/wsproxy/mastersvrmgr.cc b/server/wsproxy/mastersvrmgr.cc index c57ed44..5a8a08c 100644 --- a/server/wsproxy/mastersvrmgr.cc +++ b/server/wsproxy/mastersvrmgr.cc @@ -41,8 +41,11 @@ void MasterSvrMgr::_SS_MS_ResponseTargetServer(f8::MsgHdr& hdr, const ss::SS_MS_ f8::MsgHdr* context_hdr = GetHdrByContextId(msg.context_id()); if (context_hdr) { if (msg.error_code() == 0) { - std::string key = msg.host() + ":" + a8::XValue(msg.port()).GetString(); - TargetConn* conn = TargetConnMgr::Instance()->GetConnByKey(key); + TargetConn* conn = TargetConnMgr::Instance()->RecreateTargetConn( + msg.host(), + msg.port() + ); + assert(conn); if (conn) { conn->ForwardClientMsg(hdr); } diff --git a/server/wsproxy/target_conn_mgr.cc b/server/wsproxy/target_conn_mgr.cc index 0b0e300..2df7128 100644 --- a/server/wsproxy/target_conn_mgr.cc +++ b/server/wsproxy/target_conn_mgr.cc @@ -40,3 +40,21 @@ TargetConn* TargetConnMgr::GetConnById(int instance_id) return itr != id_hash_.end() ? itr->second : nullptr; } +TargetConn* TargetConnMgr::RecreateTargetConn(const std::string& host, int port) +{ + std::string key = host + ":" + a8::XValue(port).GetString(); + TargetConn* conn = GetConnByKey(key); + if (conn) { + return conn; + } + while (GetConnById(++curr_id_)) {}; + int instance_id = curr_id_; + std::string remote_ip = host; + int remote_port = port; + + conn->Init(instance_id, remote_ip, remote_port); + id_hash_[conn->instance_id] = conn; + key_hash_[key] = conn; + conn->Open(); + return conn; +} diff --git a/server/wsproxy/target_conn_mgr.h b/server/wsproxy/target_conn_mgr.h index ef8d742..d939be7 100644 --- a/server/wsproxy/target_conn_mgr.h +++ b/server/wsproxy/target_conn_mgr.h @@ -14,9 +14,10 @@ class TargetConnMgr : public a8::Singleton TargetConn* GetConnByKey(const std::string& key); TargetConn* GetConnById(int instance_id); + TargetConn* RecreateTargetConn(const std::string& host, int port); private: - + unsigned short curr_id_ = 1000; std::map key_hash_; std::map id_hash_; }; From 59f9908c61b02d383382e315cb4de6da8a0df3d5 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Sat, 18 May 2019 11:14:39 +0800 Subject: [PATCH 14/15] 1 --- server/wsproxy/target_conn_mgr.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/wsproxy/target_conn_mgr.cc b/server/wsproxy/target_conn_mgr.cc index 2df7128..ba1d743 100644 --- a/server/wsproxy/target_conn_mgr.cc +++ b/server/wsproxy/target_conn_mgr.cc @@ -17,7 +17,7 @@ void TargetConnMgr::Init() { TargetConn* conn = new TargetConn(); conn->Init(instance_id, remote_ip, remote_port); - target_conn_hash_[conn->instance_id] = conn; + id_hash_[conn->instance_id] = conn; conn->Open(); } } From 0ea82a59475162aa18f2238b89fcc874132bedd5 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Sat, 18 May 2019 16:52:58 +0800 Subject: [PATCH 15/15] 1 --- server/tools/protobuild/ss_msgid.proto | 2 ++ server/wsproxy/GCListener.cc | 7 +++++++ server/wsproxy/jsondatamgr.cc | 13 +------------ server/wsproxy/jsondatamgr.h | 7 ------- third_party/a8engine | 2 +- 5 files changed, 11 insertions(+), 20 deletions(-) diff --git a/server/tools/protobuild/ss_msgid.proto b/server/tools/protobuild/ss_msgid.proto index 55b8f45..e65940c 100644 --- a/server/tools/protobuild/ss_msgid.proto +++ b/server/tools/protobuild/ss_msgid.proto @@ -4,6 +4,8 @@ package ss; enum SSMessageId_e { _SS_WSP_SocketDisconnect = 10; + _SS_WSP_RequestTargetServer = 11; + _SS_MS_ResponseTargetServer = 12; _SS_CMPing = 101; _SS_SMRpcError = 102; diff --git a/server/wsproxy/GCListener.cc b/server/wsproxy/GCListener.cc index ed32398..4360f4a 100644 --- a/server/wsproxy/GCListener.cc +++ b/server/wsproxy/GCListener.cc @@ -58,6 +58,13 @@ public: .SetParam3(saddr)); } + virtual bool HandleRedirect(const std::string& url, const std::string& querystr, + std::string& location) override + { + location = "ws://192.168.100.21:7101"; + return true; + } + virtual void OnDisConnect() override { App::Instance()->AddIMMsg(IM_ClientSocketDisconnect, diff --git a/server/wsproxy/jsondatamgr.cc b/server/wsproxy/jsondatamgr.cc index aec73a2..54c0289 100644 --- a/server/wsproxy/jsondatamgr.cc +++ b/server/wsproxy/jsondatamgr.cc @@ -6,28 +6,19 @@ void JsonDataMgr::Init() { std::string wsproxyserver_cluster_json_file; -#if MASTER_MODE std::string masterserver_cluster_json_file; -#else std::string targetserver_cluster_json_file; -#endif if (f8::IsOnlineEnv()) { wsproxyserver_cluster_json_file = a8::Format("../config/game%d.wsproxy.cluster.json", {GAME_ID}); -#if MASTER_MODE masterserver_cluster_json_file = a8::Format("../config/game%d.masterserver.cluster.json", {GAME_ID}); -#else targetserver_cluster_json_file = a8::Format("../config/game%d.gameserver.cluster.json", {GAME_ID}); -#endif } else { wsproxyserver_cluster_json_file = a8::Format("/var/data/conf_test/game%d/wsproxy/game%d.wsproxy.cluster.json", {GAME_ID, GAME_ID}); -#if MASTER_MODE masterserver_cluster_json_file = a8::Format("/var/data/conf_test/game%d/wsproxy/game%d.masterserver.cluster.json", {GAME_ID, GAME_ID}); -#else targetserver_cluster_json_file = a8::Format("/var/data/conf_test/game%d/wsproxy/game%d.gameserver.cluster.json", {GAME_ID, GAME_ID}); -#endif } wsproxyserver_cluster_json_.ReadFromFile(wsproxyserver_cluster_json_file); #if MASTER_MODE @@ -49,14 +40,12 @@ std::shared_ptr JsonDataMgr::GetConf() return wsproxyserver_cluster_json_[App::Instance()->instance_id - 1]; } -#if MASTER_MODE std::shared_ptr JsonDataMgr::GetMasterServerClusterConf() { return std::make_shared(masterserver_cluster_json_); } -#else + std::shared_ptr JsonDataMgr::GetTargetServerClusterConf() { return std::make_shared(targetserver_cluster_json_); } -#endif diff --git a/server/wsproxy/jsondatamgr.h b/server/wsproxy/jsondatamgr.h index 8f69d59..bdc5bac 100644 --- a/server/wsproxy/jsondatamgr.h +++ b/server/wsproxy/jsondatamgr.h @@ -11,19 +11,12 @@ class JsonDataMgr : public a8::Singleton void UnInit(); std::shared_ptr GetConf(); -#if MASTER_MODE std::shared_ptr GetMasterServerClusterConf(); -#else std::shared_ptr GetTargetServerClusterConf(); -#endif - private: a8::XObject wsproxyserver_cluster_json_; -#if MASTER_MODE a8::XObject masterserver_cluster_json_; -#else a8::XObject targetserver_cluster_json_; -#endif }; diff --git a/third_party/a8engine b/third_party/a8engine index fc99a36..ff8c006 160000 --- a/third_party/a8engine +++ b/third_party/a8engine @@ -1 +1 @@ -Subproject commit fc99a3615db9aabc1a77489e069a4e6af26d50d5 +Subproject commit ff8c00652d5367595c4adee8f95306ae1a46236b