local skynet = require "skynet" require "skynet.manager" local socket = require "skynet.socket" local netpack = require "skynet.netpack" local datacenter = require "skynet.datacenter" local snax = require "skynet.snax" local agent_ctrl = require "services.agent_ctrl" local xxtea = require "xxtea" local mc = require "skynet.multicast" local cluster = require "skynet.cluster" local csvdata = require "shared.csvdata" require "ProtocolCode" require "GlobalVar" require "shared.init" require "utils.init" local CMD, SOCKET = {}, {} local globald, pvpd local pool_size = tonumber(...) function SOCKET.open(fd, addr) skynet.call(gate_serv, "lua", "accept" , fd) agent_ctrl:socket_open(fd, addr) end function SOCKET.close(fd) print("socket close", fd) agent_ctrl:socket_close(fd) end function SOCKET.error(fd, msg) print("socket error",fd, msg) agent_ctrl:socket_error(fd) end function SOCKET.data(fd, msg) if #msg < 2 then return end local cmd = string.unpack("H", string.sub(msg, 1, 2)) if cmd == actionCodes.Role_queryLoginRpc then local data = MsgPack.unpack(xxtea.decrypt(string.sub(msg, 3), XXTEA_KEY)) -- TODO: 先检测uid的合法性 if not data or not data.uid then return end agent_ctrl:query_agent(fd, data.uid) end end -- @desc: agent状态定时检测 function check_agent_status() pcall(agent_ctrl.check_agent_status, agent_ctrl) skynet.timeout(100, check_agent_status) end -- 创建world以及union channel 用于广播 function create_mutilcast() for i = 1, 10 do local chan_w = mc:new() datacenter.set("MC_W_CHANNEL" .. i, chan_w.channel) end end function CMD.start(conf) skynet.call(gate_serv, "lua", "open" , conf) skynet.call(pvpd, "lua", "start") skynet.call(capsuled, "lua", "start") -- 开启agent状态检测定时器 check_agent_status() -- 创建广播服务 create_mutilcast() skynet.call(conf.httpd, "lua", "start") -- roomServer = skynet.newservice("services/roomServer") -- skynet.call(roomServer, "lua", "start") skynet.newservice("services/dbseed") globald = skynet.newservice("services/globald") skynet.call(globald, "lua", "start") local servId = tonumber(skynet.getenv("servId")) cluster.open("server" .. servId) end function CMD.forceClose(fd) agent_ctrl:exit_agent(fd) end function CMD.hotfix(code) agent_ctrl:hotfix(code) end skynet.start(function() skynet.dispatch("lua", function(session, source, cmd, subcmd, ...) if cmd == "socket" then local f = SOCKET[subcmd] f(...) -- socket api don't need return else local f = assert(CMD[cmd]) skynet.ret(skynet.pack(f(subcmd, ...))) end end) skynet.register ".watchdog" -- 提前加载好 csvdata.init() print("launch csvdatad ...") -- pvp 服务 pvpd = skynet.newservice("services/pvpd") cluster.register("pvpd", pvpd) local poold = skynet.newservice("services/poold") local obj = skynet.call(poold, "lua", "start", pool_size) agent_ctrl:init(obj, poold) print(string.format("launch %d agent at the beginning", pool_size)) -- 全局工具函数 skynet.newservice("services/named") skynet.newservice("services/chated") -- 网关服务 gate_serv = skynet.newservice("gate") capsuled = skynet.newservice("services/capsuled") end)