Skip to content

Commit

Permalink
Merge pull request #22 from pwnsky/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
i0gan authored Sep 29, 2023
2 parents d7f08ac + 650dda7 commit 84435bd
Show file tree
Hide file tree
Showing 38 changed files with 546 additions and 538 deletions.
38 changes: 19 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,26 @@
## 设计目标

1. K8s集群【10%】
2. 同区节点所有节点负载状态自同步【80%】
3. 提供稳定服务、保证服务的高可用 【10%】
4. 优化超低的CPU、内存、网络的占用【20%】
5. 拥有规范的测试,能够让开发人员自测各种性能指标(CPU、内存、网络)【10%】
6. 插件模块组合,让开发功能变成开发拓展的插件模块 【100%】
7. 解耦框架代码与业务项目代码【60%】
8. 日志系统,能够将日志异步写入到文件里,且在 Linux 环境下程序崩溃时,自动 Dump Stack 调用链写入崩溃文件 【100%】
9. 分布式服务,各服务之间通过网络来进行沟通,可通过分布式+集群方式减轻服务器压力 【50%】
10. 支持业务代码的热更新 【50%】
11. 支持跨平台,可以在 Windows、Linux 上编译和开发,在 Windows 上可以支持 VS 进行项目开发【100%】
12. 支持 1 变 N 或 N 变 1,一个物理机上单个进程启动全部服务器,方便开发调试。不同物理机上启动单个或多个服务器 【100%】
13. 拥有比较完整的自动化工具,如自动化代码生成、编译、打包、部署、清理等等 【80%】
14. 支持主流的协议,如 HTTP/HTTPS、Websocket、TCP、UDP、KCP 等等 【60%】
15. 为了保证包的正确处理与不丢失,基于 TCP 之上,自定义双向 RPC 协议 【100%】
16. 可视化的抓协议包工具【0%】
17. 远程调试控制台【0%】
18. 数据库Lua接口提供【0%】
19. 统一的数据库代理存取数据命令,使用简单协议能够便捷切换使用Mysql、Redis、MongoDb数据库 【10%】
2. 使用Lua来开发业务代码IO密集型,C++开发CPU密集型服务,可用于开发战斗服。
3. 同区节点所有节点负载状态自同步【80%】
4. 提供稳定服务、保证服务的高可用 【10%】
5. 优化超低的CPU、内存、网络的占用【20%】
6. 拥有规范的测试,能够让开发人员自测各种性能指标(CPU、内存、网络)【10%】
7. 插件模块组合,让开发功能变成开发拓展的插件模块 【100%】
8. 解耦框架代码与业务项目代码【60%】
9. 日志系统,能够将日志异步写入到文件里,且在 Linux 环境下程序崩溃时,自动 Dump Stack 调用链写入崩溃文件 【100%】
10. 分布式服务,各服务之间通过网络来进行沟通,可通过分布式+集群方式减轻服务器压力 【50%】
11. 支持业务代码的热更新 【50%】
12. 支持跨平台,可以在 Windows、Linux 上编译和开发,在 Windows 上可以支持 VS 进行项目开发【100%】
13. 支持 1 变 N 或 N 变 1,一个物理机上单个进程启动全部服务器,方便开发调试。不同物理机上启动单个或多个服务器 【100%】
14. 拥有比较完整的自动化工具,如自动化代码生成、编译、打包、部署、清理等等 【80%】
15. 支持主流的协议,如 HTTP/HTTPS、Websocket、TCP、UDP、KCP 等等 【60%】
16. 为了保证包的正确处理与不丢失,基于 TCP 之上,自定义双向 RPC 协议 【100%】
17. 可视化的抓协议包工具【0%】
18. 远程调试控制台【0%】
19. 数据库代理Lua接口提供【50%】
20. Unity、UnrealEngine客户端SDK完善【50%】
21. 高可用-数据库代理服务器,支持主流的 Mysql、MongoDb、Redis 、Clickhouse等数据库,采用proto协议进行交互,Lua协程异步读取【30%】
21. 高可用-数据库代理服务器,支持主流的 MongoDb、Mysql、Redis 、Clickhouse等数据库,采用proto协议进行交互,Lua协程异步读取【40%】

## 测试

Expand Down
1 change: 0 additions & 1 deletion src/lua/common/cycel_reset.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
-- Author: i0gan
-- Email : [email protected]
-- Date : 2022-12-03
-- Github: https://github.com/i0gan/Squick
-- Description: 数据重置计时器,支持 每天初重置、每月初重置、每年初重置。
-----------------------------------------------------------------------------

