Blame view

src/agent.lua 9.44 KB
314bc5df   zhengshouren   提交服务器初始代码
1
2
3
4
5
6
7
  require "ProtocolCode"
  require "shared.init"
  require "utils.init"
  require "GlobalVar"
  require "RedisKeys"
  require "skynet.manager"
  
314bc5df   zhengshouren   提交服务器初始代码
8
9
10
  local queue = require "skynet.queue"
  local netpack = require "skynet.netpack"
  local socket = require "skynet.socket"
314bc5df   zhengshouren   提交服务器初始代码
11
12
13
14
15
16
  local xxtea = require "xxtea"
  
  skynet = require "skynet"
  redisproxy = require "shared.redisproxy"
  datacenter = require "skynet.datacenter"
  mcast_util = require "services/mcast_util"
a5486ede   zhouhaihai   csvdata 修改为 share...
17
  csvdb = require "shared.csvdata"
314bc5df   zhengshouren   提交服务器初始代码
18
19
20
21
22
23
  
  local CMD = {}
  local agentInfo = {}  -- { client_fd, role, gate_serv, open_timer}
  
  local agent_util, cs
  
5e5d7680   zhouhaihai   热更新 优化
24
  _hotfixActions = _hotfixActions or {}
2d392ede   zhouhaihai   热更新 最终版
25
  _hotfixClass = _hotfixClass or {}
5e5d7680   zhouhaihai   热更新 优化
26
  
bc36785e   zhouhaihai   增加 session
27
28
  _codeSession = {}
  
803170fd   zhouhaihai   玩家操作记录
29
30
31
  local logDoCount = 0
  local logDoStart = nil
  
314bc5df   zhengshouren   提交服务器初始代码
32
33
34
  --- {{{ 定时器相关
  local function handle_timeout()
  	if not agentInfo.open_timer then return end
314bc5df   zhengshouren   提交服务器初始代码
35
36
37
38
39
  	agent_util:update(agentInfo)
  	skynet.timeout(100, handle_timeout)
  end
  
  function start_agent_timer()
44c6e479   zhouhaihai   增加部分日志
40
  	if agentInfo.open_timer then return end
314bc5df   zhengshouren   提交服务器初始代码
41
42
43
44
45
46
47
48
49
  	agentInfo.open_timer = true
  	skynet.timeout(150, handle_timeout)
  end
  
  function cancel_agent_timer()
  	agentInfo.open_timer = false
  end
  ---- 定时器相关 }}}
  
bc36785e   zhouhaihai   增加 session
50
  
23d89d13   zhouahaihai   冒险 结构
51
  local _pipelinings = {}  --可同时多个序列  栈的形式保证嵌套不出错
314bc5df   zhengshouren   提交服务器初始代码
52
  function SendPacket(actionCode, bin, client_fd)
51d9d20b   liuzujun   付费签到,应用市场反馈
53
54
  	--print(actionHandlers[actionCode])
  	--dump(MsgPack.unpack(bin))
59acc11e   zhouhaihai   赛季更新
55
56
57
58
59
60
61
  	-- 内部消息不扩散出去
  	if actionCode == actionCodes.Sys_endlessSeason then
  		if agentInfo.role then
  			agentInfo.role:advEndlessSeasonCheck()
  		end
  		return
  	end
bc36785e   zhouhaihai   增加 session
62
  	
23d89d13   zhouahaihai   冒险 结构
63
64
  	client_fd = client_fd or agentInfo.client_fd
  	
314bc5df   zhengshouren   提交服务器初始代码
65
  	local handlerName = actionHandlers[actionCode]
bc36785e   zhouhaihai   增加 session
66
67
68
  	local isRpc = string.sub(handlerName, -3, -1) == "Rpc"
  	local session = _codeSession[actionCode] or 0
  	if isRpc then
314bc5df   zhengshouren   提交服务器初始代码
69
70
  		actionCode = actionCode + rpcResponseBegin
  	end
bc36785e   zhouhaihai   增加 session
71
  
