Blame view

src/agent.lua 8.22 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
  require "ProtocolCode"
  require "shared.init"
  require "utils.init"
  require "GlobalVar"
  require "RedisKeys"
  require "skynet.manager"
  
  local harbor = require "skynet.harbor"
  local queue = require "skynet.queue"
  local netpack = require "skynet.netpack"
  local socket = require "skynet.socket"
  local sharedata = require "skynet.sharedata"
  local xxtea = require "xxtea"
  
  skynet = require "skynet"
  redisproxy = require "shared.redisproxy"
  datacenter = require "skynet.datacenter"
  mcast_util = require "services/mcast_util"
  globalCsv = require "csvdata/GlobalDefine"
  
  local CMD = {}
  local agentInfo = {}  -- { client_fd, role, gate_serv, open_timer}
  
  local agent_util, cs
  
  --- {{{ 定时器相关
  local function handle_timeout()
  	if not agentInfo.open_timer then return end
  
  	if not agentInfo.role then
  		skynet.timeout(100, handle_timeout)
  		return
  	end
  
  	agent_util:update(agentInfo)
  	skynet.timeout(100, handle_timeout)
  end
  
  function start_agent_timer()
  	agentInfo.open_timer = true
  	skynet.timeout(150, handle_timeout)
  end
  
  function cancel_agent_timer()
  	agentInfo.open_timer = false
  end
  ---- 定时器相关 }}}
  
23d89d13   zhouahaihai   冒险 结构
49
  local _pipelinings = {}  --可同时多个序列  栈的形式保证嵌套不出错
314bc5df   zhengshouren   提交服务器初始代码
50
  function SendPacket(actionCode, bin, client_fd)
23d89d13   zhouahaihai   冒险 结构
51
52
  	client_fd = client_fd or agentInfo.client_fd
  	
314bc5df   zhengshouren   提交服务器初始代码
53
54
55
56
  	local handlerName = actionHandlers[actionCode]
  	if string.sub(handlerName, -3, -1) == "Rpc" then
  		actionCode = actionCode + rpcResponseBegin
  	end
