Support message redirect

This commit is contained in:
云风
2012-08-09 21:50:33 +08:00
parent d3f92b32d5
commit 24790ca5e4
9 changed files with 101 additions and 57 deletions

View File

@@ -4,41 +4,45 @@ local command = {}
local agent_all = {}
local gate = skynet.launch("gate" , skynet.self(), ...)
print("gate",gate)
function command:open(parm)
local fd,addr = string.match(parm,"(%d+) ([^%s]+)")
fd = tonumber(fd)
skynet.send("LOG", string.format("%d %d %s",self,fd,addr))
print("agent open",self,string.format("%d %d %s",self,fd,addr))
local agent = skynet.launch("snlua","agent")
local client = skynet.launch("client",fd)
skynet.send("LOG", "client " .. client)
local agent = skynet.launch("snlua","agent",client)
print("watchdog launch agent client:",agent,client)
if agent then
agent_all[self] = agent
skynet.send(gate, "forward ".. self .. " " .. agent)
agent_all[self] = { agent , client }
skynet.send(gate, "forward ".. self .. " " .. agent .. " " .. client)
end
end
function command:close()
skynet.send("LOG", string.format("close %d",self))
skynet.send(agent_all[self],-1,"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])
end
function command:data(data)
function command:data(data, session)
local agent = agent_all[self]
if agent then
skynet.send(agent, data)
skynet.redirect(agent[1], agent[2], session, data)
else
skynet.send("LOG", string.format("data %d size=%d",self,#data))
skynet.error(string.format("agent data drop %d size=%d",self,#data))
end
end
skynet.dispatch(function(msg, sz)
skynet.dispatch(function(msg, sz, session, address)
local message = skynet.tostring(msg,sz)
local id, cmd , parm = string.match(message, "(%d+) (%w+) ?(.*)")
id = tonumber(id)
local f = command[cmd]
if f then
f(id,parm)
f(id,parm,session)
else
skynet.error(string.format("[watchdog] Unknown command : %s",message))
end