diff --git a/README.md b/README.md index 91aeef18..dd702e7d 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,8 @@ **Unreal SDK**: [squick-unreal-sdk](https://github.com/pwnsky/squick-unreal-sdk) +**Web Admin**: [squick-web](https://github.com/pwnsky/squick-web) + **QQ Group:739065686** **Version:** 1.1.1 diff --git a/config/plugin/db_proxy.xml b/config/plugin/db_proxy.xml index 3d6db5e2..8372ad3e 100644 --- a/config/plugin/db_proxy.xml +++ b/config/plugin/db_proxy.xml @@ -1,12 +1,12 @@ - - - - - - - - - + + + + + + + + + \ No newline at end of file diff --git a/config/plugin/ds_mgr.xml b/config/plugin/ds_mgr.xml index 4e54a0bb..e0d01c6a 100644 --- a/config/plugin/ds_mgr.xml +++ b/config/plugin/ds_mgr.xml @@ -1,9 +1,8 @@ - - - - - + + + + \ No newline at end of file diff --git a/config/plugin/game.xml b/config/plugin/game.xml index af1b35cf..c860891d 100644 --- a/config/plugin/game.xml +++ b/config/plugin/game.xml @@ -1,9 +1,8 @@ - - - - - + + + + - + \ No newline at end of file diff --git a/config/plugin/login.xml b/config/plugin/login.xml index 0512921b..f8aa994f 100644 --- a/config/plugin/login.xml +++ b/config/plugin/login.xml @@ -2,8 +2,7 @@ - - + diff --git a/config/plugin/master.xml b/config/plugin/master.xml index bf0929ce..2874b45d 100644 --- a/config/plugin/master.xml +++ b/config/plugin/master.xml @@ -1,11 +1,8 @@ - - - - - - - + + + + \ No newline at end of file diff --git a/config/plugin/player.xml b/config/plugin/player.xml index bc8ae1db..b6ac8108 100644 --- a/config/plugin/player.xml +++ b/config/plugin/player.xml @@ -1,14 +1,14 @@ - - - - - - - - - - - - + + + + + + + + + + + + diff --git a/config/plugin/proxy.xml b/config/plugin/proxy.xml index c99ed4d1..a0689d10 100644 --- a/config/plugin/proxy.xml +++ b/config/plugin/proxy.xml @@ -4,8 +4,8 @@ + - \ No newline at end of file diff --git a/config/plugin/sqkcli.xml b/config/plugin/sqkcli.xml index da063313..692294fa 100644 --- a/config/plugin/sqkcli.xml +++ b/config/plugin/sqkcli.xml @@ -1,9 +1,8 @@ - - - - - - - + + + + + + \ No newline at end of file diff --git a/config/plugin/tutorial/t1_plugin.xml b/config/plugin/tutorial/t1_plugin.xml index 781532ee..56fd216e 100644 --- a/config/plugin/tutorial/t1_plugin.xml +++ b/config/plugin/tutorial/t1_plugin.xml @@ -1,5 +1,5 @@ - - - + + + \ No newline at end of file diff --git a/config/plugin/tutorial/t2_property.xml b/config/plugin/tutorial/t2_property.xml index 978ce44b..179142d1 100644 --- a/config/plugin/tutorial/t2_property.xml +++ b/config/plugin/tutorial/t2_property.xml @@ -1,5 +1,5 @@ - - - + + + \ No newline at end of file diff --git a/config/plugin/tutorial/t5_http.xml b/config/plugin/tutorial/t5_http.xml index c5ab0149..5ba15c78 100644 --- a/config/plugin/tutorial/t5_http.xml +++ b/config/plugin/tutorial/t5_http.xml @@ -2,7 +2,6 @@ - \ No newline at end of file diff --git a/config/plugin/tutorial/t6_rpc.xml b/config/plugin/tutorial/t6_rpc.xml index b7f43e3c..487769fe 100644 --- a/config/plugin/tutorial/t6_rpc.xml +++ b/config/plugin/tutorial/t6_rpc.xml @@ -2,7 +2,6 @@ - \ No newline at end of file diff --git a/config/plugin/web.xml b/config/plugin/web.xml index 4857983f..825e0f07 100644 --- a/config/plugin/web.xml +++ b/config/plugin/web.xml @@ -1,10 +1,8 @@ - - - - - - - + + + + + \ No newline at end of file diff --git a/config/plugin/world.xml b/config/plugin/world.xml index 3fd08481..d079557f 100644 --- a/config/plugin/world.xml +++ b/config/plugin/world.xml @@ -1,14 +1,13 @@ - - - + + + - + - - - - - + + + + \ No newline at end of file diff --git a/src/lua/common/base.lua b/src/lua/common/base.lua index 37b268dc..e69de29b 100644 --- a/src/lua/common/base.lua +++ b/src/lua/common/base.lua @@ -1,16 +0,0 @@ -ServerType = -{ - ST_NONE = 0, - ST_MASTER = 1, - ST_LOGIN = 2, - ST_WORLD = 3, - ST_DB_PROXY = 4, - ST_PROXY = 5, - ST_PLAYER = 6, - ST_GAME_MGR = 7, - ST_GAME = 8, - ST_MICRO = 9, - ST_CDN = 10, - ST_ROBOT = 11, - ST_MAX = 12, -}; diff --git a/src/lua/common/net.lua b/src/lua/common/net.lua index 99c398f9..f49ab230 100644 --- a/src/lua/common/net.lua +++ b/src/lua/common/net.lua @@ -8,6 +8,7 @@ Net = {} -- function Table:func(uid, msg_data, msg_id, fd) function Net:Register(msg_id, this, func) + Squick:LogInfo("Lua Register msg: " .. tostring(msg_id)) Squick:AddMsgCallBackAsServer(msg_id, this, func) end diff --git a/src/lua/lib/db/clickhouse.lua b/src/lua/lib/db/clickhouse.lua index a71ea81e..7b57de68 100644 --- a/src/lua/lib/db/clickhouse.lua +++ b/src/lua/lib/db/clickhouse.lua @@ -9,9 +9,9 @@ local DbClickhouseID = 0; Clickhouse = Clickhouse and Clickhouse or Object(QueryAsync).new() function Clickhouse:Bind() - Net:ClientRegister(ServerType.ST_DB_PROXY, NMsgId.IdNAckClickhouseExecute, self, self.AckExecute) - Net:ClientRegister(ServerType.ST_DB_PROXY, NMsgId.IdNAckClickhouseSelect, self, self.AckSelect) - Net:ClientRegister(ServerType.ST_DB_PROXY, NMsgId.IdNAckClickhouseInsert, self, self.AckInsert) + Net:ClientRegister(NodeType.ST_DB_PROXY, NMsgId.IdNAckClickhouseExecute, self, self.AckExecute) + Net:ClientRegister(NodeType.ST_DB_PROXY, NMsgId.IdNAckClickhouseSelect, self, self.AckSelect) + Net:ClientRegister(NodeType.ST_DB_PROXY, NMsgId.IdNAckClickhouseInsert, self, self.AckInsert) DbClickhouseID = GetDbProxyID() end diff --git a/src/lua/lib/db/mongo.lua b/src/lua/lib/db/mongo.lua index 46d97a29..d74dbcb5 100644 --- a/src/lua/lib/db/mongo.lua +++ b/src/lua/lib/db/mongo.lua @@ -9,11 +9,11 @@ local DbProxyID = 0; Mongo = Mongo and Mongo or Object(QueryAsync).new() function Mongo:Bind() - Net:ClientRegister(ServerType.ST_DB_PROXY, NMsgId.IdNAckMongoInsert, self, self.AckInsert) - Net:ClientRegister(ServerType.ST_DB_PROXY, NMsgId.IdNAckMongoFind, self, self.AckFind) - Net:ClientRegister(ServerType.ST_DB_PROXY, NMsgId.IdNAckMongoUpdate, self, self.AckUpdate) - Net:ClientRegister(ServerType.ST_DB_PROXY, NMsgId.IdNAckMongoDelete, self, self.AckDelete) - Net:ClientRegister(ServerType.ST_DB_PROXY, NMsgId.IdNAckMongoCreateIndex, self, self.AckCreateIndex) + Net:ClientRegister(NodeType.ST_DB_PROXY, NMsgId.IdNAckMongoInsert, self, self.AckInsert) + Net:ClientRegister(NodeType.ST_DB_PROXY, NMsgId.IdNAckMongoFind, self, self.AckFind) + Net:ClientRegister(NodeType.ST_DB_PROXY, NMsgId.IdNAckMongoUpdate, self, self.AckUpdate) + Net:ClientRegister(NodeType.ST_DB_PROXY, NMsgId.IdNAckMongoDelete, self, self.AckDelete) + Net:ClientRegister(NodeType.ST_DB_PROXY, NMsgId.IdNAckMongoCreateIndex, self, self.AckCreateIndex) DbProxyID = GetDbProxyID() end diff --git a/src/lua/lib/db/mysql.lua b/src/lua/lib/db/mysql.lua index 7c32195b..eb58889b 100644 --- a/src/lua/lib/db/mysql.lua +++ b/src/lua/lib/db/mysql.lua @@ -9,10 +9,10 @@ local DbMysqlID = 0; Mysql = Mysql and Mysql or Object(QueryAsync).new() function Mysql:Bind() - Net:ClientRegister(ServerType.ST_DB_PROXY, NMsgId.IdNAckMysqlExecute, self, self.AckExecute) - Net:ClientRegister(ServerType.ST_DB_PROXY, NMsgId.IdNAckMysqlSelect, self, self.AckSelect) - Net:ClientRegister(ServerType.ST_DB_PROXY, NMsgId.IdNAckMysqlInsert, self, self.AckInsert) - Net:ClientRegister(ServerType.ST_DB_PROXY, NMsgId.IdNAckMysqlUpdate, self, self.AckUpdate) + Net:ClientRegister(NodeType.ST_DB_PROXY, NMsgId.IdNAckMysqlExecute, self, self.AckExecute) + Net:ClientRegister(NodeType.ST_DB_PROXY, NMsgId.IdNAckMysqlSelect, self, self.AckSelect) + Net:ClientRegister(NodeType.ST_DB_PROXY, NMsgId.IdNAckMysqlInsert, self, self.AckInsert) + Net:ClientRegister(NodeType.ST_DB_PROXY, NMsgId.IdNAckMysqlUpdate, self, self.AckUpdate) DbMysqlID = GetDbProxyID() end diff --git a/src/lua/lib/db/redis.lua b/src/lua/lib/db/redis.lua index a42afecf..90413e35 100644 --- a/src/lua/lib/db/redis.lua +++ b/src/lua/lib/db/redis.lua @@ -9,8 +9,8 @@ local DbProxyID = 0; Redis = Redis and Redis or Object(QueryAsync).new() function Redis:Bind() - Net:ClientRegister(ServerType.ST_DB_PROXY, NMsgId.IdNAckRedisGet, self, self.AckGetString) - Net:ClientRegister(ServerType.ST_DB_PROXY, NMsgId.IdNAckRedisSet, self, self.AckSetString) + Net:ClientRegister(NodeType.ST_DB_PROXY, NMsgId.IdNAckRedisGet, self, self.AckGetString) + Net:ClientRegister(NodeType.ST_DB_PROXY, NMsgId.IdNAckRedisSet, self, self.AckSetString) DbProxyID = GetDbProxyID() end diff --git a/src/lua/main.lua b/src/lua/main.lua index 06b0f4fe..d31043bc 100644 --- a/src/lua/main.lua +++ b/src/lua/main.lua @@ -50,7 +50,7 @@ function ReadyUpdate() ModuleMgr:Start() end --- Update for per second +-- Update 10 fps function Update() ModuleMgr:Update() end @@ -74,12 +74,12 @@ function Load() Require("lib.init") local node_init = { - [ServerType.ST_GAME ] = function() + [NodeType.ST_GAME ] = function() Require("node.game.init") end, - [ServerType.ST_PLAYER] = function () + [NodeType.ST_PLAYER] = function () Require("node.player.init") - --Require("test.init") + -- Require("test.init") end } if(node_init[Env.app_type]) then diff --git a/src/node/db_proxy/CMakeLists.txt b/src/node/db_proxy/CMakeLists.txt index deb6fc02..2ba061c4 100644 --- a/src/node/db_proxy/CMakeLists.txt +++ b/src/node/db_proxy/CMakeLists.txt @@ -1,5 +1,5 @@ -add_subdirectory(node) add_subdirectory(mysql) add_subdirectory(mongo) add_subdirectory(redis) -add_subdirectory(clickhouse) \ No newline at end of file +add_subdirectory(clickhouse) +add_subdirectory(logic) \ No newline at end of file diff --git a/src/node/db_proxy/node/CMakeLists.txt b/src/node/db_proxy/logic/CMakeLists.txt similarity index 75% rename from src/node/db_proxy/node/CMakeLists.txt rename to src/node/db_proxy/logic/CMakeLists.txt index 96b7e872..fd240f17 100644 --- a/src/node/db_proxy/node/CMakeLists.txt +++ b/src/node/db_proxy/logic/CMakeLists.txt @@ -1,6 +1,6 @@ -set(ProjectName "db_proxy_node") +set(ProjectName "db_proxy_logic") file(GLOB soruces *.cc) file(GLOB headers *.h) add_library(${ProjectName} SHARED ${soruces} ${headers}) LinkSquick(${ProjectName}) -SquickOut(${ProjectName} "node/db_proxy" ${SQUICK_BUILD_PLUGIN_SERVER_PATH}/db_proxy "node") \ No newline at end of file +SquickOut(${ProjectName} "node/db_proxy" ${SQUICK_BUILD_PLUGIN_SERVER_PATH}/db_proxy "logic") \ No newline at end of file diff --git a/src/node/db_proxy/logic/i_logic_module.h b/src/node/db_proxy/logic/i_logic_module.h new file mode 100644 index 00000000..93a3acd2 --- /dev/null +++ b/src/node/db_proxy/logic/i_logic_module.h @@ -0,0 +1,7 @@ +#pragma once + +#include +namespace db_proxy::logic { +class ILogicModule : public IModule {}; + +} // namespace db_proxy::logic diff --git a/src/node/db_proxy/logic/logic_module.cc b/src/node/db_proxy/logic/logic_module.cc new file mode 100644 index 00000000..c1cb73b7 --- /dev/null +++ b/src/node/db_proxy/logic/logic_module.cc @@ -0,0 +1,10 @@ +#include "logic_module.h" + +namespace db_proxy::logic { +bool LogicModule::Start() { + m_node_ = pm_->FindModule(); + vector node_types = {}; + m_node_->AddSubscribeNode(node_types); + return true; +} +} // namespace db_proxy::logic \ No newline at end of file diff --git a/src/node/db_proxy/logic/logic_module.h b/src/node/db_proxy/logic/logic_module.h new file mode 100644 index 00000000..125a57a9 --- /dev/null +++ b/src/node/db_proxy/logic/logic_module.h @@ -0,0 +1,19 @@ +#pragma once +#include "i_logic_module.h" +#include +#include + +namespace db_proxy::logic { +class LogicModule : public ILogicModule { + public: + LogicModule(IPluginManager *p) { + pm_ = p; + is_update_ = true; // Update + } + virtual bool Start() override; + + private: + INodeModule *m_node_; +}; + +} // namespace db_proxy::logic \ No newline at end of file diff --git a/src/node/master/node/plugin.cc b/src/node/db_proxy/logic/plugin.cc similarity index 57% rename from src/node/master/node/plugin.cc rename to src/node/db_proxy/logic/plugin.cc index 8cc1d5c8..0fd0571e 100644 --- a/src/node/master/node/plugin.cc +++ b/src/node/db_proxy/logic/plugin.cc @@ -1,8 +1,8 @@ #include "plugin.h" -#include "node_module.h" +#include "logic_module.h" +namespace db_proxy::logic { -namespace master::node { SQUICK_EXPORT void SquickPluginLoad(IPluginManager *pm){CREATE_PLUGIN(pm, Plugin)}; SQUICK_EXPORT void SquickPluginUnload(IPluginManager *pm){DESTROY_PLUGIN(pm, Plugin)}; @@ -11,8 +11,8 @@ const int Plugin::GetPluginVersion() { return 0; } const std::string Plugin::GetPluginName() { return GET_CLASS_NAME(Plugin); } -void Plugin::Install() { REGISTER_MODULE(pm_, INodeModule, NodeModule) } +void Plugin::Install() { REGISTER_MODULE(pm_, ILogicModule, LogicModule) } -void Plugin::Uninstall() { UNREGISTER_MODULE(pm_, INodeModule, NodeModule) } +void Plugin::Uninstall() { UNREGISTER_MODULE(pm_, ILogicModule, LogicModule) } -} // namespace master::node \ No newline at end of file +} // namespace db_proxy::logic \ No newline at end of file diff --git a/src/node/web/node/plugin.h b/src/node/db_proxy/logic/plugin.h similarity index 67% rename from src/node/web/node/plugin.h rename to src/node/db_proxy/logic/plugin.h index 85a31384..df5773a7 100644 --- a/src/node/web/node/plugin.h +++ b/src/node/db_proxy/logic/plugin.h @@ -1,16 +1,17 @@ #pragma once -#include -#include - -namespace web::node { +#include +namespace db_proxy::logic { class Plugin : public IPlugin { public: Plugin(IPluginManager *p) { pm_ = p; } virtual const int GetPluginVersion(); + virtual const std::string GetPluginName(); + virtual void Install(); + virtual void Uninstall(); }; -} // namespace web::node +} // namespace db_proxy::logic \ No newline at end of file diff --git a/src/node/db_proxy/mysql/mysql_module.cc b/src/node/db_proxy/mysql/mysql_module.cc index ca4fd185..d477483a 100644 --- a/src/node/db_proxy/mysql/mysql_module.cc +++ b/src/node/db_proxy/mysql/mysql_module.cc @@ -54,6 +54,9 @@ void MysqlModule::OnReqExecute(const socket_t sock, const int msg_id, const char rpc::NAckMysqlExecute ack; uint64_t uid; try { + if (!session_) { + throw "session_ is nullptr"; + } assert(m_net_->ReceivePB(msg_id, msg, len, req, uid)); session_->sql(req.sql()).execute(); } catch (const std::exception &e) { @@ -72,6 +75,9 @@ void MysqlModule::OnReqSelect(const socket_t sock, const int msg_id, const char rpc::NAckMysqlSelect ack; uint64_t uid; try { + if (!session_) { + throw "session_ is nullptr"; + } assert(m_net_->ReceivePB(msg_id, msg, len, req, uid)); SqlResult result = session_->sql(req.sql()).execute(); auto result_data = result.fetchAll(); @@ -139,6 +145,9 @@ void MysqlModule::OnReqInsert(const socket_t sock, const int msg_id, const char rpc::NAckMysqlInsert ack; uint64_t uid; try { + if (!session_) { + throw exception("sessions_ is nullptr"); + } assert(m_net_->ReceivePB(msg_id, msg, len, req, uid)); auto schema = session_->getSchema(req.database()); auto table = schema.getTable(req.table()); @@ -176,7 +185,12 @@ void MysqlModule::OnReqInsert(const socket_t sock, const int msg_id, const char LogError(e.what(), __func__, __LINE__); code = rpc::DbProxyCode::DB_PROXY_CODE_MYSQL_EXCEPTION; ack.set_msg(e.what()); + } catch (...) { + LogError("Insert excption", __func__, __LINE__); + code = rpc::DbProxyCode::DB_PROXY_CODE_MYSQL_EXCEPTION; + ack.set_msg("Insert excption"); } + ack.set_code(code); ack.set_query_id(req.query_id()); m_net_->SendPBToNode(rpc::IdNAckMysqlInsert, ack, sock); @@ -188,6 +202,9 @@ void MysqlModule::OnReqUpdate(const socket_t sock, const int msg_id, const char rpc::NAckMysqlUpdate ack; uint64_t uid; try { + if (!session_) { + throw "session_ is nullptr"; + } assert(m_net_->ReceivePB(msg_id, msg, len, req, uid)); auto schema = session_->getSchema(req.database()); auto table = schema.getTable(req.table()); diff --git a/src/node/db_proxy/node/i_node_module.h b/src/node/db_proxy/node/i_node_module.h deleted file mode 100644 index 3b1df02d..00000000 --- a/src/node/db_proxy/node/i_node_module.h +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once - -#include -#include - -namespace db_proxy::node { -class INodeModule : public INodeBaseModule {}; - -} // namespace db_proxy::node diff --git a/src/node/db_proxy/node/node_module.cc b/src/node/db_proxy/node/node_module.cc deleted file mode 100644 index eecff82a..00000000 --- a/src/node/db_proxy/node/node_module.cc +++ /dev/null @@ -1,22 +0,0 @@ -#include - -// #include "../logic/player_redis_module.h" -#include "node_module.h" - -namespace db_proxy::node { - -bool NodeModule::AfterStart() { - - Listen(); - vector node_types = {ServerType::ST_WORLD}; - AddNodesByType(node_types); - return true; -} - -bool NodeModule::Destroy() { return true; } - -void NodeModule::OnClientDisconnect(const socket_t sock) {} - -void NodeModule::OnClientConnected(const socket_t sock) {} - -} // namespace db_proxy::node \ No newline at end of file diff --git a/src/node/db_proxy/node/node_module.h b/src/node/db_proxy/node/node_module.h deleted file mode 100644 index 5564c80b..00000000 --- a/src/node/db_proxy/node/node_module.h +++ /dev/null @@ -1,23 +0,0 @@ -#pragma once - -#include "i_node_module.h" -#include -#include -namespace db_proxy::node { -class NodeModule : public INodeModule { - public: - NodeModule(IPluginManager *p) { - pm_ = p; - is_update_ = true; - } - virtual bool Destroy(); - virtual bool AfterStart(); - - protected: - void OnClientDisconnect(const socket_t sock); - void OnClientConnected(const socket_t sock); - - protected: -}; - -} // namespace db_proxy::node \ No newline at end of file diff --git a/src/node/db_proxy/redis/redis_module.h b/src/node/db_proxy/redis/redis_module.h index 843dd1ec..ce4dbcba 100644 --- a/src/node/db_proxy/redis/redis_module.h +++ b/src/node/db_proxy/redis/redis_module.h @@ -1,4 +1,3 @@ -#include "../node/i_node_module.h" #include "i_redis_module.h" #include #include diff --git a/src/node/ds_mgr/CMakeLists.txt b/src/node/ds_mgr/CMakeLists.txt index 4e7656c1..8bf6ea94 100644 --- a/src/node/ds_mgr/CMakeLists.txt +++ b/src/node/ds_mgr/CMakeLists.txt @@ -1,2 +1 @@ -add_subdirectory(node) add_subdirectory(logic) diff --git a/src/node/ds_mgr/logic/logic_module.cc b/src/node/ds_mgr/logic/logic_module.cc index 162a8a3a..3251d3f7 100644 --- a/src/node/ds_mgr/logic/logic_module.cc +++ b/src/node/ds_mgr/logic/logic_module.cc @@ -31,7 +31,7 @@ bool LogicModule::AfterStart() { }*/ // 来自Game 服务器 - m_net_client_->AddReceiveCallBack(ServerType::ST_GAME, rpc::REQ_GAMEPLAY_CREATE, this, &LogicModule::OnReqPvpInstanceCreate); + m_net_client_->AddReceiveCallBack(rpc::ST_GAME, rpc::REQ_GAMEPLAY_CREATE, this, &LogicModule::OnReqPvpInstanceCreate); return true; } diff --git a/src/node/ds_mgr/node/CMakeLists.txt b/src/node/ds_mgr/node/CMakeLists.txt deleted file mode 100644 index 96f32553..00000000 --- a/src/node/ds_mgr/node/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -set(ProjectName "ds_mgr_node") -file(GLOB soruces *.cc) -file(GLOB headers *.h) -add_library(${ProjectName} SHARED ${soruces} ${headers}) -LinkSquick(${ProjectName}) -SquickOut(${ProjectName} "node/ds_mgr" ${SQUICK_BUILD_PLUGIN_SERVER_PATH}/ds_mgr "node") \ No newline at end of file diff --git a/src/node/ds_mgr/node/i_node_module.h b/src/node/ds_mgr/node/i_node_module.h deleted file mode 100644 index 03eac446..00000000 --- a/src/node/ds_mgr/node/i_node_module.h +++ /dev/null @@ -1,10 +0,0 @@ -#pragma once -#include - -namespace gameplay_manager::node { -class INodeModule : public INodeBaseModule { - public: - virtual bool Transport(const socket_t sock, const int msg_id, const char *msg, const uint32_t len) = 0; -}; - -} // namespace gameplay_manager::node \ No newline at end of file diff --git a/src/node/ds_mgr/node/node_module.cc b/src/node/ds_mgr/node/node_module.cc deleted file mode 100644 index 783601b9..00000000 --- a/src/node/ds_mgr/node/node_module.cc +++ /dev/null @@ -1,32 +0,0 @@ - - -#include "node_module.h" -#include -#include -#include -namespace gameplay_manager::node { - -bool NodeModule::Destroy() { return true; } - -bool NodeModule::AfterStart() { - // PVP校验连接Key,校验成功后,之后的数据直接转发到Game服务器上 - // m_net_->AddReceiveCallBack(rpc::REQ_GAMEPLAY_CONNECT_GAME_SERVER, this, &NodeModule::OnReqConnect); - // m_net_->AddReceiveCallBack(this, &NodeModule::OnOtherMessage); - Listen(); - // AddServer(ServerType::ST); - return true; -} - -// Gameplay服务器请求连接 Gameplay manager服务器 -void NodeModule::OnReqConnect(const socket_t sock, const int msg_id, const char *msg, const uint32_t len) {} - -// 来自PVP服务器消息,根据校验连接成功PVP服务器数据包转发达到Game服务器 -// void NodeModule::OnOtherMessage(const socket_t sock, const int msg_id, const char *msg, const uint32_t len) {} - -// PVP 服务端断开连接 -void NodeModule::OnClientDisconnect(const socket_t sock) {} - -// 转发给PVP服务器 -bool NodeModule::Transport(const socket_t sock, const int msg_id, const char *msg, const uint32_t len) { return true; } - -} // namespace gameplay_manager::node \ No newline at end of file diff --git a/src/node/ds_mgr/node/node_module.h b/src/node/ds_mgr/node/node_module.h deleted file mode 100644 index 4a440959..00000000 --- a/src/node/ds_mgr/node/node_module.h +++ /dev/null @@ -1,33 +0,0 @@ -#pragma once - -#include "i_node_module.h" -#include - -namespace gameplay_manager::node { - -class NodeModule : public INodeModule { - public: - NodeModule(IPluginManager *p) { - pm_ = p; - is_update_ = true; - } - - virtual bool Destroy(); - - virtual bool AfterStart(); - - virtual bool Transport(const socket_t sock, const int msg_id, const char *msg, const uint32_t len); - - protected: - // PVP服务器连接处理 - void OnReqConnect(const socket_t sock, const int msg_id, const char *msg, const uint32_t len); - void OnOtherMessage(const socket_t sock, const int msg_id, const char *msg, const uint32_t len); - - // PVP服务器断开连接 - void OnClientDisconnect(const socket_t sock); - // 转发消息给PVP服务器 - - protected: -}; - -} // namespace gameplay_manager::node diff --git a/src/node/ds_mgr/node/plugin.cc b/src/node/ds_mgr/node/plugin.cc deleted file mode 100644 index 29563afd..00000000 --- a/src/node/ds_mgr/node/plugin.cc +++ /dev/null @@ -1,22 +0,0 @@ - -#include "plugin.h" -#include "node_module.h" - -namespace gameplay_manager::node { -SQUICK_EXPORT void SquickPluginLoad(IPluginManager *pm){ - - CREATE_PLUGIN(pm, Plugin) - -}; - -SQUICK_EXPORT void SquickPluginUnload(IPluginManager *pm){DESTROY_PLUGIN(pm, Plugin)}; - -const int Plugin::GetPluginVersion() { return 0; } - -const std::string Plugin::GetPluginName() { return GET_CLASS_NAME(Plugin); } - -void Plugin::Install() { REGISTER_MODULE(pm_, INodeModule, NodeModule) } - -void Plugin::Uninstall() { UNREGISTER_MODULE(pm_, INodeModule, NodeModule) } - -} // namespace gameplay_manager::node \ No newline at end of file diff --git a/src/node/ds_mgr/node/plugin.h b/src/node/ds_mgr/node/plugin.h deleted file mode 100644 index 2cac6c62..00000000 --- a/src/node/ds_mgr/node/plugin.h +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once - -#include -#include - -namespace gameplay_manager::node { -class Plugin : public IPlugin { - public: - Plugin(IPluginManager *p) { pm_ = p; } - virtual const int GetPluginVersion(); - virtual const std::string GetPluginName(); - virtual void Install(); - virtual void Uninstall(); -}; - -} // namespace gameplay_manager::node \ No newline at end of file diff --git a/src/node/game/CMakeLists.txt b/src/node/game/CMakeLists.txt index 515ca71f..8bf6ea94 100644 --- a/src/node/game/CMakeLists.txt +++ b/src/node/game/CMakeLists.txt @@ -1,2 +1 @@ add_subdirectory(logic) -add_subdirectory(node) diff --git a/src/node/game/logic/game_mgr_module.cc b/src/node/game/logic/game_mgr_module.cc index cb4ddab8..c5d02559 100644 --- a/src/node/game/logic/game_mgr_module.cc +++ b/src/node/game/logic/game_mgr_module.cc @@ -9,7 +9,7 @@ bool GameMgrModule::Start() { m_class_ = pm_->FindModule(); m_net_ = pm_->FindModule(); m_net_client_ = pm_->FindModule(); - m_node_ = pm_->FindModule(); + m_node_ = pm_->FindModule(); return true; } diff --git a/src/node/game/logic/i_game_mgr_module.h b/src/node/game/logic/i_game_mgr_module.h index 9ea21715..91d6de0f 100644 --- a/src/node/game/logic/i_game_mgr_module.h +++ b/src/node/game/logic/i_game_mgr_module.h @@ -3,10 +3,10 @@ #include #include -#include #include #include #include +#include namespace game::logic { @@ -54,7 +54,7 @@ class IGameMgrModule : public IModule { ILogModule *m_log_; IClassModule *m_class_; IElementModule *m_element_; - node::INodeModule *m_node_; + INodeModule *m_node_; INetClientModule *m_net_client_; private: diff --git a/src/node/game/node/CMakeLists.txt b/src/node/game/node/CMakeLists.txt deleted file mode 100644 index c057c50e..00000000 --- a/src/node/game/node/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -set(ProjectName "game_node") -file(GLOB soruces *.cc) -file(GLOB headers *.h) -add_library(${ProjectName} SHARED ${soruces} ${headers}) -LinkSquick(${ProjectName}) -SquickOut(${ProjectName} "node/game" ${SQUICK_BUILD_PLUGIN_SERVER_PATH}/game "node") \ No newline at end of file diff --git a/src/node/game/node/i_node_module.h b/src/node/game/node/i_node_module.h deleted file mode 100644 index 4d098962..00000000 --- a/src/node/game/node/i_node_module.h +++ /dev/null @@ -1,18 +0,0 @@ - -// Author: i0gan -// Email : l418894113@gmail.com -// Date : 2023-03-22 -// Github: https://github.com/pwnsky/squick -// Description: gameplay node module - -#pragma once - -#include - -namespace game::node { -class INodeModule : public INodeBaseModule { - public: - protected: - private: -}; -} // namespace game::node \ No newline at end of file diff --git a/src/node/game/node/node_module.cc b/src/node/game/node/node_module.cc deleted file mode 100644 index 07170493..00000000 --- a/src/node/game/node/node_module.cc +++ /dev/null @@ -1,10 +0,0 @@ - -#include "node_module.h" - -namespace game::node { - -bool NodeModule::Destroy() { return true; } - -bool NodeModule::AfterStart() { return true; } - -} // namespace game::node diff --git a/src/node/game/node/node_module.h b/src/node/game/node/node_module.h deleted file mode 100644 index 48ff2666..00000000 --- a/src/node/game/node/node_module.h +++ /dev/null @@ -1,26 +0,0 @@ -// Author: i0gan -// Email : l418894113@gmail.com -// Date : 2022-03-22 -// Github: https://github.com/pwnsky/squick -// Description: game server module - -#pragma once - -#include -#include -#include -#include - -#include "i_node_module.h" -namespace game::node { -class NodeModule : public INodeModule { - public: - NodeModule(IPluginManager *p) { pm_ = p; } - - virtual bool Destroy(); - virtual bool AfterStart(); - - protected: - private: -}; -} // namespace game::node diff --git a/src/node/game/node/plugin.cc b/src/node/game/node/plugin.cc deleted file mode 100644 index 7a5dfc0c..00000000 --- a/src/node/game/node/plugin.cc +++ /dev/null @@ -1,17 +0,0 @@ -#include "plugin.h" -#include "node_module.h" - -namespace game::node { -SQUICK_EXPORT void SquickPluginLoad(IPluginManager *pm){CREATE_PLUGIN(pm, Plugin)}; - -SQUICK_EXPORT void SquickPluginUnload(IPluginManager *pm){DESTROY_PLUGIN(pm, Plugin)}; - -const int Plugin::GetPluginVersion() { return 0; } - -const std::string Plugin::GetPluginName() { return GET_CLASS_NAME(Plugin); } - -void Plugin::Install() { REGISTER_MODULE(pm_, INodeModule, NodeModule) } - -void Plugin::Uninstall() { UNREGISTER_MODULE(pm_, INodeModule, NodeModule) } - -} // namespace game::node \ No newline at end of file diff --git a/src/node/game/node/plugin.h b/src/node/game/node/plugin.h deleted file mode 100644 index 6bc09604..00000000 --- a/src/node/game/node/plugin.h +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once -#include -#include - -namespace game::node { -class Plugin : public IPlugin { - public: - Plugin(IPluginManager *p) { pm_ = p; } - virtual const int GetPluginVersion(); - virtual const std::string GetPluginName(); - virtual void Install(); - virtual void Uninstall(); -}; - -} // namespace game::node diff --git a/src/node/global/CMakeLists.txt b/src/node/global/CMakeLists.txt index 83f38ba6..77f77563 100644 --- a/src/node/global/CMakeLists.txt +++ b/src/node/global/CMakeLists.txt @@ -1 +1 @@ -add_subdirectory(node) +add_subdirectory(logic) \ No newline at end of file diff --git a/src/node/global/node/CMakeLists.txt b/src/node/global/logic/CMakeLists.txt similarity index 77% rename from src/node/global/node/CMakeLists.txt rename to src/node/global/logic/CMakeLists.txt index ad869f04..e4935a4d 100644 --- a/src/node/global/node/CMakeLists.txt +++ b/src/node/global/logic/CMakeLists.txt @@ -1,6 +1,6 @@ -set(ProjectName "global_node") +set(ProjectName "global_logic") file(GLOB soruces *.cc) file(GLOB headers *.h) add_library(${ProjectName} SHARED ${soruces} ${headers}) LinkSquick(${ProjectName}) -SquickOut(${ProjectName} "node/global" ${SQUICK_BUILD_PLUGIN_SERVER_PATH}/global "node") +SquickOut(${ProjectName} "node/global" ${SQUICK_BUILD_PLUGIN_SERVER_PATH}/global "logic") \ No newline at end of file diff --git a/src/node/global/logic/i_logic_module.h b/src/node/global/logic/i_logic_module.h new file mode 100644 index 00000000..239669e6 --- /dev/null +++ b/src/node/global/logic/i_logic_module.h @@ -0,0 +1,7 @@ +#pragma once + +#include +namespace global::logic { +class ILogicModule : public IModule {}; + +} // namespace global::logic diff --git a/src/node/global/logic/logic_module.cc b/src/node/global/logic/logic_module.cc new file mode 100644 index 00000000..a11b3793 --- /dev/null +++ b/src/node/global/logic/logic_module.cc @@ -0,0 +1,10 @@ +#include "logic_module.h" + +namespace global::logic { +bool LogicModule::Start() { + m_node_ = pm_->FindModule(); + vector node_types = {rpc::ST_DB_PROXY}; + m_node_->AddSubscribeNode(node_types); + return true; +} +} // namespace global::logic \ No newline at end of file diff --git a/src/node/global/logic/logic_module.h b/src/node/global/logic/logic_module.h new file mode 100644 index 00000000..146a494f --- /dev/null +++ b/src/node/global/logic/logic_module.h @@ -0,0 +1,19 @@ +#pragma once +#include "i_logic_module.h" +#include +#include + +namespace global::logic { +class LogicModule : public ILogicModule { + public: + LogicModule(IPluginManager *p) { + pm_ = p; + is_update_ = true; // Update + } + virtual bool Start() override; + + private: + INodeModule *m_node_; +}; + +} // namespace global::logic \ No newline at end of file diff --git a/src/node/db_proxy/node/plugin.cc b/src/node/global/logic/plugin.cc similarity index 58% rename from src/node/db_proxy/node/plugin.cc rename to src/node/global/logic/plugin.cc index e6b7d9b6..ce2c0d5b 100644 --- a/src/node/db_proxy/node/plugin.cc +++ b/src/node/global/logic/plugin.cc @@ -1,18 +1,18 @@ - #include "plugin.h" -#include "node_module.h" +#include "logic_module.h" +namespace global::logic { -namespace db_proxy::node { SQUICK_EXPORT void SquickPluginLoad(IPluginManager *pm){CREATE_PLUGIN(pm, Plugin)}; + SQUICK_EXPORT void SquickPluginUnload(IPluginManager *pm){DESTROY_PLUGIN(pm, Plugin)}; const int Plugin::GetPluginVersion() { return 0; } const std::string Plugin::GetPluginName() { return GET_CLASS_NAME(Plugin); } -void Plugin::Install() { REGISTER_MODULE(pm_, INodeModule, NodeModule) } +void Plugin::Install() { REGISTER_MODULE(pm_, ILogicModule, LogicModule) } -void Plugin::Uninstall() { UNREGISTER_MODULE(pm_, INodeModule, NodeModule) } +void Plugin::Uninstall() { UNREGISTER_MODULE(pm_, ILogicModule, LogicModule) } -} // namespace db_proxy::node \ No newline at end of file +} // namespace global::logic \ No newline at end of file diff --git a/src/node/login/node/plugin.h b/src/node/global/logic/plugin.h similarity index 83% rename from src/node/login/node/plugin.h rename to src/node/global/logic/plugin.h index b18880f2..047e495b 100644 --- a/src/node/login/node/plugin.h +++ b/src/node/global/logic/plugin.h @@ -1,8 +1,7 @@ #pragma once #include - -namespace login::node { +namespace global::logic { class Plugin : public IPlugin { public: Plugin(IPluginManager *p) { pm_ = p; } @@ -12,4 +11,4 @@ class Plugin : public IPlugin { virtual void Uninstall(); }; -} // namespace login::node +} // namespace global::logic \ No newline at end of file diff --git a/src/node/global/node/i_node_module.h b/src/node/global/node/i_node_module.h deleted file mode 100644 index 008c8691..00000000 --- a/src/node/global/node/i_node_module.h +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once - -#include -namespace global::node { -class INodeModule : public INodeBaseModule { - public: -}; - -} // namespace global::node diff --git a/src/node/global/node/node_module.cc b/src/node/global/node/node_module.cc deleted file mode 100644 index b282f77b..00000000 --- a/src/node/global/node/node_module.cc +++ /dev/null @@ -1,15 +0,0 @@ -#include "node_module.h" -#include - -namespace global::node { -bool NodeModule::AfterStart() { - Listen(); - - vector node_types = {ServerType::ST_GLOBAL, ServerType::ST_DB_PROXY}; - AddNodesByType(node_types); - return true; -} - -bool NodeModule::Destroy() { return true; } - -} // namespace global::node diff --git a/src/node/global/node/node_module.h b/src/node/global/node/node_module.h deleted file mode 100644 index dfbe7182..00000000 --- a/src/node/global/node/node_module.h +++ /dev/null @@ -1,19 +0,0 @@ -#pragma once - -#include "i_node_module.h" - -namespace global::node { -class NodeModule : public INodeModule { - public: - NodeModule(IPluginManager *p) { - pm_ = p; - is_update_ = true; - } - virtual bool Destroy(); - virtual bool AfterStart(); - - protected: - private: -}; - -} // namespace global::node diff --git a/src/node/global/node/plugin.cc b/src/node/global/node/plugin.cc deleted file mode 100644 index ef083db3..00000000 --- a/src/node/global/node/plugin.cc +++ /dev/null @@ -1,17 +0,0 @@ -#include "plugin.h" -#include "node_module.h" - -namespace global::node { -SQUICK_EXPORT void SquickPluginLoad(IPluginManager *pm){CREATE_PLUGIN(pm, Plugin)}; - -SQUICK_EXPORT void SquickPluginUnload(IPluginManager *pm){DESTROY_PLUGIN(pm, Plugin)}; - -const int Plugin::GetPluginVersion() { return 0; } - -const std::string Plugin::GetPluginName() { return GET_CLASS_NAME(Plugin); } - -void Plugin::Install() { REGISTER_MODULE(pm_, INodeModule, NodeModule) } - -void Plugin::Uninstall() { UNREGISTER_MODULE(pm_, INodeModule, NodeModule) } - -} // namespace global::node diff --git a/src/node/global/node/plugin.h b/src/node/global/node/plugin.h deleted file mode 100644 index 42aa506c..00000000 --- a/src/node/global/node/plugin.h +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once - -#include -#include - -namespace global::node { -class Plugin : public IPlugin { - public: - Plugin(IPluginManager *p) { pm_ = p; } - virtual const int GetPluginVersion(); - virtual const std::string GetPluginName(); - virtual void Install(); - virtual void Uninstall(); -}; - -} // namespace global::node diff --git a/src/node/login/CMakeLists.txt b/src/node/login/CMakeLists.txt index c3ebfb40..77f77563 100644 --- a/src/node/login/CMakeLists.txt +++ b/src/node/login/CMakeLists.txt @@ -1,2 +1 @@ -add_subdirectory(logic) -add_subdirectory(node) \ No newline at end of file +add_subdirectory(logic) \ No newline at end of file diff --git a/src/node/login/logic/logic_module.cc b/src/node/login/logic/logic_module.cc index d4df4f55..212c8d18 100644 --- a/src/node/login/logic/logic_module.cc +++ b/src/node/login/logic/logic_module.cc @@ -6,7 +6,7 @@ namespace login::logic { bool LogicModule::Start() { m_http_server_ = pm_->FindModule(); - m_node_ = pm_->FindModule(); + m_node_ = pm_->FindModule(); m_net_client_ = pm_->FindModule(); m_net_ = pm_->FindModule(); m_log_ = pm_->FindModule(); @@ -23,6 +23,8 @@ bool LogicModule::AfterStart() { m_net_->AddReceiveCallBack(rpc::IdNReqConnectProxyVerify, this, &LogicModule::OnConnectProxyVerify); m_http_server_->StartServer(pm_->GetArg("http_port=", 80)); + vector node_types = {rpc::ST_DB_PROXY}; + m_node_->AddSubscribeNode(node_types); return true; } @@ -61,7 +63,7 @@ Coroutine LogicModule::OnLogin(std::shared_ptr request) { // find min work load proxy rpc::NReqMinWorkloadNodeInfo pbreq; - pbreq.add_type_list(ST_PROXY); + pbreq.add_type_list(rpc::ST_PROXY); auto data = co_await m_net_client_->RequestPB(DEFAULT_MASTER_ID, rpc::IdNReqMinWorkloadNodeInfo, pbreq, rpc::IdNAckMinWorkloadNodeInfo); if (data.error) { ack.code = IResponse::SERVER_ERROR; diff --git a/src/node/login/logic/logic_module.h b/src/node/login/logic/logic_module.h index d533ac3d..f55d16b4 100644 --- a/src/node/login/logic/logic_module.h +++ b/src/node/login/logic/logic_module.h @@ -2,10 +2,10 @@ #include "i_logic_module.h" #include #include -#include #include #include #include +#include #include #include #include @@ -51,7 +51,7 @@ class LogicModule : public ILogicModule { INetClientModule *m_net_client_; INetModule *m_net_; IHttpServerModule *m_http_server_; - node::INodeModule *m_node_; + INodeModule *m_node_; ILogModule *m_log_; private: diff --git a/src/node/login/node/CMakeLists.txt b/src/node/login/node/CMakeLists.txt deleted file mode 100644 index 9eb83219..00000000 --- a/src/node/login/node/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -set(ProjectName "login_node") -file(GLOB soruces *.cc) -file(GLOB headers *.h) -add_library(${ProjectName} SHARED ${soruces} ${headers}) -LinkSquick(${ProjectName}) -SquickOut(${ProjectName} "node/login" ${SQUICK_BUILD_PLUGIN_SERVER_PATH}/login "node") \ No newline at end of file diff --git a/src/node/login/node/i_node_module.h b/src/node/login/node/i_node_module.h deleted file mode 100644 index 8eb995e1..00000000 --- a/src/node/login/node/i_node_module.h +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once -#include - -namespace login::node { -class INodeModule : public INodeBaseModule { - public: -}; - -} // namespace login::node diff --git a/src/node/login/node/node_module.cc b/src/node/login/node/node_module.cc deleted file mode 100644 index 5f97b616..00000000 --- a/src/node/login/node/node_module.cc +++ /dev/null @@ -1,24 +0,0 @@ - -#include "node_module.h" -#include "plugin.h" - -namespace login::node { - -bool NodeModule::Destroy() { return true; } - -bool NodeModule::BeforeDestroy() { return true; } - -bool NodeModule::AfterStart() { - m_net_->AddReceiveCallBack(this, &NodeModule::InvalidMessage); - Listen(); - - vector node_types = {}; - AddNodesByType(node_types); - return true; -} - -void NodeModule::OnClientConnected(const socket_t sock) {} - -void NodeModule::InvalidMessage(const socket_t sock, const int msg_id, const char *msg, const uint32_t len) { printf("Net || umsg_id=%d\n", msg_id); } - -} // namespace login::node \ No newline at end of file diff --git a/src/node/login/node/node_module.h b/src/node/login/node/node_module.h deleted file mode 100644 index c49903e8..00000000 --- a/src/node/login/node/node_module.h +++ /dev/null @@ -1,23 +0,0 @@ -#pragma once -#include "../logic/i_logic_module.h" -#include "i_node_module.h" - -namespace login::node { -class NodeModule : public INodeModule { - - public: - NodeModule(IPluginManager *p) { - pm_ = p; - is_update_ = true; - } - - virtual bool Destroy(); - virtual bool BeforeDestroy(); - virtual bool AfterStart(); - - protected: - virtual void OnClientConnected(const socket_t sock) override; - void InvalidMessage(const socket_t sock, const int msg_id, const char *msg, const uint32_t len); -}; - -} // namespace login::node \ No newline at end of file diff --git a/src/node/login/node/plugin.cc b/src/node/login/node/plugin.cc deleted file mode 100644 index 180c1e9e..00000000 --- a/src/node/login/node/plugin.cc +++ /dev/null @@ -1,17 +0,0 @@ -#include "plugin.h" -#include "node_module.h" - -namespace login::node { -SQUICK_EXPORT void SquickPluginLoad(IPluginManager *pm){CREATE_PLUGIN(pm, Plugin)}; - -SQUICK_EXPORT void SquickPluginUnload(IPluginManager *pm){DESTROY_PLUGIN(pm, Plugin)}; - -const int Plugin::GetPluginVersion() { return 0; } - -const std::string Plugin::GetPluginName() { return GET_CLASS_NAME(Plugin); } - -void Plugin::Install() { REGISTER_MODULE(pm_, INodeModule, NodeModule) } - -void Plugin::Uninstall() { UNREGISTER_MODULE(pm_, INodeModule, NodeModule) } - -} // namespace login::node \ No newline at end of file diff --git a/src/node/master/CMakeLists.txt b/src/node/master/CMakeLists.txt index 8633b65b..77f77563 100644 --- a/src/node/master/CMakeLists.txt +++ b/src/node/master/CMakeLists.txt @@ -1,3 +1 @@ -add_subdirectory(logic) -add_subdirectory(http) -add_subdirectory(node) +add_subdirectory(logic) \ No newline at end of file diff --git a/src/node/master/http/CMakeLists.txt b/src/node/master/http/CMakeLists.txt deleted file mode 100644 index 2bf38dc1..00000000 --- a/src/node/master/http/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -set(ProjectName "master_http") -file(GLOB soruces *.cc) -file(GLOB headers *.h) -add_library(${ProjectName} SHARED ${soruces} ${headers}) -LinkSquick(${ProjectName}) -SquickOut(${ProjectName} "node/master" ${SQUICK_BUILD_PLUGIN_SERVER_PATH}/master "http") \ No newline at end of file diff --git a/src/node/master/http/http_module.cc b/src/node/master/http/http_module.cc deleted file mode 100644 index 0739d271..00000000 --- a/src/node/master/http/http_module.cc +++ /dev/null @@ -1,60 +0,0 @@ -#include "http_module.h" -#include - -using json = nlohmann::json; - -namespace master::http { -bool HttpModule::Start() { - m_http_server_ = pm_->FindModule<::IHttpServerModule>(); - m_node_ = pm_->FindModule(); - return true; -} -bool HttpModule::Destroy() { return true; } - -bool HttpModule::AfterStart() { - // http://127.0.0.1/json - m_http_server_->AddRequestHandler("/node/list", HttpType::SQUICK_HTTP_REQ_GET, this, &HttpModule::OnGetNodeList); - - m_http_server_->StartServer(pm_->GetArg("http_port=", ARG_DEFAULT_HTTP_PORT)); - return true; -} - -bool HttpModule::Update() { - m_http_server_->Update(); - return true; -} - -bool HttpModule::OnGetNodeList(std::shared_ptr req) { - - auto &nodes = m_node_->GetAllNodes(); - - json statusRoot; - - statusRoot["code"] = 0; - statusRoot["msg"] = ""; - statusRoot["time"] = pm_->GetNowTime(); - for (auto &s : nodes) { - auto &sd = s.second.info; - json n; - n["area"] = sd->area(); - n["type"] = sd->type(); - n["id"] = sd->id(); - n["name"] = sd->name().c_str(); - n["ip"] = sd->ip().c_str(); - n["public_ip"] = sd->public_ip().c_str(); - n["port"] = sd->port(); - n["cpu_count"] = sd->cpu_count(); - n["status"] = sd->state(); - n["workload"] = sd->workload(); - n["max_online"] = sd->max_online(); - n["update_time"] = sd->update_time(); - n["ws_port"] = sd->ws_port(); - n["http_port"] = sd->http_port(); - n["https_port"] = sd->https_port(); - - statusRoot["node_list"][to_string(sd->id())] = n; - } - return m_http_server_->ResponseMsg(req, statusRoot.dump(), WebStatus::WEB_OK); -} - -} // namespace master::http \ No newline at end of file diff --git a/src/node/master/http/http_module.h b/src/node/master/http/http_module.h deleted file mode 100644 index 90cc02c5..00000000 --- a/src/node/master/http/http_module.h +++ /dev/null @@ -1,37 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -#include "i_http_module.h" -#include - -namespace master::http { -class HttpModule : public IHttpModule { - public: - HttpModule(IPluginManager *p) { - is_update_ = true; - pm_ = p; - } - - virtual bool Start(); - virtual bool Destroy(); - - virtual bool AfterStart(); - virtual bool Update(); - - protected: - bool OnGetNodeList(std::shared_ptr req); - - WebStatus OnFilter(std::shared_ptr req); - - private: - ::IHttpServerModule *m_http_server_; - node::INodeModule *m_node_; - IClassModule *m_class_; - IElementModule *m_element_; -}; - -} // namespace master::http \ No newline at end of file diff --git a/src/node/master/http/i_http_module.h b/src/node/master/http/i_http_module.h deleted file mode 100644 index 85f2692d..00000000 --- a/src/node/master/http/i_http_module.h +++ /dev/null @@ -1,11 +0,0 @@ -#pragma once - -#include -#include -namespace master::http { -class IHttpModule : public IModule { - - public: -}; - -} // namespace master::http diff --git a/src/node/master/http/plugin.h b/src/node/master/http/plugin.h deleted file mode 100644 index 750da610..00000000 --- a/src/node/master/http/plugin.h +++ /dev/null @@ -1,14 +0,0 @@ -#pragma once -#include -#include - -namespace master::http { -class Plugin : public IPlugin { - public: - Plugin(IPluginManager *p) { pm_ = p; } - virtual const int GetPluginVersion(); - virtual const std::string GetPluginName(); - virtual void Install(); - virtual void Uninstall(); -}; -} // namespace master::http \ No newline at end of file diff --git a/src/node/master/logic/logic_module.cc b/src/node/master/logic/logic_module.cc index 3252cc6d..56806161 100644 --- a/src/node/master/logic/logic_module.cc +++ b/src/node/master/logic/logic_module.cc @@ -1,9 +1,285 @@ #include "logic_module.h" #include "plugin.h" +#include +#define DEFAULT_MASTER_UPDATE_SELF_TIME 10 namespace master::logic { -bool LogicModule::Start() { return true; } +using json = nlohmann::json; +bool LogicModule::Start() { + m_net_ = pm_->FindModule(); + m_log_ = pm_->FindModule(); + m_node_ = pm_->FindModule(); + return true; +} + +bool LogicModule::AfterStart() { + m_net_->AddReceiveCallBack(rpc::IdNReqNodeRegister, this, &LogicModule::OnNReqNodeRegister); + m_net_->AddReceiveCallBack(rpc::IdNReqNodeUnregister, this, &LogicModule::OnNReqNodeUnregistered); + m_net_->AddReceiveCallBack(rpc::IdNNtfNodeReport, this, &LogicModule::OnNNtfNodeReport); + m_net_->AddReceiveCallBack(rpc::IdNReqMinWorkloadNodeInfo, this, &LogicModule::OnNReqMinWorkNodeInfo); + m_net_->AddReceiveCallBack(rpc::IdNNtfNodeMsgForward, this, &LogicModule::OnNNtfNodeMsgForward); + m_net_->AddReceiveCallBack(rpc::IdNReqAllNodesInfo, this, &LogicModule::OnNReqAllNodesInfo); + + m_http_server_ = pm_->FindModule<::IHttpServerModule>(); + + m_http_server_->AddRequestHandler("/node/list", HttpType::SQUICK_HTTP_REQ_GET, this, &LogicModule::OnGetNodeList); + m_http_server_->StartServer(pm_->GetArg("http_port=", ARG_DEFAULT_HTTP_PORT)); + + UpdateStatus(); + return true; +} bool LogicModule::Destroy() { return true; } -bool LogicModule::Update() { return true; } +bool LogicModule::Update() { + + if (last_report_time_ + DEFAULT_MASTER_UPDATE_SELF_TIME > pm_->GetNowTime()) { + return true; + } + if (last_report_time_ > 0) { + UpdateStatus(); + } + last_report_time_ = pm_->GetNowTime(); + + return true; +} + +void LogicModule::UpdateStatus() { node_map_[pm_->GetAppID()] = m_node_->GetNodeInfo(); } + +void LogicModule::OnNReqNodeRegister(const socket_t sock, const int msg_id, const char *msg, const uint32_t len) { + uint64_t uid; + rpc::NReqNodeRegister req; + if (!m_net_->ReceivePB(msg_id, msg, len, req, uid)) { + return; + } + rpc::NAckNodeRegister ack; + int new_node_id = 0; + ack.set_code(1); + do { + auto &new_node = req.node(); + new_node_id = new_node.id(); + if (new_node_id == pm_->GetAppID() || new_node_id == 0) { + break; + } + ServerInfo info; + info.fd = sock; + info.status = ServerInfo::Status::Connected; + *info.info = new_node; + node_map_[new_node_id] = info; + + vector types; + std::string log; + for (auto t : req.listen_type_list()) { + types.push_back(t); + log += ", " + to_string(t); + } + LOG_INFO("Register node<%v>, subscribe node type:[ %v ]", new_node_id, log); + AddSubscribeNode(new_node_id, types); + + // pack current subscribe nodes to new nodes + for (auto s : node_map_) { + auto s_info = s.second.info; + // except self + if (s_info->id() == new_node_id) + continue; + + // except no subscribe type + if (!IsHaveThisType(types, s_info->type())) + continue; + + auto add_node = ack.add_node_add_list(); + *add_node = *s_info; + } + ack.set_code(0); + } while (false); + + m_net_->SendPBToNode(rpc::IdNAckNodeRegister, ack, sock); + + NtfSubscribNode(new_node_id); +} + +void LogicModule::AddSubscribeNode(int new_node_id, vector types) { + for (auto t : types) { + if (t != 0) { + nodes_subscribe_[t].insert(new_node_id); + } else { + LOG_ERROR("AddSubscribeNode error type<%v> ", t); + } + } +} + +void LogicModule::NtfSubscribNode(int new_node_id) { + if (new_node_id == 0) + return; + auto iter = node_map_.find(new_node_id); + if (iter == node_map_.end()) { + LOG_ERROR("NtfSubscribNode error id<%v>", new_node_id); + return; + } + + auto new_node = iter->second.info; + int new_node_type = new_node->type(); + for (auto ns : nodes_subscribe_) { + int type = ns.first; + if (type != new_node_type) + continue; + for (auto sub_id : ns.second) { + if (sub_id == new_node_id) + continue; + rpc::NNtfNodeAdd ntf; + auto p = ntf.add_node_list(); + *p = *new_node; + LOG_INFO("Ntf sub_id<%v> to add new_node<%v>", sub_id, new_node_id); + SendPBByID(sub_id, rpc::IdNNtfNodeAdd, ntf); + } + } +} + +bool LogicModule::SendPBByID(const int node_id, const uint16_t msg_id, const google::protobuf::Message &pb) { + + auto iter = node_map_.find(node_id); + if (iter == node_map_.end()) { + LOG_ERROR("SendPBByID no this node id<%v>", node_id); + return false; + } + return m_net_->SendPBToNode(msg_id, pb, iter->second.fd); +} + +void LogicModule::OnNReqNodeUnregistered(const socket_t sock, const int msg_id, const char *msg, const uint32_t len) { + uint64_t uid; + rpc::NReqNodeUnregister req; + if (!m_net_->ReceivePB(msg_id, msg, len, req, uid)) { + return; + } + + int id = req.id(); + auto iter = node_map_.find(id); + if (iter != node_map_.end()) { + node_map_.erase(iter); + } + // m_log_->LogInfo(Guid(0, id, s.name(), " UnRegistered"); +} + +// Master +void LogicModule::OnNNtfNodeReport(const socket_t sock, const int msg_id, const char *msg, const uint32_t len) { + + uint64_t uid; + rpc::NNtfNodeReport ntf; + if (!INetModule::ReceivePB(msg_id, msg, len, ntf, uid)) { + return; + } + + do { + for (auto s : ntf.list()) { + if (s.id() == pm_->GetAppID() || s.id() == 0) { + continue; + } + auto iter = node_map_.find(s.id()); + if (iter != node_map_.end()) { + if (iter->second.info->update_time() >= s.update_time()) { + continue; + } + *iter->second.info = s; + continue; + } + + // New + ServerInfo info; + info.fd = 0; + info.status = ServerInfo::Status::Unknowing; + *info.info = s; + node_map_[s.id()] = info; + } + } while (false); +} + +void LogicModule::OnNReqMinWorkNodeInfo(const socket_t sock, const int msg_id, const char *msg, const uint32_t len) { + rpc::NReqMinWorkloadNodeInfo req; + rpc::NAckMinWorkloadNodeInfo ack; + string guid; + + rpc::MsgBase msg_base; + if (!msg_base.ParseFromArray(msg, len)) { + return; + } + if (!req.ParseFromString(msg_base.msg_data())) { + return; + } + for (auto type : req.type_list()) { + int id = GetLoadBanlanceNode(type); + if (id == -1) + continue; + auto p = ack.add_list(); + auto iter = node_map_.find(id); + *p = *iter->second.info; + } + reqid_t req_id = msg_base.req_id(); + m_net_->SendPBToNode(rpc::IdNAckMinWorkloadNodeInfo, ack, sock, 0, req_id); +} + +int LogicModule::GetLoadBanlanceNode(int type) { + int node_id = -1; + int min_workload = 0x7fffffff; + for (auto &iter : node_map_) { + auto server = iter.second; + if (server.info->type() == type) { + if (min_workload > server.info->workload()) { + node_id = iter.first; + } + } + } + if (node_id == -1) { + return -1; + } + return node_id; +} + +void LogicModule::OnNNtfNodeMsgForward(const socket_t sock, const int msg_id, const char *msg, const uint32_t len) {} + +void LogicModule::OnNReqAllNodesInfo(const socket_t sock, const int msg_id, const char *msg, const uint32_t len) { + rpc::MsgBase msg_base; + if (!msg_base.ParseFromArray(msg, len)) { + LOG_ERROR("ParseFromArray: is error, the msg len %v", len); + return; + } + reqid_t req_id = msg_base.req_id(); + rpc::NAckAllNodesInfo ack; + for (auto &node : node_map_) { + auto node_info = ack.add_node_list(); + *node_info = *node.second.info; + } + m_net_->SendPBToNode(rpc::IdNAckMinWorkloadNodeInfo, ack, sock, 0, req_id); +} + +bool LogicModule::OnGetNodeList(std::shared_ptr req) { + + json statusRoot; + statusRoot["code"] = 0; + statusRoot["msg"] = ""; + statusRoot["time"] = pm_->GetNowTime(); + for (auto &s : node_map_) { + auto &sd = s.second.info; + json n; + n["area"] = sd->area(); + n["type"] = sd->type(); + n["id"] = sd->id(); + n["name"] = sd->name().c_str(); + n["ip"] = sd->ip().c_str(); + n["public_ip"] = sd->public_ip().c_str(); + n["port"] = sd->port(); + n["cpu_count"] = sd->cpu_count(); + n["status"] = sd->state(); + n["workload"] = sd->workload(); + n["max_online"] = sd->max_online(); + n["update_time"] = sd->update_time(); + n["ws_port"] = sd->ws_port(); + n["http_port"] = sd->http_port(); + n["https_port"] = sd->https_port(); + n["connections"] = sd->connections(); + n["net_client_connections"] = sd->net_client_connections(); + + statusRoot["node_list"][to_string(sd->id())] = n; + } + return m_http_server_->ResponseMsg(req, statusRoot.dump(), WebStatus::WEB_OK); +} + } // namespace master::logic diff --git a/src/node/master/logic/logic_module.h b/src/node/master/logic/logic_module.h index 05bad871..923946e4 100644 --- a/src/node/master/logic/logic_module.h +++ b/src/node/master/logic/logic_module.h @@ -1,18 +1,60 @@ #pragma once #include "i_logic_module.h" +#include +#include +#include +#include namespace master::logic { class LogicModule : public ILogicModule { public: - LogicModule(IPluginManager *p) { pm_ = p; } + LogicModule(IPluginManager *p) { + pm_ = p; + is_update_ = true; + } - virtual bool Start(); - virtual bool Destroy(); - virtual bool Update(); + virtual bool Start() override; + virtual bool AfterStart() override; + virtual bool Destroy() override; + virtual bool Update() override; + + void OnNNtfNodeReport(const socket_t sock, const int msg_id, const char *msg, const uint32_t len); + void OnNReqNodeRegister(const socket_t sock, const int msg_id, const char *msg, const uint32_t len); + void OnNReqNodeUnregistered(const socket_t sock, const int msg_id, const char *msg, const uint32_t len); + void OnNReqMinWorkNodeInfo(const socket_t sock, const int msg_id, const char *msg, const uint32_t len); + void OnNNtfNodeMsgForward(const socket_t sock, const int msg_id, const char *msg, const uint32_t len); + void OnNReqAllNodesInfo(const socket_t sock, const int msg_id, const char *msg, const uint32_t len); + + int GetLoadBanlanceNode(int type); + + void AddSubscribeNode(int new_node_id, vector types); + void NtfSubscribNode(int new_node_id); + + bool SendPBByID(const int node_id, const uint16_t msg_id, const google::protobuf::Message &pb); + void UpdateStatus(); + + inline bool IsHaveThisType(const vector &types, int type) { + for (auto t : types) { + if (t == type) { + return true; + } + } + return false; + } + + bool OnGetNodeList(std::shared_ptr req); - protected: private: + // 服务表 + map node_map_; // ikey -> node id, value -> ServerInfo + std::map> nodes_subscribe_; // key -> node type, value -> node ids; + + INetModule *m_net_; + ILogModule *m_log_; + INodeModule *m_node_; + IHttpServerModule *m_http_server_; + time_t last_report_time_; }; } // namespace master::logic \ No newline at end of file diff --git a/src/node/master/node/CMakeLists.txt b/src/node/master/node/CMakeLists.txt deleted file mode 100644 index fb63eef9..00000000 --- a/src/node/master/node/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -set(ProjectName "master_node") -file(GLOB soruces *.cc) -file(GLOB headers *.h) -add_library(${ProjectName} SHARED ${soruces} ${headers}) -LinkSquick(${ProjectName}) -SquickOut(${ProjectName} "node/master" ${SQUICK_BUILD_PLUGIN_SERVER_PATH}/master "node") \ No newline at end of file diff --git a/src/node/master/node/i_node_module.h b/src/node/master/node/i_node_module.h deleted file mode 100644 index 2489d550..00000000 --- a/src/node/master/node/i_node_module.h +++ /dev/null @@ -1,10 +0,0 @@ -#pragma once -#include - -namespace master::node { -class INodeModule : public INodeBaseModule { - public: - virtual map &GetAllNodes() = 0; -}; - -} // namespace master::node \ No newline at end of file diff --git a/src/node/master/node/node_module.cc b/src/node/master/node/node_module.cc deleted file mode 100644 index 45c8844a..00000000 --- a/src/node/master/node/node_module.cc +++ /dev/null @@ -1,214 +0,0 @@ - -#include "node_module.h" -#include "plugin.h" - -namespace master::node { -NodeModule::~NodeModule() {} - -bool NodeModule::Destroy() { return true; } - -bool NodeModule::AfterStart() { - - m_net_->AddReceiveCallBack(rpc::IdNReqNodeRegister, this, &NodeModule::OnNReqNodeRegister); - m_net_->AddReceiveCallBack(rpc::IdNReqNodeUnregister, this, &NodeModule::OnNReqNodeUnregistered); - m_net_->AddReceiveCallBack(rpc::IdNNtfNodeReport, this, &NodeModule::OnNNtfNodeReport); - m_net_->AddReceiveCallBack(rpc::IdNReqMinWorkloadNodeInfo, this, &NodeModule::OnNReqMinWorkNodeInfo); - m_net_->AddReceiveCallBack(rpc::IdNNtfNodeMsgForward, this, &NodeModule::OnNNtfNodeMsgForward); - Listen(); - - return true; -} - -void NodeModule::OnNReqNodeRegister(const socket_t sock, const int msg_id, const char *msg, const uint32_t len) { - uint64_t uid; - rpc::NReqNodeRegister req; - if (!m_net_->ReceivePB(msg_id, msg, len, req, uid)) { - return; - } - rpc::NAckNodeRegister ack; - int new_node_id = 0; - ack.set_code(1); - do { - auto &new_node = req.node(); - new_node_id = new_node.id(); - if (new_node_id == pm_->GetAppID() || new_node_id == 0) { - break; - } - ServerInfo info; - info.fd = sock; - info.status = ServerInfo::Status::Connected; - *info.info = new_node; - node_map_[new_node_id] = info; - - vector types; - std::string log; - for (auto t : req.listen_type_list()) { - types.push_back(t); - log += ", " + to_string(t); - } - LOG_INFO("Register node<%v>, subscribe node type:[ %v ]", new_node_id, log); - AddSubscribeNode(new_node_id, types); - - // pack current subscribe nodes to new nodes - for (auto s : node_map_) { - auto s_info = s.second.info; - // except self - if (s_info->id() == new_node_id) - continue; - - // except no subscribe type - if (!IsHaveThisType(types, s_info->type())) - continue; - - auto add_node = ack.add_node_add_list(); - *add_node = *s_info; - } - ack.set_code(0); - } while (false); - - m_net_->SendPBToNode(rpc::IdNAckNodeRegister, ack, sock); - - NtfSubscribNode(new_node_id); -} - -void NodeModule::AddSubscribeNode(int new_node_id, vector types) { - for (auto t : types) { - if (t != 0) { - nodes_subscribe_[t].insert(new_node_id); - } else { - LOG_ERROR("AddSubscribeNode error type<%v> ", t); - } - } -} - -void NodeModule::NtfSubscribNode(int new_node_id) { - if (new_node_id == 0) - return; - auto iter = node_map_.find(new_node_id); - if (iter == node_map_.end()) { - LOG_ERROR("NtfSubscribNode error id<%v>", new_node_id); - return; - } - - auto new_node = iter->second.info; - int new_node_type = new_node->type(); - for (auto ns : nodes_subscribe_) { - int type = ns.first; - if (type != new_node_type) - continue; - for (auto sub_id : ns.second) { - if (sub_id == new_node_id) - continue; - rpc::NNtfNodeAdd ntf; - auto p = ntf.add_node_list(); - *p = *new_node; - LOG_INFO("Ntf sub_id<%v> to add new_node<%v>", sub_id, new_node_id); - SendPBByID(sub_id, rpc::IdNNtfNodeAdd, ntf); - } - } -} - -bool NodeModule::SendPBByID(const int node_id, const uint16_t msg_id, const google::protobuf::Message &pb) { - - auto iter = node_map_.find(node_id); - if (iter == node_map_.end()) { - LOG_ERROR("SendPBByID no this node id<%v>", node_id); - return false; - } - return m_net_->SendPBToNode(msg_id, pb, iter->second.fd); -} - -void NodeModule::OnNReqNodeUnregistered(const socket_t sock, const int msg_id, const char *msg, const uint32_t len) { - uint64_t uid; - rpc::NReqNodeUnregister req; - if (!m_net_->ReceivePB(msg_id, msg, len, req, uid)) { - return; - } - - int id = req.id(); - auto iter = node_map_.find(id); - if (iter != node_map_.end()) { - node_map_.erase(iter); - } - // m_log_->LogInfo(Guid(0, id, s.name(), " UnRegistered"); -} - -// Master -void NodeModule::OnNNtfNodeReport(const socket_t sock, const int msg_id, const char *msg, const uint32_t len) { - - uint64_t uid; - rpc::NNtfNodeReport ntf; - if (!INetModule::ReceivePB(msg_id, msg, len, ntf, uid)) { - return; - } - - do { - for (auto s : ntf.list()) { - if (s.id() == pm_->GetAppID() || s.id() == 0) { - continue; - } - auto iter = node_map_.find(s.id()); - if (iter != node_map_.end()) { - if (iter->second.info->update_time() >= s.update_time()) { - continue; - } - *iter->second.info = s; - continue; - } - - // New - ServerInfo info; - info.fd = 0; - info.status = ServerInfo::Status::Unknowing; - *info.info = s; - node_map_[s.id()] = info; - } - } while (false); -} - -void NodeModule::OnNReqMinWorkNodeInfo(const socket_t sock, const int msg_id, const char *msg, const uint32_t len) { - rpc::NReqMinWorkloadNodeInfo req; - rpc::NAckMinWorkloadNodeInfo ack; - string guid; - - rpc::MsgBase msg_base; - if (!msg_base.ParseFromArray(msg, len)) { - return; - } - if (!req.ParseFromString(msg_base.msg_data())) { - return; - } - for (auto type : req.type_list()) { - int id = GetLoadBanlanceNode((ServerType)type); - if (id == -1) - continue; - auto p = ack.add_list(); - auto iter = node_map_.find(id); - *p = *iter->second.info; - } - reqid_t req_id = msg_base.req_id(); - m_net_->SendPBToNode(rpc::IdNAckMinWorkloadNodeInfo, ack, sock, 0, req_id); -} - -void NodeModule::OnNNtfNodeMsgForward(const socket_t sock, const int msg_id, const char *msg, const uint32_t len) {} - -int NodeModule::GetLoadBanlanceNode(ServerType type) { - int node_id = -1; - int min_workload = 99999; - for (auto &iter : node_map_) { - auto server = iter.second; - if (server.info->type() == type && server.info->area() == pm_->GetArea()) { - if (min_workload > server.info->workload()) { - node_id = iter.first; - } - } - } - if (node_id == -1) { - return -1; - } - return node_id; -} - -map &NodeModule::GetAllNodes() { return node_map_; } - -} // namespace master::node diff --git a/src/node/master/node/node_module.h b/src/node/master/node/node_module.h deleted file mode 100644 index be7c374d..00000000 --- a/src/node/master/node/node_module.h +++ /dev/null @@ -1,44 +0,0 @@ -#pragma once -#include "i_node_module.h" -#include - -namespace master::node { -class NodeModule : public INodeModule { - public: - NodeModule(IPluginManager *p) { pm_ = p; } - virtual ~NodeModule(); - - virtual bool Destroy(); - virtual bool AfterStart(); - - map &GetAllNodes(); - - void OnNNtfNodeReport(const socket_t sock, const int msg_id, const char *msg, const uint32_t len); - void OnNReqNodeRegister(const socket_t sock, const int msg_id, const char *msg, const uint32_t len); - void OnNReqNodeUnregistered(const socket_t sock, const int msg_id, const char *msg, const uint32_t len); - void OnNReqMinWorkNodeInfo(const socket_t sock, const int msg_id, const char *msg, const uint32_t len); - void OnNNtfNodeMsgForward(const socket_t sock, const int msg_id, const char *msg, const uint32_t len); - - int GetLoadBanlanceNode(ServerType type); - - void AddSubscribeNode(int new_node_id, vector types); - void NtfSubscribNode(int new_node_id); - - bool SendPBByID(const int node_id, const uint16_t msg_id, const google::protobuf::Message &pb); - - inline bool IsHaveThisType(const vector &types, int type) { - for (auto t : types) { - if (t == type) { - return true; - } - } - return false; - } - - private: - // 服务表 - map node_map_; // ikey -> node id, value -> ServerInfo - std::map> nodes_subscribe_; // key -> node type, value -> node ids; -}; - -} // namespace master::node \ No newline at end of file diff --git a/src/node/master/node/plugin.h b/src/node/master/node/plugin.h deleted file mode 100644 index 438e2b9f..00000000 --- a/src/node/master/node/plugin.h +++ /dev/null @@ -1,14 +0,0 @@ -#pragma once - -#include -#include -namespace master::node { -class Plugin : public IPlugin { - public: - Plugin(IPluginManager *p) { pm_ = p; } - virtual const int GetPluginVersion(); - virtual const std::string GetPluginName(); - virtual void Install(); - virtual void Uninstall(); -}; -} // namespace master::node \ No newline at end of file diff --git a/src/node/match/CMakeLists.txt b/src/node/match/CMakeLists.txt index 83f38ba6..8b137891 100644 --- a/src/node/match/CMakeLists.txt +++ b/src/node/match/CMakeLists.txt @@ -1 +1 @@ -add_subdirectory(node) + diff --git a/src/node/match/node/CMakeLists.txt b/src/node/match/node/CMakeLists.txt deleted file mode 100644 index 0f1b0044..00000000 --- a/src/node/match/node/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -set(ProjectName "match_node") -file(GLOB soruces *.cc) -file(GLOB headers *.h) -add_library(${ProjectName} SHARED ${soruces} ${headers}) -LinkSquick(${ProjectName}) -SquickOut(${ProjectName} "node/match" ${SQUICK_BUILD_PLUGIN_SERVER_PATH}/match "node") diff --git a/src/node/match/node/i_node_module.h b/src/node/match/node/i_node_module.h deleted file mode 100644 index 06030e2a..00000000 --- a/src/node/match/node/i_node_module.h +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once - -#include -namespace match::node { -class INodeModule : public INodeBaseModule { - public: -}; - -} // namespace match::node diff --git a/src/node/match/node/node_module.cc b/src/node/match/node/node_module.cc deleted file mode 100644 index 6edb8700..00000000 --- a/src/node/match/node/node_module.cc +++ /dev/null @@ -1,15 +0,0 @@ -#include "node_module.h" -#include - -namespace match::node { -bool NodeModule::AfterStart() { - Listen(); - - vector node_types = {ServerType::ST_GLOBAL, ServerType::ST_DB_PROXY}; - AddNodesByType(node_types); - return true; -} - -bool NodeModule::Destroy() { return true; } - -} // namespace match::node diff --git a/src/node/match/node/node_module.h b/src/node/match/node/node_module.h deleted file mode 100644 index 15e2a480..00000000 --- a/src/node/match/node/node_module.h +++ /dev/null @@ -1,19 +0,0 @@ -#pragma once - -#include "i_node_module.h" - -namespace match::node { -class NodeModule : public INodeModule { - public: - NodeModule(IPluginManager *p) { - pm_ = p; - is_update_ = true; - } - virtual bool Destroy(); - virtual bool AfterStart(); - - protected: - private: -}; - -} // namespace match::node diff --git a/src/node/match/node/plugin.cc b/src/node/match/node/plugin.cc deleted file mode 100644 index 3ef6f96f..00000000 --- a/src/node/match/node/plugin.cc +++ /dev/null @@ -1,17 +0,0 @@ -#include "plugin.h" -#include "node_module.h" - -namespace match::node { -SQUICK_EXPORT void SquickPluginLoad(IPluginManager *pm){CREATE_PLUGIN(pm, Plugin)}; - -SQUICK_EXPORT void SquickPluginUnload(IPluginManager *pm){DESTROY_PLUGIN(pm, Plugin)}; - -const int Plugin::GetPluginVersion() { return 0; } - -const std::string Plugin::GetPluginName() { return GET_CLASS_NAME(Plugin); } - -void Plugin::Install() { REGISTER_MODULE(pm_, INodeModule, NodeModule) } - -void Plugin::Uninstall() { UNREGISTER_MODULE(pm_, INodeModule, NodeModule) } - -} // namespace match::node diff --git a/src/node/player/CMakeLists.txt b/src/node/player/CMakeLists.txt index 474c2bb1..77f77563 100644 --- a/src/node/player/CMakeLists.txt +++ b/src/node/player/CMakeLists.txt @@ -1,2 +1 @@ -add_subdirectory(node) -add_subdirectory(lua) \ No newline at end of file +add_subdirectory(logic) \ No newline at end of file diff --git a/src/node/player/lua/CMakeLists.txt b/src/node/player/logic/CMakeLists.txt similarity index 85% rename from src/node/player/lua/CMakeLists.txt rename to src/node/player/logic/CMakeLists.txt index feda8fb4..8b04e9e7 100644 --- a/src/node/player/lua/CMakeLists.txt +++ b/src/node/player/logic/CMakeLists.txt @@ -1,4 +1,4 @@ -set(ProjectName "player_lua") +set(ProjectName "player_logic") file(GLOB soruces *.cc) file(GLOB headers *.h) add_library(${ProjectName} SHARED ${soruces} ${headers}) @@ -12,4 +12,4 @@ else() endif() LinkSquick(${ProjectName}) -SquickOut(${ProjectName} "node/player" ${SQUICK_BUILD_PLUGIN_SERVER_PATH}/player "lua") \ No newline at end of file +SquickOut(${ProjectName} "node/player" ${SQUICK_BUILD_PLUGIN_SERVER_PATH}/player "logic") \ No newline at end of file diff --git a/src/node/player/lua/README.md b/src/node/player/logic/README.md similarity index 100% rename from src/node/player/lua/README.md rename to src/node/player/logic/README.md diff --git a/src/node/player/lua/lua_bind_module.cc b/src/node/player/logic/logic_module.cc similarity index 54% rename from src/node/player/lua/lua_bind_module.cc rename to src/node/player/logic/logic_module.cc index cb6ce425..8fb88b13 100644 --- a/src/node/player/lua/lua_bind_module.cc +++ b/src/node/player/logic/logic_module.cc @@ -1,31 +1,33 @@ -#include "lua_bind_module.h" +#include "logic_module.h" #include // #include -namespace player::lua { -bool LuaBindModule::Start() { +namespace player::logic { +bool LogicModule::Start() { m_lua_script_ = pm_->FindModule(); - // m_player_manager_ = pm_->FindModule(); - // m_node_ = pm_->FindModule(); - Bind(); + m_node_ = pm_->FindModule(); + vector node_types = {rpc::ST_WORLD, rpc::ST_DB_PROXY, rpc::ST_PLAYER}; + m_node_->AddSubscribeNode(node_types); + + LuaBind(); return true; } -bool LuaBindModule::Destroy() { return true; } +bool LogicModule::Destroy() { return true; } -bool LuaBindModule::AfterStart() { return true; } +bool LogicModule::AfterStart() { return true; } -bool LuaBindModule::Update() { return true; } +bool LogicModule::Update() { return true; } -bool LuaBindModule::Bind() { +bool LogicModule::LuaBind() { LuaScriptModule *luaModule = dynamic_cast(m_lua_script_); LuaIntf::LuaContext &luaEnv = luaModule->GetLuaEnv(); // SendMsgToGate(const uint16_t msg_id, const std::string& msg, const Guid& self) LuaIntf::LuaBinding(luaEnv) - .beginClass("GameServer") - .addFunction("send_to_player", &LuaBindModule::SendToPlayer) - .addFunction("test", &LuaBindModule::Test) + .beginClass("GameServer") + .addFunction("send_to_player", &LogicModule::SendToPlayer) + .addFunction("test", &LogicModule::Test) .endClass(); // 将该模块传递到lua层 try { @@ -42,13 +44,11 @@ bool LuaBindModule::Bind() { // 所以在该动态链接库的函数参数 类型,顺序需要符合 core/lua 中已定义的函数参数顺序 // const Guid player, const uint16_t msg_id, const std::string& data // 由于是跨dll进行解析,无法对已在core/lua上的Guid进行解析,所以只能传普通类型的数据。 -void LuaBindModule::SendToPlayer(string &player_guid_str, uint16_t msg_id, std::string &data) { - // std::cout << "this: " << this << " " << m_node_ << " LuaBindModule::SendToPlayer " << msg_id << " msg: " << data << std::endl; - // std::cout << " \n length: " << data.length() << std::endl; +void LogicModule::SendToPlayer(string &player_guid_str, uint16_t msg_id, std::string &data) { Guid guid = Guid(player_guid_str); // m_node_->SendToPlayer(msg_id, data, guid); } -void LuaBindModule::Test(const uint16_t msg_id, string &msg, int a) { std::cout << "LuaBindModule::Test\n" << msg_id << " " << msg << a << std::endl; } +void LogicModule::Test(const uint16_t msg_id, string &msg, int a) { std::cout << "LuaBindModule::Test\n" << msg_id << " " << msg << a << std::endl; } -} // namespace player::lua \ No newline at end of file +} // namespace player::logic \ No newline at end of file diff --git a/src/node/player/lua/lua_bind_module.h b/src/node/player/logic/logic_module.h similarity index 68% rename from src/node/player/lua/lua_bind_module.h rename to src/node/player/logic/logic_module.h index 1326dd58..5610d521 100644 --- a/src/node/player/lua/lua_bind_module.h +++ b/src/node/player/logic/logic_module.h @@ -7,14 +7,15 @@ #include #include #include +#include #include -namespace player::lua { -class ILuaBindModule : public IModule {}; +namespace player::logic { +class ILogicModule : public IModule {}; -class LuaBindModule : public ILuaBindModule { +class LogicModule : public ILogicModule { public: - LuaBindModule(IPluginManager *p) { + LogicModule(IPluginManager *p) { pm_ = p; is_update_ = false; } @@ -26,15 +27,14 @@ class LuaBindModule : public ILuaBindModule { virtual bool Update(); protected: - bool Bind(); + bool LuaBind(); void SendToPlayer(string &player_guid_str, uint16_t msg_id, std::string &data); void Test(const uint16_t msg_id, string &msg, int a); private: ILuaScriptModule *m_lua_script_; - // player::IPlayerManagerModule *m_player_manager_; - // node::INodeModule *m_node_; + INodeModule *m_node_; }; -} // namespace player::lua +} // namespace player::logic diff --git a/src/node/master/http/plugin.cc b/src/node/player/logic/plugin.cc similarity index 58% rename from src/node/master/http/plugin.cc rename to src/node/player/logic/plugin.cc index 09a02fd8..08bfe47a 100644 --- a/src/node/master/http/plugin.cc +++ b/src/node/player/logic/plugin.cc @@ -1,9 +1,8 @@ - #include "plugin.h" -#include "http_module.h" -// -namespace master::http { +#include "logic_module.h" + +namespace player::logic { SQUICK_EXPORT void SquickPluginLoad(IPluginManager *pm){CREATE_PLUGIN(pm, Plugin)}; SQUICK_EXPORT void SquickPluginUnload(IPluginManager *pm){DESTROY_PLUGIN(pm, Plugin)}; @@ -12,8 +11,8 @@ const int Plugin::GetPluginVersion() { return 0; } const std::string Plugin::GetPluginName() { return GET_CLASS_NAME(Plugin); } -void Plugin::Install() { REGISTER_MODULE(pm_, IHttpModule, HttpModule) } +void Plugin::Install() { REGISTER_MODULE(pm_, ILogicModule, LogicModule) } -void Plugin::Uninstall() { UNREGISTER_MODULE(pm_, IHttpModule, HttpModule) } +void Plugin::Uninstall() { UNREGISTER_MODULE(pm_, ILogicModule, LogicModule) } -} // namespace master::http \ No newline at end of file +} // namespace player::logic \ No newline at end of file diff --git a/src/node/match/node/plugin.h b/src/node/player/logic/plugin.h similarity index 85% rename from src/node/match/node/plugin.h rename to src/node/player/logic/plugin.h index 0844cb06..be0797f4 100644 --- a/src/node/match/node/plugin.h +++ b/src/node/player/logic/plugin.h @@ -3,7 +3,7 @@ #include #include -namespace match::node { +namespace player::logic { class Plugin : public IPlugin { public: Plugin(IPluginManager *p) { pm_ = p; } @@ -13,4 +13,4 @@ class Plugin : public IPlugin { virtual void Uninstall(); }; -} // namespace match::node +} // namespace player::logic \ No newline at end of file diff --git a/src/node/player/lua/plugin.cc b/src/node/player/lua/plugin.cc deleted file mode 100644 index f4a33c7c..00000000 --- a/src/node/player/lua/plugin.cc +++ /dev/null @@ -1,18 +0,0 @@ - -#include "plugin.h" -#include "lua_bind_module.h" - -namespace player::lua { -SQUICK_EXPORT void SquickPluginLoad(IPluginManager *pm){CREATE_PLUGIN(pm, Plugin)}; - -SQUICK_EXPORT void SquickPluginUnload(IPluginManager *pm){DESTROY_PLUGIN(pm, Plugin)}; - -const int Plugin::GetPluginVersion() { return 0; } - -const std::string Plugin::GetPluginName() { return GET_CLASS_NAME(Plugin); } - -void Plugin::Install() { REGISTER_MODULE(pm_, ILuaBindModule, LuaBindModule) } - -void Plugin::Uninstall() { UNREGISTER_MODULE(pm_, ILuaBindModule, LuaBindModule) } - -} // namespace player::lua \ No newline at end of file diff --git a/src/node/player/lua/plugin.h b/src/node/player/lua/plugin.h deleted file mode 100644 index 0db6c8fe..00000000 --- a/src/node/player/lua/plugin.h +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once - -#include -#include - -namespace player::lua { -class Plugin : public IPlugin { - public: - Plugin(IPluginManager *p) { pm_ = p; } - virtual const int GetPluginVersion(); - virtual const std::string GetPluginName(); - virtual void Install(); - virtual void Uninstall(); -}; - -} // namespace player::lua \ No newline at end of file diff --git a/src/node/player/node/CMakeLists.txt b/src/node/player/node/CMakeLists.txt deleted file mode 100644 index f8804463..00000000 --- a/src/node/player/node/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -set(ProjectName "player_node") -file(GLOB soruces *.cc) -file(GLOB headers *.h) -add_library(${ProjectName} SHARED ${soruces} ${headers}) -LinkSquick(${ProjectName}) -SquickOut(${ProjectName} "node/player" ${SQUICK_BUILD_PLUGIN_SERVER_PATH}/player "node") \ No newline at end of file diff --git a/src/node/player/node/i_node_module.h b/src/node/player/node/i_node_module.h deleted file mode 100644 index 68a52db0..00000000 --- a/src/node/player/node/i_node_module.h +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once - -#include -namespace player::node { -class INodeModule : public INodeBaseModule { - public: -}; - -} // namespace player::node \ No newline at end of file diff --git a/src/node/player/node/node_module.cc b/src/node/player/node/node_module.cc deleted file mode 100644 index e1821885..00000000 --- a/src/node/player/node/node_module.cc +++ /dev/null @@ -1,15 +0,0 @@ -#include "node_module.h" -#include - -namespace player::node { -bool NodeModule::AfterStart() { - Listen(); - - vector node_types = {ServerType::ST_WORLD, ServerType::ST_DB_PROXY, ServerType::ST_PLAYER}; - AddNodesByType(node_types); - return true; -} - -bool NodeModule::Destroy() { return true; } - -} // namespace player::node \ No newline at end of file diff --git a/src/node/player/node/node_module.h b/src/node/player/node/node_module.h deleted file mode 100644 index ae702b50..00000000 --- a/src/node/player/node/node_module.h +++ /dev/null @@ -1,19 +0,0 @@ -#pragma once - -#include "i_node_module.h" - -namespace player::node { -class NodeModule : public INodeModule { - public: - NodeModule(IPluginManager *p) { - pm_ = p; - is_update_ = true; - } - virtual bool Destroy(); - virtual bool AfterStart(); - - protected: - private: -}; - -} // namespace player::node \ No newline at end of file diff --git a/src/node/player/node/plugin.cc b/src/node/player/node/plugin.cc deleted file mode 100644 index 8197a972..00000000 --- a/src/node/player/node/plugin.cc +++ /dev/null @@ -1,20 +0,0 @@ -#include "plugin.h" -#include "node_module.h" -// -// -namespace player::node { -SQUICK_EXPORT void SquickPluginLoad(IPluginManager *pm){CREATE_PLUGIN(pm, Plugin)}; - -SQUICK_EXPORT void SquickPluginUnload(IPluginManager *pm){DESTROY_PLUGIN(pm, Plugin)}; - -////////////////////////////////////////////////////////////////////////// - -const int Plugin::GetPluginVersion() { return 0; } - -const std::string Plugin::GetPluginName() { return GET_CLASS_NAME(Plugin); } - -void Plugin::Install() { REGISTER_MODULE(pm_, INodeModule, NodeModule) } - -void Plugin::Uninstall() { UNREGISTER_MODULE(pm_, INodeModule, NodeModule) } - -} // namespace player::node \ No newline at end of file diff --git a/src/node/player/node/plugin.h b/src/node/player/node/plugin.h deleted file mode 100644 index 4331dfd7..00000000 --- a/src/node/player/node/plugin.h +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once - -#include -#include - -namespace player::node { -class Plugin : public IPlugin { - public: - Plugin(IPluginManager *p) { pm_ = p; } - virtual const int GetPluginVersion(); - virtual const std::string GetPluginName(); - virtual void Install(); - virtual void Uninstall(); -}; - -} // namespace player::node \ No newline at end of file diff --git a/src/node/proxy/CMakeLists.txt b/src/node/proxy/CMakeLists.txt index c3ebfb40..77f77563 100644 --- a/src/node/proxy/CMakeLists.txt +++ b/src/node/proxy/CMakeLists.txt @@ -1,2 +1 @@ -add_subdirectory(logic) -add_subdirectory(node) \ No newline at end of file +add_subdirectory(logic) \ No newline at end of file diff --git a/src/node/proxy/logic/logic_module.cc b/src/node/proxy/logic/logic_module.cc index c62c6ad3..ec5de2c6 100644 --- a/src/node/proxy/logic/logic_module.cc +++ b/src/node/proxy/logic/logic_module.cc @@ -28,7 +28,7 @@ bool LogicModule::AfterStart() { m_net_client_ = pm_->FindModule(); m_log_ = pm_->FindModule(); m_schedule_ = pm_->FindModule(); - m_node_ = pm_->FindModule(); + m_node_ = pm_->FindModule(); m_ws_ = pm_->FindModule(); m_net_->AddReceiveCallBack(this, &LogicModule::OnOtherMessage); @@ -44,21 +44,44 @@ bool LogicModule::AfterStart() { m_ws_->AddReceiveCallBack(rpc::IdReqPlayerLeave, this, &LogicModule::OnReqPlayerLeave); // Master - m_net_client_->AddReceiveCallBack(ServerType::ST_MASTER, this, &LogicModule::OnNAckMinWorkloadNodeInfo); + m_net_client_->AddReceiveCallBack(rpc::ST_MASTER, this, &LogicModule::OnNAckMinWorkloadNodeInfo); // Lobby - m_net_client_->AddReceiveCallBack(ServerType::ST_PLAYER, this, &LogicModule::OnRecivedPlayerNodeMsg); - m_net_client_->AddReceiveCallBack(ServerType::ST_LOGIN, rpc::IdNAckConnectProxyVerify, this, &LogicModule::OnNAckConnectVerify); - m_net_client_->AddReceiveCallBack(ServerType::ST_PLAYER, rpc::IdAckPlayerEnter, this, &LogicModule::OnAckPlayerEnter); + m_net_client_->AddReceiveCallBack(rpc::ST_PLAYER, this, &LogicModule::OnRecivedPlayerNodeMsg); + m_net_client_->AddReceiveCallBack(rpc::ST_LOGIN, rpc::IdNAckConnectProxyVerify, this, &LogicModule::OnNAckConnectVerify); + m_net_client_->AddReceiveCallBack(rpc::ST_PLAYER, rpc::IdAckPlayerEnter, this, &LogicModule::OnAckPlayerEnter); + + auto &node_info = m_node_->GetNodeInfo(); + node_info.info->set_ws_port(pm_->GetArg("ws_port=", 10502)); + m_ws_->Listen(pm_->GetArg("max_conn=", ARG_DEFAULT_MAX_CONNECTION), pm_->GetArg("ws_port=", ARG_DEFAULT_WS_PORT), + pm_->GetArg("cpu_count=", ARG_DEFAULT_CPU_COUNT), pm_->GetArg("net_server_buffer=", ARG_DEFAULT_NET_SERVER_BUFFER_SIZE)); + m_ws_->AddEventCallBack(this, &LogicModule::OnSocketEvent); + m_net_->AddEventCallBack(this, &LogicModule::OnSocketEvent); + + vector node_types = {rpc::ST_WORLD, rpc::ST_LOGIN, rpc::ST_PLAYER}; + m_node_->AddSubscribeNode(node_types); + return true; } +void LogicModule::OnSocketEvent(socket_t sock, const SQUICK_NET_EVENT eEvent, INet *pNet) { + if (eEvent & SQUICK_NET_EVENT_EOF) { + OnClientDisconnected(sock); + } else if (eEvent & SQUICK_NET_EVENT_ERROR) { + OnClientDisconnected(sock); + } else if (eEvent & SQUICK_NET_EVENT_TIMEOUT) { + OnClientDisconnected(sock); + } else if (eEvent & SQUICK_NET_EVENT_CONNECTED) { + LOG_INFO("New socket client connected, sock<%v>", sock); + } +} + // request per 5 sec void LogicModule::NReqMinWorkloadNodeInfo() { // find min work load proxy rpc::NReqMinWorkloadNodeInfo pbreq; - pbreq.add_type_list(ST_PLAYER); - pbreq.add_type_list(ST_WORLD); + pbreq.add_type_list(rpc::ST_PLAYER); + pbreq.add_type_list(rpc::ST_WORLD); m_net_client_->SendPBByID(DEFAULT_MASTER_ID, rpc::IdNReqMinWorkloadNodeInfo, pbreq); } @@ -82,7 +105,7 @@ void LogicModule::OnReqPlayerEnter(const socket_t sock, const int msg_id, const return; } - int player_node = GetLoadBanlanceNode(ST_PLAYER); + int player_node = GetLoadBanlanceNode(rpc::ST_PLAYER); if (player_node <= 0 || pInfo->player_node > 0) { LOG_WARN("No player node find: %v ", player_node); return; @@ -148,7 +171,7 @@ void LogicModule::OnAckPlayerEnter(const socket_t sock, const int msg_id, const } } -int LogicModule::GetLoadBanlanceNode(ServerType type) { +int LogicModule::GetLoadBanlanceNode(int type) { auto iter = min_workload_nodes_.find(type); if (iter == min_workload_nodes_.end()) { return 0; diff --git a/src/node/proxy/logic/logic_module.h b/src/node/proxy/logic/logic_module.h index 687ff375..86056874 100644 --- a/src/node/proxy/logic/logic_module.h +++ b/src/node/proxy/logic/logic_module.h @@ -4,10 +4,10 @@ #include #include #include +#include #include #include "i_logic_module.h" -#include #include enum class ProtocolType { Tcp = 1, @@ -82,9 +82,9 @@ class LogicModule : public ILogicModule { int OnHeatbeatCheck(const Guid &self, const std::string &heartBeat, const float time, const int count); void OnWS(const socket_t sock, const int msg_id, const char *msg, const uint32_t len); - void OnWebSocketClientEvent(socket_t sockIndex, const SQUICK_NET_EVENT eEvent, INet *pNet); + void OnSocketEvent(socket_t sock, const SQUICK_NET_EVENT eEvent, INet *pNet); - int GetLoadBanlanceNode(ServerType type); + int GetLoadBanlanceNode(int type); PlayerConnInfo *GetPlayerConnInfoByUID(const uint64_t uid); PlayerConnInfo *GetPlayerConnInfo(const socket_t player_sock); bool RemovePlayerConnInfo(const socket_t player_sock); @@ -102,7 +102,7 @@ class LogicModule : public ILogicModule { INetModule *m_net_; IWSModule *m_ws_; INetClientModule *m_net_client_; - node::INodeModule *m_node_; + INodeModule *m_node_; private: time_t last_update_work_load_info_time_ = 0; diff --git a/src/node/proxy/node/CMakeLists.txt b/src/node/proxy/node/CMakeLists.txt deleted file mode 100644 index e30c88d3..00000000 --- a/src/node/proxy/node/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -set(ProjectName "proxy_node") -file(GLOB soruces *.cc) -file(GLOB headers *.h) -add_library(${ProjectName} SHARED ${soruces} ${headers}) -LinkSquick(${ProjectName}) -SquickOut(${ProjectName} "node/proxy" ${SQUICK_BUILD_PLUGIN_SERVER_PATH}/proxy "node") \ No newline at end of file diff --git a/src/node/proxy/node/i_node_module.h b/src/node/proxy/node/i_node_module.h deleted file mode 100644 index 883dc1b7..00000000 --- a/src/node/proxy/node/i_node_module.h +++ /dev/null @@ -1,10 +0,0 @@ -#pragma once - -#include -#include - -namespace proxy::node { -class INodeModule : public INodeBaseModule { - public: -}; -} // namespace proxy::node \ No newline at end of file diff --git a/src/node/proxy/node/i_ws_module.h b/src/node/proxy/node/i_ws_module.h deleted file mode 100644 index f6f809ee..00000000 --- a/src/node/proxy/node/i_ws_module.h +++ /dev/null @@ -1,11 +0,0 @@ -#pragma once - -#include -#include -#include -namespace proxy::node { -class IWS_Module : public IModule { - - public: -}; -} // namespace proxy::node \ No newline at end of file diff --git a/src/node/proxy/node/node_module.cc b/src/node/proxy/node/node_module.cc deleted file mode 100644 index d348a6af..00000000 --- a/src/node/proxy/node/node_module.cc +++ /dev/null @@ -1,40 +0,0 @@ -#include "plugin.h" - -#include "node_module.h" -namespace proxy::node { - -bool NodeModule::AfterStart() { - m_logic_ = pm_->FindModule(); - m_ws_ = pm_->FindModule(); - - Listen(); - - node_info_.info->set_ws_port(pm_->GetArg("ws_port=", 10502)); - - m_ws_->Listen(pm_->GetArg("max_conn=", ARG_DEFAULT_MAX_CONNECTION), pm_->GetArg("ws_port=", ARG_DEFAULT_WS_PORT), - pm_->GetArg("cpu_count=", ARG_DEFAULT_CPU_COUNT), pm_->GetArg("net_server_buffer=", ARG_DEFAULT_NET_SERVER_BUFFER_SIZE)); - m_ws_->AddEventCallBack(this, &NodeModule::OnWebSocketClientEvent); - - vector node_types = {ServerType::ST_WORLD, ServerType::ST_LOGIN, ServerType::ST_PLAYER}; - AddNodesByType(node_types); - return true; -} - -bool NodeModule::Destroy() { return true; } - -void NodeModule::OnClientConnected(socket_t sock) {} - -void NodeModule::OnClientDisconnected(socket_t sock) { m_logic_->OnClientDisconnected(sock); } - -void NodeModule::OnWebSocketClientEvent(socket_t sock, const SQUICK_NET_EVENT eEvent, INet *pNet) { - if (eEvent & SQUICK_NET_EVENT_EOF) { - m_logic_->OnClientDisconnected(sock); - } else if (eEvent & SQUICK_NET_EVENT_ERROR) { - m_logic_->OnClientDisconnected(sock); - } else if (eEvent & SQUICK_NET_EVENT_TIMEOUT) { - m_logic_->OnClientDisconnected(sock); - } else if (eEvent & SQUICK_NET_EVENT_CONNECTED) { - LOG_INFO("New websocket client connected, sock<%v>", sock); - } -} -} // namespace proxy::node \ No newline at end of file diff --git a/src/node/proxy/node/node_module.h b/src/node/proxy/node/node_module.h deleted file mode 100644 index e39ce6d3..00000000 --- a/src/node/proxy/node/node_module.h +++ /dev/null @@ -1,23 +0,0 @@ -#pragma once -#include "i_node_module.h" -#include -namespace proxy::node { -class NodeModule : public INodeModule { - public: - NodeModule(IPluginManager *p) { - pm_ = p; - is_update_ = true; - } - virtual bool Destroy(); - virtual bool AfterStart(); - virtual void OnClientConnected(socket_t sock) override; - virtual void OnClientDisconnected(socket_t sock) override; - - private: - void OnWebSocketClientEvent(socket_t sock, const SQUICK_NET_EVENT eEvent, INet *pNet); - - private: - logic::ILogicModule *m_logic_; - IWSModule *m_ws_; -}; -} // namespace proxy::node \ No newline at end of file diff --git a/src/node/proxy/node/plugin.cc b/src/node/proxy/node/plugin.cc deleted file mode 100644 index f3b9a201..00000000 --- a/src/node/proxy/node/plugin.cc +++ /dev/null @@ -1,19 +0,0 @@ - -#include "plugin.h" -#include "node_module.h" - -namespace proxy::node { - -SQUICK_EXPORT void SquickPluginLoad(IPluginManager *pm){CREATE_PLUGIN(pm, Plugin)}; - -SQUICK_EXPORT void SquickPluginUnload(IPluginManager *pm){DESTROY_PLUGIN(pm, Plugin)}; - -const int Plugin::GetPluginVersion() { return 0; } - -const std::string Plugin::GetPluginName() { return GET_CLASS_NAME(Plugin); } - -void Plugin::Install() { REGISTER_MODULE(pm_, INodeModule, NodeModule) } - -void Plugin::Uninstall() { UNREGISTER_MODULE(pm_, INodeModule, NodeModule) } - -} // namespace proxy::node \ No newline at end of file diff --git a/src/node/proxy/node/plugin.h b/src/node/proxy/node/plugin.h deleted file mode 100644 index 56b02282..00000000 --- a/src/node/proxy/node/plugin.h +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once - -#include -#include - -namespace proxy::node { -class Plugin : public IPlugin { - public: - Plugin(IPluginManager *p) { pm_ = p; } - virtual const int GetPluginVersion(); - virtual const std::string GetPluginName(); - virtual void Install(); - virtual void Uninstall(); -}; - -} // namespace proxy::node \ No newline at end of file diff --git a/src/node/room/CMakeLists.txt b/src/node/room/CMakeLists.txt index 83f38ba6..8b137891 100644 --- a/src/node/room/CMakeLists.txt +++ b/src/node/room/CMakeLists.txt @@ -1 +1 @@ -add_subdirectory(node) + diff --git a/src/node/room/node/CMakeLists.txt b/src/node/room/node/CMakeLists.txt deleted file mode 100644 index 314258f3..00000000 --- a/src/node/room/node/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -set(ProjectName "room_node") -file(GLOB soruces *.cc) -file(GLOB headers *.h) -add_library(${ProjectName} SHARED ${soruces} ${headers}) -LinkSquick(${ProjectName}) -SquickOut(${ProjectName} "node/room" ${SQUICK_BUILD_PLUGIN_SERVER_PATH}/room "node") diff --git a/src/node/room/node/i_node_module.h b/src/node/room/node/i_node_module.h deleted file mode 100644 index 3e20a797..00000000 --- a/src/node/room/node/i_node_module.h +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once - -#include -namespace room::node { -class INodeModule : public INodeBaseModule { - public: -}; - -} // namespace room::node diff --git a/src/node/room/node/node_module.cc b/src/node/room/node/node_module.cc deleted file mode 100644 index 394fa6a4..00000000 --- a/src/node/room/node/node_module.cc +++ /dev/null @@ -1,15 +0,0 @@ -#include "node_module.h" -#include - -namespace room::node { -bool NodeModule::AfterStart() { - Listen(); - - vector node_types = {ServerType::ST_GLOBAL, ServerType::ST_DB_PROXY}; - AddNodesByType(node_types); - return true; -} - -bool NodeModule::Destroy() { return true; } - -} // namespace room::node diff --git a/src/node/room/node/node_module.h b/src/node/room/node/node_module.h deleted file mode 100644 index b50d6d45..00000000 --- a/src/node/room/node/node_module.h +++ /dev/null @@ -1,19 +0,0 @@ -#pragma once - -#include "i_node_module.h" - -namespace room::node { -class NodeModule : public INodeModule { - public: - NodeModule(IPluginManager *p) { - pm_ = p; - is_update_ = true; - } - virtual bool Destroy(); - virtual bool AfterStart(); - - protected: - private: -}; - -} // namespace room::node diff --git a/src/node/room/node/plugin.cc b/src/node/room/node/plugin.cc deleted file mode 100644 index c3bc935f..00000000 --- a/src/node/room/node/plugin.cc +++ /dev/null @@ -1,17 +0,0 @@ -#include "plugin.h" -#include "node_module.h" - -namespace room::node { -SQUICK_EXPORT void SquickPluginLoad(IPluginManager *pm){CREATE_PLUGIN(pm, Plugin)}; - -SQUICK_EXPORT void SquickPluginUnload(IPluginManager *pm){DESTROY_PLUGIN(pm, Plugin)}; - -const int Plugin::GetPluginVersion() { return 0; } - -const std::string Plugin::GetPluginName() { return GET_CLASS_NAME(Plugin); } - -void Plugin::Install() { REGISTER_MODULE(pm_, INodeModule, NodeModule) } - -void Plugin::Uninstall() { UNREGISTER_MODULE(pm_, INodeModule, NodeModule) } - -} // namespace room::node diff --git a/src/node/room/node/plugin.h b/src/node/room/node/plugin.h deleted file mode 100644 index 2da78eeb..00000000 --- a/src/node/room/node/plugin.h +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once - -#include -#include - -namespace room::node { -class Plugin : public IPlugin { - public: - Plugin(IPluginManager *p) { pm_ = p; } - virtual const int GetPluginVersion(); - virtual const std::string GetPluginName(); - virtual void Install(); - virtual void Uninstall(); -}; - -} // namespace room::node diff --git a/src/node/web/CMakeLists.txt b/src/node/web/CMakeLists.txt index c3ebfb40..77f77563 100644 --- a/src/node/web/CMakeLists.txt +++ b/src/node/web/CMakeLists.txt @@ -1,2 +1 @@ -add_subdirectory(logic) -add_subdirectory(node) \ No newline at end of file +add_subdirectory(logic) \ No newline at end of file diff --git a/src/node/web/logic/logic_module.cc b/src/node/web/logic/logic_module.cc index 8cb4574f..e47f8557 100644 --- a/src/node/web/logic/logic_module.cc +++ b/src/node/web/logic/logic_module.cc @@ -7,7 +7,7 @@ namespace web::logic { bool LogicModule::Start() { m_http_server_ = pm_->FindModule(); - m_node_ = pm_->FindModule(); + m_node_ = pm_->FindModule(); m_net_client_ = pm_->FindModule(); m_net_ = pm_->FindModule(); m_log_ = pm_->FindModule(); @@ -19,10 +19,15 @@ bool LogicModule::Destroy() { return true; } bool LogicModule::AfterStart() { m_http_server_->AddMiddleware(this, &LogicModule::Middleware); - m_http_server_->AddRequestHandler(WEB_BASE_PATH"/login", HttpType::SQUICK_HTTP_REQ_POST, this, &LogicModule::OnLogin); - m_http_server_->AddRequestHandler(WEB_BASE_PATH"/auth_check", HttpType::SQUICK_HTTP_REQ_POST, this, &LogicModule::OnAuthCheck); - m_http_server_->AddRequestHandler(WEB_BASE_PATH"/auth_check", HttpType::SQUICK_HTTP_REQ_GET, this, &LogicModule::OnAuthCheck); + m_http_server_->AddRequestHandler(WEB_BASE_PATH "/login", HttpType::SQUICK_HTTP_REQ_POST, this, &LogicModule::OnLogin); + m_http_server_->AddRequestHandler(WEB_BASE_PATH "/all_nodes", HttpType::SQUICK_HTTP_REQ_GET, this, &LogicModule::OnGetAllNodes); + m_http_server_->AddRequestHandler(WEB_BASE_PATH "/auth_check", HttpType::SQUICK_HTTP_REQ_POST, this, &LogicModule::OnAuthCheck); + m_http_server_->AddRequestHandler(WEB_BASE_PATH "/auth_check", HttpType::SQUICK_HTTP_REQ_GET, this, &LogicModule::OnAuthCheck); m_http_server_->StartServer(pm_->GetArg("http_port=", ARG_DEFAULT_HTTP_PORT)); + + vector node_types = {rpc::ST_GLOBAL, rpc::ST_DB_PROXY, rpc::ST_PLAYER}; + m_node_->AddSubscribeNode(node_types); + LoadConfig(); return true; } @@ -36,7 +41,7 @@ bool LogicModule::LoadConfig() { } config_file >> web_config_; config_file.close(); - + try { json header = web_config_.at("ResponseHttpHeader"); for (auto iter = header.begin(); iter != header.end(); ++iter) { @@ -46,8 +51,7 @@ bool LogicModule::LoadConfig() { for (auto v : white_uri_list) { white_uri_list_.insert(v); } - } - catch (std::exception err) { + } catch (std::exception err) { LOG_ERROR("Get config from json is error <%v>", err.what()); return false; } @@ -224,4 +228,53 @@ string LogicModule::MakeToken(string account_id) { } void LogicModule::SetToken(const std::string &account_id, const std::string &user_token) { auth_token_[account_id] = user_token; } + +Coroutine LogicModule::OnGetAllNodes(std::shared_ptr request) { + json rsp; + + rpc::NReqAllNodesInfo pbreq; + auto data = co_await m_net_client_->RequestPB(DEFAULT_MASTER_ID, rpc::IdNReqAllNodesInfo, pbreq, rpc::IdNAckAllNodesInfo); + if (data.error) { + rsp["code"] = IResponse::SERVER_ERROR; + rsp["msg"] = "Server get network error\n"; + m_http_server_->ResponseMsg(request, rsp.dump(), WebStatus::WEB_ERROR); + co_return; + } + + uint64_t uid; + rpc::NAckAllNodesInfo pback; + if (!INetModule::ReceivePB(data.ack_msg_id, data.data, data.length, pback, uid)) { + rsp["code"] = IResponse::SERVER_ERROR; + rsp["msg"] = "Parse msg is error error\n"; + m_http_server_->ResponseMsg(request, rsp.dump(), WebStatus::WEB_ERROR); + co_return; + } + + nlohmann::json node_list = nlohmann::json::array(); + for (auto &sd : pback.node_list()) { + json n; + n["area"] = sd.area(); + n["type"] = sd.type(); + n["id"] = sd.id(); + n["name"] = sd.name(); + n["ip"] = sd.ip(); + n["public_ip"] = sd.public_ip(); + n["port"] = sd.port(); + n["cpu_count"] = sd.cpu_count(); + n["status"] = sd.state(); + n["workload"] = sd.workload(); + n["max_online"] = sd.max_online(); + n["update_time"] = sd.update_time(); + n["ws_port"] = sd.ws_port(); + n["http_port"] = sd.http_port(); + n["https_port"] = sd.https_port(); + n["connections"] = sd.connections(); + n["net_client_connections"] = sd.net_client_connections(); + node_list.push_back(n); + } + rsp["node_list"] = node_list; + rsp["code"] = IResponse::SUCCESS; + m_http_server_->ResponseMsg(request, rsp.dump(), WebStatus::WEB_OK); + co_return; +} } // namespace web::logic diff --git a/src/node/web/logic/logic_module.h b/src/node/web/logic/logic_module.h index 58b0cb12..a4949d3f 100644 --- a/src/node/web/logic/logic_module.h +++ b/src/node/web/logic/logic_module.h @@ -2,14 +2,13 @@ #include "i_logic_module.h" #include #include -#include +#include #include #include #include +#include #include #include -#include -#include struct LoginInfo { std::string account; @@ -43,13 +42,14 @@ class LogicModule : public ILogicModule { void SetToken(const std::string &account_id, const std::string &user_token); bool LoadConfig(); bool OnAuthCheck(std::shared_ptr request); + Coroutine OnGetAllNodes(std::shared_ptr request); private: string MakeToken(string sguid); INetClientModule *m_net_client_; INetModule *m_net_; IHttpServerModule *m_http_server_; - node::INodeModule *m_node_; + INodeModule *m_node_; ILogModule *m_log_; private: @@ -57,9 +57,10 @@ class LogicModule : public ILogicModule { std::map login_info_; std::set white_uri_list_; std::map config_response_header_; + private: int player_index = 0; json web_config_; }; -} // namespace login::logic \ No newline at end of file +} // namespace web::logic \ No newline at end of file diff --git a/src/node/web/node/i_node_module.h b/src/node/web/node/i_node_module.h deleted file mode 100644 index b95b433f..00000000 --- a/src/node/web/node/i_node_module.h +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once - -#include -namespace web::node { -class INodeModule : public INodeBaseModule { - public: -}; - -} // namespace web::node diff --git a/src/node/web/node/node_module.cc b/src/node/web/node/node_module.cc deleted file mode 100644 index 23058ba7..00000000 --- a/src/node/web/node/node_module.cc +++ /dev/null @@ -1,15 +0,0 @@ -#include "node_module.h" -#include - -namespace web::node { -bool NodeModule::AfterStart() { - Listen(); - - vector node_types = {ServerType::ST_GLOBAL, ServerType::ST_DB_PROXY, ServerType::ST_PLAYER}; - AddNodesByType(node_types); - return true; -} - -bool NodeModule::Destroy() { return true; } - -} // namespace web::node diff --git a/src/node/web/node/node_module.h b/src/node/web/node/node_module.h deleted file mode 100644 index 37df990d..00000000 --- a/src/node/web/node/node_module.h +++ /dev/null @@ -1,19 +0,0 @@ -#pragma once - -#include "i_node_module.h" - -namespace web::node { -class NodeModule : public INodeModule { - public: - NodeModule(IPluginManager *p) { - pm_ = p; - is_update_ = true; - } - virtual bool Destroy(); - virtual bool AfterStart(); - - protected: - private: -}; - -} // namespace web::node diff --git a/src/node/world/CMakeLists.txt b/src/node/world/CMakeLists.txt index ec3ca387..77f77563 100644 --- a/src/node/world/CMakeLists.txt +++ b/src/node/world/CMakeLists.txt @@ -1,3 +1 @@ -add_subdirectory(node) -add_subdirectory(logic) -add_subdirectory(player) \ No newline at end of file +add_subdirectory(logic) \ No newline at end of file diff --git a/src/node/world/logic/i_logic_module.h b/src/node/world/logic/i_logic_module.h index 7d55def9..62321533 100644 --- a/src/node/world/logic/i_logic_module.h +++ b/src/node/world/logic/i_logic_module.h @@ -2,10 +2,6 @@ #include namespace world::logic { -class ILogicModule : public IModule { - public: - protected: - private: -}; +class ILogicModule : public IModule {}; } // namespace world::logic diff --git a/src/node/world/logic/logic_module.cc b/src/node/world/logic/logic_module.cc index e69de29b..0106afe9 100644 --- a/src/node/world/logic/logic_module.cc +++ b/src/node/world/logic/logic_module.cc @@ -0,0 +1,10 @@ +#include "logic_module.h" + +namespace world::logic { +bool LogicModule::Start() { + m_node_ = pm_->FindModule(); + vector node_types = {rpc::ST_DB_PROXY}; + m_node_->AddSubscribeNode(node_types); + return true; +} +} // namespace world::logic \ No newline at end of file diff --git a/src/node/world/logic/logic_module.h b/src/node/world/logic/logic_module.h index e69de29b..0da64724 100644 --- a/src/node/world/logic/logic_module.h +++ b/src/node/world/logic/logic_module.h @@ -0,0 +1,19 @@ +#pragma once +#include "i_logic_module.h" +#include +#include + +namespace world::logic { +class LogicModule : public ILogicModule { + public: + LogicModule(IPluginManager *p) { + pm_ = p; + is_update_ = true; // Update + } + virtual bool Start() override; + + private: + INodeModule *m_node_; +}; + +} // namespace world::logic \ No newline at end of file diff --git a/src/node/world/logic/plugin.cc b/src/node/world/logic/plugin.cc index 2d9b421c..fb3d5f40 100644 --- a/src/node/world/logic/plugin.cc +++ b/src/node/world/logic/plugin.cc @@ -1,6 +1,6 @@ #include "plugin.h" - +#include "logic_module.h" namespace world::logic { SQUICK_EXPORT void SquickPluginLoad(IPluginManager *pm){CREATE_PLUGIN(pm, Plugin)}; @@ -11,8 +11,8 @@ const int Plugin::GetPluginVersion() { return 0; } const std::string Plugin::GetPluginName() { return GET_CLASS_NAME(Plugin); } -void Plugin::Install() {} +void Plugin::Install() { REGISTER_MODULE(pm_, ILogicModule, LogicModule) } -void Plugin::Uninstall() {} +void Plugin::Uninstall() { UNREGISTER_MODULE(pm_, ILogicModule, LogicModule) } } // namespace world::logic \ No newline at end of file diff --git a/src/node/world/node/CMakeLists.txt b/src/node/world/node/CMakeLists.txt deleted file mode 100644 index 1e9b5e81..00000000 --- a/src/node/world/node/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -set(ProjectName "world_node") -file(GLOB soruces *.cc) -file(GLOB headers *.h) -add_library(${ProjectName} SHARED ${soruces} ${headers}) -LinkSquick(${ProjectName}) -SquickOut(${ProjectName} "node/world" ${SQUICK_BUILD_PLUGIN_SERVER_PATH}/world "node") \ No newline at end of file diff --git a/src/node/world/node/i_node_module.h b/src/node/world/node/i_node_module.h deleted file mode 100644 index a14ff73f..00000000 --- a/src/node/world/node/i_node_module.h +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once -#include -namespace world::node { -class INodeModule : public INodeBaseModule { - public: - protected: -}; - -} // namespace world::node \ No newline at end of file diff --git a/src/node/world/node/node_module.cc b/src/node/world/node/node_module.cc deleted file mode 100644 index 3f004f49..00000000 --- a/src/node/world/node/node_module.cc +++ /dev/null @@ -1,15 +0,0 @@ - -#include "node_module.h" -#include "plugin.h" -#include -namespace world::node { -bool NodeModule::AfterStart() { - Listen(); - - vector node_types = {}; - AddNodesByType(node_types); - return true; -} -bool NodeModule::Destroy() { return true; } - -} // namespace world::node \ No newline at end of file diff --git a/src/node/world/node/node_module.h b/src/node/world/node/node_module.h deleted file mode 100644 index d8203781..00000000 --- a/src/node/world/node/node_module.h +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once -#include "i_node_module.h" -namespace world::node { -class NodeModule : public INodeModule { - public: - NodeModule(IPluginManager *p) { - pm_ = p; - is_update_ = true; - } - - virtual bool Destroy(); - virtual bool AfterStart(); - - protected: - void SyncServer(); - - private: -}; - -} // namespace world::node \ No newline at end of file diff --git a/src/node/world/node/plugin.cc b/src/node/world/node/plugin.cc deleted file mode 100644 index a0295ab9..00000000 --- a/src/node/world/node/plugin.cc +++ /dev/null @@ -1,19 +0,0 @@ - -#include "plugin.h" -#include "node_module.h" - -namespace world::node { - -SQUICK_EXPORT void SquickPluginLoad(IPluginManager *pm){CREATE_PLUGIN(pm, Plugin)}; - -SQUICK_EXPORT void SquickPluginUnload(IPluginManager *pm){DESTROY_PLUGIN(pm, Plugin)}; - -const int Plugin::GetPluginVersion() { return 0; } - -const std::string Plugin::GetPluginName() { return GET_CLASS_NAME(Plugin); } - -void Plugin::Install() { REGISTER_MODULE(pm_, INodeModule, NodeModule) } - -void Plugin::Uninstall() { UNREGISTER_MODULE(pm_, INodeModule, NodeModule) } - -} // namespace world::node \ No newline at end of file diff --git a/src/node/world/node/plugin.h b/src/node/world/node/plugin.h deleted file mode 100644 index 059de085..00000000 --- a/src/node/world/node/plugin.h +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once - -#include -#include -namespace world::node { -class Plugin : public IPlugin { - public: - Plugin(IPluginManager *p) { pm_ = p; } - virtual const int GetPluginVersion(); - - virtual const std::string GetPluginName(); - - virtual void Install(); - - virtual void Uninstall(); -}; - -} // namespace world::node \ No newline at end of file diff --git a/src/node/world/player/CMakeLists.txt b/src/node/world/player/CMakeLists.txt deleted file mode 100644 index dff83de6..00000000 --- a/src/node/world/player/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -set(ProjectName "world_player") -file(GLOB soruces *.cc) -file(GLOB headers *.h) -add_library(${ProjectName} SHARED ${soruces} ${headers}) -LinkSquick(${ProjectName}) -SquickOut(${ProjectName} "node/world" ${SQUICK_BUILD_PLUGIN_SERVER_PATH}/world "player") \ No newline at end of file diff --git a/src/node/world/player/i_player_module.h b/src/node/world/player/i_player_module.h deleted file mode 100644 index 8140ab96..00000000 --- a/src/node/world/player/i_player_module.h +++ /dev/null @@ -1,13 +0,0 @@ -#pragma once - -#include - -namespace world::player { - -class IPlayerModule : public IModule { - public: - protected: - private: -}; - -} // namespace world::player diff --git a/src/node/world/player/player_module.cc b/src/node/world/player/player_module.cc deleted file mode 100644 index e69de29b..00000000 diff --git a/src/node/world/player/player_module.h b/src/node/world/player/player_module.h deleted file mode 100644 index e69de29b..00000000 diff --git a/src/proto/n_master.proto b/src/proto/n_master.proto index bea4a5eb..b8c5af9f 100644 --- a/src/proto/n_master.proto +++ b/src/proto/n_master.proto @@ -2,21 +2,41 @@ syntax = "proto3"; package rpc; -enum ServerState { - ServerCrash = 0; - ServerNormal = 1; - ServerBusy = 2; - ServerFrie = 3; +enum NodeState { + NodeStateCrash = 0; + NodeStateNormal = 1; + NodeStateBusy = 2; + NodeStateFrie = 3; } -message Server { +enum NodeType { + ST_NONE = 0; // NONE + ST_MASTER = 1; // + ST_LOGIN = 2; // + ST_WORLD = 3; // + ST_DB_PROXY = 4; // + ST_PROXY = 5; // + ST_PLAYER = 6; + ST_GAME_MGR = 7; // + ST_GAME = 8; // + ST_MICRO = 9; // Micro + ST_CDN = 10; // CDN + ST_WEB = 11; // Web + ST_GLOBAL = 12; // Global + ST_ROOM = 13; // Room + ST_MATCH = 14; // Match + ST_GM = 15; // GM + ST_MAX = 16; +} + +message Node { int32 id = 1; bytes name = 2; bytes ip = 3; int32 port = 4; int32 max_online = 5; int32 cpu_count = 6; - ServerState state = 7; + NodeState state = 7; int32 type = 8; int32 area = 9; int32 connections = 10; @@ -28,16 +48,17 @@ message Server { int32 ws_port = 16; int32 http_port = 17; int32 https_port = 18; + int32 net_client_connections = 19; } message NReqNodeRegister { // msg_id=500; repeated int32 listen_type_list = 1; // set bind type, When new same type node connect to master, it will ntf self node. - Server node = 2; + Node node = 2; } message NAckNodeRegister { // msg_id=501; int32 code = 1; - repeated Server node_add_list = 2; + repeated Node node_add_list = 2; } message NReqNodeUnregister { // msg_id=502; @@ -53,7 +74,7 @@ message NReqMinWorkloadNodeInfo { // msg_id=504; } message NAckMinWorkloadNodeInfo { // msg_id=505; - repeated Server list = 1; + repeated Node list = 1; } message NReqNodeInfo { // msg_id=506; @@ -61,22 +82,29 @@ message NReqNodeInfo { // msg_id=506; } message NAckNodeInfo { // msg_id=507; - repeated Server list = 1; + repeated Node list = 1; } message NNtfNodeReport { // msg_id=508; desc=Report current node status; int32 id = 1; - repeated Server list = 2; + repeated Node list = 2; } message NNtfNodeAdd { // msg_id=509; - repeated Server node_list = 1; + repeated Node node_list = 1; } message NNtfNodeRemove { // msg_id=510; - repeated Server node_list = 1; + repeated Node node_list = 1; } message NNtfNodeMsgForward { // msg_id=511; bytes data = 1; +} + +message NReqAllNodesInfo { // msg_id=512; +} + +message NAckAllNodesInfo { // msg_id=513; + repeated Node node_list = 1; } \ No newline at end of file diff --git a/src/squick/core/base.h b/src/squick/core/base.h index 509f83fa..7adc1d0e 100644 --- a/src/squick/core/base.h +++ b/src/squick/core/base.h @@ -14,26 +14,6 @@ #define SQUICK_VERSION "1.1.1" #define SERVER_NAME "Squick/" SQUICK_VERSION -enum ServerType { - ST_NONE = 0, // NONE - ST_MASTER = 1, // - ST_LOGIN = 2, // - ST_WORLD = 3, // - ST_DB_PROXY = 4, // - ST_PROXY = 5, // - ST_PLAYER = 6, - ST_GAME_MGR = 7, // - ST_GAME = 8, // - ST_MICRO = 9, // Micro - ST_CDN = 10, // CDN - ST_WEB = 11, // Web - ST_GLOBAL = 12, // Global - ST_ROOM = 12, // Room - ST_MATCH = 13, // Match - ST_GM = 14, // GM - ST_MAX = 15, -}; - // 线程池 每一帧睡眠时间,单位毫秒 #define THREAD_POOL_SLEEP_TIME 100 // 主线程 每一帧睡眠时间,单位毫秒 diff --git a/src/squick/imodule/i_node_module.h b/src/squick/imodule/i_node_module.h deleted file mode 100644 index 01f1cc12..00000000 --- a/src/squick/imodule/i_node_module.h +++ /dev/null @@ -1,308 +0,0 @@ -// Author: i0gan -// Email : l418894113@gmail.com -// Date : 2023-04-11 -// Description: Server node base module -#pragma once -#include -#include -#include -#include -#include - -#define NODE_REPORT_TIME 10 -class INodeBaseModule : public IModule { - public: - virtual bool Awake() final { return true; } - - virtual bool Start() override final { - m_net_ = pm_->FindModule(); - m_log_ = pm_->FindModule(); - m_net_client_ = pm_->FindModule(); - is_update_ = true; - - pm_->SetAppType(StringNodeTypeToEnum(pm_->GetArg("type=", "proxy"))); - pm_->SetArea(pm_->GetArg("area=", 0)); - - return true; - } - - virtual bool Update() override final { - // calc work load - CalcWorkLoad(); - if (last_report_time_ + NODE_REPORT_TIME > pm_->GetNowTime()) { - return true; - } - if (last_report_time_ > 0) { - UpdateState(); - } - last_report_time_ = pm_->GetNowTime(); - return true; - } - - inline void CalcWorkLoad() { - // handle time * 100 + m_net connections + m_net_client connections - time_t now_time = SquickGetTimeMS(); - // The first update will int overflow, but who care? - workload_ = (now_time - last_update_time_) * 100; - last_update_time_ = now_time; - } - - static std::string EnumNodeTypeToString(ServerType type) { - switch (type) { - case ServerType::ST_MASTER: - return "master"; - case ServerType::ST_LOGIN: - return "login"; - case ServerType::ST_WORLD: - return "world"; - case ServerType::ST_DB_PROXY: - return "db_proxy"; - case ServerType::ST_PROXY: - return "proxy"; - case ServerType::ST_PLAYER: - return "player"; - case ServerType::ST_GAME_MGR: - return "game_mgr"; - case ServerType::ST_GAME: - return "game"; - case ServerType::ST_MICRO: - return "micro"; - case ServerType::ST_CDN: - return "cdn"; - case ServerType::ST_WEB: - return "web"; - } - return ""; - } - - static ServerType StringNodeTypeToEnum(const std::string &type) { - if (type == "master") - return ServerType::ST_MASTER; - else if (type == "login") - return ServerType::ST_LOGIN; - else if (type == "world") - return ServerType::ST_WORLD; - else if (type == "db_proxy") - return ServerType::ST_DB_PROXY; - else if (type == "proxy") - return ServerType::ST_PROXY; - else if (type == "player") - return ServerType::ST_PLAYER; - else if (type == "game_mgr") - return ServerType::ST_GAME_MGR; - else if (type == "game") - return ServerType::ST_GAME; - else if (type == "micro") - return ServerType::ST_MICRO; - else if (type == "cdn") - return ServerType::ST_CDN; - else if (type == "web") - return ServerType::ST_WEB; - return ServerType::ST_NONE; - } - - virtual bool Listen() { - m_net_->AddReceiveCallBack(this, &INodeBaseModule::InvalidMessage); - m_net_->AddEventCallBack(this, &INodeBaseModule::OnServerSocketEvent); - - node_info_.info->set_id(pm_->GetArg("id=", ARG_DEFAULT_ID)); - node_info_.info->set_type(pm_->GetAppType()); - node_info_.info->set_port(pm_->GetArg("port=", ARG_DEFAULT_PORT)); - - node_info_.info->set_name(pm_->GetAppName()); - node_info_.info->set_ip(pm_->GetArg("ip=", ARG_DEFAULT_IP)); - node_info_.info->set_public_ip(pm_->GetArg("public_ip=", ARG_DEFAULT_PUBLIC_IP)); - node_info_.info->set_area(pm_->GetArea()); - node_info_.info->set_update_time(SquickGetTimeS()); - node_info_.info->set_max_online(pm_->GetArg("max_conn=", ARG_DEFAULT_MAX_CONNECTION)); - node_info_.info->set_cpu_count(pm_->GetArg("cpu_count=", ARG_DEFAULT_CPU_COUNT)); - - int nRet = m_net_->Listen(node_info_.info->max_online(), node_info_.info->port(), node_info_.info->cpu_count(), - pm_->GetArg("net_server_buffer=", ARG_DEFAULT_NET_SERVER_BUFFER_SIZE)); - LOG_INFO("Node Listen at 0.0.0.0:%v Name :", node_info_.info->port(), node_info_.info->name()); - - if (nRet < 0) { - LOG_ERROR("Cannot init server net, Port<%v>", node_info_.info->port()); - SQUICK_ASSERT(nRet, "Cannot init server net", __FILE__, __FUNCTION__); - exit(0); - } - return true; - } - - // Add upper server - bool AddNodesByType(const vector &types) { - m_net_client_->AddEventCallBack(ST_MASTER, this, &INodeBaseModule::OnClientSocketEvent); - m_net_client_->AddReceiveCallBack(ST_MASTER, rpc::IdNNtfNodeAdd, this, &INodeBaseModule::OnNNtfNodeAdd); - m_net_client_->AddReceiveCallBack(ST_MASTER, rpc::IdNNtfNodeRemove, this, &INodeBaseModule::OnNNtfNodeRemove); - m_net_client_->AddReceiveCallBack(ST_MASTER, rpc::IdNAckNodeRegister, this, &INodeBaseModule::OnNAckNodeRegister); - bool ret = false; - node_info_.listen_types = types; - ConnectData s; - s.id = DEFAULT_MASTER_ID; - s.type = StringNodeTypeToEnum("master"); - s.ip = pm_->GetArg("master_ip=", "127.0.0.1"); - s.port = pm_->GetArg("master_port=", 10001); - s.name = "master"; - s.buffer_size = pm_->GetArg("net_client_buffer=", ARG_DEFAULT_NET_CLIENT_BUFFER_SIZE); - LOG_INFO("Node Connect to %v %v:%v cur_area<%v>", s.name, s.ip, s.port, pm_->GetArg("area=", 0)); - m_net_client_->AddNode(s); - return true; - } - - void InvalidMessage(const socket_t sock, const int msg_id, const char *msg, const uint32_t len) { printf("Net || umsg_id=%d\n", msg_id); } - - // Add upper server - void OnDynamicServerAdd(const socket_t sock, const int msg_id, const char *msg, const uint32_t len) { - uint64_t uid; - rpc::NNtfNodeAdd ntf; - if (!INetModule::ReceivePB(msg_id, msg, len, ntf, uid)) { - return; - } - for (int i = 0; i < ntf.node_list().size(); ++i) { - const rpc::Server &sd = ntf.node_list(i); - // type - ConnectData s; - s.id = sd.id(); - s.ip = sd.ip(); - s.port = sd.port(); - s.name = sd.name(); - s.type = (ServerType)sd.type(); - m_net_client_->AddNode(s); - } - } - //////////////////////////// PRIVATE //////////////////////////// - private: - // 热加载配置文件 - virtual void OnReloadConfig(const socket_t sock, const int msg_id, const char *msg, const uint32_t len) {} - // 热加载Lua脚本 - virtual void OnReloadLua(const socket_t sock, const int msg_id, const char *msg, const uint32_t len) {} - - // Report to upper server - void UpdateState() { - - node_info_.info->set_update_time(SquickGetTimeS()); - node_info_.info->set_workload(workload_); - // Update status to master - if (pm_->GetAppType() != ST_MASTER) { - rpc::NNtfNodeReport req; - req.set_id(pm_->GetAppID()); - auto s = req.add_list(); - *s = *node_info_.info.get(); - m_net_client_->SendPBByID(DEFAULT_MASTER_ID, rpc::IdNNtfNodeReport, req); - } - } - - // 作为服务的监听socket状态事件 - void OnServerSocketEvent(const socket_t sock, const SQUICK_NET_EVENT eEvent, INet *pNet) { - if (eEvent & SQUICK_NET_EVENT_EOF) { - LOG_INFO("Net Server: SQUICK_NET_EVENT_EOF Connection closed, sock<%v>", sock); - OnClientDisconnected(sock); - } else if (eEvent & SQUICK_NET_EVENT_ERROR) { - LOG_INFO("Net Server: SQUICK_NET_EVENT_ERROR Got an error on the connection, sock<%v>", sock); - OnClientDisconnected(sock); - } else if (eEvent & SQUICK_NET_EVENT_TIMEOUT) { - LOG_INFO("Net Server: SQUICK_NET_EVENT_TIMEOUT read timeout, sock<%v>", sock); - OnClientDisconnected(sock); - } else if (eEvent & SQUICK_NET_EVENT_CONNECTED) { - LOG_INFO("Net Server: SQUICK_NET_EVENT_CONNECTED connected success, sock<%v>", sock); - OnClientConnected(sock); - } - } - - virtual void OnClientConnected(socket_t sock){}; - virtual void OnClientDisconnected(socket_t sock){}; - - // 作为客户端连接socket事件 - void OnClientSocketEvent(const socket_t sock, const SQUICK_NET_EVENT eEvent, INet *pNet) { - if (eEvent & SQUICK_NET_EVENT_EOF) { - LOG_WARN("Net Client: SQUICK_NET_EVENT_EOF, sock<%v>", sock); - } else if (eEvent & SQUICK_NET_EVENT_ERROR) { - LOG_ERROR("Net Client: SQUICK_NET_EVENT_ERROR, sock<%v>", sock); - } else if (eEvent & SQUICK_NET_EVENT_TIMEOUT) { - LOG_ERROR("Net Client: SQUICK_NET_EVENT_TIMEOUT, sock<%v>", sock); - } else if (eEvent & SQUICK_NET_EVENT_CONNECTED) { - LOG_INFO("Net Client: SQUICK_NET_EVENT_CONNECTED connected success, sock<%v>", sock); - OnUpperNodeConnected(pNet); - } - } - - void OnUpperNodeConnected(INet *pNet) { - std::shared_ptr ts = m_net_client_->GetServerNetInfo(pNet); - if (ts == nullptr) { - ostringstream msg; - LOG_ERROR("OnUpperNodeConnected: %v", "Cannot find server info"); - return; - } - ts->state = ConnectDataState::NORMAL; - - // target type only master can register - if (ts->type != ST_MASTER) - return; - - rpc::NReqNodeRegister req; - *req.mutable_node() = *node_info_.info.get(); - - for (auto type : node_info_.listen_types) { - req.add_listen_type_list(type); - } - - m_net_client_->SendPBByID(ts->id, rpc::IdNReqNodeRegister, req); - LOG_INFO("Register node <%v>", ts->name); - } - - void OnNAckNodeRegister(const socket_t sock, const int msg_id, const char *msg, const uint32_t len) { - uint64_t uid; - rpc::NAckNodeRegister ack; - if (!m_net_->ReceivePB(msg_id, msg, len, ack, uid)) { - return; - } - - if (ack.code() == 0) { - AddNodes(ack.node_add_list()); - } else { - LOG_ERROR("Node Register faild! sock<%v>", sock); - } - } - - // Add node ntf - void OnNNtfNodeAdd(const socket_t sock, const int msg_id, const char *msg, const uint32_t len) { - uint64_t uid; - rpc::NNtfNodeAdd ntf; - if (!m_net_->ReceivePB(msg_id, msg, len, ntf, uid)) { - return; - } - AddNodes(ntf.node_list(), true); - } - - bool AddNodes(const google::protobuf::RepeatedPtrField &list, bool from_ntf = false) { - for (const auto &n : list) { - LOG_INFO("Add node from master, is_ntf<%v> added:", from_ntf, n.name()); - ConnectData s; - s.id = n.id(); - s.ip = n.ip(); - s.port = n.port(); - s.name = n.name(); - s.type = (ServerType)n.type(); - m_net_client_->AddNode(s); - } - return true; - } - - // Add node ntf - void OnNNtfNodeRemove(const socket_t sock, const int msg_id, const char *msg, const uint32_t len) {} - - bool RemoveNodes() { return true; } - - public: - ILogModule *m_log_; - INetModule *m_net_; - INetClientModule *m_net_client_; - ServerInfo node_info_; - - private: - time_t last_report_time_ = 0; - time_t last_update_time_ = 0; - int workload_; - - protected: -}; diff --git a/src/squick/plugin/CMakeLists.txt b/src/squick/plugin/CMakeLists.txt index 215e73f4..01ab68e2 100644 --- a/src/squick/plugin/CMakeLists.txt +++ b/src/squick/plugin/CMakeLists.txt @@ -6,4 +6,5 @@ add_subdirectory(net) add_subdirectory(navigation) add_subdirectory(lua) add_subdirectory(utils) -add_subdirectory(thread) \ No newline at end of file +add_subdirectory(thread) +add_subdirectory(node) \ No newline at end of file diff --git a/src/squick/plugin/lua/lua_script_module.cc b/src/squick/plugin/lua/lua_script_module.cc index 83e668bb..27ad0b69 100644 --- a/src/squick/plugin/lua/lua_script_module.cc +++ b/src/squick/plugin/lua/lua_script_module.cc @@ -572,10 +572,10 @@ void LuaScriptModule::AddMsgCallBackAsServer(const int msg_id, const LuaIntf::Lu callbackList->Add({luaTable, luaFunc}); } -void LuaScriptModule::RemoveMsgCallBackAsClient(const ServerType serverType, const int msg_id) { m_net_client_->RemoveReceiveCallBack(serverType, msg_id); } +void LuaScriptModule::RemoveMsgCallBackAsClient(const int serverType, const int msg_id) { m_net_client_->RemoveReceiveCallBack(serverType, msg_id); } // 做为服务器做为客户端连接的网络 回调 -void LuaScriptModule::AddMsgCallBackAsClient(const ServerType serverType, const int msg_id, const LuaIntf::LuaRef &luaTable, const LuaIntf::LuaRef &luaFunc) { +void LuaScriptModule::AddMsgCallBackAsClient(const int serverType, const int msg_id, const LuaIntf::LuaRef &luaTable, const LuaIntf::LuaRef &luaFunc) { auto callbackList = mxNetMsgCallBackFuncMapAsClient.GetElement(msg_id); if (!callbackList) { callbackList = new List(); @@ -612,7 +612,7 @@ void LuaScriptModule::SendToServerByServerID(const int server_id, const uint16_t m_net_client_->SendByID(server_id, msg_id, data, uid); } -void LuaScriptModule::SendToAllServerByServerType(const ServerType server_type, const uint16_t msg_id, const std::string &data, const uint64_t uid) { +void LuaScriptModule::SendToAllServerByServerType(const int server_type, const uint16_t msg_id, const std::string &data, const uint64_t uid) { m_net_client_->SendToAllNodeByType(server_type, msg_id, data, uid); } diff --git a/src/squick/plugin/lua/lua_script_module.h b/src/squick/plugin/lua/lua_script_module.h index 43237d28..44cc4673 100644 --- a/src/squick/plugin/lua/lua_script_module.h +++ b/src/squick/plugin/lua/lua_script_module.h @@ -130,15 +130,15 @@ class LuaScriptModule : public ILuaScriptModule { void AddMsgCallBackAsServer(const int msg_id, const LuaIntf::LuaRef &luaTable, const LuaIntf::LuaRef &luaFunc); // as client - void RemoveMsgCallBackAsClient(const ServerType serverType, const int msg_id); - void AddMsgCallBackAsClient(const ServerType serverType, const int msg_id, const LuaIntf::LuaRef &luaTable, const LuaIntf::LuaRef &luaFunc); + void RemoveMsgCallBackAsClient(const int serverType, const int msg_id); + void AddMsgCallBackAsClient(const int serverType, const int msg_id, const LuaIntf::LuaRef &luaTable, const LuaIntf::LuaRef &luaFunc); bool ImportProtoFile(const std::string &fileName); const std::string Encode(const std::string &msgTypeName, const LuaIntf::LuaRef &luaTable); LuaIntf::LuaRef Decode(const std::string &msgTypeName, const std::string &data); void SendToServerByServerID(const int server_id, const uint16_t msg_id, const std::string &data, const uint64_t uid); - void SendToAllServerByServerType(const ServerType server_type, const uint16_t msg_id, const std::string &data, const uint64_t uid); + void SendToAllServerByServerType(const int server_type, const uint16_t msg_id, const std::string &data, const uint64_t uid); // for net module void SendByFD(const socket_t fd, const uint16_t msg_id, const std::string &data, const uint64_t uid); diff --git a/src/squick/plugin/net/http_server.cc b/src/squick/plugin/net/http_server.cc index 9df788ad..b2ec8d2c 100644 --- a/src/squick/plugin/net/http_server.cc +++ b/src/squick/plugin/net/http_server.cc @@ -168,7 +168,7 @@ void HttpServer::listener_cb(struct evhttp_request *req, void *arg) { try { WebStatus xWebStatus = pNet->mFilter(pRequest); if (xWebStatus == WebStatus::WEB_IGNORE) { - + } else if (xWebStatus == WebStatus::WEB_RETURN) { return; } else { diff --git a/src/squick/plugin/net/i_net.h b/src/squick/plugin/net/i_net.h index 5fcac089..fb2e54cd 100644 --- a/src/squick/plugin/net/i_net.h +++ b/src/squick/plugin/net/i_net.h @@ -305,4 +305,6 @@ class INet { virtual bool IsServer() = 0; virtual bool Log(int severity, const char *msg) = 0; + + virtual int GetConnections() = 0; }; \ No newline at end of file diff --git a/src/squick/plugin/net/i_net_client_module.h b/src/squick/plugin/net/i_net_client_module.h index fb5604a5..2b08f09a 100644 --- a/src/squick/plugin/net/i_net_client_module.h +++ b/src/squick/plugin/net/i_net_client_module.h @@ -19,14 +19,14 @@ struct ConnectData { port = 0; name = ""; ip = ""; - type = ST_NONE; + type = 0; state = ConnectDataState::DISCONNECT; last_time = 0; buffer_size = 0; } int id; - ServerType type; + int type; std::string ip; int port; std::string name; @@ -53,7 +53,7 @@ class INetClientModule : public IModule { }; template - bool AddReceiveCallBack(const ServerType eType, const uint16_t msg_id, BaseType *pBase, + bool AddReceiveCallBack(const int eType, const uint16_t msg_id, BaseType *pBase, void (BaseType::*handleReceiver)(const socket_t, const int, const char *, const uint32_t)) { NET_RECEIVE_FUNCTOR functor = std::bind(handleReceiver, pBase, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4); @@ -65,7 +65,7 @@ class INetClientModule : public IModule { } template - int AddReceiveCallBack(const ServerType eType, BaseType *pBase, void (BaseType::*handleReceiver)(const socket_t, const int, const char *, const uint32_t)) { + int AddReceiveCallBack(const int eType, BaseType *pBase, void (BaseType::*handleReceiver)(const socket_t, const int, const char *, const uint32_t)) { NET_RECEIVE_FUNCTOR functor = std::bind(handleReceiver, pBase, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4); NET_RECEIVE_FUNCTOR_PTR functorPtr(new NET_RECEIVE_FUNCTOR(functor)); @@ -76,7 +76,7 @@ class INetClientModule : public IModule { } template - bool AddEventCallBack(const ServerType eType, BaseType *pBase, void (BaseType::*handler)(const socket_t, const SQUICK_NET_EVENT, INet *)) { + bool AddEventCallBack(const int eType, BaseType *pBase, void (BaseType::*handler)(const socket_t, const SQUICK_NET_EVENT, INet *)) { NET_EVENT_FUNCTOR functor = std::bind(handler, pBase, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3); NET_EVENT_FUNCTOR_PTR functorPtr(new NET_EVENT_FUNCTOR(functor)); @@ -85,11 +85,11 @@ class INetClientModule : public IModule { return true; } - virtual int AddReceiveCallBack(const ServerType eType, NET_RECEIVE_FUNCTOR_PTR functorPtr) = 0; - virtual int AddReceiveCallBack(const ServerType eType, const uint16_t msg_id, NET_RECEIVE_FUNCTOR_PTR functorPtr) = 0; - virtual int AddEventCallBack(const ServerType eType, NET_EVENT_FUNCTOR_PTR functorPtr) = 0; + virtual int AddReceiveCallBack(const int eType, NET_RECEIVE_FUNCTOR_PTR functorPtr) = 0; + virtual int AddReceiveCallBack(const int eType, const uint16_t msg_id, NET_RECEIVE_FUNCTOR_PTR functorPtr) = 0; + virtual int AddEventCallBack(const int eType, NET_EVENT_FUNCTOR_PTR functorPtr) = 0; - virtual void RemoveReceiveCallBack(const ServerType eType, const uint16_t msg_id) = 0; + virtual void RemoveReceiveCallBack(const int eType, const uint16_t msg_id) = 0; //////////////////////////////////////////////////////////////////////////////// virtual void AddNode(const ConnectData &xInfo) = 0; @@ -99,9 +99,9 @@ class INetClientModule : public IModule { virtual bool SendByID(const int serverID, const uint16_t msg_id, const std::string &strData, const uint64_t uid = 0, reqid_t req_id = 0) = 0; virtual bool SendPBByID(const int serverID, const uint16_t msg_id, const google::protobuf::Message &xData, const uint64_t uid = 0, reqid_t req_id = 0) = 0; virtual void SendToAllNode(const uint16_t msg_id, const std::string &strData, const uint64_t uid = 0) = 0; - virtual void SendToAllNodeByType(const ServerType eType, const uint16_t msg_id, const std::string &strData, const uint64_t uid = 0) = 0; + virtual void SendToAllNodeByType(const int eType, const uint16_t msg_id, const std::string &strData, const uint64_t uid = 0) = 0; virtual void SendPBToAllNode(const uint16_t msg_id, const google::protobuf::Message &xData, const uint64_t uid = 0) = 0; - virtual void SendPBToAllNodeByType(const ServerType eType, const uint16_t msg_id, const google::protobuf::Message &xData, const uint64_t uid = 0) = 0; + virtual void SendPBToAllNodeByType(const int eType, const uint16_t msg_id, const google::protobuf::Message &xData, const uint64_t uid = 0) = 0; //////////////////////////////////////////////////////////////////////////////// // coroutine virtual Awaitable Request(const int serverID, const uint16_t msg_id, const std::string &data, int ack_msg_id, @@ -110,7 +110,8 @@ class INetClientModule : public IModule { const uint64_t uid = 0) = 0; virtual MapEx &GetServerList() = 0; - virtual std::shared_ptr GetServerNetInfo(const ServerType eType) = 0; virtual std::shared_ptr GetServerNetInfo(const int serverID) = 0; virtual std::shared_ptr GetServerNetInfo(const INet *pNet) = 0; + + virtual int GetConnections() = 0; }; diff --git a/src/squick/plugin/net/i_net_module.h b/src/squick/plugin/net/i_net_module.h index 7ba5576d..d2f2350b 100644 --- a/src/squick/plugin/net/i_net_module.h +++ b/src/squick/plugin/net/i_net_module.h @@ -18,7 +18,7 @@ struct ServerInfo { ServerInfo() { fd = 0; - info = std::shared_ptr(new rpc::Server()); + info = std::shared_ptr(new rpc::Node()); } ~ServerInfo() { fd = 0; @@ -33,7 +33,7 @@ struct ServerInfo { }; Status status = ServerInfo::Status::Unknowing; socket_t fd; - std::shared_ptr info; + std::shared_ptr info; vector listen_types; }; diff --git a/src/squick/plugin/net/net.cc b/src/squick/plugin/net/net.cc index 065af283..52b0252d 100644 --- a/src/squick/plugin/net/net.cc +++ b/src/squick/plugin/net/net.cc @@ -516,6 +516,8 @@ bool Net::SendMsgToAllClient(const int16_t msg_id, const char *msg, const size_t return false; } +int Net::GetConnections() { return mmObject.size(); } + int Net::EnCode(const uint16_t umsg_id, const char *strData, const uint32_t unDataLen, std::string &strOutData) { rpcHead xHead; xHead.SetMsgID(umsg_id); diff --git a/src/squick/plugin/net/net.h b/src/squick/plugin/net/net.h index 1be65aec..b4c44f61 100644 --- a/src/squick/plugin/net/net.h +++ b/src/squick/plugin/net/net.h @@ -72,6 +72,8 @@ class Net : public INet { virtual bool IsServer() override; virtual bool Log(int severity, const char *msg) override; + virtual int GetConnections() override; + private: bool SendMsg(const int16_t msg_id, const char *msg, const size_t len, const std::list &fdList); bool SendData(const char *msg, const size_t len, const std::list &fdList); diff --git a/src/squick/plugin/net/net_client_module.cc b/src/squick/plugin/net/net_client_module.cc index 0ff2baa5..a997d4d9 100644 --- a/src/squick/plugin/net/net_client_module.cc +++ b/src/squick/plugin/net/net_client_module.cc @@ -10,9 +10,9 @@ NetClientModule::NetClientModule(IPluginManager *p) { bool NetClientModule::Start() { m_log_ = pm_->FindModule(); - for (int i = 0; i < ServerType::ST_MAX; ++i) { - INetClientModule::AddEventCallBack((ServerType)i, this, &NetClientModule::OnSocketEvent); - INetClientModule::AddReceiveCallBack((ServerType)i, this, &NetClientModule::OnAckHandler); + for (int i = 0; i < rpc::ST_MAX; ++i) { + INetClientModule::AddEventCallBack(i, this, &NetClientModule::OnSocketEvent); + INetClientModule::AddReceiveCallBack(i, this, &NetClientModule::OnAckHandler); } return true; } @@ -29,7 +29,7 @@ bool NetClientModule::Update() { return true; } -void NetClientModule::RemoveReceiveCallBack(const ServerType eType, const uint16_t msg_id) { +void NetClientModule::RemoveReceiveCallBack(const int eType, const uint16_t msg_id) { std::shared_ptr xCallBack = mxCallBack.GetElement(eType); if (xCallBack) { std::map>::iterator it = xCallBack->mxReceiveCallBack.find(msg_id); @@ -41,7 +41,7 @@ void NetClientModule::RemoveReceiveCallBack(const ServerType eType, const uint16 void NetClientModule::AddNode(const ConnectData &xInfo) { mxTempNetList.push_back(xInfo); } -int NetClientModule::AddReceiveCallBack(const ServerType eType, const uint16_t msg_id, NET_RECEIVE_FUNCTOR_PTR functorPtr) { +int NetClientModule::AddReceiveCallBack(const int eType, const uint16_t msg_id, NET_RECEIVE_FUNCTOR_PTR functorPtr) { std::shared_ptr xCallBack = mxCallBack.GetElement(eType); if (!xCallBack) { xCallBack = std::shared_ptr(new CallBack); @@ -61,7 +61,7 @@ int NetClientModule::AddReceiveCallBack(const ServerType eType, const uint16_t m return 0; } -int NetClientModule::AddReceiveCallBack(const ServerType eType, NET_RECEIVE_FUNCTOR_PTR functorPtr) { +int NetClientModule::AddReceiveCallBack(const int eType, NET_RECEIVE_FUNCTOR_PTR functorPtr) { std::shared_ptr xCallBack = mxCallBack.GetElement(eType); if (!xCallBack) { xCallBack = std::shared_ptr(new CallBack); @@ -73,7 +73,7 @@ int NetClientModule::AddReceiveCallBack(const ServerType eType, NET_RECEIVE_FUNC return 0; } -int NetClientModule::AddEventCallBack(const ServerType eType, NET_EVENT_FUNCTOR_PTR functorPtr) { +int NetClientModule::AddEventCallBack(const int eType, NET_EVENT_FUNCTOR_PTR functorPtr) { std::shared_ptr xCallBack = mxCallBack.GetElement(eType); if (!xCallBack) { xCallBack = std::shared_ptr(new CallBack); @@ -126,7 +126,7 @@ void NetClientModule::SendToAllNode(const uint16_t msg_id, const std::string &st } } -void NetClientModule::SendToAllNodeByType(const ServerType eType, const uint16_t msg_id, const std::string &strData, const uint64_t uid) { +void NetClientModule::SendToAllNodeByType(const int eType, const uint16_t msg_id, const std::string &strData, const uint64_t uid) { std::shared_ptr pServer = mxServerMap.First(); while (pServer) { std::shared_ptr pNetModule = pServer->net_module; @@ -171,7 +171,7 @@ void NetClientModule::SendPBToAllNode(const uint16_t msg_id, const google::proto } } -void NetClientModule::SendPBToAllNodeByType(const ServerType eType, const uint16_t msg_id, const google::protobuf::Message &xData, const uint64_t uid) { +void NetClientModule::SendPBToAllNodeByType(const int eType, const uint16_t msg_id, const google::protobuf::Message &xData, const uint64_t uid) { std::shared_ptr pServer = mxServerMap.First(); while (pServer) { std::shared_ptr pNetModule = pServer->net_module; @@ -264,15 +264,6 @@ void NetClientModule::OnAckHandler(const socket_t sock, const int msg_id, const return; } -std::shared_ptr NetClientModule::GetServerNetInfo(const ServerType eType) { - std::shared_ptr> xConnectDataMap = mxServerTypeMap.GetElement(eType); - if (xConnectDataMap) { - return xConnectDataMap->GetElementBySuitRandom(); - } - - return nullptr; -} - std::shared_ptr NetClientModule::GetServerNetInfo(const int serverID) { return mxServerMap.GetElement(serverID); } MapEx &NetClientModule::GetServerList() { return mxServerMap; } @@ -489,4 +480,6 @@ void NetClientModule::ProcessNetConnect() { mxTempNetList.clear(); } -} \ No newline at end of file +} + +int NetClientModule::GetConnections() { return mxServerTypeMap.Count(); } \ No newline at end of file diff --git a/src/squick/plugin/net/net_client_module.h b/src/squick/plugin/net/net_client_module.h index 82dcd4b5..447576e5 100644 --- a/src/squick/plugin/net/net_client_module.h +++ b/src/squick/plugin/net/net_client_module.h @@ -20,10 +20,10 @@ class NetClientModule : public INetClientModule { virtual bool Update(); virtual void AddNode(const ConnectData &xInfo); - virtual int AddReceiveCallBack(const ServerType eType, NET_RECEIVE_FUNCTOR_PTR functorPtr); - virtual int AddReceiveCallBack(const ServerType eType, const uint16_t msg_id, NET_RECEIVE_FUNCTOR_PTR functorPtr); - virtual int AddEventCallBack(const ServerType eType, NET_EVENT_FUNCTOR_PTR functorPtr); - virtual void RemoveReceiveCallBack(const ServerType eType, const uint16_t msg_id); + virtual int AddReceiveCallBack(const int eType, NET_RECEIVE_FUNCTOR_PTR functorPtr); + virtual int AddReceiveCallBack(const int eType, const uint16_t msg_id, NET_RECEIVE_FUNCTOR_PTR functorPtr); + virtual int AddEventCallBack(const int eType, NET_EVENT_FUNCTOR_PTR functorPtr); + virtual void RemoveReceiveCallBack(const int eType, const uint16_t msg_id); //////////////////////////////////////////////////////////////////////////////// virtual bool IsConnected(const int node_id) override; @@ -31,9 +31,9 @@ class NetClientModule : public INetClientModule { virtual bool SendPBByID(const int serverID, const uint16_t msg_id, const google::protobuf::Message &xData, const uint64_t uid = 0, reqid_t req_id = 0) override; virtual void SendToAllNode(const uint16_t msg_id, const std::string &strData, const uint64_t uid = 0) override; - virtual void SendToAllNodeByType(const ServerType eType, const uint16_t msg_id, const std::string &strData, const uint64_t uid = 0) override; + virtual void SendToAllNodeByType(const int eType, const uint16_t msg_id, const std::string &strData, const uint64_t uid = 0) override; virtual void SendPBToAllNode(const uint16_t msg_id, const google::protobuf::Message &xData, const uint64_t uid = 0) override; - virtual void SendPBToAllNodeByType(const ServerType eType, const uint16_t msg_id, const google::protobuf::Message &xData, const uint64_t uid = 0) override; + virtual void SendPBToAllNodeByType(const int eType, const uint16_t msg_id, const google::protobuf::Message &xData, const uint64_t uid = 0) override; // coroutine virtual Awaitable Request(const int node_id, const uint16_t msg_id, const std::string &data, int ack_msg_id, @@ -47,10 +47,11 @@ class NetClientModule : public INetClientModule { virtual MapEx &GetServerList(); - virtual std::shared_ptr GetServerNetInfo(const ServerType eType); virtual std::shared_ptr GetServerNetInfo(const int serverID); virtual std::shared_ptr GetServerNetInfo(const INet *pNet); + virtual int GetConnections() override; + protected: void StartCallBacks(std::shared_ptr pServerData); diff --git a/src/squick/plugin/net/pb_log_module.cc b/src/squick/plugin/net/pb_log_module.cc index b7e2acae..86879d46 100644 --- a/src/squick/plugin/net/pb_log_module.cc +++ b/src/squick/plugin/net/pb_log_module.cc @@ -66,7 +66,7 @@ void PbLogModule::Log(const std::string &prefix, const int msg_id, const char *d std::string msg_name = GetMessageNameByID(msg_id); bool is_node_msg = false; - if (pm_->GetAppType() != ServerType::ST_PROXY) { + if (pm_->GetAppType() != rpc::ST_PROXY) { // inner msg is_node_msg = true; } else if (msg_name.find("rpc.NR") == 0) { diff --git a/src/node/web/node/CMakeLists.txt b/src/squick/plugin/node/CMakeLists.txt similarity index 54% rename from src/node/web/node/CMakeLists.txt rename to src/squick/plugin/node/CMakeLists.txt index f757ca5d..77fa65f3 100644 --- a/src/node/web/node/CMakeLists.txt +++ b/src/squick/plugin/node/CMakeLists.txt @@ -1,6 +1,6 @@ -set(ProjectName "web_node") +set(ProjectName "core_node") file(GLOB soruces *.cc) file(GLOB headers *.h) add_library(${ProjectName} SHARED ${soruces} ${headers}) LinkSquick(${ProjectName}) -SquickOut(${ProjectName} "node/web" ${SQUICK_BUILD_PLUGIN_SERVER_PATH}/web "node") +SquickOut(${ProjectName} "squick/plugin" ${SQUICK_BUILD_PLUGIN_CORE_PATH} "node") \ No newline at end of file diff --git a/src/squick/plugin/node/export.h b/src/squick/plugin/node/export.h new file mode 100644 index 00000000..da7ba18e --- /dev/null +++ b/src/squick/plugin/node/export.h @@ -0,0 +1,7 @@ +// Author: i0gan +// Email : l418894113@gmail.com +// Date : 2024-04-27 +// Github: https://github.com/pwnsky/squick +// Description: Export current plugin's all modules in a single header file +#pragma once +#include "i_node_module.h" diff --git a/src/squick/plugin/node/i_node_module.h b/src/squick/plugin/node/i_node_module.h new file mode 100644 index 00000000..ee5a97c6 --- /dev/null +++ b/src/squick/plugin/node/i_node_module.h @@ -0,0 +1,14 @@ +// Author: i0gan +// Email : l418894113@gmail.com +// Date : 2024-04-27 +// Description: node plugin module +#pragma once +#include +#include +#include + +class INodeModule : public IModule { + public: + virtual ServerInfo &GetNodeInfo() = 0; + virtual bool AddSubscribeNode(const vector &types) = 0; +}; \ No newline at end of file diff --git a/src/squick/plugin/node/node_module.cc b/src/squick/plugin/node/node_module.cc new file mode 100644 index 00000000..9f8c8ccd --- /dev/null +++ b/src/squick/plugin/node/node_module.cc @@ -0,0 +1,291 @@ + +#include "node_module.h" + +NodeModule::NodeModule(IPluginManager *p) { + pm_ = p; + is_update_ = true; +} + +bool NodeModule::Awake() { return true; } + +bool NodeModule::Start() { + m_net_ = pm_->FindModule(); + m_log_ = pm_->FindModule(); + m_net_client_ = pm_->FindModule(); + is_update_ = true; + + pm_->SetAppType(StringNodeTypeToEnum(pm_->GetArg("type=", "proxy"))); + pm_->SetArea(pm_->GetArg("area=", 0)); + + Listen(); + return true; +} + +bool NodeModule::Update() { + + if (last_report_time_ + NODE_REPORT_TIME > pm_->GetNowTime()) { + return true; + } + if (last_report_time_ > 0) { + // calc work load + CalcWorkLoad(); + UpdateState(); + } + last_report_time_ = pm_->GetNowTime(); + return true; +} + +inline void NodeModule::CalcWorkLoad() { + // handle time * 100 + m_net connections + m_net_client connections + time_t now_time = SquickGetTimeMS(); + // The first update will int overflow, but who care? + workload_ = (now_time - last_update_time_) * 100; + last_update_time_ = now_time; +} + +std::string NodeModule::EnumNodeTypeToString(rpc::NodeType type) { + switch (type) { + case rpc::ST_MASTER: + return "master"; + case rpc::ST_LOGIN: + return "login"; + case rpc::ST_WORLD: + return "world"; + case rpc::ST_DB_PROXY: + return "db_proxy"; + case rpc::ST_PROXY: + return "proxy"; + case rpc::ST_PLAYER: + return "player"; + case rpc::ST_GAME_MGR: + return "game_mgr"; + case rpc::ST_GAME: + return "game"; + case rpc::ST_MICRO: + return "micro"; + case rpc::ST_CDN: + return "cdn"; + case rpc::ST_WEB: + return "web"; + } + return ""; +} + +rpc::NodeType NodeModule::StringNodeTypeToEnum(const std::string &type) { + if (type == "master") + return rpc::ST_MASTER; + else if (type == "login") + return rpc::ST_LOGIN; + else if (type == "world") + return rpc::ST_WORLD; + else if (type == "db_proxy") + return rpc::ST_DB_PROXY; + else if (type == "proxy") + return rpc::ST_PROXY; + else if (type == "player") + return rpc::ST_PLAYER; + else if (type == "game_mgr") + return rpc::ST_GAME_MGR; + else if (type == "game") + return rpc::ST_GAME; + else if (type == "micro") + return rpc::ST_MICRO; + else if (type == "cdn") + return rpc::ST_CDN; + else if (type == "web") + return rpc::ST_WEB; + return rpc::ST_NONE; +} + +bool NodeModule::Listen() { + m_net_->AddReceiveCallBack(this, &NodeModule::InvalidMessage); + m_net_->AddEventCallBack(this, &NodeModule::OnServerSocketEvent); + + node_info_.info->set_id(pm_->GetArg("id=", ARG_DEFAULT_ID)); + node_info_.info->set_type(pm_->GetAppType()); + node_info_.info->set_port(pm_->GetArg("port=", ARG_DEFAULT_PORT)); + + node_info_.info->set_name(pm_->GetAppName()); + node_info_.info->set_ip(pm_->GetArg("ip=", ARG_DEFAULT_IP)); + node_info_.info->set_public_ip(pm_->GetArg("public_ip=", ARG_DEFAULT_PUBLIC_IP)); + node_info_.info->set_area(pm_->GetArea()); + node_info_.info->set_update_time(SquickGetTimeS()); + node_info_.info->set_max_online(pm_->GetArg("max_conn=", ARG_DEFAULT_MAX_CONNECTION)); + node_info_.info->set_cpu_count(pm_->GetArg("cpu_count=", ARG_DEFAULT_CPU_COUNT)); + + int nRet = m_net_->Listen(node_info_.info->max_online(), node_info_.info->port(), node_info_.info->cpu_count(), + pm_->GetArg("net_server_buffer=", ARG_DEFAULT_NET_SERVER_BUFFER_SIZE)); + LOG_INFO("Node Listen at 0.0.0.0:%v Name :", node_info_.info->port(), node_info_.info->name()); + + if (nRet < 0) { + LOG_ERROR("Cannot init server net, Port<%v>", node_info_.info->port()); + SQUICK_ASSERT(nRet, "Cannot init server net", __FILE__, __FUNCTION__); + exit(0); + } + return true; +} + +// Add upper server +bool NodeModule::AddSubscribeNode(const vector &types) { + m_net_client_->AddEventCallBack(rpc::ST_MASTER, this, &NodeModule::OnClientSocketEvent); + m_net_client_->AddReceiveCallBack(rpc::ST_MASTER, rpc::IdNNtfNodeAdd, this, &NodeModule::OnNNtfNodeAdd); + m_net_client_->AddReceiveCallBack(rpc::ST_MASTER, rpc::IdNNtfNodeRemove, this, &NodeModule::OnNNtfNodeRemove); + m_net_client_->AddReceiveCallBack(rpc::ST_MASTER, rpc::IdNAckNodeRegister, this, &NodeModule::OnNAckNodeRegister); + bool ret = false; + node_info_.listen_types = types; + ConnectData s; + s.id = DEFAULT_MASTER_ID; + s.type = StringNodeTypeToEnum("master"); + s.ip = pm_->GetArg("master_ip=", "127.0.0.1"); + s.port = pm_->GetArg("master_port=", 10001); + s.name = "master"; + s.buffer_size = pm_->GetArg("net_client_buffer=", ARG_DEFAULT_NET_CLIENT_BUFFER_SIZE); + LOG_INFO("Node Connect to %v %v:%v cur_area<%v>", s.name, s.ip, s.port, pm_->GetArg("area=", 0)); + m_net_client_->AddNode(s); + return true; +} + +void NodeModule::InvalidMessage(const socket_t sock, const int msg_id, const char *msg, const uint32_t len) { printf("Net || umsg_id=%d\n", msg_id); } + +// Add upper server +void NodeModule::OnDynamicServerAdd(const socket_t sock, const int msg_id, const char *msg, const uint32_t len) { + uint64_t uid; + rpc::NNtfNodeAdd ntf; + if (!INetModule::ReceivePB(msg_id, msg, len, ntf, uid)) { + return; + } + for (int i = 0; i < ntf.node_list().size(); ++i) { + const rpc::Node &sd = ntf.node_list(i); + // type + ConnectData s; + s.id = sd.id(); + s.ip = sd.ip(); + s.port = sd.port(); + s.name = sd.name(); + s.type = sd.type(); + m_net_client_->AddNode(s); + } +} + +void NodeModule::OnReloadConfig(const socket_t sock, const int msg_id, const char *msg, const uint32_t len) {} + +void NodeModule::OnReloadLua(const socket_t sock, const int msg_id, const char *msg, const uint32_t len) {} + +// Report to upper server +void NodeModule::UpdateState() { + node_info_.info->set_update_time(SquickGetTimeS()); + node_info_.info->set_workload(workload_); + node_info_.info->set_connections(m_net_->GetNet()->GetConnections()); + node_info_.info->set_net_client_connections(m_net_client_->GetConnections()); + // Update status to master + if (pm_->GetAppType() != rpc::ST_MASTER) { + rpc::NNtfNodeReport req; + req.set_id(pm_->GetAppID()); + auto s = req.add_list(); + *s = *node_info_.info.get(); + m_net_client_->SendPBByID(DEFAULT_MASTER_ID, rpc::IdNNtfNodeReport, req); + } +} + +// 作为服务的监听socket状态事件 +void NodeModule::OnServerSocketEvent(const socket_t sock, const SQUICK_NET_EVENT eEvent, INet *pNet) { + if (eEvent & SQUICK_NET_EVENT_EOF) { + LOG_INFO("Net Server: SQUICK_NET_EVENT_EOF Connection closed, sock<%v>", sock); + OnClientDisconnected(sock); + } else if (eEvent & SQUICK_NET_EVENT_ERROR) { + LOG_INFO("Net Server: SQUICK_NET_EVENT_ERROR Got an error on the connection, sock<%v>", sock); + OnClientDisconnected(sock); + } else if (eEvent & SQUICK_NET_EVENT_TIMEOUT) { + LOG_INFO("Net Server: SQUICK_NET_EVENT_TIMEOUT read timeout, sock<%v>", sock); + OnClientDisconnected(sock); + } else if (eEvent & SQUICK_NET_EVENT_CONNECTED) { + LOG_INFO("Net Server: SQUICK_NET_EVENT_CONNECTED connected success, sock<%v>", sock); + OnClientConnected(sock); + } +} + +void NodeModule::OnClientConnected(socket_t sock){}; +void NodeModule::OnClientDisconnected(socket_t sock){}; + +// 作为客户端连接socket事件 +void NodeModule::OnClientSocketEvent(const socket_t sock, const SQUICK_NET_EVENT eEvent, INet *pNet) { + if (eEvent & SQUICK_NET_EVENT_EOF) { + LOG_WARN("Net Client: SQUICK_NET_EVENT_EOF, sock<%v>", sock); + } else if (eEvent & SQUICK_NET_EVENT_ERROR) { + LOG_ERROR("Net Client: SQUICK_NET_EVENT_ERROR, sock<%v>", sock); + } else if (eEvent & SQUICK_NET_EVENT_TIMEOUT) { + LOG_ERROR("Net Client: SQUICK_NET_EVENT_TIMEOUT, sock<%v>", sock); + } else if (eEvent & SQUICK_NET_EVENT_CONNECTED) { + LOG_INFO("Net Client: SQUICK_NET_EVENT_CONNECTED connected success, sock<%v>", sock); + OnUpperNodeConnected(pNet); + } +} + +void NodeModule::OnUpperNodeConnected(INet *pNet) { + std::shared_ptr ts = m_net_client_->GetServerNetInfo(pNet); + if (ts == nullptr) { + ostringstream msg; + LOG_ERROR("OnUpperNodeConnected: %v", "Cannot find server info"); + return; + } + ts->state = ConnectDataState::NORMAL; + + // target type only master can register + if (ts->type != rpc::ST_MASTER) + return; + + rpc::NReqNodeRegister req; + *req.mutable_node() = *node_info_.info.get(); + + for (auto type : node_info_.listen_types) { + req.add_listen_type_list(type); + } + + m_net_client_->SendPBByID(ts->id, rpc::IdNReqNodeRegister, req); + LOG_INFO("Register node <%v>", ts->name); +} + +void NodeModule::OnNAckNodeRegister(const socket_t sock, const int msg_id, const char *msg, const uint32_t len) { + uint64_t uid; + rpc::NAckNodeRegister ack; + if (!m_net_->ReceivePB(msg_id, msg, len, ack, uid)) { + return; + } + + if (ack.code() == 0) { + AddNodes(ack.node_add_list()); + } else { + LOG_ERROR("Node Register faild! sock<%v>", sock); + } +} + +// Add node ntf +void NodeModule::OnNNtfNodeAdd(const socket_t sock, const int msg_id, const char *msg, const uint32_t len) { + uint64_t uid; + rpc::NNtfNodeAdd ntf; + if (!m_net_->ReceivePB(msg_id, msg, len, ntf, uid)) { + return; + } + AddNodes(ntf.node_list(), true); +} + +bool NodeModule::AddNodes(const google::protobuf::RepeatedPtrField &list, bool from_ntf) { + for (const auto &n : list) { + LOG_INFO("Add node from master, is_ntf<%v> added:", from_ntf, n.name()); + ConnectData s; + s.id = n.id(); + s.ip = n.ip(); + s.port = n.port(); + s.name = n.name(); + s.type = n.type(); + m_net_client_->AddNode(s); + } + return true; +} + +// Add node ntf +void NodeModule::OnNNtfNodeRemove(const socket_t sock, const int msg_id, const char *msg, const uint32_t len) {} + +bool NodeModule::RemoveNodes() { return true; } + +ServerInfo &NodeModule::GetNodeInfo() { return node_info_; } \ No newline at end of file diff --git a/src/squick/plugin/node/node_module.h b/src/squick/plugin/node/node_module.h new file mode 100644 index 00000000..65453cd4 --- /dev/null +++ b/src/squick/plugin/node/node_module.h @@ -0,0 +1,68 @@ +#pragma once + +#include "i_node_module.h" +#include +#include +#include +#include + +#define NODE_REPORT_TIME 10 +class NodeModule : public INodeModule { + public: + NodeModule(IPluginManager *p); + virtual bool Awake() override; + virtual bool Start() override; + virtual bool Update() override; + + inline void CalcWorkLoad(); + static std::string EnumNodeTypeToString(rpc::NodeType type); + static rpc::NodeType StringNodeTypeToEnum(const std::string &type); + + virtual ServerInfo &GetNodeInfo() override; + + // Add upper server + virtual bool AddSubscribeNode(const vector &types) override; + void InvalidMessage(const socket_t sock, const int msg_id, const char *msg, const uint32_t len); + + // Add upper server + void OnDynamicServerAdd(const socket_t sock, const int msg_id, const char *msg, const uint32_t len); + + private: + bool Listen(); + virtual void OnReloadConfig(const socket_t sock, const int msg_id, const char *msg, const uint32_t len); + virtual void OnReloadLua(const socket_t sock, const int msg_id, const char *msg, const uint32_t len); + + // Report to upper server + void UpdateState(); + + // 作为服务的监听socket状态事件 + void OnServerSocketEvent(const socket_t sock, const SQUICK_NET_EVENT eEvent, INet *pNet); + virtual void OnClientConnected(socket_t sock); + virtual void OnClientDisconnected(socket_t sock); + + // 作为客户端连接socket事件 + void OnClientSocketEvent(const socket_t sock, const SQUICK_NET_EVENT eEvent, INet *pNet); + void OnUpperNodeConnected(INet *pNet); + void OnNAckNodeRegister(const socket_t sock, const int msg_id, const char *msg, const uint32_t len); + + // Add node ntf + void OnNNtfNodeAdd(const socket_t sock, const int msg_id, const char *msg, const uint32_t len); + bool AddNodes(const google::protobuf::RepeatedPtrField &list, bool from_ntf = false); + + // Add node ntf + void OnNNtfNodeRemove(const socket_t sock, const int msg_id, const char *msg, const uint32_t len); + bool RemoveNodes(); + + public: + ILogModule *m_log_; + INetModule *m_net_; + INetClientModule *m_net_client_; + ServerInfo node_info_; + + private: + time_t last_report_time_ = 0; + time_t last_update_time_ = 0; + int workload_; + + protected: +}; diff --git a/src/node/web/node/plugin.cc b/src/squick/plugin/node/plugin.cc similarity index 89% rename from src/node/web/node/plugin.cc rename to src/squick/plugin/node/plugin.cc index cf5c6e47..9509c584 100644 --- a/src/node/web/node/plugin.cc +++ b/src/squick/plugin/node/plugin.cc @@ -1,9 +1,9 @@ + + #include "plugin.h" #include "node_module.h" -namespace web::node { SQUICK_EXPORT void SquickPluginLoad(IPluginManager *pm){CREATE_PLUGIN(pm, Plugin)}; - SQUICK_EXPORT void SquickPluginUnload(IPluginManager *pm){DESTROY_PLUGIN(pm, Plugin)}; const int Plugin::GetPluginVersion() { return 0; } @@ -12,6 +12,4 @@ const std::string Plugin::GetPluginName() { return GET_CLASS_NAME(Plugin); } void Plugin::Install() { REGISTER_MODULE(pm_, INodeModule, NodeModule) } -void Plugin::Uninstall() { UNREGISTER_MODULE(pm_, INodeModule, NodeModule) } - -} // namespace web::node +void Plugin::Uninstall() { UNREGISTER_MODULE(pm_, INodeModule, NodeModule) } \ No newline at end of file diff --git a/src/node/db_proxy/node/plugin.h b/src/squick/plugin/node/plugin.h similarity index 83% rename from src/node/db_proxy/node/plugin.h rename to src/squick/plugin/node/plugin.h index 1fdbc5ed..eb2878d8 100644 --- a/src/node/db_proxy/node/plugin.h +++ b/src/squick/plugin/node/plugin.h @@ -1,7 +1,6 @@ #include #include -namespace db_proxy::node { class Plugin : public IPlugin { public: Plugin(IPluginManager *p) { pm_ = p; } @@ -9,6 +8,4 @@ class Plugin : public IPlugin { virtual const std::string GetPluginName(); virtual void Install(); virtual void Uninstall(); -}; - -} // namespace db_proxy::node \ No newline at end of file +}; \ No newline at end of file diff --git a/src/tools/sqkcli/core/tester_module.cc b/src/tools/sqkcli/core/tester_module.cc index 46463f96..d8c6275e 100644 --- a/src/tools/sqkcli/core/tester_module.cc +++ b/src/tools/sqkcli/core/tester_module.cc @@ -37,12 +37,12 @@ bool TesterModule::AfterStart() { void TesterModule::TestProxyTransferSpeed_Init() { std::cout << "Test proxy Transfer speed!\n"; - m_net_client_->AddReceiveCallBack(ServerType::ST_PROXY, rpc::TestRPC::ACK_TEST_PROXY, this, &TesterModule::TestProxyTransferSpeed_Ack); - m_net_client_->AddEventCallBack(ServerType::ST_PROXY, this, &TesterModule::OnClientSocketEvent); + m_net_client_->AddReceiveCallBack(rpc::ST_PROXY, rpc::TestRPC::ACK_TEST_PROXY, this, &TesterModule::TestProxyTransferSpeed_Ack); + m_net_client_->AddEventCallBack(rpc::ST_PROXY, this, &TesterModule::OnClientSocketEvent); ConnectData s; s.id = 1; // Just for test - s.type = ServerType::ST_PROXY; + s.type = rpc::ST_PROXY; s.ip = pm_->GetArg("ip=", "127.0.0.1"); s.port = pm_->GetArg("port=", 10501); s.name = "test"; @@ -55,7 +55,7 @@ void TesterModule::TestProxyTransferSpeed_Req() { test.set_index(test_req_index_); test.set_data(test_req_data_); test.set_req_time(SquickGetTimeMSEx()); - m_net_client_->SendPBToAllNodeByType(ServerType::ST_PROXY, rpc::TestRPC::REQ_TEST_PROXY, test, 0); + m_net_client_->SendPBToAllNodeByType(rpc::ST_PROXY, rpc::TestRPC::REQ_TEST_PROXY, test, 0); test_req_index_++; test_req_data_ = "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"; } diff --git a/src/tutorial/t6_rpc/rpc_module.cc b/src/tutorial/t6_rpc/rpc_module.cc index 5a0504fe..1d39a3dd 100644 --- a/src/tutorial/t6_rpc/rpc_module.cc +++ b/src/tutorial/t6_rpc/rpc_module.cc @@ -3,7 +3,7 @@ namespace tutorial { bool SimpleModule::Start() { m_net_client_ = pm_->FindModule(); - m_net_client_->AddReceiveCallBack(ServerType::ST_GAME, 1123, this, &SimpleModule::OnRecivedMsg); + m_net_client_->AddReceiveCallBack(rpc::ST_GAME, 1123, this, &SimpleModule::OnRecivedMsg); dout << "RPC Start ...\n"; return true; }