源战役客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

471 lines
17 KiB

  1. --[[
  2. :
  3. :
  4. i: 32
  5. I: 32
  6. h: 16
  7. H: 16
  8. c: 8
  9. C: 8
  10. l: 64
  11. L: 64
  12. s:
  13. :
  14. self:SendFmtToGame(10003, "ihs", val_int, val_short, val_string)
  15. 10003,
  16. 32,
  17. 16,
  18. ,
  19. val_int, val_short, val_str = self:ReadFmt("ihs")
  20. ,
  21. val_int, val_short, val_str
  22. ]]
  23. UserMsgAdapter = UserMsgAdapter or BaseClass()
  24. local UserMsgAdapter = UserMsgAdapter
  25. local networkMgr = networkMgr
  26. local Time = Time
  27. local Status = Status
  28. local print = print
  29. local rawget = rawget
  30. local unpack = unpack
  31. local tostring = tostring
  32. local tonumber = tonumber
  33. local string_sub = string.sub
  34. local table_insert = table.insert
  35. local string_len = string.len
  36. local ByteBuffer = ByteBuffer
  37. local table_concat = table.concat
  38. local logWarn = logWarn
  39. local Util = Util
  40. local can_print_cmd = RuntimePlatform and ApplicationPlatform ~= RuntimePlatform.Android and ApplicationPlatform ~= RuntimePlatform.IPhonePlayer
  41. UserMsgAdapter.SPLIT_CHAT = '' --协议传输分隔符
  42. function UserMsgAdapter:__init()
  43. UserMsgAdapter.Instance = self
  44. UserMsgAdapter.send_byteBuff = false
  45. UserMsgAdapter.receive_byteBuff = false
  46. UserMsgAdapter.register_list = {}
  47. UserMsgAdapter.new_fmt_mode = false --新的协议解释类型
  48. self.is_game_connected = false --是否已经连接服务器
  49. self.reconnect_times = 0 --重连次数
  50. UserMsgAdapter.result_list = {}
  51. self.cmd_callback_queue = Array.New() --协议callback队列
  52. self.curr_frame_receive_count = 0
  53. UserMsgAdapter.write_cmd = 0
  54. -- local function onUpdate()
  55. -- self:Update()
  56. -- end
  57. -- TimerQuest.AddPeriodQuest(GlobalTimerQuest, onUpdate, 0.2, -1)
  58. LateUpdateBeat:Add(UserMsgAdapter.Update,self)
  59. UserMsgAdapter.initEvents(self)
  60. end
  61. function UserMsgAdapter:getInstance()
  62. if UserMsgAdapter.Instance == nil then
  63. UserMsgAdapter.Instance = UserMsgAdapter.New()
  64. end
  65. return UserMsgAdapter.Instance
  66. end
  67. function UserMsgAdapter:IsGameConnected()
  68. return self.is_game_connected
  69. end
  70. function UserMsgAdapter:IsWaitPlayerHandle()
  71. return self.wait_player_handle
  72. end
  73. function UserMsgAdapter:initEvents()
  74. --当连接建立时--
  75. local function OnConnect()
  76. logWarn("Game Server connected!!")
  77. self.is_game_connected = true
  78. EventSystem.Fire(GlobalEventSystem,EventName.GAME_CONNECT)
  79. end
  80. --异常断线--
  81. local function OnException()
  82. self.is_game_connected = false
  83. -- networkMgr:SendConnect()
  84. --判断在游戏中进行静默重连, 10004进入游戏之后,is_back_from_game为false
  85. if LoginModel.Instance and LoginModel.Instance.is_back_from_game == false then
  86. self.reconnect_times = 0
  87. self.default_auto_connect = 4
  88. UserMsgAdapter.ReconnectHandler(self)
  89. --判断从游戏中返回,在选角、创角界面,普通重连,is_back_from_game为true nil
  90. -- elseif LoginModel.Instance and ((LoginModel.Instance.is_back_from_game or LoginModel.Instance.is_back_from_game == nil) )then --and LoginModel.Instance.account_data_10000) then
  91. elseif LoginModel.Instance == nil or LoginModel.Instance.is_back_from_game ~= true then
  92. self.reconnect_times = 0
  93. self.default_auto_connect = 0
  94. UserMsgAdapter.ReconnectHandler(self)
  95. end
  96. EventSystem.Fire(GlobalEventSystem,EventName.GAME_DISCONNECT)
  97. -- LogError("OnException------->>>>")
  98. end
  99. --连接中断,或者被踢掉--
  100. local function OnDisconnect()
  101. self.is_game_connected = false
  102. if LoginModel.Instance == nil or LoginModel.Instance.is_back_from_game == false then
  103. self.reconnect_times = 0
  104. self.default_auto_connect = 2
  105. UserMsgAdapter.ReconnectHandler(self)
  106. end
  107. EventSystem.Fire(GlobalEventSystem,EventName.GAME_DISCONNECT)
  108. -- LogError("OnDisconnect------->>>>")
  109. end
  110. --收到服务端消息--
  111. local function OnMessage(buffer)
  112. -- if currProtoType == ProtocalType.BINARY then
  113. UserMsgAdapter.receiveMessage(self, buffer)
  114. -- end
  115. ----------------------------------------------------
  116. --print('OnMessage-------->>>')
  117. end
  118. EventSystem.Bind(GlobalEventSystem,Protocal.Connect, OnConnect)
  119. EventSystem.Bind(GlobalEventSystem,Protocal.Message, OnMessage)
  120. EventSystem.Bind(GlobalEventSystem,Protocal.Exception, OnException)
  121. EventSystem.Bind(GlobalEventSystem,Protocal.Disconnect, OnDisconnect)
  122. end
  123. function UserMsgAdapter:ReconnectHandler()
  124. if LoginModel.Instance and LoginModel.Instance.not_show_reconnect then
  125. return
  126. end
  127. --玩家选择重连、返回处理
  128. if self.wait_player_handle then
  129. return
  130. end
  131. --静默连接处理
  132. if not ClientConfig.is_ban_auto_connect and self.default_auto_connect and self.default_auto_connect > 0 then
  133. self.default_auto_connect = self.default_auto_connect - 1
  134. self.default_auto_connect = self.default_auto_connect > 0 and self.default_auto_connect or nil
  135. self:SetIsAutoConnecting(true)
  136. local function default_connect()
  137. local function on_connect( )
  138. if LoginModel.Instance then
  139. LoginModel.Instance.reconnect_on_game = true
  140. end
  141. --清除所有model的缓存(部分model除外)
  142. LuaMemManager:getInstance():ClearModelData( )
  143. EventSystem.Fire(GlobalEventSystem,EventName.SHOW_LOADING_VIEW)
  144. EventSystem.Fire(GlobalEventSystem,LoginStateEvent.START_GAME_CONNECT)
  145. end
  146. LoginController.Instance:CheckResUpdate(on_connect, false)
  147. self:AddConnectCheckTimer()
  148. end
  149. GlobalTimerQuest:AddDelayQuest(default_connect,2)
  150. return
  151. end
  152. if self.has_show_alert_view then
  153. return
  154. end
  155. self.has_show_alert_view = true
  156. local function ok()
  157. self.wait_player_handle = false
  158. local run = function()
  159. if LoginModel.Instance then
  160. LoginModel.Instance.reconnect_on_game = true
  161. end
  162. --清除所有model的缓存(部分model除外)
  163. LuaMemManager:getInstance():ClearModelData( )
  164. EventSystem.Fire(GlobalEventSystem,EventName.SHOW_LOADING_VIEW)
  165. EventSystem.Fire(GlobalEventSystem,LoginStateEvent.START_GAME_CONNECT)
  166. end
  167. LoginController.Instance:CheckResUpdate(run, false)
  168. self:AddConnectCheckTimer()
  169. self.has_show_alert_view = false
  170. end
  171. local function cancle()
  172. self.wait_player_handle = false
  173. local run = function()
  174. LoginModel.Instance.reconnect_on_game = false
  175. EventSystem.Fire(GlobalEventSystem,EventName.SHOW_LOADING_VIEW)
  176. EventSystem.Fire(GlobalEventSystem,EventName.CHANGE_ACCOUNT)
  177. end
  178. LoginController.Instance:CheckResUpdate(run, false)
  179. self.has_show_alert_view = false
  180. end
  181. if self.reconnect_times < 4 then
  182. self.reconnect_times = self.reconnect_times + 1
  183. local check_gameout_state = function()
  184. if LoginModel.Instance:IsGameOutState() then
  185. LoginModel.Instance:ResetGameOutState()
  186. self.reconnect_times = 0
  187. return true
  188. else
  189. return false
  190. end
  191. end
  192. if self.reconnect_times == 4 then
  193. local function func()
  194. if check_gameout_state() then
  195. return
  196. end
  197. self.wait_player_handle = true
  198. Alert.show("无法重连到服务器,请确认网络后重试", Alert.Type.One, cancle, cancle, "返回登录")
  199. end
  200. GlobalTimerQuest:AddDelayQuest(func,2)
  201. else
  202. local function func()
  203. if LoginModel.Instance.not_show_reconnect then
  204. return
  205. end
  206. if check_gameout_state() then
  207. return
  208. end
  209. self.wait_player_handle = true
  210. Alert.show("网络已断开链接", Alert.Type.Two, ok, cancle, "重连", "返回登录")
  211. end
  212. GlobalTimerQuest:AddDelayQuest(func,2)
  213. end
  214. end
  215. end
  216. function UserMsgAdapter:SetIsAutoConnecting( flag )
  217. self.is_auto_connecting = flag
  218. end
  219. function UserMsgAdapter:GetIsAutoConnecting( )
  220. return self.is_auto_connecting
  221. end
  222. function UserMsgAdapter:AddConnectCheckTimer()
  223. self:RemoveConnectCheckTimer()
  224. local time = 0
  225. local last_net_available = Util.NetAvailable
  226. local function onTimer()
  227. time = time + 1
  228. if time >= 12 or self.is_game_connected or (LoginModel.Instance and LoginModel.Instance.is_back_from_game and not LoginModel.Instance.account_data_10000) then
  229. self:RemoveConnectCheckTimer()
  230. if time >= 12 then
  231. Message.show("重连失败")
  232. self:ReconnectHandler()
  233. end
  234. return
  235. end
  236. --print("Net Available Check...")
  237. if not self.is_game_connected and LoginModel:getInstance().reconnect_on_game then
  238. if not last_net_available and Util.NetAvailable then
  239. networkMgr:SendConnect()
  240. end
  241. end
  242. last_net_available = Util.NetAvailable
  243. end
  244. self.connect_timer = GlobalTimerQuest:AddPeriodQuest(onTimer,1,-1)
  245. end
  246. function UserMsgAdapter:RemoveConnectCheckTimer()
  247. if self.connect_timer then
  248. GlobalTimerQuest:CancelQuest(self.connect_timer)
  249. self.connect_timer = nil
  250. end
  251. end
  252. function UserMsgAdapter:Update()
  253. if self.cmd_callback_queue:GetSize() > 0 and (not self.cmd_handler_time or Time.time - self.cmd_handler_time > 0.5) then
  254. local size = self.cmd_callback_queue:GetSize()
  255. if size <= 4 then
  256. UserMsgAdapter.ImmeHandlerCallback(self, self.cmd_callback_queue:PopFront())
  257. else
  258. -- local len = math.ceil(size * 0.25)
  259. -- print("---------------------------->self.curr_frame_receive_count= ", self.curr_frame_receive_count)
  260. for i = 1, self.curr_frame_receive_count + 1 do --保证比增加的量多一个
  261. if i > size then break end
  262. UserMsgAdapter.ImmeHandlerCallback(self, self.cmd_callback_queue:PopFront())
  263. end
  264. -- for i = 1, len do --保证比增加的量多一个
  265. -- self:ImmeHandlerCallback(self.cmd_callback_queue:PopFront())
  266. -- end
  267. end
  268. self.curr_frame_receive_count = 0
  269. end
  270. end
  271. function UserMsgAdapter:ImmeHandlerCallback(buffer)
  272. if buffer then
  273. self.cmd_handler_time = Time.time
  274. local cmd = buffer:ReadUshort()
  275. local can_rar = buffer:ReadByte()
  276. local call_back = UserMsgAdapter.register_list[cmd]
  277. if can_print_cmd then
  278. if cmd ~= 10006 and cmd ~= 12008 and cmd ~= 12001 and cmd then
  279. logWarn("receive cmd = "..cmd)
  280. end
  281. end
  282. if call_back then
  283. UserMsgAdapter.receive_byteBuff = buffer
  284. call_back()
  285. end
  286. self.cmd_handler_time = false
  287. end
  288. end
  289. function UserMsgAdapter:receiveMessage(buffer)
  290. if self.cmd_callback_queue:GetSize() == 0 --没有队列
  291. and (not self.last_handler_time or Time.time - self.last_handler_time >= 0.01) --超过一定的频率
  292. and (not self.cmd_handler_time or Time.time - self.cmd_handler_time > 0.5) then --保护措施
  293. self.last_handler_time = Time.time
  294. self.curr_frame_receive_count = 0
  295. UserMsgAdapter.ImmeHandlerCallback(self, buffer)
  296. else
  297. self.curr_frame_receive_count = self.curr_frame_receive_count + 1
  298. self.cmd_callback_queue:PushBack(buffer)
  299. end
  300. --print('receiveMessage->call_back: cmd:>'..cmd)
  301. end
  302. --发送一次性封装好的数据给服务器
  303. function UserMsgAdapter.SendAllFmtToGame(cmd, fmt_str, ...)
  304. UserMsgAdapter.WriteBegin(cmd)
  305. UserMsgAdapter.WriteFMT(fmt_str, ...)
  306. UserMsgAdapter.SendToGame()
  307. end
  308. --发送一次性封装好的数据给服务器
  309. function UserMsgAdapter.SendAllFmtToGame2(cmd, fmt_str, ...)
  310. local args_list = {...}
  311. networkMgr:SendAllFmtToGame(cmd, fmt_str, table_concat(args_list, UserMsgAdapter.SPLIT_CHAT))
  312. end
  313. function UserMsgAdapter.WriteBegin(cmd)
  314. UserMsgAdapter.send_byteBuff = ByteBuffer.New()
  315. UserMsgAdapter.send_byteBuff:WriteShort(cmd)
  316. if can_print_cmd then
  317. if cmd ~= 10006 and cmd ~= 12001 and cmd then
  318. logWarn("send cmd="..cmd)
  319. end
  320. end
  321. UserMsgAdapter.write_cmd = cmd or 0
  322. end
  323. function UserMsgAdapter.WriteFMT(fmt_str, ...)
  324. if fmt_str then
  325. local args_list = {...}
  326. local buffer = UserMsgAdapter.send_byteBuff
  327. local function writeStream(buffer,type,value)
  328. -- log("send Message = "..value)
  329. if value == nil then
  330. GameError.Instance:SendErrorToPHP("UserMsgAdapter WriteFMT nil cmd is = "..UserMsgAdapter.write_cmd.." "..debug.traceback())
  331. if type == "s" then
  332. value = ""
  333. else
  334. value = 0
  335. end
  336. end
  337. if type == "c" then
  338. buffer:WriteByte(value)
  339. elseif type == "i" then
  340. buffer:WriteUint(tonumber(value))
  341. elseif type == "I" then
  342. buffer:WriteInt(value)
  343. elseif type == "h" then
  344. buffer:WriteUshort(value)
  345. elseif type == "H" then
  346. buffer:WriteShort(value)
  347. elseif type == "l" then
  348. buffer:WriteUlong(value)
  349. elseif type == "L" then
  350. buffer:WriteLong(value)
  351. elseif type == "s" then
  352. buffer:WriteString(value)
  353. elseif type == "B" then
  354. buffer:WriteBytes(value)
  355. end
  356. end
  357. for i = 1, string_len(fmt_str) do
  358. writeStream(buffer,string_sub(fmt_str,i,i),args_list[i])
  359. end
  360. end
  361. end
  362. function UserMsgAdapter.SendToGame()
  363. if UserMsgAdapter.send_byteBuff then
  364. networkMgr:SendMessage(UserMsgAdapter.send_byteBuff)
  365. end
  366. UserMsgAdapter.send_byteBuff = false
  367. end
  368. function UserMsgAdapter.ReadFmt(fmt_str)
  369. if UserMsgAdapter.receive_byteBuff and fmt_str then
  370. local buffer = UserMsgAdapter.receive_byteBuff
  371. local function readStream(buffer,type)
  372. local value = nil
  373. if type == "c" then
  374. value = buffer:ReadByte()
  375. elseif type == "i" then
  376. value = buffer:ReadUint()
  377. elseif type == "I" then
  378. value = buffer:ReadInt()
  379. elseif type == "h" then
  380. value = buffer:ReadUshort()
  381. elseif type == "H" then
  382. value = buffer:ReadShort()
  383. elseif type == "l" then
  384. value = tonumber(tostring(buffer:ReadUlong()))
  385. elseif type == "L" then
  386. value = tonumber(tostring(buffer:ReadLong()))
  387. elseif type == "s" then
  388. value = buffer:ReadString()
  389. elseif type == "B" then
  390. value = buffer:ReadBytes()
  391. end
  392. -- log("receive message = "..value)
  393. return value
  394. end
  395. local len = string_len(fmt_str)
  396. if len == 1 then
  397. return readStream(buffer, fmt_str)
  398. else
  399. for i = 1, len do
  400. UserMsgAdapter.result_list[i] = readStream(buffer,string_sub(fmt_str,i,i))
  401. end
  402. return unpack(UserMsgAdapter.result_list)
  403. end
  404. end
  405. end
  406. function UserMsgAdapter.ReadFmt2(fmt_str)
  407. -- return UserMsgAdapter.ReadFmt(fmt_str)
  408. local result = networkMgr:ReadFmt(UserMsgAdapter.receive_byteBuff, fmt_str)
  409. if result then
  410. local list = Split(result, UserMsgAdapter.SPLIT_CHAT)
  411. if list then
  412. local n_v = nil
  413. for k, v in ipairs(list) do
  414. n_v = tonumber(v)
  415. if n_v then
  416. list[k] = n_v
  417. end
  418. end
  419. return unpack(list)
  420. end
  421. end
  422. end
  423. function UserMsgAdapter.RegisterMsgOperate(id, register_func)
  424. UserMsgAdapter.register_list[id] = register_func
  425. end