23d89d13   zhouahaihai   冒险 结构
57
58
59
60
61
62
63
64
65
66
67
  	--  查看是否是在 流水线操作中
  	if #_pipelinings > 0 then
  		local _curPipelining = _pipelinings[#_pipelinings]
  		_curPipelining[client_fd] = _curPipelining[client_fd] or {} --区分不同客户端
  		table.insert(_curPipelining[client_fd], {actionCode, bin})
  	else
  		if #bin > 0 then bin = xxtea.encrypt(bin, XXTEA_KEY) end
  		local head = string.pack("H", actionCode)
  		return socket.write(client_fd, netpack.pack(head .. bin))
  	end
  end
314bc5df   zhengshouren   提交服务器初始代码
68
  
23d89d13   zhouahaihai   冒险 结构
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
  function SendPipelining(callback)
  	if type(callback) ~= "function" then return end
  	--push 当前队列
  	table.insert(_pipelinings, {})
  	-- 执行代码块  输出错误信息
  	local ok, err = pcall(callback)
  	--弹出当前队列
  	local curPipelining = table.remove(_pipelinings)
  	-- 查看是否有消息
  	if next(curPipelining) then  
  		for client_fd, msg in pairs(curPipelining) do
  			SendPacket(actionCodes.Role_pipelining, MsgPack.pack(msg), client_fd)
  		end
  	end
  	if not ok then error(err) end
314bc5df   zhengshouren   提交服务器初始代码
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
  end
  
  function rpcAgent(roleId, funcName, ...)
  	local agent = datacenter.get("agent", roleId)
  	if agent then
  		return skynet.call(agent.serv, "lua", funcName, ...)
  	end
  end
  
  function rpcParter(serv, func, ...)
  	if serv then
  		local ok, result = pcall(skynet.call, serv, "role", func, ...)
  		if ok then
  			return result
  		end
  	end
  end
  
  local string_format = string.format
  local table_unpack = table.unpack
  function rpcRole(roleId, funcName, ...)
  	local fields = ...
  	local agent = datacenter.get("agent", roleId)
  	if agent and agent.serv then
  		if funcName == "getProperties" then
  			return true, skynet.call(agent.serv, "role", funcName, fields)
  		else
  			return true, skynet.call(agent.serv, "role", funcName, ...)
  		end
  	else
  		local rediskey = string_format("role:%d", roleId)
  		if funcName == "setProperty" then
  			return false, redisproxy:hset(rediskey, ...)
  		elseif funcName == "getProperty" then
  			return false, redisproxy:hget(rediskey, ...)
  		elseif funcName == "getProperties" then
  			local sRole = require("models.Role")
  			local returnValue = redisproxy:hmget(rediskey, table_unpack(...))
  			local ret = {}
  			for index, key in ipairs(fields) do
  			    local typ = sRole.schema[key][1]
  			    local def = sRole.schema[key][2]
  			    if typ == "number" then
  			    	ret[key] = tonumber(returnValue[index] or def)
  				else
  			    	ret[key] = returnValue[index]
  			    end
  			end
  			return false, ret
  		elseif funcName == "setProperties" then
  			local result = {}
  			for k,v in pairs(fields) do
  				result[#result+1] = k
  				result[#result+1] = v
  			end
  			return false, redisproxy:hmset(rediskey, table_unpack(result))
  		end
  	end
  end
  
  function rpcUnion(funcName, ...)
  	local serv = agentInfo.userv
  	if not serv then
  		local consortiaId = agentInfo.role:getProperty("consortiaId")
  		if consortiaId == 0 then return true,1000 end
  		local union = datacenter.get("union", consortiaId)
  		if not union or not union.serv then
  			skynet.error("rpcUnion error: union serv addr not exist", funcName, consortiaId)
  			return
  		end
  		serv = union.serv
  	end
  	return skynet.call(serv, "lua", funcName, ...)
  end
  
  function rpcOtherUnion(id, funcName, ...)
  	local union = datacenter.get("union", id)
  	if union and union.serv then
  		return skynet.call(union.serv, "lua", funcName, ...)
  	end
  end
  
  skynet.register_protocol {
  	name = "client",
  	id = skynet.PTYPE_CLIENT,
  	unpack = function (msg, sz)
  		local data = skynet.tostring(msg, sz)
  		local cmd = string.unpack("H", string.sub(data, 1, 2))
  		return cmd, string.sub(data, 3)
  	end,
  	dispatch = function(session, address, cmd, data)
188558e8   zhouahaihai   skynet 1.2.0 use...
175
  		skynet.ignoreret()
9ad697c8   zhouahaihai   删除 fields ,增加数据库...
176
  		-- skynet.trace() --执行序的跟踪统计
314bc5df   zhengshouren   提交服务器初始代码
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
  		cs(function()
  			if cmd == actionCodes.Sys_heartBeat then
  				agent_util:heart_beat(agentInfo)
  				return
  			end
  			local actionName = actionHandlers[cmd]
  			if not actionName then
  				print("actionName not exist", actionName)
  				return
  			end
  			local modName, funcName = actionName:match("(%w+)%.(%w+)")
  
  			local ok, action = pcall(require, "actions." .. modName .. "Action")
  			if not ok then
  				print("require module name error", action, modName)
  				return
  			end
  
  			local method = action[funcName]
  
  			if type(method) ~= "function" then
  				print("ERROR_SERVER_INVALID_ACTION", modName, funcName)
  				return
  			end
  
  			if #data > 0 then data = xxtea.decrypt(data, XXTEA_KEY) end
  			local result = method(agentInfo, data)
214061fa   zhengshouren   错误情况,返回错误码
204
205
  			if not result or type(result) == "number" then
  				SendPacket(actionCodes.Sys_innerErrorMsg, MsgPack.pack({id = cmd * 100 + (result or 0)}))
314bc5df   zhengshouren   提交服务器初始代码
206
207
208
209
210
211
212
  			end
  		end)
  	end
  }
  
  skynet.register_protocol {
  	name = "role",
3c8a6b8a   zhouhaihai   get equip
213
  	id = 13,
314bc5df   zhengshouren   提交服务器初始代码
214
215
216
  	pack = skynet.pack,
  	unpack = skynet.unpack,
  	dispatch = function(session, address, submethod, ...)
9ad697c8   zhouahaihai   删除 fields ,增加数据库...
217
  		-- skynet.trace() --执行序的跟踪统计
314bc5df   zhengshouren   提交服务器初始代码
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
  		local result
  		if not agentInfo.role then
  			result = "__OFFLINE__"
  		else
  			result = agentInfo.role[submethod](agentInfo.role, ...)
  		end
  
  		skynet.ret(skynet.pack(result))
  	end,
  }
  
  -- function CMD.start(gate, fd, ip)
  function CMD.start(session, source, gate, fd, ip)
  	ignoreHeartbeat = false
  
  	agentInfo.client_fd = fd
  	agentInfo.gate_serv = gate
  	agentInfo.ip = ip
  
  	agent_util:reset()
  	math.randomInit()
  
  	-- 这里将消息伪装成 watchdog 发出,这样就由 A->B->C->B->A 变成 A->B->C->A
  	skynet.redirect(gate, source, "lua", session, skynet.pack("forward", fd, 0, skynet.self()))
  end
  
  function CMD.close()
  	cancel_agent_timer()
  	mcast_util.usub_world()
  	mcast_util.usub_union()
  	
  	local role = agentInfo.role
  	if not role then return end
0a07bdd9   zhouahaihai   角色升级 。gm
251
  	role:log("logout", {online = skynet.timex()-role:getProperty("ltime")})
314bc5df   zhengshouren   提交服务器初始代码
252
253
254
255
256
  	role:onOfflineEvent()
  end
  
  function CMD.exit()
  	if agentInfo.role then
0a07bdd9   zhouahaihai   角色升级 。gm
257
  		-- role:log("logout", {online = skynet.timex()-role:getProperty("ltime")})
314bc5df   zhengshouren   提交服务器初始代码
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
  		datacenter.set("agent", agentInfo.role:getProperty("id"), nil)
  	end
  	skynet.exit()
  end
  
  function CMD.subUnion(consortiaId, union)
  	mcast_util.sub_union(consortiaId, union.chan)
  	agentInfo.userv = union.serv
  end
  
  function CMD:usubUnion()
  	mcast_util.usub_union()
  	agentInfo.userv = nil
  end
  
  local function routeGM(cmd, params)
  	if type(params) ~= "table" or not agentInfo.role then
  		return "指令失败"
  	end
  	local _M = require "actions.GmAction"
  	return _M[cmd](agentInfo.role, params)
  end
  
  skynet.start(function()
  	skynet.dispatch("lua", function(session, source, command, ...)
9ad697c8   zhouahaihai   删除 fields ,增加数据库...
283
  		-- skynet.trace() --执行序的跟踪统计
314bc5df   zhengshouren   提交服务器初始代码
284
285
286
  		local f = CMD[command]
  		if f then
  			if command == "exit" then
188558e8   zhouahaihai   skynet 1.2.0 use...
287
  				skynet.ignoreret()
314bc5df   zhengshouren   提交服务器初始代码
288
289
  				f(...)
  			elseif command == "start" then
188558e8   zhouahaihai   skynet 1.2.0 use...
290
  				skynet.ignoreret()
314bc5df   zhengshouren   提交服务器初始代码
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
  				f(session, source, ...)
  			else
  				skynet.ret(skynet.pack(f(...)))
  			end
  		else
  			skynet.ret(skynet.pack(routeGM(command, ...)))
  		end
  	end)
  
  	redisd = harbor.queryname("REDIS")
  	if tonumber(skynet.getenv "logd") == 1 then
  		logd = harbor.queryname("LOGD")
  	end
  
  	cs = queue()
  
  	-- csv
  	csvdb = sharedata.query("csvdata")
  	-- 错误码特殊处理
  	-- todo
  	-- for key, value in pairs(csvdb["sys_codesCsv"]) do
  	-- 	_G[string.upper(value.varname)] = key
  	-- end
  
  	agent_util = require "services/agent_util"
  end)