diff --git a/etc/config b/etc/config index bc56050..8052cc5 100644 --- a/etc/config +++ b/etc/config @@ -31,3 +31,5 @@ cpath = skynet_root.."cservice/?.so;"..app_root.."cservice/?.so" if $DAEMON then daemon = app_root.."run/skynet.pid" end + + diff --git a/etc/config2 b/etc/config2 new file mode 100644 index 0000000..5945e43 --- /dev/null +++ b/etc/config2 @@ -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 + + diff --git a/etc/runconfig.lua b/etc/runconfig.lua index ca669fc..be666e9 100644 --- a/etc/runconfig.lua +++ b/etc/runconfig.lua @@ -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"}, + }, }, } diff --git a/lualib/agent/agent_init.lua b/lualib/agent/agent_init.lua index b33875a..669ae0b 100644 --- a/lualib/agent/agent_init.lua +++ b/lualib/agent/agent_init.lua @@ -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" diff --git a/lualib/agent/agent_login.lua b/lualib/agent/agent_login.lua index baf1696..7d207e9 100644 --- a/lualib/agent/agent_login.lua +++ b/lualib/agent/agent_login.lua @@ -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 @@ -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(), @@ -66,6 +73,8 @@ function env.login(account) } player.data = load_all_data() + --初始化数据 + player.data.player.login_time = os.time() end diff --git a/lualib/agent/agent_room.lua b/lualib/agent/agent_room.lua index 867e2dd..5cb4c5f 100644 --- a/lualib/agent/agent_room.lua +++ b/lualib/agent/agent_room.lua @@ -1,6 +1,7 @@ local skynet = require "skynet" local log = require "log" local env = require "env" +local libcenter = require "libcenter" local M = env.dispatch @@ -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_initrequireʽ + --ȸֻܸ± + reload.loadmod("agent.agent_room") + return nil +end diff --git a/lualib/center.lua b/lualib/center.lua index 9da8815..8b30a6b 100644 --- a/lualib/center.lua +++ b/lualib/center.lua @@ -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 @@ -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 \ No newline at end of file diff --git a/lualib/dbproxy.lua b/lualib/dbproxy.lua index 493ccd0..c473296 100644 --- a/lualib/dbproxy.lua +++ b/lualib/dbproxy.lua @@ -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) diff --git a/lualib/libcenter.lua b/lualib/libcenter.lua index 4d72e8e..1c16cd2 100644 --- a/lualib/libcenter.lua +++ b/lualib/libcenter.lua @@ -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 @@ -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 + diff --git a/lualib/libdbproxy.lua b/lualib/libdbproxy.lua index e838a15..48eb053 100644 --- a/lualib/libdbproxy.lua +++ b/lualib/libdbproxy.lua @@ -2,17 +2,16 @@ local skynet = require "skynet" local log = require "log" local runconf = require(skynet.getenv("runconfig")) -local nodeconf = runconf[skynet.getenv("nodename")] - -local MAX_DBPROXY_COUNT = nodeconf.dbproxy_num +local servconf = runconf.service +local MAX_DBPROXY_COUNT = #servconf.dbproxy local M = {} - local dbproxy = {} + local function init() log.debug("init libdbproxy") for i = 1, MAX_DBPROXY_COUNT do - dbproxy[i] = string.format(".dbproxyd%d", i) + dbproxy[i] = string.format("dbproxyd%d", i) end end @@ -44,10 +43,10 @@ function M.find(key, cname, selector, field_selector) return skynet.call(db, "lua", "find", cname, selector, field_selector) end -function M.update(key, cname, selector, update) +function M.update(key, cname, selector, update, upsert) local db = fetch_dbproxy(key) assert(db) - return skynet.call(db, "lua", "update", cname, selector, field_selector) + return skynet.call(db, "lua", "update", cname, selector, update, upsert) end function M.insert(key, cname, data) diff --git a/lualib/liblogin.lua b/lualib/liblogin.lua index ac53d91..b4b8843 100644 --- a/lualib/liblogin.lua +++ b/lualib/liblogin.lua @@ -2,9 +2,8 @@ local skynet = require "skynet" local log = require "log" local runconf = require(skynet.getenv("runconfig")) -local nodeconf = runconf[skynet.getenv("nodename")] - -local MAX_LOGIN_NUM = nodeconf.login_num +local servconf = runconf.service +local MAX_LOGIN_NUM = #servconf.login local M = {} @@ -12,7 +11,7 @@ local login = {} local function init() log.debug("init liblogin") for i = 1, MAX_LOGIN_NUM do - login[i] = string.format(".logind%d", i) + login[i] = string.format("logind%d", i) end end diff --git a/lualib/login.lua b/lualib/login.lua index d02cd53..060515e 100644 --- a/lualib/login.lua +++ b/lualib/login.lua @@ -26,6 +26,10 @@ end function M.login(msg) local account = msg.account + if not account then + print("login not account: " .. tool.dump(msg)) + return false + end local password = msg.password local ret = dbproxy.findOne(nil, "account", {account=account}) if ret and ret.password == password then diff --git a/lualib/mongodb.lua b/lualib/mongodb.lua index 1c1c94d..81a0e9a 100644 --- a/lualib/mongodb.lua +++ b/lualib/mongodb.lua @@ -24,6 +24,7 @@ end function M.update(cname, selector, update, upsert) local collection = db[cname] + collection:update(selector, update, upsert) local r = db:runCommand("getLastError") if r.err ~= bson.null then @@ -34,7 +35,7 @@ function M.update(cname, selector, update, upsert) if r.n <= 0 then skynet.error("mongodb update "..cname.." failed") end - + skynet.error("update finish") return true, r.err end diff --git a/lualib/mysqldb.lua b/lualib/mysqldb.lua index 54d4bfc..1bcc58d 100644 --- a/lualib/mysqldb.lua +++ b/lualib/mysqldb.lua @@ -122,13 +122,12 @@ end function M.find(cname, selector, field_selector) local selector_str = build_selector(selector) - local field_selector_str = build_selector(field_selector) + local field_selector_str = build_field_selector(field_selector) local sql = string.format("select %s from %s where %s", field_selector_str, cname, selector_str) local ret = db:query(sql) - for k, v in pairs(ret) do - ret[k] = build_find_data(cname, v) - end + log.debug("=-==ret: %s", tool.dump(ret) .. " sql: " .. sql) + ret = build_find_data(cname, ret) return ret end diff --git a/lualib/redisdb.lua b/lualib/redisdb.lua new file mode 100644 index 0000000..e69de29 diff --git a/lualib/reload.lua b/lualib/reload.lua index f461a32..daa879c 100644 --- a/lualib/reload.lua +++ b/lualib/reload.lua @@ -108,8 +108,8 @@ local protection = { reload = true, } -local change_func = {} -local visited_sig = {} +local change_func = nil +local visited_sig = nil local update_obj local update_function local update_table @@ -132,6 +132,7 @@ function update_upvalue(old_func, new_func, name, deep) debug.setupvalue(new_func, i, _ENV) else if old_exist_name[name] then + --print("=== name: " .. name) local old_value = old_upvalue[name] if type(old_value) ~= type(value) then debug.setupvalue(new_func, i, old_value) @@ -148,13 +149,32 @@ function update_upvalue(old_func, new_func, name, deep) end end +function update_function(old_func, new_func, name, deep) + if protection[old_func] or protection[new_func] then return end + if old_func == new_func then return end + local signature = tostring(old_func) .. tostring(new_func) + if visited_sig[signature] then return end + visited_sig[signature] = true + + --print("update function name: " .. name) + + update_upvalue(old_func, new_func, name, deep) + change_func[old_func] = new_func +end + function update_table(old_table, new_table, name, deep) + + --print("==== test update table name: " .. name) + if protection[old_table] or protection[new_table] then return end - if old_table == new_table then return end + if old_table == new_table then print("table is same") return end + local signature = tostring(old_table) .. tostring(new_table) - if visited_sig[signature] then return end + if visited_sig[signature] then print("==11xxx==") return end visited_sig[signature] = true + --print("update table name: " .. name) + for name, value in pairs(new_table) do local old_value = old_table[name] if type(value) == type(old_value) then @@ -182,6 +202,7 @@ end function update_obj(old_obj, new_obj, name, deep) if type(old_obj) == type(new_obj) then if type(old_obj) == "table" then + --print("=== update obj name: " .. name) update_table(old_obj, new_obj, name, deep) elseif type(old_obj) == "function" then update_function(old_obj, new_obj, name, deep) @@ -231,7 +252,7 @@ end local function travel_all() local visited = {} - visited[reload] = true + visited.reload = true local function f(t) if type(t) ~= "function" and type(t) ~= "table" then return end if visited[t] then return end @@ -252,6 +273,7 @@ local function travel_all() if type(v) == "function" then if change_func[v] then t[k] = change_func[v] + --print("=== travel all k: " .. k) end end if type(k) == "fuction" then @@ -275,7 +297,15 @@ local function travel_all() end end +local function init() + change_func = {} + visited_sig = {} +end + + function reload.loadmod(mod) + init() + local ret, func, obj = loadmod(mod, _ENV) if not ret then return false, "load mod false" @@ -283,7 +313,11 @@ function reload.loadmod(mod) return obj end + + function reload.reload(mod) + init() + local old_obj = package.loaded[mod] if not old_obj then return false, "mod not found" @@ -294,16 +328,22 @@ function reload.reload(mod) return false, "load mod false" end + --print("===111===") update_obj(old_obj, new_obj, "reload", "") + --print("===2222===") + + for name, value in pairs(sandbox_mods) do local old_value = package.loaded[name] update_obj(old_value, value, name, "") + --print("reload mod: " .. name) end setmetatable(env, nil) update_obj(_ENV, env, "ENV", "") - + --print("trave all =====") travel_all() + --print("trave all ggg=====") return true end diff --git "a/service/\\" "b/service/\\" new file mode 100644 index 0000000..8b2c108 --- /dev/null +++ "b/service/\\" @@ -0,0 +1,12 @@ +local skynet = require "skynet" + + +skynet.start(function() + + local p = skynet.queryservice(true, "monitord") + skynet.call(p, "lua", "WATCH", skynet.queryservice("testd")) + + skynet.dispatch("lua", function(session, source, cmd, subcmd, ...) + print("cmd: " .. cmd) + end) +end) diff --git a/service/exitd.lua b/service/exitd.lua new file mode 100644 index 0000000..a95f8d9 --- /dev/null +++ b/service/exitd.lua @@ -0,0 +1,17 @@ +local skynet = require "skynet" + +local function init() + local addr = skynet.queryservice("testd") + + print("==== 8888 1000 === p: " .. addr) + local ok, ret = pcall(skynet.call, addr, "debug", "LINK") + if not ok then + print(ok) + print(ret) + end + print("1111 ==== 21000 ===") +end + +skynet.start(function() + init() +end) diff --git a/service/main.lua b/service/main.lua index a52fb60..4965010 100644 --- a/service/main.lua +++ b/service/main.lua @@ -1,59 +1,98 @@ local skynet = require "skynet" -require "skynet.manager" +local cluster = require "skynet.cluster" local log = require "log" +require "skynet.manager" local runconf = require(skynet.getenv("runconfig")) -local nodeconf = runconf[skynet.getenv("nodename")] +local servconf = runconf.service +local nodename = skynet.getenv("nodename") skynet.start(function() log.debug("Server start version: " .. runconf.version) - - skynet.uniqueservice("debug_console", nodeconf.debug_console_port) - log.debug("start debug_console in port: " .. nodeconf.debug_console_port) - + --集群信息 + cluster.reload(runconf.cluster) + cluster.open(nodename) + --开启debug_console服务 + for i,v in pairs(servconf.debug_console) do + if nodename == v.node then + skynet.uniqueservice("debug_console", v.port) + log.debug("start debug_console in port: " .. v.port.."...") + end + end + --开启热更新模块 log.debug("start setupd...") skynet.newservice("setupd") - - log.debug("start dbproxyd...") - for i = 1, nodeconf.dbproxy_num do - local name = string.format(".dbproxyd%d", i) - local p = skynet.newservice("dbproxyd") - skynet.call(p, "lua", "start", nodeconf.db) - skynet.name(name, p) + --开启dbproxyd服务 + for i,v in pairs(servconf.dbproxy) do + local name = string.format("dbproxyd%d", i) + if nodename == v.node then + local p = skynet.newservice("dbproxyd") + skynet.call(p, "lua", "start", servconf.dbproxy_common) + skynet.name(name, p) + log.debug("start "..name.."...") + else + local proxy = cluster.proxy(v.node, name) + skynet.name(name, proxy) + end end - - log.debug("start centerd...") - for i = 1, nodeconf.centerd_num do - local name = string.format(".centerd%d", i) - local p = skynet.newservice("centerd") - skynet.name(name, p) + --开启centerd服务 + for i,v in pairs(servconf.center) do + local name = string.format("centerd%d", i) + if nodename == v.node then + local p = skynet.newservice("centerd") + skynet.name(name, p) + log.debug("start "..name.."...") + else + local proxy = cluster.proxy(v.node, name) + skynet.name(name, proxy) + end end - - log.debug("start logind...") - for i = 1, nodeconf.login_num do - local name = string.format(".logind%d", i) - local p = skynet.newservice("logind") - skynet.name(name, p) + --开启login服务 + for i,v in pairs(servconf.login) do + local name = string.format("logind%d", i) + if nodename == v.node then + local p = skynet.newservice("logind") + skynet.name(name, p) + log.debug("start "..name.."...") + else + local proxy = cluster.proxy(v.node, name) + skynet.name(name, proxy) + end end - - log.debug("start agent pool...") - local agentname = "wsagent" - local maxnum = 10 - local recyremove = 2 - local brokecachelen = 10 - agentpool = skynet.uniqueservice("agentpool", agentname, maxnum, recyremove, brokecachelen) - - local watchdogconf = nodeconf.watchdog - local watchdog = skynet.newservice("wswatchdog") - skynet.call(watchdog, "lua", "start", { - port = watchdogconf.port, - maxclient = watchdogconf.maxclient, - nodelay = watchdogconf.nodelay - }) - log.debug("start wswatchdog in port: " .. watchdogconf.port) - - skynet.newservice("testd") - + --开启agentpool服务 + for i,v in pairs(servconf.agentpool) do + if nodename == v.node then + local agentname = servconf.agentpool_common.name + local maxnum = servconf.agentpool_common.maxnum + local recyremove = servconf.agentpool_common.recyremove + local brokecachelen = servconf.agentpool_common.brokecachelen + agentpool = skynet.uniqueservice("agentpool", agentname, maxnum, recyremove, brokecachelen) + log.debug("start agent pool...") + end + end + --开启watchdog服务 + for i,v in pairs(servconf.watchdog) do + if nodename == v.node then + local maxclient = servconf.watchdog_common.maxclient + local nodelay = servconf.watchdog_common.nodelay + local watchdog = skynet.newservice("wswatchdog") + skynet.call(watchdog, "lua", "start", { + port = v.port, + maxclient = maxclient, + nodelay = nodelay + }) + log.debug("start wswatchdog in port: " .. v.port) + end + end + --测试 + --skynet.uniqueservice("testd") + --skynet.newservice("exitd") + --skynet.newservice("monitord") + --log.debug("=== test monitor ===") + --require "skynet.manager" + --skynet.monitor("monitord") + --log.debug("=== end test monitor ===") + skynet.exit() end) diff --git a/service/monitord.lua b/service/monitord.lua new file mode 100644 index 0000000..05a85d6 --- /dev/null +++ b/service/monitord.lua @@ -0,0 +1,44 @@ +local skynet = require "skynet" +local tool = require "tool" + + +local service_map = {} + +skynet.register_protocol { + name = "client", + id = skynet.PTYPE_CLIENT, -- PTYPE_CLIENT = 3 + unpack = function() end, + dispatch = function(_, address) + print("======= exit address: " .. address .. " service: " .. tool.dump(service_map)) + local w = service_map[address] + if w then + for watcher in pairs(w) do + print("=== watcher: " .. watcher) + skynet.redirect(watcher, address, "error", 0, "") + end + service_map[address] = false + end + end +} + +local function monitor(session, watcher, command, service) + assert(command, "WATCH") + print("====== command: " .. command) + + local w = service_map[service] + if not w then + if w == false then + skynet.ret(skynet.pack(false)) + return + end + w = {} + service_map[service] = w + end + w[watcher] = true + skynet.ret(skynet.pack(true)) +end + +skynet.start(function() + skynet.dispatch("lua", monitor) +end) + diff --git a/service/testd.lua b/service/testd.lua index 6fb76f6..d9a2fd5 100644 --- a/service/testd.lua +++ b/service/testd.lua @@ -35,12 +35,26 @@ local function testmysql() local ret = mysql.findOne("test", {id=1}) - log.debug("===test ==ret: %s", tool.dump(ret)) + log.debug("findOne ret: %s", tool.dump(ret)) + + + local ret = mysql.findOne("test", {name="test"}, {"id", "tname"}) + log.debug("findOne ret: %s", tool.dump(ret)) + + local ret = mysql.find("test", {name="test"}) + log.debug("find ret: %s", tool.dump(ret)) + + local ret = mysql.find("test", {name="test"}, {"id", "tname"}) + log.debug("find select ret: %s", tool.dump(ret)) + + local ret = mysql.find("test", {name="itest"}) + log.debug("find ret: %s", tool.dump(ret)) end skynet.start(function() log.debug("start test...") testmysql() log.debug("end test...") - skynet.exit() + --skynet.exit() end) + diff --git a/service/watchdog.lua b/service/watchdog.lua index 4d80aed..7ff88e3 100644 --- a/service/watchdog.lua +++ b/service/watchdog.lua @@ -13,7 +13,7 @@ local agentpool = ... ---------------------------socket数据处理---------------------------- local sock_handler = {} sock_handler.login = function (fd, msg) - + msg.fd = fd msg.watchdog = skynet.self() @@ -29,9 +29,11 @@ sock_handler.login = function (fd, msg) }) log.log("verify account %s success!", msg.account) + else + SOCKET.send(fd, "login", {ret=ret}) + skynet.call(gate,"lua","kick",fd) + log.log("verify account %s fail!") end - - SOCKET.send(fd, "login", {ret=ret}) end sock_handler.register = function (fd, msg) diff --git a/service/wsagent.lua b/service/wsagent.lua index 1d4739f..a2a10ba 100644 --- a/service/wsagent.lua +++ b/service/wsagent.lua @@ -6,6 +6,7 @@ local env = require "env" require "libstring" require "agent.agent_init" +local libcenter = require "libcenter" local CMD = {} @@ -28,7 +29,7 @@ function default_dispatch(cmd, msg) local isok, ret = pcall(cb, msg) if not isok then - log.error("handle msg error, cmd = %s, str = %s", cmd, str) + log.error("handle msg error, cmd = %s, str = %s, err=%s", cmd, str, ret) return end return ret @@ -51,8 +52,10 @@ function dispatch(_, _, str) local length = #cmdlist local ret if length == 2 then + skynet.error("dispatch length == 2") ret = service_dispatch(cmdlist[1], cmdlist[2], msg) elseif length == 1 then + skynet.error("dispatch length == 1") ret = default_dispatch(cmd, msg) end if ret then @@ -75,8 +78,12 @@ function CMD.start(conf) skynet.call(gate,"lua","forward",fd) env.login(account) - - skynet.error("agent login") + local data = { + node = skynet.getenv("nodename"), + agent = skynet.self() + } + libcenter.register_agent(account.uid, data) + skynet.error("agent login "..account.uid) end function CMD.disconnect() @@ -91,11 +98,10 @@ function CMD.send(msg) websocket:send_text(fd, data) end + skynet.start(function() skynet.dispatch("lua",function(_,_,cmd,...) local f = CMD[cmd] skynet.ret(skynet.pack(f(...))) end) -end) - - +end) \ No newline at end of file diff --git a/service/wswatchdog.lua b/service/wswatchdog.lua index 89a898d..9e145fc 100644 --- a/service/wswatchdog.lua +++ b/service/wswatchdog.lua @@ -150,5 +150,4 @@ skynet.start(function() end end) gate = skynet.newservice("wsgate") -end) - +end) \ No newline at end of file diff --git a/start2.sh b/start2.sh new file mode 100755 index 0000000..e0a30c1 --- /dev/null +++ b/start2.sh @@ -0,0 +1,23 @@ +#!/bin/sh + +export ROOT=$(cd `dirname $0`; pwd) +export SKYNET_ROOT=$ROOT/skynet +export DAEMON=false + +## echo $ROOT + echo $SKYNET_ROOT +while getopts "Dk" arg +do + case $arg in + D) + export DAEMON=true + ;; + k) + kill `cat $ROOT/run/skynet.pid` + exit 0; + ;; + esac +done + +$SKYNET_ROOT/skynet $ROOT/etc/config2 + diff --git a/test/wschat.lua b/test/wschat.lua new file mode 100644 index 0000000..880a6a4 --- /dev/null +++ b/test/wschat.lua @@ -0,0 +1,80 @@ +package.cpath = "../skynet/luaclib/?.so" +package.path = "../skynet/lualib/?.lua;../lualib/?.lua;../examples/?.lua" + +if _VERSION ~= "Lua 5.3" then + error "Use lua 5.3" +end + + +local socket = require "clientwebsocket" +local json = require "cjson" +local tool = require "tool" + +local fd = assert(socket.connect("127.0.0.1", 8799)) + +local function request(name, args, session) + local t = { + cmd = name, + seq = session, + } + if type(args) == "table" then + for k, v in pairs(args) do + t[k] = v + end + end + local str = json.encode(t) + return str +end + +local function send_package(fd, pack) + socket.send(fd, pack) +end + +local function recv_package() + local r , istimeout= socket.recv(fd, 100) + if not r then + return nil + end + if r == "" and istimeout == 0 then + error "Server closed" + end + return r +end + +local session = 0 + +local function send_request(name, args) + session = session + 1 + local str = request(name, args, session) + send_package(fd, str) + + print("Request:", str) +end + + +local function dispatch_package() + while true do + local v + v = recv_package() + if not v or v == "" then + break + end + + print("recv: " .. tool.dump(v)) + end +end + + +print("your account: ") +send_request("login", {account=4, password="11111"}) + +while true do + dispatch_package() + local msg = socket.readstdin() + if msg then + send_request("chat", { str = msg }) + else + socket.usleep(2000) + end +end + diff --git a/test/wschat2.lua b/test/wschat2.lua new file mode 100644 index 0000000..1840836 --- /dev/null +++ b/test/wschat2.lua @@ -0,0 +1,79 @@ +package.cpath = "../skynet/luaclib/?.so" +package.path = "../skynet/lualib/?.lua;../lualib/?.lua;../examples/?.lua" + +if _VERSION ~= "Lua 5.3" then + error "Use lua 5.3" +end + + +local socket = require "clientwebsocket" +local json = require "cjson" +local tool = require "tool" + +local fd = assert(socket.connect("127.0.0.1", 8799)) + +local function request(name, args, session) + local t = { + cmd = name, + seq = session, + } + if type(args) == "table" then + for k, v in pairs(args) do + t[k] = v + end + end + local str = json.encode(t) + return str +end + +local function send_package(fd, pack) + socket.send(fd, pack) +end + +local function recv_package() + local r , istimeout= socket.recv(fd, 100) + if not r then + return nil + end + if r == "" and istimeout == 0 then + error "Server closed" + end + return r +end + +local session = 0 + +local function send_request(name, args) + session = session + 1 + local str = request(name, args, session) + send_package(fd, str) + print("Request:", session) +end + + +local function dispatch_package() + while true do + local v + v = recv_package() + if not v or v == "" then + break + end + + print("recv: " .. tool.dump(v)) + end +end + + +print("your account: ") +send_request("login", {account=5, password="11111"}) + +while true do + dispatch_package() + local msg = socket.readstdin() + if msg then + send_request("chat", { str = msg }) + else + socket.usleep(2000) + end +end + diff --git a/test/wsclient.lua b/test/wsclient.lua index f84c610..1f8a418 100644 --- a/test/wsclient.lua +++ b/test/wsclient.lua @@ -64,6 +64,7 @@ local function dispatch_package() end send_request("login", {account="2", password="11111"}) + while true do dispatch_package() local cmd = socket.readstdin() diff --git a/test/wsecho.lua b/test/wsecho.lua new file mode 100644 index 0000000..b707541 --- /dev/null +++ b/test/wsecho.lua @@ -0,0 +1,77 @@ +package.cpath = "../skynet/luaclib/?.so" +package.path = "../skynet/lualib/?.lua;../lualib/?.lua;../examples/?.lua" + +if _VERSION ~= "Lua 5.3" then + error "Use lua 5.3" +end + + +local socket = require "clientwebsocket" +local json = require "cjson" +local tool = require "tool" + +local fd = assert(socket.connect("127.0.0.1", 8799)) + +local function request(name, args, session) + local t = { + cmd = name, + seq = session, + } + if type(args) == "table" then + for k, v in pairs(args) do + t[k] = v + end + end + local str = json.encode(t) + return str +end + +local function send_package(fd, pack) + socket.send(fd, pack) +end + +local function recv_package() + local r , istimeout= socket.recv(fd, 100) + if not r then + return nil + end + if r == "" and istimeout == 0 then + error "Server closed" + end + return r +end + +local session = 0 + +local function send_request(name, args) + session = session + 1 + local str = request(name, args, session) + send_package(fd, str) + print("Request:", session) +end + + +local function dispatch_package() + while true do + local v + v = recv_package() + if not v or v == "" then + break + end + + print("recv: " .. tool.dump(v)) + end +end + +send_request("login", {account="2", password="11111"}) + +while true do + dispatch_package() + local msg = socket.readstdin() + if msg then + send_request("echo", { str = msg }) + else + socket.usleep(2000) + end +end + diff --git a/test/wsname.lua b/test/wsname.lua new file mode 100644 index 0000000..c06f8e9 --- /dev/null +++ b/test/wsname.lua @@ -0,0 +1,77 @@ +package.cpath = "../skynet/luaclib/?.so" +package.path = "../skynet/lualib/?.lua;../lualib/?.lua;../examples/?.lua" + +if _VERSION ~= "Lua 5.3" then + error "Use lua 5.3" +end + + +local socket = require "clientwebsocket" +local json = require "cjson" +local tool = require "tool" + +local fd = assert(socket.connect("127.0.0.1", 8799)) + +local function request(name, args, session) + local t = { + cmd = name, + seq = session, + } + if type(args) == "table" then + for k, v in pairs(args) do + t[k] = v + end + end + local str = json.encode(t) + return str +end + +local function send_package(fd, pack) + socket.send(fd, pack) +end + +local function recv_package() + local r , istimeout= socket.recv(fd, 100) + if not r then + return nil + end + if r == "" and istimeout == 0 then + error "Server closed" + end + return r +end + +local session = 0 + +local function send_request(name, args) + session = session + 1 + local str = request(name, args, session) + send_package(fd, str) + print("Request:", session) +end + + +local function dispatch_package() + while true do + local v + v = recv_package() + if not v or v == "" then + break + end + + print("recv: " .. tool.dump(v)) + end +end + +send_request("login", {account="2", password="11111"}) + +while true do + dispatch_package() + local msg = socket.readstdin() + if msg then + send_request("set_name", { str = msg }) + else + socket.usleep(2000) + end +end +