23d89d13   zhouahaihai   冒险 结构
72
73
74
75
  	--  查看是否是在 流水线操作中
  	if #_pipelinings > 0 then
  		local _curPipelining = _pipelinings[#_pipelinings]
  		_curPipelining[client_fd] = _curPipelining[client_fd] or {} --区分不同客户端
bc36785e   zhouhaihai   增加 session
76
77
78
79
  		if not isRpc then
  			session = nil
  		end
  		table.insert(_curPipelining[client_fd], {actionCode, bin, session})
23d89d13   zhouahaihai   冒险 结构
80
81
82
  	else
  		if #bin > 0 then bin = xxtea.encrypt(bin, XXTEA_KEY) end
  		local head = string.pack("H", actionCode)
bc36785e   zhouhaihai   增加 session
83
84
85
86
  		if isRpc then
  			-- 是回复 -- 有session号
  			head = head .. string.pack("H", session)
  		end
23d89d13   zhouahaihai   冒险 结构
87
88
89
  		return socket.write(client_fd, netpack.pack(head .. bin))
  	end
  end
314bc5df   zhengshouren   提交服务器初始代码
90
  
23d89d13   zhouahaihai   冒险 结构
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
  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   提交服务器初始代码
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
  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
fa565e0c   zhouhaihai   优化结构
136
  		local roleCross = require("models.RoleCross")
da898074   zhouhaihai   pvp 高级领奖
137
138
139
140
141
  		if funcName == "getProperties" then
  			return false, roleCross.handle(funcName, roleId, fields)
  		else
  			return false, roleCross.handle(funcName, roleId, ...)
  		end
314bc5df   zhengshouren   提交服务器初始代码
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
  	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
  
bc36785e   zhouhaihai   增加 session
167
  
314bc5df   zhengshouren   提交服务器初始代码
168
169
170
171
172
173
174
175
176
  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...
177
  		skynet.ignoreret()
9ad697c8   zhouahaihai   删除 fields ,增加数据库...
178
  		-- skynet.trace() --执行序的跟踪统计
314bc5df   zhengshouren   提交服务器初始代码
179
180
181
182
183
184
185
186
187
188
  		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
314bc5df   zhengshouren   提交服务器初始代码
189
  
bc36785e   zhouhaihai   增加 session
190
191
192
193
194
195
196
  			if string.sub(actionName, -3, -1) == "Rpc" then
  				-- 是请求 -- 有session号
  				local codeSession = string.unpack("H", string.sub(data, 1, 2))
  				_codeSession[cmd] = codeSession
  				data = string.sub(data, 3)
  			end
  
5e5d7680   zhouhaihai   热更新 优化
197
198
199
200
201
202
203
204
205
206
207
208
  			local method
  			if _hotfixActions[actionName] then
  				method = _hotfixActions[actionName]
  			else
  				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
  				method = action[funcName]
314bc5df   zhengshouren   提交服务器初始代码
209
  			end
5e5d7680   zhouhaihai   热更新 优化
210
  			
314bc5df   zhengshouren   提交服务器初始代码
211
212
213
214
215
216
  			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
44c6e479   zhouhaihai   增加部分日志
217
218
219
220
221
  
  			-- 一次操作是一个关联操作 记录 ucode 是一样的
  			if agentInfo.role then
  				agentInfo.role:startActionUcode()
  			end
803170fd   zhouhaihai   玩家操作记录
222
  			logDoCount = logDoCount + 1
314bc5df   zhengshouren   提交服务器初始代码
223
  			local result = method(agentInfo, data)
44c6e479   zhouhaihai   增加部分日志
224
225
226
227
228
  
  			if agentInfo.role then
  				agentInfo.role:endActionUcode()
  			end
  
bc36785e   zhouhaihai   增加 session
229
230
231
  			-- 清掉请求session
  			_codeSession[cmd] = nil
  
214061fa   zhengshouren   错误情况,返回错误码
232
233
  			if not result or type(result) == "number" then
  				SendPacket(actionCodes.Sys_innerErrorMsg, MsgPack.pack({id = cmd * 100 + (result or 0)}))
314bc5df   zhengshouren   提交服务器初始代码
234
235
236
237
238
239
240
  			end
  		end)
  	end
  }
  
  skynet.register_protocol {
  	name = "role",
a5486ede   zhouhaihai   csvdata 修改为 share...
241
  	id = 101,
314bc5df   zhengshouren   提交服务器初始代码
242
243
244
  	pack = skynet.pack,
  	unpack = skynet.unpack,
  	dispatch = function(session, address, submethod, ...)
9ad697c8   zhouahaihai   删除 fields ,增加数据库...
245
  		-- skynet.trace() --执行序的跟踪统计
314bc5df   zhengshouren   提交服务器初始代码
246
247
248
249
250
251
252
253
254
255
256
  		local result
  		if not agentInfo.role then
  			result = "__OFFLINE__"
  		else
  			result = agentInfo.role[submethod](agentInfo.role, ...)
  		end
  
  		skynet.ret(skynet.pack(result))
  	end,
  }
  
803170fd   zhouhaihai   玩家操作记录
257
  
314bc5df   zhengshouren   提交服务器初始代码
258
  -- function CMD.start(gate, fd, ip)
5e5d7680   zhouhaihai   热更新 优化
259
  function CMD.start(session, source, gate, fd, ip, hotfixs)
314bc5df   zhengshouren   提交服务器初始代码
260
261
262
263
264
265
266
267
268
  	ignoreHeartbeat = false
  
  	agentInfo.client_fd = fd
  	agentInfo.gate_serv = gate
  	agentInfo.ip = ip
  
  	agent_util:reset()
  	math.randomInit()
  
a5b025b0   zhouhaihai   已存在agent 玩家重新登录不进行更新
269
270
271
272
  	if hotfixs then
  		for _, hotfix in ipairs(hotfixs) do
  			CMD.hotfix(hotfix)
  		end
5e5d7680   zhouhaihai   热更新 优化
273
  	end
3fe4471e   zhouhaihai   热更新 demo
274
  
7cde0c44   zhouhaihai   忽略心跳和未登录状态 超时处理
275
  	start_agent_timer()
