Commit a5486ede2d28fbf4981c678624674dc690d6d45d
1 parent
65421af2
csvdata 修改为 sharetable
Showing
18 changed files
with
233 additions
and
347 deletions
Show diff stats
src/actions/GmAction.lua
| ... | ... | @@ -16,7 +16,7 @@ function _M.clientRequest(agent, data) |
| 16 | 16 | end |
| 17 | 17 | |
| 18 | 18 | function _M.testhotfix(role, pms) |
| 19 | - return csvdb["itemCsv"][1]["name"] .. " -=- " .. csvdb["itemCsv"][2]["name"] .. " -=- " .. role:getItemCount(123) .. " -=- " .. table.pack(next(role.heros))[2]:getBattleValue() | |
| 19 | + return csvdb["itemCsv"][1]["name"] .. " -=- " .. globalCsv["codeVersion"] .. " -=- " .. role:getItemCount(123) .. " -=- " .. table.pack(next(role.heros))[2]:getBattleValue() | |
| 20 | 20 | end |
| 21 | 21 | |
| 22 | 22 | function _M.ignoreout(role, pms) | ... | ... |
src/actions/HeroAction.lua
| ... | ... | @@ -233,7 +233,7 @@ function _M.commentHeroRpc(agent, data) |
| 233 | 233 | result.status = 1 |
| 234 | 234 | else |
| 235 | 235 | local commentKey = getCommentKey(heroType) |
| 236 | - local SERV = string.format(".NAMED%d", math.random(1, 5)) | |
| 236 | + local SERV = string.format(".chatd%d", math.random(1, 5)) | |
| 237 | 237 | local legal, mod = skynet.call(SERV, "lua", "check", content) |
| 238 | 238 | if not legal then |
| 239 | 239 | content = mod or "" | ... | ... |
src/actions/HttpAction.lua
| 1 | -local codecache = require "skynet.codecache" -- 清空缓存用 | |
| 2 | - | |
| 3 | 1 | |
| 4 | 2 | local _M = {} |
| 5 | 3 | |
| ... | ... | @@ -19,45 +17,12 @@ local _M = {} |
| 19 | 17 | ]=] |
| 20 | 18 | |
| 21 | 19 | |
| 22 | --- 清空缓存 | |
| 23 | -function _M.clearcache(query, body) | |
| 24 | - skynet.error(string.format("clearcache time: %s", skynet.timex())) | |
| 25 | - codecache.clear() | |
| 26 | - return 'success' | |
| 27 | -end | |
| 28 | - | |
| 29 | ---重新加载 需要修改的csvdb -- 单字段修改 优先使用hotfix_csvdata | |
| 30 | ---[=[ eg: | |
| 31 | - body = """ | |
| 32 | - csvdb["itemCsv"][1]["name"] = "测试一下" | |
| 33 | - """ | |
| 34 | -]=] | |
| 35 | - | |
| 36 | -function _M.reload_csvdata(query, body) | |
| 37 | - if not body or body == "" then | |
| 38 | - return 'no body' | |
| 39 | - end | |
| 40 | - | |
| 41 | - local ok = pcall(load, body) | |
| 42 | - if not ok then | |
| 43 | - return "code error" | |
| 44 | - end | |
| 45 | - | |
| 46 | - local ok, status = pcall(skynet.call, '.CSVDATA', "lua", "reload", body) | |
| 47 | - if status == "ok" then | |
| 48 | - skynet.error(string.format("reload_csvdata time: %s, code: %s", skynet.timex(), body)) | |
| 49 | - return 'success' | |
| 50 | - else | |
| 51 | - return 'error update' | |
| 52 | - end | |
| 53 | -end | |
| 54 | - | |
| 55 | ---指定更新某个字段值 | |
| 56 | 20 | --[=[ |
| 57 | 21 | 解码后 |
| 58 | 22 | body = { |
| 59 | - {"itemCsv", 1, "name", "测试一下"}, | |
| 60 | - {"itemCsv", 2, "name", "测试一下"} | |
| 23 | + "src/csvdata/init.lua", | |
| 24 | + "unitCsv", | |
| 25 | + "story_cgCsv", | |
| 61 | 26 | } |
| 62 | 27 | ]=] |
| 63 | 28 | function _M.hotfix_csvdata(query, body) |
| ... | ... | @@ -69,14 +34,10 @@ function _M.hotfix_csvdata(query, body) |
| 69 | 34 | if not ok or type(result) ~= 'table' then |
| 70 | 35 | return "decode error" |
| 71 | 36 | end |
| 72 | - local ok, status = pcall(skynet.call, '.CSVDATA', "lua", "hotfix", result) | |
| 73 | 37 | |
| 74 | - if status == "ok" then | |
| 75 | - skynet.error(string.format("hotfix_csvdata time: %s, code: %s", skynet.timex(), body)) | |
| 76 | - return 'success' | |
| 77 | - else | |
| 78 | - return 'error update' | |
| 79 | - end | |
| 38 | + csvdb.hotfix(table.unpack(result)) | |
| 39 | + | |
| 40 | + return 'success' | |
| 80 | 41 | end |
| 81 | 42 | |
| 82 | 43 | -- 热更新代码 -- 针对 agent 执行发送过来的代码 -- 代码要规范~ |
| ... | ... | @@ -150,7 +111,7 @@ function _M.hotfix_code(query, body) |
| 150 | 111 | |
| 151 | 112 | skynet.error(string.format("hotfix_code time: %s, code: %s", skynet.timex(), body)) |
| 152 | 113 | |
| 153 | - pcall(skynet.call, '.WATCHDOG', "lua", "hotfix", body) | |
| 114 | + pcall(skynet.call, '.watchdog', "lua", "hotfix", body) | |
| 154 | 115 | return 'success' |
| 155 | 116 | end |
| 156 | 117 | ... | ... |
src/actions/RoleAction.lua
| ... | ... | @@ -22,7 +22,7 @@ local function validName(name) |
| 22 | 22 | local exist = redisproxy:exists(string_format("user:%s", name)) |
| 23 | 23 | if exist then return "existed" end |
| 24 | 24 | |
| 25 | - local SERV = string_format(".NAMED%d", math.random(1, 5)) | |
| 25 | + local SERV = string_format(".named%d", math.random(1, 5)) | |
| 26 | 26 | local legal = skynet.call(SERV, "lua", "check", name) |
| 27 | 27 | return legal and "ok" or "illegal" |
| 28 | 28 | end |
| ... | ... | @@ -217,7 +217,7 @@ function _M.loginRpc( agent, data ) |
| 217 | 217 | |
| 218 | 218 | -- 发下缓存的世界消息 |
| 219 | 219 | local worldChatResponse = {worldChats = {}} |
| 220 | - local ok, msgs = pcall(skynet.call, '.GLOBALD', "lua", "getWorldMsg", role._channelIdx) | |
| 220 | + local ok, msgs = pcall(skynet.call, '.globald', "lua", "getWorldMsg", role._channelIdx) | |
| 221 | 221 | if not ok then |
| 222 | 222 | msgs = {} |
| 223 | 223 | end |
| ... | ... | @@ -365,7 +365,7 @@ function _M.changeIntroRpc(agent, data) |
| 365 | 365 | local content = msg.content |
| 366 | 366 | if not content or type(content) ~= "string" then return end |
| 367 | 367 | |
| 368 | - local SERV = string_format(".CHATED%d", math.random(1, 5)) | |
| 368 | + local SERV = string_format(".chated%d", math.random(1, 5)) | |
| 369 | 369 | local legal, mod = skynet.call(SERV, "lua", "check", content) |
| 370 | 370 | if not legal then |
| 371 | 371 | content = mod or "" |
| ... | ... | @@ -750,7 +750,7 @@ function _M.chatRpc(agent, data) |
| 750 | 750 | -- 判断禁言 |
| 751 | 751 | local result = nil |
| 752 | 752 | |
| 753 | - local SERV = string_format(".CHATED%d", math.random(1, 5)) | |
| 753 | + local SERV = string_format(".chated%d", math.random(1, 5)) | |
| 754 | 754 | local legal, mod = skynet.call(SERV, "lua", "check", content) |
| 755 | 755 | if not legal then |
| 756 | 756 | content = mod or "" |
| ... | ... | @@ -805,7 +805,7 @@ function _M.chatRpc(agent, data) |
| 805 | 805 | end |
| 806 | 806 | end |
| 807 | 807 | mcast_util.pub_world(actionCodes.Role_chat, MsgPack.pack(response)) |
| 808 | - pcall(skynet.send, '.GLOBALD', "lua", "sendWorldMsg", role._channelIdx, response) | |
| 808 | + pcall(skynet.send, '.globald', "lua", "sendWorldMsg", role._channelIdx, response) | |
| 809 | 809 | end, |
| 810 | 810 | -- 私聊 |
| 811 | 811 | [2] = function () | ... | ... |
src/agent.lua
| ... | ... | @@ -8,14 +8,13 @@ require "skynet.manager" |
| 8 | 8 | local queue = require "skynet.queue" |
| 9 | 9 | local netpack = require "skynet.netpack" |
| 10 | 10 | local socket = require "skynet.socket" |
| 11 | -local sharedata = require "skynet.sharedata" | |
| 12 | 11 | local xxtea = require "xxtea" |
| 13 | 12 | |
| 14 | 13 | skynet = require "skynet" |
| 15 | 14 | redisproxy = require "shared.redisproxy" |
| 16 | 15 | datacenter = require "skynet.datacenter" |
| 17 | 16 | mcast_util = require "services/mcast_util" |
| 18 | -globalCsv = require "csvdata/GlobalDefine" | |
| 17 | +csvdb = require "shared.csvdata" | |
| 19 | 18 | |
| 20 | 19 | local CMD = {} |
| 21 | 20 | local agentInfo = {} -- { client_fd, role, gate_serv, open_timer} |
| ... | ... | @@ -217,7 +216,7 @@ skynet.register_protocol { |
| 217 | 216 | |
| 218 | 217 | skynet.register_protocol { |
| 219 | 218 | name = "role", |
| 220 | - id = 13, | |
| 219 | + id = 101, | |
| 221 | 220 | pack = skynet.pack, |
| 222 | 221 | unpack = skynet.unpack, |
| 223 | 222 | dispatch = function(session, address, submethod, ...) |
| ... | ... | @@ -326,20 +325,19 @@ skynet.start(function() |
| 326 | 325 | end |
| 327 | 326 | end) |
| 328 | 327 | |
| 329 | - redisd = skynet.localname(".REDIS") | |
| 328 | + redisd = skynet.localname(".redis") | |
| 330 | 329 | if tonumber(skynet.getenv "logd") == 1 then |
| 331 | - logd = skynet.localname(".LOGD") | |
| 330 | + logd = skynet.localname(".log") | |
| 332 | 331 | end |
| 333 | 332 | |
| 334 | 333 | cs = queue() |
| 335 | 334 | |
| 336 | - csvdb = sharedata.query("csvdata") | |
| 337 | - pvpd = skynet.localname(".PVPCROSS") | |
| 335 | + pvpd = skynet.localname(".pvpcross") | |
| 338 | 336 | -- 错误码特殊处理 |
| 339 | 337 | -- todo |
| 340 | 338 | -- for key, value in pairs(csvdb["sys_codesCsv"]) do |
| 341 | 339 | -- _G[string.upper(value.varname)] = key |
| 342 | 340 | -- end |
| 343 | - | |
| 341 | + globalCsv = csvdb["GlobalDefineCsv"] | |
| 344 | 342 | agent_util = require "services/agent_util" |
| 345 | 343 | end) | ... | ... |
src/services/chated.lua
| ... | ... | @@ -35,12 +35,13 @@ if mode == "sub" then |
| 35 | 35 | skynet.ret(skynet.pack(f(...))) |
| 36 | 36 | end) |
| 37 | 37 | |
| 38 | - skynet.register(string.format(".CHATED%d", id)) | |
| 38 | + skynet.register(string.format(".chated%d", id)) | |
| 39 | 39 | end) |
| 40 | 40 | else |
| 41 | 41 | skynet.start(function() |
| 42 | - local ok, forbidNames = pcall(require, "csvdata.forbid_chat") | |
| 43 | - if not ok then forbidNames = {} end | |
| 42 | + local csvdb = require "shared.csvdata" | |
| 43 | + local forbidNames = csvdb["forbid_chatCsv"] | |
| 44 | + if not forbidNames then forbidNames = {} end | |
| 44 | 45 | |
| 45 | 46 | local words = {} |
| 46 | 47 | for _, data in ipairs(forbidNames) do | ... | ... |
src/services/csvdatad.lua deleted
| ... | ... | @@ -1,115 +0,0 @@ |
| 1 | -local sharedata = require "skynet.sharedata" | |
| 2 | -local skynet = require "skynet" | |
| 3 | -local lfs = require "lfs" | |
| 4 | -local redisproxy = require "shared.redisproxy" | |
| 5 | -require "shared.init" | |
| 6 | -require "utils.init" | |
| 7 | -require "csvdata.init" | |
| 8 | -require "skynet.manager" | |
| 9 | -require "RedisKeys" | |
| 10 | - | |
| 11 | --- local csvdb = {} | |
| 12 | - | |
| 13 | -local function formatFileName(filename) | |
| 14 | - filename = string.trim(filename) | |
| 15 | - local basename = filename:match("([^/]+)%.lua$") | |
| 16 | - if not basename then return end | |
| 17 | - local loadname = filename:match("^src/([^.]+)%.lua$") | |
| 18 | - loadname = loadname:gsub('/', '.') | |
| 19 | - return basename, loadname | |
| 20 | -end | |
| 21 | - | |
| 22 | -local function travCsv(rootPath, pathes) | |
| 23 | - pathes = pathes or {} | |
| 24 | - local modified = false | |
| 25 | - local ok, files, iter = pcall(lfs.dir, rootPath) | |
| 26 | - if not ok then return modified end | |
| 27 | - for entry in files, iter do | |
| 28 | - -- 过滤 . 开始的字符串包括 . .. .git .开头的文件名 | |
| 29 | - if string.byte(entry, 1) ~= 46 then | |
| 30 | - local pathfile = rootPath .. '/' .. entry | |
| 31 | - local attrs = lfs.attributes(pathfile) | |
| 32 | - if attrs.mode == 'directory' then | |
| 33 | - modified = travCsv(pathfile, pathes) or modified | |
| 34 | - else | |
| 35 | - local basename, loadname = formatFileName(pathfile) | |
| 36 | - if basename then | |
| 37 | - if tonum(pathes[loadname]) < attrs.modification then | |
| 38 | - modified = true | |
| 39 | - pathes[loadname] = attrs.modification | |
| 40 | - end | |
| 41 | - if basename == "init" or basename == "init_adv" then | |
| 42 | - require(loadname) | |
| 43 | - end | |
| 44 | - end | |
| 45 | - end | |
| 46 | - end | |
| 47 | - end | |
| 48 | - return modified | |
| 49 | -end | |
| 50 | - | |
| 51 | --- 每分钟检查是否有更改 | |
| 52 | -local file2timeMap = {} | |
| 53 | -local function handle_timeout() | |
| 54 | - if travCsv("src/csvdata", file2timeMap) then | |
| 55 | - sharedata.update("csvdata", csvdb) | |
| 56 | - end | |
| 57 | - skynet.timeout(100*5, handle_timeout) | |
| 58 | -end | |
| 59 | - | |
| 60 | --- 重新加载csvdb | |
| 61 | - | |
| 62 | - | |
| 63 | -local CMD = {} | |
| 64 | - | |
| 65 | -function CMD.reload(code) | |
| 66 | - csvdb = csvdb or {} | |
| 67 | - | |
| 68 | - local ok, func = pcall(load, code) | |
| 69 | - if ok then | |
| 70 | - ok = pcall(func) | |
| 71 | - end | |
| 72 | - if not ok then | |
| 73 | - skynet.error("reload_csvdata error by code " .. code) | |
| 74 | - return 'error' | |
| 75 | - end | |
| 76 | - | |
| 77 | - sharedata.update("csvdata", csvdb) | |
| 78 | - return 'ok' | |
| 79 | -end | |
| 80 | - | |
| 81 | ---更新已经存在的 | |
| 82 | -function CMD.hotfix(fieldss) | |
| 83 | - csvdb = csvdb or {} | |
| 84 | - for _, fields in ipairs(fieldss) do | |
| 85 | - if #fields >= 3 and (type(fields[#fields]) == 'number' or type(fields[#fields]) == 'string') then | |
| 86 | - local temp = csvdb | |
| 87 | - local ok = false | |
| 88 | - for i = 1, #fields - 1 do | |
| 89 | - if type(temp) ~= 'table' then break end | |
| 90 | - if type(fields[i]) ~= 'number' and type(fields[i]) ~= 'string' then break end | |
| 91 | - if i == #fields - 1 then | |
| 92 | - if type(temp[fields[i]]) ~= 'number' and type(temp[fields[i]]) ~= 'string' then break end | |
| 93 | - temp[fields[i]] = fields[#fields] | |
| 94 | - else | |
| 95 | - temp = temp[fields[i]] | |
| 96 | - end | |
| 97 | - end | |
| 98 | - end | |
| 99 | - end | |
| 100 | - sharedata.update("csvdata", csvdb) | |
| 101 | - return 'ok' | |
| 102 | -end | |
| 103 | - | |
| 104 | -skynet.start(function () | |
| 105 | - travCsv("src/csvdata", file2timeMap) | |
| 106 | - sharedata.new("csvdata", csvdb) | |
| 107 | - -- handle_timeout() | |
| 108 | - | |
| 109 | - skynet.dispatch("lua", function(_, _, command, ...) | |
| 110 | - local f = CMD[command] | |
| 111 | - skynet.ret(skynet.pack(f(...))) | |
| 112 | - end) | |
| 113 | - | |
| 114 | - skynet.register(".CSVDATA") | |
| 115 | -end) |
src/services/dbseed.lua
| 1 | -require "csvdata.init" | |
| 2 | 1 | require "shared.init" |
| 3 | 2 | require "utils.init" |
| 4 | 3 | require "GlobalVar" |
| ... | ... | @@ -9,7 +8,6 @@ require "skynet.manager" |
| 9 | 8 | skynet = require "skynet" |
| 10 | 9 | |
| 11 | 10 | redisproxy = require("shared.redisproxy") |
| 12 | -globalCsv = require "csvdata/GlobalDefine" | |
| 13 | 11 | |
| 14 | 12 | SendPacket = function ( ... ) end |
| 15 | 13 | |
| ... | ... | @@ -36,7 +34,7 @@ local steps = { |
| 36 | 34 | } |
| 37 | 35 | |
| 38 | 36 | skynet.start(function () |
| 39 | - redisd = skynet.localname(".REDIS") | |
| 37 | + redisd = skynet.localname(".redis") | |
| 40 | 38 | |
| 41 | 39 | redisproxy = require("shared.redisproxy") |
| 42 | 40 | |
| ... | ... | @@ -46,6 +44,8 @@ skynet.start(function () |
| 46 | 44 | skynet.exit() |
| 47 | 45 | return |
| 48 | 46 | end |
| 47 | + csvdb = require "shared.csvdata" | |
| 48 | + globalCsv = csvdb["GlobalDefineCsv"] | |
| 49 | 49 | |
| 50 | 50 | for _, action in ipairs(steps) do |
| 51 | 51 | print(action.desc .. "start ...") | ... | ... |
src/services/globald.lua
src/services/httpweb.lua
| ... | ... | @@ -8,9 +8,8 @@ skynet = require "skynet" |
| 8 | 8 | redisproxy = require "shared.redisproxy" |
| 9 | 9 | netpack = require "skynet.netpack" |
| 10 | 10 | datacenter = require "skynet.datacenter" |
| 11 | -sharedata = require "skynet.sharedata" | |
| 12 | 11 | mcast_util = require "services/mcast_util" |
| 13 | -globalCsv = require "csvdata/GlobalDefine" | |
| 12 | +csvdb = require "shared.csvdata" | |
| 14 | 13 | |
| 15 | 14 | local socket = require "skynet.socket" |
| 16 | 15 | |
| ... | ... | @@ -33,7 +32,7 @@ local string = string |
| 33 | 32 | |
| 34 | 33 | skynet.register_protocol { |
| 35 | 34 | name = "role", |
| 36 | - id = 13, | |
| 35 | + id = 101, | |
| 37 | 36 | pack = skynet.pack, |
| 38 | 37 | unpack = skynet.unpack, |
| 39 | 38 | } |
| ... | ... | @@ -54,8 +53,8 @@ end |
| 54 | 53 | local CMD = require "actions.HttpAction" |
| 55 | 54 | |
| 56 | 55 | local function start() |
| 57 | - redisd = skynet.localname(".REDIS") | |
| 58 | - csvdb = sharedata.query("csvdata") | |
| 56 | + redisd = skynet.localname(".redis") | |
| 57 | + globalCsv = csvdb["GlobalDefineCsv"] | |
| 59 | 58 | |
| 60 | 59 | local listen_socket = socket.listen("0.0.0.0", port) |
| 61 | 60 | print("Listen web port " .. port) | ... | ... |
src/services/logd.lua
src/services/named.lua
| ... | ... | @@ -35,12 +35,13 @@ if mode == "sub" then |
| 35 | 35 | skynet.ret(skynet.pack(f(...))) |
| 36 | 36 | end) |
| 37 | 37 | |
| 38 | - skynet.register(string.format(".NAMED%d", id)) | |
| 38 | + skynet.register(string.format(".named%d", id)) | |
| 39 | 39 | end) |
| 40 | 40 | else |
| 41 | 41 | skynet.start(function() |
| 42 | - local ok, forbidNames = pcall(require, "csvdata.forbid_name") | |
| 43 | - if not ok then forbidNames = {} end | |
| 42 | + local csvdb = require "shared.csvdata" | |
| 43 | + local forbidNames = csvdb["forbid_nameCsv"] | |
| 44 | + if not forbidNames then forbidNames = {} end | |
| 44 | 45 | |
| 45 | 46 | local words = {} |
| 46 | 47 | for _, data in ipairs(forbidNames) do | ... | ... |
src/services/pvpd.lua
| ... | ... | @@ -3,13 +3,12 @@ local json = require("shared.json") |
| 3 | 3 | redisproxy = require("shared.redisproxy") |
| 4 | 4 | local cluster = require "skynet.cluster" |
| 5 | 5 | local serverId = tonumber(skynet.getenv("servId")) |
| 6 | -local sharedata = require "skynet.sharedata" | |
| 7 | 6 | datacenter = require "skynet.datacenter" |
| 7 | +csvdb = require "shared.csvdata" | |
| 8 | 8 | require "shared.init" |
| 9 | 9 | require "utils.init" |
| 10 | 10 | require "RedisKeys" |
| 11 | 11 | require "skynet.manager" |
| 12 | -globalCsv = require "csvdata/GlobalDefine" | |
| 13 | 12 | require "GlobalVar" |
| 14 | 13 | |
| 15 | 14 | |
| ... | ... | @@ -30,7 +29,7 @@ end |
| 30 | 29 | |
| 31 | 30 | skynet.register_protocol { |
| 32 | 31 | name = "role", |
| 33 | - id = 13, | |
| 32 | + id = 101, | |
| 34 | 33 | pack = skynet.pack, |
| 35 | 34 | unpack = skynet.unpack, |
| 36 | 35 | dispatch = function(session, address, submethod, ...) |
| ... | ... | @@ -294,7 +293,9 @@ end |
| 294 | 293 | |
| 295 | 294 | ------------------------------------------------------ |
| 296 | 295 | function CMD.start() |
| 297 | - redisd = skynet.localname(".REDIS") | |
| 296 | + redisd = skynet.localname(".redis") | |
| 297 | + globalCsv = csvdb["GlobalDefineCsv"] | |
| 298 | + | |
| 298 | 299 | pvpInfo = require("models.Pvpd").new({key = "cross:pvpInfo"}) |
| 299 | 300 | pvpInfo:load() |
| 300 | 301 | end |
| ... | ... | @@ -308,9 +309,8 @@ local function __init__() |
| 308 | 309 | skynet.ret(skynet.pack(f(...))) |
| 309 | 310 | end |
| 310 | 311 | end) |
| 311 | - csvdb = sharedata.query("csvdata") | |
| 312 | 312 | |
| 313 | - skynet.register(".PVPCROSS") | |
| 313 | + skynet.register(".pvpcross") | |
| 314 | 314 | end |
| 315 | 315 | |
| 316 | 316 | skynet.start(__init__) | ... | ... |
src/services/redisd.lua
src/services/uniond.lua deleted
| ... | ... | @@ -1,143 +0,0 @@ |
| 1 | -require "shared.init" | |
| 2 | -require "utils.init" | |
| 3 | -require "ProtocolCode" | |
| 4 | -require "GlobalVar" | |
| 5 | -require "RedisKeys" | |
| 6 | -require "skynet.manager" | |
| 7 | - | |
| 8 | -local sharedata = require "sharedata" | |
| 9 | -local redisproxy = require "shared.redisproxy" | |
| 10 | -local datacenter = require "datacenter" | |
| 11 | -local queue = require "skynet.queue" | |
| 12 | - | |
| 13 | -skynet = require "skynet" | |
| 14 | -globalCsv = require "csvdata.GlobalDefine" | |
| 15 | - | |
| 16 | -local table_pack = table.pack | |
| 17 | -local table_unpack = table.unpack | |
| 18 | -local string_format = string.format | |
| 19 | -local pairs = pairs | |
| 20 | -local ipairs = ipairs | |
| 21 | -local tonumber = tonumber | |
| 22 | - | |
| 23 | --- 维护在线状态 | |
| 24 | - | |
| 25 | -local unionInfo, CMD, cs = {}, {} | |
| 26 | - | |
| 27 | -skynet.register_protocol { | |
| 28 | - name = "role", | |
| 29 | - id = 13, | |
| 30 | - pack = skynet.pack, | |
| 31 | - unpack = skynet.unpack, | |
| 32 | -} | |
| 33 | - | |
| 34 | -local function handle_timeout() | |
| 35 | - local now = skynet.timex() | |
| 36 | - unionInfo:onTimer(now) | |
| 37 | - skynet.timeout(100, handle_timeout) | |
| 38 | -end | |
| 39 | - | |
| 40 | ---[[ | |
| 41 | -getProperties({"field1", "field2", ...}) | |
| 42 | -return: {field1 = value1, field2 = value2, ...} | |
| 43 | -------- | |
| 44 | -setProperties({field1 = value1, field2 = value2, ...}) | |
| 45 | -]] | |
| 46 | -function rpcRole(roleId, funcName, ...) | |
| 47 | - if not unionInfo.members[roleId] then return end | |
| 48 | - local serv = unionInfo.members[roleId].serv | |
| 49 | - if serv then | |
| 50 | - if funcName == "getProperties" then | |
| 51 | - return skynet.call(serv, "role", funcName, table_unpack(...)) | |
| 52 | - else | |
| 53 | - return skynet.call(serv, "role", funcName, ...) | |
| 54 | - end | |
| 55 | - else | |
| 56 | - local rediskey = string_format("role:%d", roleId) | |
| 57 | - if funcName == "getProperty" then | |
| 58 | - return redisproxy:hget(rediskey, ...) | |
| 59 | - elseif funcName == "setProperty" then | |
| 60 | - return redisproxy:hset(rediskey, ...) | |
| 61 | - elseif funcName == "getProperties" then | |
| 62 | - local sRole = require "models.Role" | |
| 63 | - local fields = table_pack(...) | |
| 64 | - local rets = redisproxy:hmget(rediskey, table_unpack(fields, 1, fields.n)) | |
| 65 | - local result = {} | |
| 66 | - for i=1, fields.n do | |
| 67 | - local typ = sRole.schema[fields[i]][1] | |
| 68 | - local def = sRole.schema[fields[i]][2] | |
| 69 | - if typ == "number" then | |
| 70 | - result[fields[i]] = tonumber(rets[i] or def) | |
| 71 | - else | |
| 72 | - result[fields[i]] = rets[i] | |
| 73 | - end | |
| 74 | - end | |
| 75 | - return result | |
| 76 | - elseif funcName == "setProperties" then | |
| 77 | - local fields = ... | |
| 78 | - local params = {} | |
| 79 | - for field, value in pairs(fields) do | |
| 80 | - params[#params+1] = field | |
| 81 | - params[#params+1] = value | |
| 82 | - end | |
| 83 | - return redisproxy:hmset(rediskey, table_unpack(params)) | |
| 84 | - end | |
| 85 | - end | |
| 86 | -end | |
| 87 | - | |
| 88 | --- 加载联盟数据 | |
| 89 | -function CMD.load(unionId) | |
| 90 | - unionInfo = require("models.Union").new({key = UNION_KEY:format(unionId)}) | |
| 91 | - unionInfo:load() | |
| 92 | - unionInfo:loadMembers() | |
| 93 | -end | |
| 94 | - | |
| 95 | --- 创建一个联盟 | |
| 96 | -function CMD.new(roleId) | |
| 97 | - local unionId = redisproxy:hincrby("autoincrement_set", "union", 1) | |
| 98 | - unionInfo = require("models.Union").new({ | |
| 99 | - key = UNION_KEY:format(unionId), | |
| 100 | - master = roleId | |
| 101 | - }) | |
| 102 | - unionInfo:create() | |
| 103 | - unionInfo:addMember(roleId, true) | |
| 104 | - redisproxy:sadd(UNION_SET, unionId) | |
| 105 | - return unionId | |
| 106 | -end | |
| 107 | - | |
| 108 | --- 登录/登出 需要向联盟服务报备 | |
| 109 | -function CMD.sign(cmd, roleId, serv) | |
| 110 | - if not unionInfo.members[roleId] then return end | |
| 111 | - if cmd == "login" then | |
| 112 | - unionInfo.members[roleId].serv = serv | |
| 113 | - elseif cmd == "logout" then | |
| 114 | - unionInfo.members[roleId].serv = nil | |
| 115 | - end | |
| 116 | -end | |
| 117 | - | |
| 118 | -local function __init__() | |
| 119 | - skynet.dispatch("lua", function(_, _, command, ...) | |
| 120 | - cs(function (...) | |
| 121 | - if CMD[command] then | |
| 122 | - skynet.ret(skynet.pack(CMD[command](...))) | |
| 123 | - return | |
| 124 | - else | |
| 125 | - if unionInfo and unionInfo[submethod] then | |
| 126 | - if command == 'dismiss' then | |
| 127 | - unionInfo:dismiss(...) | |
| 128 | - return | |
| 129 | - end | |
| 130 | - local result = unionInfo[submethod](unionInfo, ...) | |
| 131 | - skynet.ret(skynet.pack(result)) | |
| 132 | - return | |
| 133 | - end | |
| 134 | - end | |
| 135 | - skynet.error("uniond commond error cmd=", command) | |
| 136 | - end) | |
| 137 | - end) | |
| 138 | - skynet.register("UNIOND") | |
| 139 | - csvdb = sharedata.query("csvdata") | |
| 140 | - cs = queue() | |
| 141 | -end | |
| 142 | - | |
| 143 | -skynet.start(__init__) | |
| 144 | 0 | \ No newline at end of file |
src/services/watchdog.lua
| ... | ... | @@ -9,6 +9,7 @@ local agent_ctrl = require "services.agent_ctrl" |
| 9 | 9 | local xxtea = require "xxtea" |
| 10 | 10 | local mc = require "skynet.multicast" |
| 11 | 11 | local cluster = require "skynet.cluster" |
| 12 | +local csvdata = require "shared.csvdata" | |
| 12 | 13 | |
| 13 | 14 | require "ProtocolCode" |
| 14 | 15 | require "GlobalVar" |
| ... | ... | @@ -107,12 +108,12 @@ skynet.start(function() |
| 107 | 108 | skynet.ret(skynet.pack(f(subcmd, ...))) |
| 108 | 109 | end |
| 109 | 110 | end) |
| 110 | - skynet.register ".WATCHDOG" | |
| 111 | + skynet.register ".watchdog" | |
| 111 | 112 | -- 数据库服务 |
| 112 | 113 | redisd = skynet.newservice("services/redisd") |
| 113 | 114 | |
| 114 | - -- load all csv data | |
| 115 | - skynet.newservice("services/csvdatad") | |
| 115 | + -- 提前加载好 | |
| 116 | + csvdata.init() | |
| 116 | 117 | print("launch csvdatad ...") |
| 117 | 118 | |
| 118 | 119 | -- 日志服务 | ... | ... |
| ... | ... | @@ -0,0 +1,183 @@ |
| 1 | +-- 对sharetable 的一层封装 实现热更新 | |
| 2 | + | |
| 3 | +local skynet = require "skynet" | |
| 4 | +local service = require "skynet.service" | |
| 5 | +local sharetable = require "skynet.sharetable" | |
| 6 | + | |
| 7 | +local function sharetable_service() | |
| 8 | + local skynet = require "skynet" | |
| 9 | + local sharetable = require "skynet.sharetable" | |
| 10 | + | |
| 11 | + local files = {} -- filename | |
| 12 | + local csvdata = {} | |
| 13 | + | |
| 14 | + local initfile = { | |
| 15 | + ["src/csvdata/init.lua"] = {}, | |
| 16 | + ["src/csvdata/init_adv.lua"] = {}, | |
| 17 | + } | |
| 18 | + | |
| 19 | + function csvdata.query(source, filename) | |
| 20 | + files[filename] = files[filename] or {} | |
| 21 | + files[filename][source] = 1 | |
| 22 | + end | |
| 23 | + | |
| 24 | + function csvdata.close(source) | |
| 25 | + for filename, info in pairs(files) do | |
| 26 | + info[source] = nil | |
| 27 | + end | |
| 28 | + end | |
| 29 | + | |
| 30 | + function csvdata.realName(file) | |
| 31 | + if string.match(file, "Csv$") then | |
| 32 | + if initfile["src/csvdata/init.lua"] and initfile["src/csvdata/init.lua"][file] then | |
| 33 | + file = initfile["src/csvdata/init.lua"][file] | |
| 34 | + elseif initfile["src/csvdata/init_adv.lua"] and initfile["src/csvdata/init_adv.lua"][file] then | |
| 35 | + file = initfile["src/csvdata/init_adv.lua"][file] | |
| 36 | + end | |
| 37 | + file = "src/" .. file .. ".lua" | |
| 38 | + end | |
| 39 | + return file | |
| 40 | + end | |
| 41 | + | |
| 42 | + function csvdata.hotfix(_, ...) | |
| 43 | + local now = skynet.timex() | |
| 44 | + | |
| 45 | + local filenames = {...} | |
| 46 | + | |
| 47 | + local needHotfix = {} | |
| 48 | + for _, filename in ipairs(filenames) do | |
| 49 | + -- 更新下 | |
| 50 | + skynet.error(string.format("hotfix_csvdata time: %s, file: %s", now, filename)) | |
| 51 | + | |
| 52 | + if initfile[filename] then | |
| 53 | + sharetable.loadfile(filename) | |
| 54 | + sharetable.update(filename) | |
| 55 | + else | |
| 56 | + sharetable.loadfile(csvdata.realName(filename)) | |
| 57 | + end | |
| 58 | + | |
| 59 | + if files[filename] then | |
| 60 | + for source, _ in pairs(files[filename]) do | |
| 61 | + needHotfix[source] = needHotfix[source] or {} | |
| 62 | + table.insert(needHotfix[source], filename) | |
| 63 | + end | |
| 64 | + end | |
| 65 | + end | |
| 66 | + | |
| 67 | + for source, files in pairs(needHotfix) do | |
| 68 | + skynet.send(source, "csvdata", "hotfix", table.unpack(files)) | |
| 69 | + end | |
| 70 | + end | |
| 71 | + | |
| 72 | + skynet.dispatch("lua", function(_,source,cmd,...) | |
| 73 | + skynet.ignoreret() | |
| 74 | + csvdata[cmd](source,...) | |
| 75 | + end) | |
| 76 | + | |
| 77 | + skynet.register_protocol { | |
| 78 | + name = "csvdata", | |
| 79 | + id = 102, | |
| 80 | + pack = skynet.pack, | |
| 81 | + unpack = skynet.unpack, | |
| 82 | + } | |
| 83 | + | |
| 84 | + -- 初始化csvdata | |
| 85 | + skynet.start(function() | |
| 86 | + for file, _ in pairs(initfile) do | |
| 87 | + sharetable.loadfile(file) | |
| 88 | + local init = sharetable.query(file) | |
| 89 | + if not init then | |
| 90 | + error("csvdata load init file error " .. file) | |
| 91 | + return | |
| 92 | + end | |
| 93 | + initfile[file] = init | |
| 94 | + for _, one in pairs(init) do | |
| 95 | + sharetable.loadfile("src/" .. one .. ".lua") | |
| 96 | + end | |
| 97 | + end | |
| 98 | + end) | |
| 99 | +end | |
| 100 | + | |
| 101 | + | |
| 102 | +local cache = {} | |
| 103 | +local csvdata | |
| 104 | + | |
| 105 | +local function load_csvdata(t, key) | |
| 106 | + if key == "address" then | |
| 107 | + t.address = service.new("csvdata", sharetable_service) | |
| 108 | + return t.address | |
| 109 | + else | |
| 110 | + if cache[key] then return cache[key] end | |
| 111 | + | |
| 112 | + local realName = csvdata.realName(key) | |
| 113 | + | |
| 114 | + local tab = sharetable.query(realName) | |
| 115 | + if not tab then | |
| 116 | + error("dont have csvdata : " .. realName) | |
| 117 | + return | |
| 118 | + end | |
| 119 | + cache[key] = tab | |
| 120 | + -- 增加引用 | |
| 121 | + skynet.send(csvdata.address, "lua", "query", key) | |
| 122 | + return tab | |
| 123 | + end | |
| 124 | +end | |
| 125 | + | |
| 126 | +local function close_hotfix(t) | |
| 127 | + local addr = rawget(t, "address") | |
| 128 | + if addr then | |
| 129 | + skynet.send(addr, "lua", "close") | |
| 130 | + end | |
| 131 | +end | |
| 132 | + | |
| 133 | +csvdata = setmetatable ( {} , { | |
| 134 | + __index = load_csvdata, | |
| 135 | + __gc = close_hotfix, | |
| 136 | +}) | |
| 137 | + | |
| 138 | +function csvdata.init() | |
| 139 | + return csvdata.address | |
| 140 | +end | |
| 141 | + | |
| 142 | +function csvdata.hotfix(...) | |
| 143 | + skynet.send(csvdata.address, "lua", "hotfix", ...) | |
| 144 | +end | |
| 145 | + | |
| 146 | +function csvdata.realName(file) | |
| 147 | + if string.match(file, "Csv$") then | |
| 148 | + if csvdata["src/csvdata/init.lua"] and csvdata["src/csvdata/init.lua"][file] then | |
| 149 | + file = csvdata["src/csvdata/init.lua"][file] | |
| 150 | + elseif csvdata["src/csvdata/init_adv.lua"] and csvdata["src/csvdata/init_adv.lua"][file] then | |
| 151 | + file = csvdata["src/csvdata/init_adv.lua"][file] | |
| 152 | + end | |
| 153 | + file = "src/" .. file .. ".lua" | |
| 154 | + end | |
| 155 | + return file | |
| 156 | +end | |
| 157 | + | |
| 158 | +local function update(file) | |
| 159 | + -- 更新一下 | |
| 160 | + if cache[file] then | |
| 161 | + sharetable.update(csvdata.realName(file)) | |
| 162 | + end | |
| 163 | +end | |
| 164 | + | |
| 165 | +skynet.register_protocol { | |
| 166 | + name = "csvdata", | |
| 167 | + id = 102, | |
| 168 | + pack = skynet.pack, | |
| 169 | + unpack = skynet.unpack, | |
| 170 | + dispatch = function(_, _, cmd, ...) | |
| 171 | + skynet.ignoreret() | |
| 172 | + if cmd == "hotfix" then | |
| 173 | + -- skynet.error("csvdara hotfix update", ...) | |
| 174 | + local files = {...} | |
| 175 | + for _, file in ipairs(files) do | |
| 176 | + update(file) | |
| 177 | + end | |
| 178 | + return | |
| 179 | + end | |
| 180 | + end, | |
| 181 | +} | |
| 182 | + | |
| 183 | +return csvdata | ... | ... |