mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-23 19:43:09 +00:00
global group multicast
This commit is contained in:
58
service/group_agent.lua
Normal file
58
service/group_agent.lua
Normal 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)
|
||||
Reference in New Issue
Block a user