Expand Down
3 changes: 2 additions & 1 deletion src/lua/common/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ require "common.dump"
require "common.cycel_reset"
require "common.module_mgr"
require "common.module"
require "common.net"
require "common.net"
require "common.query_async"
2 changes: 1 addition & 1 deletion src/lua/common/module_mgr.lua
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ end

function ModuleMgr:Update()
for key, value in pairs(self.modules) do
value:Update()
value:Update()
end
end

Expand Down
3 changes: 3 additions & 0 deletions src/lua/common/net.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ function Net:SendToPlayer(msg_id)
end

function Net:SendToServer(server_id, msg_id, msg_data, guid)
if(guid == nil) then
guid = ""
end
Squick:SendToServerByServerID(server_id, msg_id, msg_data, guid)
end

Expand Down
1 change: 0 additions & 1 deletion src/lua/common/object.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
-- Author: i0gan
-- Email : [email protected]
-- Date : 2022-12-03
-- Github: https://github.com/pwnsky/squick
-- Description: 提供面向对象的支持
-----------------------------------------------------------------------------

Expand Down
1 change: 0 additions & 1 deletion src/lua/common/print_table.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
-- Author: i0gan
-- Email : [email protected]
-- Date : 2022-12-04
-- Github: https://github.com/i0gan/Squick
-- Description: 打印table
-----------------------------------------------------------------------------

Expand Down
33 changes: 33 additions & 0 deletions src/lua/common/query_async.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
-----------------------------------------------------------------------------
-- Author: i0gan
-- Email : [email protected]
-- Date : 2023-09-29
-- Description: Query async
-----------------------------------------------------------------------------

QueryAsync = {
query_id = 0;
co = {}
}

function QueryAsync:QueryInit()
self.query_id = self.query_id + 1 % 1000000
return self.query_id
end

function QueryAsync:QueryAwait(query_id)
self.co[query_id] = coroutine.running()
return coroutine.yield()
end

function QueryAsync:QueryClean(query_id)
self.co[query_id] = nil
end

function QueryAsync:QueryResume(query_id, data)
local co = self.co[query_id]
local status, err = coroutine.resume(co, data)
if(err) then
print("Error: ",status, err)
end
end
57 changes: 57 additions & 0 deletions src/lua/lib/db/mongo.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
-----------------------------------------------------------------------------
-- Author: i0gan
-- Email : [email protected]
-- Date : 2023-09-29
-- Description: async mongo cli
-----------------------------------------------------------------------------

local DbProxyID = 300;
Mongo = Mongo and Mongo or QueryAsync


function Mongo:Bind()
Net:ClientRegister(ServerType.ST_DB_PROXY, DbProxyRPC.ACK_MONGO_INSERT, self, self.AckInsert)
Net:ClientRegister(ServerType.ST_DB_PROXY, DbProxyRPC.ACK_MONGO_FIND, self, self.AckFind)
end

function Mongo:InsertAsync(db, collection, insert_json)
local query_id = self:QueryInit()
local req = {
query_id = query_id,
db = db,
collection = collection,
insert_json = insert_json,
}
Net:SendToServer(DbProxyID, DbProxyRPC.REQ_MONGO_INSERT, Squick:Encode("rpc.ReqMongoInsert", req))
local data = self:QueryAwait(query_id)
self:QueryClean(query_id)
return data.code
end

function Mongo:AckInsert(guid, msg_data, msg_id, fd)
local data = Squick:Decode("rpc.AckMongoInsert", msg_data);
self:QueryResume(data.query_id, data)
end

function Mongo:FindAsync(db, collection, condition_json)
local query_id = self:QueryInit()
local req = {
query_id = query_id,
db = db,
collection = collection,
condition_json = condition_json,
}
Net:SendToServer(DbProxyID, DbProxyRPC.REQ_MONGO_FIND, Squick:Encode("rpc.ReqMongoFind", req))
local data = self:QueryAwait(query_id)
self:QueryClean(query_id)
return data.result_json
end

function Mongo:AckFind(guid, msg_data, msg_id, fd)
local data = Squick:Decode("rpc.AckMongoFind", msg_data);
print("Find ack")
PrintTable(data)
self:QueryResume(data.query_id, data)
end

Mongo:Bind()
57 changes: 33 additions & 24 deletions src/lua/lib/db/redis.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,54 @@
-- Author: i0gan
-- Email : [email protected]
-- Date : 2023-09-24
-- Github: https://github.com/pwnsky/squick
-- Description: async redis cli
-----------------------------------------------------------------------------

