Blame view

src/services/mcast_util.lua 2.06 KB
314bc5df   zhengshouren   提交服务器初始代码
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
101
102
103
104
105
106
107
108
109
  
  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.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
  
  function _M.pub_person(fromFd, toRoleId, actionCode, bin)
  	if 0 == redisproxy:exists(string.format("role:%d", toRoleId)) then
  		return SYS_ERROR_CHAT_NOT_EXIST
  	end
  	-- 若在线,实时发送聊天信息
  	local agent = datacenter.get("agent", toRoleId)
  	if agent then
  		SendPacket(actionCode, bin, agent.fd)
  		SendPacket(actionCode, bin, fromFd)
  		return
  	end
  	return SYS_ERROR_CHAT_NOT_ONLINE
  end
  
  return _M