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)
-- lualib/snax/gateserver.lua skynet.register_protocol { name = "socket", id = skynet.PTYPE_SOCKET, -- PTYPE_SOCKET = 6 unpack = function( msg, sz ) return netpack.filter( queue, msg, sz) end, dispatch = function(_, _, q, type, ...) queue = q iftypethen MSG[type](...) end end }
skynet.start(function() skynet.dispatch("lua", function(_, address, cmd, ...) local f = CMD[cmd] if f then skynet.ret(skynet.pack(f(address, ...))) else skynet.ret(skynet.pack(handler.command(cmd, address, ...))) end end) end)
functionhandler.open(source, conf) watchdog = conf.watchdog or source end
functionhandler.message(fd, msg, sz) -- recv a package, forward it local c = connection[fd] local agent = c.agent if agent then skynet.redirect(agent, c.client, "client", 1, msg, sz) else skynet.send(watchdog, "lua", "socket", "data", fd, netpack.tostring(msg, sz)) end end
functionhandler.connect(fd, addr) local c = { fd = fd, ip = addr, } connection[fd] = c skynet.send(watchdog, "lua", "socket", "open", fd, addr) end
skynet.register_protocol { name = "socket", id = skynet.PTYPE_SOCKET, -- PTYPE_SOCKET = 6 unpack = function( msg, sz ) return netpack.filter( queue, msg, sz) end, dispatch = function(_, _, q, type, ...) queue = q iftypethen MSG[type](...) end end }
skynet.start(function() skynet.dispatch("lua", function(_, address, cmd, ...) local f = CMD[cmd] if f then skynet.ret(skynet.pack(f(address, ...))) else skynet.ret(skynet.pack(handler.command(cmd, address, ...))) end end) end)
local handler = { open, -- serivce start message, -- data connect, -- new connect disconnect, -- client close error, -- 错误 warning, command, -- 自定义命令 }
完全不用关注 gateserver.lua中定义的那些函数,我们只关注一下我们自己定义的。
local handler = {}
functionhandler.open(source, conf) watchdog = conf.watchdog or source end
functionhandler.connect(fd, addr) local c = { fd = fd, ip = addr, } connection[fd] = c skynet.send(watchdog, "lua", "socket", "open", fd, addr) end
functionhandler.message(fd, msg, sz) -- recv a package, forward it local c = connection[fd] local agent = c.agent if agent then skynet.redirect(agent, c.client, "client", 1, msg, sz) else skynet.send(watchdog, "lua", "socket", "data", fd, netpack.tostring(msg, sz)) end end
functionhandler.disconnect(fd) close_fd(fd) skynet.send(watchdog, "lua", "socket", "close", fd) end
functionhandler.error(fd, msg) close_fd(fd) skynet.send(watchdog, "lua", "socket", "error", fd, msg) end
functionhandler.warning(fd, size) skynet.send(watchdog, "lua", "socket", "warning", fd, size) end
skynet.register_protocol { name = "client", id = skynet.PTYPE_CLIENT, unpack = function(msg, sz) return host:dispatch(msg, sz) end, dispatch = function(fd, _, type, ...) assert(fd == client_fd) -- You can use fd to reply message skynet.ignoreret() -- session is fd, don't call skynet.ret skynet.trace() iftype == "REQUEST"then local ok, result = pcall(request, ...) if ok then if result then send_package(result) end else skynet.error(result) end else assert(type == "RESPONSE") error"This example doesn't support request client" end end }