Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
zhanjunxiong committed Jul 24, 2017
1 parent bb9864c commit 0eca658
Show file tree
Hide file tree
Showing 30 changed files with 847 additions and 118 deletions.
2 changes: 2 additions & 0 deletions etc/config
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ cpath = skynet_root.."cservice/?.so;"..app_root.."cservice/?.so"
if $DAEMON then
daemon = app_root.."run/skynet.pid"
end


35 changes: 35 additions & 0 deletions etc/config2
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
app_root = "$ROOT/"
skynet_root = "$SKYNET_ROOT/"

logpath = app_root.."run/"

thread = 4
harbor = 0
start = "main"
bootstrap = "snlua bootstrap"

runconfig = "runconfig"

nodename = "node2"

--logger = "logd"
--logservice = "snlua"
--logfilename = "logtest"
--logfilemaxline = 2



luaservice = skynet_root.."service/?.lua;"..app_root.."service/?.lua;"
lualoader = skynet_root.."lualib/loader.lua"

lua_path = skynet_root.."lualib/?.lua;"..skynet_root.."lualib/compat10/?.lua;"..skynet_root.."lualib/?/init.lua;"..app_root.."etc/?.lua;"..app_root.."lualib/?.lua;"..app_root.."config/?.lua;"

lua_cpath = skynet_root.."luaclib/?.so;"..app_root.."luaclib/?.so"
cpath = skynet_root.."cservice/?.so;"..app_root.."cservice/?.so"


if $DAEMON then
daemon = app_root.."run/skynet.pid"
end


