mcast_util.lua
1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
local _M = {}
local mc = require "skynet.multicast"
local datacenter = require "skynet.datacenter"
local skynet = require "skynet"
local chan_w, chan_g
local chan_ws = {}
function _M.sub_world(w_channel)
	chan_w = mc.new {
		channel = w_channel,
		dispatch = function (channel, source, ...)
			if select("#", ...) == 0 then
				return
			end
			local actionCode, bin = ...
			if SendPacket then
				SendPacket(actionCode, bin)
			end
		end
	}
	chan_w:subscribe()
end
function _M.channel_world()
	return chan_w
end
function _M.usub_world()
	if chan_w then
		chan_w:unsubscribe()
		chan_w = nil
	end
end
function _M.sub_worlds(w_channels)
	for _, cell in pairs(w_channels) do
		local chan = mc.new {
			channel = cell,
			dispatch = function (channel, source, ...)
				if select("#", ...) == 0 then
					return
				end
				local actionCode, bin = ...
				if SendPacket then
					SendPacket(actionCode, bin)
				end
			end
		}
		chan:subscribe()
		table.insert(chan_ws, chan)
	end
end
function _M.pub_world(actionCode, bin)
	if not bin then
		return
	end
	if #chan_ws > 0 then
		for _, chan in pairs(chan_ws) do
			chan:publish(actionCode, bin)
		end
		return
	end
	if not bin or not chan_w then return end
	chan_w:publish(actionCode, bin)
end
function _M.sub_union(gid, chan)
	chan_g = mc.new {
		channel = chan,
		dispatch = function (channel, source, ...)
			if select("#", ...) == 0 then
				return
			end
			local actionCode, bin = ...
			if SendPacket then
				SendPacket(actionCode, bin)
			end
		end
	}
	chan_g:subscribe()
end
function _M.usub_union()
	if chan_g then
		chan_g:unsubscribe()
		chan_g = nil
	end
end
function _M.pub_union(actionCode, bin)
	if not bin or not chan_g then return end
	chan_g:publish(actionCode, bin)
end
return _M