RedisServerID = 300;

Redis = {
query_id = 0;
co = {}
}

function Blank()

end
local DbProxyID = 300;
Redis = Redis and Redis or QueryAsync

function Redis:Bind()
Net:ClientRegister(ServerType.ST_DB_PROXY, DbProxyRPC.ACK_REDIS_GET, self, self.AckGet)
Net:ClientRegister(ServerType.ST_DB_PROXY, DbProxyRPC.ACK_REDIS_GET, self, self.AckGetString)
Net:ClientRegister(ServerType.ST_DB_PROXY, DbProxyRPC.ACK_REDIS_SET, self, self.AckSetString)
end

function Redis:Get(co, key)
function Redis:GetStringAsync(key)
local query_id = self:QueryInit()
local req = {
query_id = self.query_id,
query_id = query_id,
key = key,
}
self.co[self.query_id] = co;
Net:SendToServer(RedisServerID, DbProxyRPC.REQ_REDIS_GET, Squick:Encode("rpc.ReqRedisGet", req), "lobby")
local result = coroutine.yield()
self.query_id = self.query_id + 1 % 1000000;
return result.value
Net:SendToServer(DbProxyID, DbProxyRPC.REQ_REDIS_GET, Squick:Encode("rpc.ReqRedisGet", req))
local data = self:QueryAwait(query_id)
self:QueryClean(query_id)
return data.value
end

function Redis:AckGet(guid, msg_data, msg_id, fd)
function Redis:AckGetString(guid, msg_data, msg_id, fd)
local data = Squick:Decode("rpc.AckRedisGet", msg_data);
local co = self.co[data.query_id]
coroutine.resume(co, data)
self:QueryResume(data.query_id, data)
end

function Redis:SetStringAsync(key, value, ttl)
local query_id = self:QueryInit()
if(ttl == nil) then
ttl = 0
end
local req = {
query_id = query_id,
key = key,
value = value,
ttl = ttl,
}
Net:SendToServer(DbProxyID, DbProxyRPC.REQ_REDIS_SET, Squick:Encode("rpc.ReqRedisSet", req))
local data = self:QueryAwait(query_id)
self:QueryClean(query_id)
return data.code
end

function Redis:Set()

function Redis:AckSetString(guid, msg_data, msg_id, fd)
local data = Squick:Decode("rpc.AckRedisSet", msg_data);
self:QueryResume(data.query_id, data)
end

Redis:Bind();
1 change: 1 addition & 0 deletions src/lua/lib/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
-- Date : 2023-09-24
-- Description: lib init
-----------------------------------------------------------------------------
Json = require "lib.json.json"
require "lib.db.init"
25 changes: 15 additions & 10 deletions src/lua/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Env = {
app_id = -1,
app_type = '',
app_name = '',
start_time = 0,
area = 0,
}

function Require(file)
Expand All @@ -22,9 +24,10 @@ function Main(context)
package.path = path .. '/?.lua;'

Squick:LogInfo("lua module execute");
Env.app_id = Squick:AppID();
Env.app_type= Squick:AppType();
Env.app_name= Squick:AppName();
Env.app_id = Squick:AppID()
Env.app_type= Squick:AppType()
Env.app_name= Squick:AppName()
Env.area = Squick:Area()
end


Expand All @@ -34,7 +37,7 @@ function Awake()
end

function Start()

Env.start_time = os.time()
end

function AfterStart()
Expand All @@ -47,6 +50,7 @@ function ReadyUpdate()
ModuleMgr:Start()
end

-- Update for per second
function Update()
ModuleMgr:Update()
end
Expand All @@ -64,16 +68,17 @@ function HotReload()
end

function Load()
Require("common.init");
Require("proto.init");
Require("test.init");
Require("lib.init");
Require("common.init")
Require("proto.init")
Require("lib.init")

local node_init = {
[ServerType.ST_GAME ] = function()
Require("node.game.init");
Require("node.game.init")
end,
[ServerType.ST_LOBBY] = function ()
Require("node.lobby.init");
Require("node.lobby.init")
--Require("test.init")
end
}
if(node_init[Env.app_type]) then
Expand Down
4 changes: 3 additions & 1 deletion src/lua/node/lobby/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ local modules = {
{ name = "player_mgr", is_hot = true, },
}

ModuleMgr:Register("node.lobby", modules)
ModuleMgr:Register("node.lobby", modules)

MONGO_PLAYERS_DB = "players_" .. Env.area
Loading

0 comments on commit 84435bd

Please sign in to comment.