76 changes: 49 additions & 27 deletions etc/runconfig.lua
Original file line number Diff line number Diff line change
@@ -1,32 +1,54 @@
return {
TEST = true,
version = "1.0.0",

node1 = {
-- 当前节点控制台监听端口
debug_console_port = 8701,
-- agent 池配置
agentpool = {
name = "agent", -- 要启动缓存的 agent 文件名
maxmun = 2, -- 池的最大容量
recyremove = 0, -- 如果池的最大容量都用完之后, 后续扩展的容量在回收时是否删除,0:不删除 1:删除
},

db = {
db_type = "mongodb",
host = "127.0.0.1",
db_name = "test",
},


centerd_num = 2,
dbproxy_num = 2,
login_num = 2,

watchdog = {
port = 8799,
maxclient = 1024,
nodelay = true,
}

--集群地址配置
cluster = {
node1 = "127.0.0.1:2528",
node2 = "127.0.0.1:2529",
},

--各个服务配置
service = {
--debug_console服务
--每个节点都需要配置一个
debug_console = {
[1] = {port=8701, node = "node1"},
[2] = {port=8702, node = "node2"},
},

--watchdog服务
--每个节点都需要配置一个
watchdog_common = {maxclient = 1024, nodelay = true},
watchdog = {
[1] = {port = 8798, node = "node2"},
[2] = {port = 8799, node = "node1"},
},
--center服务
center = {
[1] = {node = "node1"},
[2] = {node = "node1"},
},
--login服务
login = {
[1] = {node = "node1"},
[2] = {node = "node1"},
},
--dbproxy服务
dbproxy_common = {db_type = "mongodb",host = "127.0.0.1",db_name = "test"},
dbproxy = {
[1] = {node = "node1"},
[2] = {node = "node1"},
},
--agentpool服务
--每个有agent的节点需要开启
--agentpool_common.name 要启动缓存的 agent 文件名
--agentpool_common.maxmun 池的最大容量
--agentpool_common.recyremove 如果池的最大容量都用完之后, 后续扩展的容量在回收时是否删除,0:不删除 1:删除
agentpool_common = {name = "wsagent", maxnum = 10, recyremove = 2, brokecachelen=10},
agentpool = {
[1] = {node = "node2"},
[2] = {node = "node1"},
},
},
}
3 changes: 3 additions & 0 deletions lualib/agent/agent_init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ local skynet = require "skynet"
local log = require "log"
local env = require "env"

local reload = require "reload"

require "agent.agent_login"
--reload.loadmod("agent.agent_room")
require "agent.agent_room"

19 changes: 14 additions & 5 deletions lualib/agent/agent_login.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ local function get_init_data(cname)
end

local function load_data(cname, uid)
local ret = libdbproxy.findOne(uid, cname)
log.debug("cname: " .. cname .. " uid:" .. uid .. " ret: " .. tool.dump(ret))

local ret = libdbproxy.findOne(uid, cname, {uid=uid})
log.debug("===load_data cname: " .. cname .. " uid:" .. uid .. " ret: " .. tool.dump(ret))
ret = ret or get_init_data(cname)
ret.dirty = ret.dirty or true
ret.uid = uid
setmetatable(ret, {
__newindex = function(t, k, v)
t.dirty = true
Expand All @@ -46,18 +48,23 @@ end


local function save_data()

local data = player.data
data.player.name = "ada"

for k, v in pairs(data) do
if v.dirty then
v.dirty = nil

libdbproxy.update(player.uid, k, v)
local select = {uid=player.uid}
libdbproxy.update(player.uid, k, select, v, true)
end
end
end

function env.login(account)

log.debug("=== test login ===")

-- 从数据库里加载数据
player = {
id = skynet.self(),
Expand All @@ -66,6 +73,8 @@ function env.login(account)
}

player.data = load_all_data()
--初始化数据
player.data.player.login_time = os.time()
end


Expand Down
48 changes: 48 additions & 0 deletions lualib/agent/agent_room.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local skynet = require "skynet"
local log = require "log"
local env = require "env"
local libcenter = require "libcenter"

local M = env.dispatch

Expand All @@ -16,4 +17,51 @@ end



--示例1 echo
function M.echo(msg)
local cmd = msg.cmd
local str = msg.str
skynet.error("agent echo ! "..cmd.." "..str)
return msg
end

--示例2 name
function M.set_name(msg)
local cmd = msg.cmd
local str = msg.str
local playerdata = env.get_playerdata()

skynet.error("name "..cmd.." "..(playerdata.player.name or "none"))
skynet.error("set_name "..cmd.." "..str)
skynet.error("login_time "..cmd.." "..playerdata.player.login_time)

playerdata.player.name = str

--msg.str="succ"
return msg
end



--示例3 chat
function env.dispatch.chat(msg)
local cmd = msg.cmd
local str = msg.str
libcenter.broadcast(env.get_player().uid, "broadcast_msg", msg)
skynet.error("===agent chat 666! "..cmd.." "..str)


return nil
end

--示例4 测试热更
local reload = require "reload"

function M.chatreload(msg)
local cmd = msg.cmd
local str = msg.str
--注意agent_init中require的形式
--这种热更只能更新本服
reload.loadmod("agent.agent_room")
return nil
end
67 changes: 67 additions & 0 deletions lualib/center.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ local env = require "env"
local M = env.dispatch

local users = {}
--users[uid]={
-- watchdog
-- fd
-- agent (register_agent之后)
-- node (register_agent之后)
--}

function M.login(uid, data)
local user = users[uid]
if not user then
Expand All @@ -26,3 +33,63 @@ function M.logout(uid)
users[uid] = nil
end

--register_agent
function M.register_agent(uid, data)
if not users[uid] then
return false
end
log.debug("center register_agent: " .. uid.." "..data.agent)
users[uid].agent = data.agent
users[uid].node = data.node
end

--broadcast msg to all player
function M.broadcast_msg(uid, data)
for uid, uid_data in pairs(users) do
log.debug("center broadcast_msg send to: " .. uid)
M.send_agent(uid, "send", data)
end
end

--发送
function M.send(node, adress, cmd, data)
if node == skynet.getenv("nodename") then
skynet.send(adress, "lua", cmd, data)
else
cluster.send(node, adress, cmd, data)
end
end

--发送给某个agent
function M.send_agent(uid, cmd, data)
local uid_data = users[uid]
--未登陆
if not uid_data then
log.debug("center send_agent not uid_data " .. uid)
return
end
--未生成agent
if not uid_data.node then
log.debug("center send_agent not uid_data.node " .. uid)
return
end
local node = uid_data.node
M.send(node, uid_data.agent, cmd, data)
end

--发给某个agent的watchdog
function M.send_watchdog(uid, cmd, data)
local uid_data = users[uid]
--未登陆
if not uid_data then
log.debug("center send_watchdog not uid_data " .. uid)
return
end
--未生成agent
if not uid_data.node then
log.debug("center send_watchdog not uid_data.node " .. uid)
return
end
local node = uid_data.node
M.send(node, uid_data.watchdog, cmd, data)
end
4 changes: 2 additions & 2 deletions lualib/dbproxy.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ function M.find(cname, selector, field_selector)
return db.find(cname, selector, field_selector)
end

function M.update(cname, selector, update)
return db.update(cname, selector, update)
function M.update(cname, selector, update, upsert)
return db.update(cname, selector, update, upsert)
end

function M.insert(cname, data)
Expand Down
41 changes: 36 additions & 5 deletions lualib/libcenter.lua
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
local skynet = require "skynet"
local env = require "env"
local log = require "log"

local runconf = require(skynet.getenv("runconfig"))
local nodeconf = runconf[skynet.getenv("nodename")]
local servconf = runconf.service
local MAX_CENTER_COUNT = #servconf.center

local M = {}

local MAX_CENTER_COUNT = nodeconf.centerd_num

local M = {}
local centers = {}

local function init()
for i = 1, MAX_CENTER_COUNT do
centers[i] = string.format(".centerd%d", i)
centers[i] = string.format("centerd%d", i)
end
end

Expand All @@ -32,7 +33,37 @@ function M.logout(uid)
return skynet.call(centerd, "lua", "logout", uid)
end

--register_agent
function M.register_agent(uid, data)
local centerd = fetch_centerd(uid)
assert(centerd)
return skynet.call(centerd, "lua", "register_agent", uid, data)
end

--broadcast msg to all centers
function M.broadcast(uid, cmd, data)
for i = 1, MAX_CENTER_COUNT do
log.debug("centerlib broadcast_msg send to: " .. i)
skynet.send(centers[i], "lua", cmd, uid, data)
end
end

skynet.init(init)

--发送给某个agent
function M.send_agent(uid, cmd, data)
local centerd = fetch_centerd(uid)
assert(centerd)
skynet.send(centerd, "lua", "send_agent", uid, cmd, data)
end

--发送给某个agent的watchdog
function M.send_watchdog(uid, cmd, data)
local centerd = fetch_centerd(uid)
assert(centerd)
skynet.send(centerd, "lua", "send_watchdog", uid, cmd, data)
end

return M


Loading

0 comments on commit 0eca658

Please sign in to comment.