global group multicast

This commit is contained in:
云风
2012-08-30 23:31:33 +08:00
parent 161528e686
commit 1ee802c5c5
17 changed files with 285 additions and 40 deletions

58
service/group_agent.lua Normal file
View File

@@ -0,0 +1,58 @@
local skynet = require "skynet"
local command = {}
local group = {}
function command.CREATE(id)
assert(group[id] == nil)
group[id] = {}
local recv_id = id * 2
local addr = skynet.query_group(recv_id)
skynet.ret(skynet.pack(addr))
end
function command.AGENT(id, handle)
local tunnel = skynet.launch("tunnel", skynet.address(handle))
assert(tunnel)
local send_id = id * 2 + 1
skynet.enter_group(send_id, tunnel)
table.insert(group[id] , tunnel)
end
function command.ENTER(id, handle)
local recv_id = id * 2
local send_id = id * 2 + 1
skynet.enter_group(recv_id, handle)
skynet.enter_group(send_id, handle)
end
function command.LEAVE(id, handle)
local recv_id = id * 2
local send_id = id * 2 + 1
skynet.leave_group(recv_id, handle)
skynet.leave_group(send_id, handle)
end
function command.CLEAR(id)
local g = group[id]
assert(g)
local recv_id = id * 2
local send_id = id * 2 + 1
skynet.clear_group(recv_id)
skynet.clear_group(send_id)
group[id] = nil
for _,v in ipairs(g) do
skynet.kill(v)
end
end
skynet.dispatch(function (msg,sz)
local cmd , id , handle = skynet.unpack(msg,sz)
local f = command[cmd]
assert(f, cmd)
f(id,handle)
end)
skynet.start(function()
skynet.send("GROUPMGR" , 0, skynet.pack("MASTER", skynet.self()))
end)