rewrite watchdog/agent, use new gate

This commit is contained in:
Cloud Wu
2014-04-14 14:21:31 +08:00
parent 476fd10889
commit 1e9a27232b
5 changed files with 151 additions and 58 deletions

View File

@@ -1,5 +1,7 @@
local skynet = require "skynet"
local client = ...
local client
local CMD = {}
skynet.register_protocol {
name = "client",
@@ -8,12 +10,25 @@ skynet.register_protocol {
unpack = skynet.tostring,
dispatch = function (session, address, text)
-- It's client, there is no session
-- skynet.send("LOG", "text", "client message :" .. text)
local result = skynet.call("SIMPLEDB", "text", text)
skynet.ret(result)
end
}
skynet.start(function()
function CMD.start(gate , fd)
client = skynet.launch("client", fd)
skynet.call(gate, "lua", "forward", fd, client)
skynet.send(client,"text","Welcome to skynet")
end
function CMD.exit()
skynet.kill(client)
client = nil
end
skynet.start(function()
skynet.dispatch("lua", function(_,_, command, ...)
local f = CMD[command]
skynet.ret(skynet.pack(f(...)))
end)
end)

View File

@@ -9,9 +9,12 @@ skynet.start(function()
local lualog = skynet.newservice("lualog")
local console = skynet.newservice("console")
-- skynet.newservice("debug_console",8000)
local watchdog = skynet.newservice("watchdog","8888", max_client, 0)
local db = skynet.newservice("simpledb")
-- skynet.launch("snlua","testgroup")
skynet.newservice("simpledb")
local watchdog = skynet.newservice("watchdog")
skynet.call(watchdog, "lua", "start", {
port = 8888,
maxclient = max_client,
})
skynet.exit()
end)

View File

@@ -1,58 +1,49 @@
local skynet = require "skynet"
local port, max_agent, buffer = ...
local command = {}
local agent_all = {}
local CMD = {}
local SOCKET = {}
local gate
local agent = {}
function command:open(parm)
local fd,addr = string.match(parm,"(%d+) ([^%s]+)")
fd = tonumber(fd)
print("agent open",self,string.format("%d %d %s",self,fd,addr))
local client = skynet.launch("client",fd)
local agent = skynet.launch("snlua","agent",skynet.address(client))
if agent then
agent_all[self] = { agent , client }
skynet.send(gate, "text", "forward" , self, skynet.address(agent) , skynet.address(client))
function SOCKET.open(fd, addr)
agent[fd] = skynet.newservice("agent")
skynet.call(agent[fd], "lua", "start", gate, fd)
end
local function close_agent(fd)
local a = agent[fd]
if a then
skynet.call(a, "lua", "exit")
skynet.kill(a)
agent[fd] = nil
end
end
function command:close()
print("agent close",self,string.format("close %d",self))
local agent = agent_all[self]
agent_all[self] = nil
skynet.kill(agent[1])
skynet.kill(agent[2])
function SOCKET.close(fd)
print("socket close",fd)
close_agent(fd)
end
function command:data(data, session)
local agent = agent_all[self]
if agent then
-- PTYPE_CLIENT = 3 , read skynet.h
skynet.redirect(agent[1], agent[2], "client", 0, data)
else
skynet.error(string.format("agent data drop %d size=%d",self,#data))
end
function SOCKET.error(fd, msg)
print("socket error",fd, msg)
close_agent(fd)
end
skynet.register_protocol {
name = "client",
id = skynet.PTYPE_CLIENT,
}
function CMD.start(conf)
skynet.call(gate, "lua", "open" , conf)
end
skynet.start(function()
skynet.dispatch("text", function(session, from, message)
local id, cmd , parm = string.match(message, "(%d+) (%w+) ?(.*)")
id = tonumber(id)
local f = command[cmd]
if f then
f(id,parm,session)
skynet.dispatch("lua", function(session, source, cmd, subcmd, ...)
if cmd == "socket" then
local f = SOCKET[subcmd]
f(...)
-- socket api don't need return
else
error(string.format("[watchdog] Unknown command : %s",message))
local f = assert(CMD[cmd])
skynet.ret(skynet.pack(f(subcmd, ...)))
end
end)
-- 0 for default client tag
gate = skynet.launch("gate" , "S" , skynet.address(skynet.self()), port, 0, max_agent, buffer)
skynet.send(gate,"text", "start")
skynet.register(".watchdog")
gate = skynet.newservice("gate")
end)