Blame view

robot/robot.lua 5.1 KB
4dc77717   zhouhaihai   压测
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  require "ProtocolCode"
  require "shared.init"
  require "utils.init"
  skynet = require "skynet"
  local socketdriver = require "skynet.socketdriver"
  local netpack = require "skynet.netpack"
  local xxtea = require "xxtea"
  local httpc = require("http.httpc")
  local config = require "robot_config"
  
  local XXTEA_KEY = "699D448D6D24f7F941E9F6E99F823E18"
  
  local CMD = {}
  local client = {}
  local eventListener = {}
  
  local ignoreListener = {
  	["Role.updateProperty"] = function(data)
cbfd0513   zhouhaihai   增加池子
19
  		if not client.role then return end
4dc77717   zhouhaihai   压测
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
  		local msg = MsgPack.unpack(data)
  		for _, one in pairs(msg) do
  			client.role[one.key] = one.newValue
  		end
  	end,
  	["Role.updateProperties"] = function(data)
  		local msg = MsgPack.unpack(data)
  		for field, value in pairs(msg) do
  			client.role[field] = value
  		end
  	end,
  	["Role.notifyNewEvent"] = true,
  	["Role.chat"] = true,
  	["Role.updateItems"] = true,
  	["Gm.receiveResponse"] = true,
  	["Role.changeUpdate"] = true,
  	["Role.loadRunes"] = true,
  	["Hero.loadInfos"] = true,
7d31daa8   zhouhaihai   调整压测
38
39
40
  	["Store.updateproperty"] = true,
  	["Sys.maintainNotice"] = true,
  	["Hero.drawHeroExtraRewardNtf"] = true,
4dc77717   zhouhaihai   压测
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
  	["Sys.innerErrorMsg"] = function(data)
  		local msg = MsgPack.unpack(data)
  		log("innerErrorMsg: " .. msg.id)
  	end,
  }
  
  function addListener(actionCode, callback, isKeep)
  	callback = callback or function() end
  	local handlerName = actionHandlers[actionCode]
  	if string.sub(handlerName, -3, -1) == "Rpc" then
  		actionCode = actionCode + rpcResponseBegin
  	end
  	if eventListener[actionHandlers[actionCode]] then
  		log(handlerName .. " had listener")
  	end
  	local trueCall = callback
  	if not isKeep then
  		trueCall = function(data)
  			removeListener(actionCode)
  			callback(data)
  		end
  	end
  	eventListener[actionHandlers[actionCode]] = trueCall
  end
  
  function removeListener(actionCode)
  	local handlerName = actionHandlers[actionCode]
  	if string.sub(handlerName, -3, -1) == "Rpc" then
  		actionCode = actionCode + rpcResponseBegin
  	end
  	eventListener[actionHandlers[actionCode]] = nil
  end
  
  function triggerListener(listener, data)
  	if eventListener[listener] then
  		if #data > 0 then data = xxtea.decrypt(data, XXTEA_KEY) end
  		eventListener[listener](data)
  	elseif ignoreListener[listener] then
  		if type(ignoreListener[listener]) == "function" then
  			if #data > 0 then data = xxtea.decrypt(data, XXTEA_KEY) end
  			ignoreListener[listener](data)
  		end
  	else
cbfd0513   zhouhaihai   增加池子
84
  		-- log(listener .. " no handle!!!")
4dc77717   zhouhaihai   压测
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
  	end
  end
  
  function sendServer(actionCode, bin)
  	local session = 0
  	local handlerName = actionHandlers[actionCode]
  	if actionCode == actionCodes.Role_queryLoginRpc or string.sub(handlerName, -3, -1) ~= "Rpc" then
  		session = nil
  	end
  	
  	local data = string.pack("H", actionCode)
  	if session then
  		data = data .. string.pack("H", session)
  	end
  
  	if #bin > 0 then bin = xxtea.encrypt(bin, XXTEA_KEY) end
  	data = data .. bin
  
  	socketdriver.send(client.fd, netpack.pack(data))
  end
  
  function requestServer(actionCode, bin, callback, isKeep)
4dc77717   zhouhaihai   压测
107
  	addListener(actionCode, callback, isKeep)
1b6307bc   zhouhaihai   监听再前
108
  	sendServer(actionCode, bin)
4dc77717   zhouhaihai   压测
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
  end
  
  local function heartBeat()
  	sendServer(actionCodes.Sys_heartBeat, "")
  	skynet.timeout(500, heartBeat)
  end
  
  local function startUnit(unit)
  	if unit == "login" then
  	else
  		unit = math.randWeight(config.units, "weight")
  	end
  
  	local ok, unitTest = pcall(require, "unitTest." .. unit)
  	if not ok then
a5fa0074   zhouhaihai   robot
124
  		log("unitTest load error : " .. unitTest)
4dc77717   zhouhaihai   压测
125
126
127
128
129
  	end
  
  	unitTest.new(client):startTest()
  end
  
4dc77717   zhouhaihai   压测
130
131
132
133
  
  -- 登录成功开始任务
  function CMD.task()
  	heartBeat()
d9c023b8   zhouhaihai   压测
134
135
  	-- 20 秒后开始执行任务 错开登录
  	skynet.sleep(math.randomInt(2000, 3000))
4dc77717   zhouhaihai   压测
136
137
138
  	startUnit()
  end
  
f2c7c6fa   saicom   添加补发充值邮件
139
140
141
142
143
144
145
146
147
148
149
150
  function randomName()
      local str = "1234567890abcdefghijklmnopqrstuvwxyz"
      local len = #str
      local result = ""
      for i=1,10 do
          local idx = math.random(1, len)
          result = result .. string.sub(str, idx, idx)
      end
  
      return result
  end
  
4dc77717   zhouhaihai   压测
151
152
153
154
155
156
157
158
159
  -- 开始登录
  function CMD.start(fd, id)
  	client.fd = fd
  	client.clientId = id
  
  	local uname = config.uname_prefix .. id
  	client.uname = uname
  	local status, body = httpc.get(config.http, "/login?" .. httpGetFormatData({token = uname, device = "test", channel = "develop"}))
  	if tonumber(status) ~= 200 then
a5fa0074   zhouhaihai   robot
160
  		log("http get error", uname, body)
4dc77717   zhouhaihai   压测
161
162
163
164
165
166
167
168
169
170
  		return
  	end
  	local servInfo = json.decode(body)
  	client.uid = servInfo.userid
  	
  	startUnit("login")
  end
  
  -- 退出
  function CMD.exit()
7d31daa8   zhouhaihai   调整压测
171
172
173
  	sendServer(actionCodes.Gm_clientRequest,  MsgPack.pack({cmd = "gmmsg", pm1 = "123"}))
  	skynet.sleep(50)
  	skynet.ret(skynet.pack())
4dc77717   zhouhaihai   压测
174
175
176
177
178
179
180
181
182
183
184
185
186
187
  	skynet.exit()
  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)
  		local actionName = actionHandlers[cmd]
  		if not actionName then
a5fa0074   zhouhaihai   robot
188
  			log("actionName not exist", actionName)
4dc77717   zhouhaihai   压测
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
  			return
  		end
  
  		if cmd > rpcResponseBegin and actionName ~= "Role.queryLoginRpcResponse" then
  			-- 回复 有session
  			session = string.unpack("H", string.sub(data, 1, 2))
  			data = string.sub(data, 3)
  		end
  
  		triggerListener(actionName, data)
  	end,
  }
  
  skynet.start(function ()
  	skynet.dispatch("lua", function (_,_,cmd,...)
  		skynet.ret(skynet.pack(CMD[cmd](...)))
  	end)
  end)