803170fd   zhouhaihai   玩家操作记录
276
  	logDoStart = skynet.timex()
314bc5df   zhengshouren   提交服务器初始代码
277
278
279
280
281
282
283
284
  	-- 这里将消息伪装成 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()
314bc5df   zhengshouren   提交服务器初始代码
285
286
  	local role = agentInfo.role
  	if not role then return end
39bcd7ca   zhouhaihai   LOG
287
  	role:log("onLogout", {logtime = skynet.timex()-role:getProperty("ltime")})
f22a33af   zhouhaihai   自己的日志
288
  	role:mylog("logout", {int1 = skynet.timex()-role:getProperty("ltime")})
803170fd   zhouhaihai   玩家操作记录
289
290
291
292
  	if logDoCount == 0 then
  		logDoCount = 1
  	end
  	skynet.error("LOG_PLAYER_DO " .. math.ceil((skynet.timex() - logDoStart) / logDoCount))
314bc5df   zhengshouren   提交服务器初始代码
293
294
295
296
297
  	role:onOfflineEvent()
  end
  
  function CMD.exit()
  	if agentInfo.role then
314bc5df   zhengshouren   提交服务器初始代码
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
  		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
  
3fe4471e   zhouhaihai   热更新 demo
313
314
315
  function CMD.hotfix(code)
  	local ok, func = pcall(load, code)
  	if ok then
2d392ede   zhouhaihai   热更新 最终版
316
  		ok = pcall(func, agentInfo.role)
3fe4471e   zhouhaihai   热更新 demo
317
318
  	end
  	if not ok then
2d392ede   zhouhaihai   热更新 最终版
319
  		skynet.error("hotfix_code error by code " .. code)
3fe4471e   zhouhaihai   热更新 demo
320
321
322
  	end
  end
  
314bc5df   zhengshouren   提交服务器初始代码
323
324
325
326
327
  local function routeGM(cmd, params)
  	if type(params) ~= "table" or not agentInfo.role then
  		return "指令失败"
  	end
  	local _M = require "actions.GmAction"
3133cb76   zhouhaihai   日志
328
329
330
331
332
333
  
  	agentInfo.role:startActionUcode()
  	local status = _M[cmd](agentInfo.role, params)
  	agentInfo.role:endActionUcode()
  	
  	return status
314bc5df   zhengshouren   提交服务器初始代码
334
335
336
337
  end
  
  skynet.start(function()
  	skynet.dispatch("lua", function(session, source, command, ...)
9ad697c8   zhouahaihai   删除 fields ,增加数据库...
338
  		-- skynet.trace() --执行序的跟踪统计
314bc5df   zhengshouren   提交服务器初始代码
339
340
341
  		local f = CMD[command]
  		if f then
  			if command == "exit" then
188558e8   zhouahaihai   skynet 1.2.0 use...
342
  				skynet.ignoreret()
314bc5df   zhengshouren   提交服务器初始代码
343
344
  				f(...)
  			elseif command == "start" then
188558e8   zhouahaihai   skynet 1.2.0 use...
345
  				skynet.ignoreret()
314bc5df   zhengshouren   提交服务器初始代码
346
  				f(session, source, ...)
836228c9   zhouhaihai   热更新 bug
347
348
349
  			elseif command == "hotfix" then
  				skynet.ignoreret()
  				f(...)
314bc5df   zhengshouren   提交服务器初始代码
350
351
352
353
354
355
356
357
  			else
  				skynet.ret(skynet.pack(f(...)))
  			end
  		else
  			skynet.ret(skynet.pack(routeGM(command, ...)))
  		end
  	end)
  
3c0ea5fb   zhouhaihai   抽英雄
358
359
360
361
362
363
364
365
366
  	skynet.info_func(function()
  		local info = {}
  		info.ip = agentInfo.ip
  		if agentInfo.role then
  			info.roldId = agentInfo.role:getProperty("id")
  		end
  		return info
  	end)
  
a5486ede   zhouhaihai   csvdata 修改为 share...
367
  	redisd = skynet.localname(".redis")
314bc5df   zhengshouren   提交服务器初始代码
368
  	if tonumber(skynet.getenv "logd") == 1 then
a5486ede   zhouhaihai   csvdata 修改为 share...
369
  		logd = skynet.localname(".log")
314bc5df   zhengshouren   提交服务器初始代码
370
371
372
373
  	end
  
  	cs = queue()
  
a5486ede   zhouhaihai   csvdata 修改为 share...
374
  	pvpd = skynet.localname(".pvpcross")
314bc5df   zhengshouren   提交服务器初始代码
375
376
377
378
379
  	-- 错误码特殊处理
  	-- todo
  	-- for key, value in pairs(csvdb["sys_codesCsv"]) do
  	-- 	_G[string.upper(value.varname)] = key
  	-- end
a5486ede   zhouhaihai   csvdata 修改为 share...
380
  	globalCsv = csvdb["GlobalDefineCsv"]
314bc5df   zhengshouren   提交服务器初始代码
381
382
  	agent_util = require "services/agent_util"
  end)