NetManager.lua 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. local NetManager = class("NetManager", function ()
  2. return NetworkMgr.Instance
  3. end)
  4. local connectGateSuccess = false; --连接gate是否成功,如果成功就不需要重新断开再此重连。除非强制断开重连
  5. local registerPbCallback = {}
  6. local waitingCo = nil
  7. function NetManager:ctor()
  8. self.connectGated = Enum.ParamState.None
  9. self.isConnectingGate = false
  10. self.ReceiveMessageFunc = self.ReceiveMessage
  11. self.OpenReconnectionWndFunc = self.OpenReconnectionWnd
  12. self.LuaNoitceFunc = self.LuaNoitce
  13. self:SetGameServerConnectedLuaFunc(self.OnConnectGateCallback)
  14. end
  15. function NetManager:OnInit()
  16. Log("LuaNetwork...OnInit...OK!!!");
  17. end
  18. --向服务器发送消息...
  19. function NetManager:SendMessage(id, data)
  20. LogWarning("Send msgId : " .. id);
  21. Log("Send msgId : " .. id);
  22. local connectStatus = self:GetConnectStatus()
  23. if not connectStatus then
  24. LogError("net is disconnected ")
  25. ManagerContainer.LuaUIMgr:ShowNetWaiting()
  26. return
  27. end
  28. if self.IsLogin == false and (id ~= ProtoMsgId.CS_LOGIN_REQ and id ~= ProtoMsgId.CS_RECONNECT_REQ) then
  29. LogError("未登录状态,不能发送消息: " .. id)
  30. return
  31. end
  32. local msgBody_ = ManagerContainer.PbManager:EncodePb(id, data)
  33. if id == ProtoMsgId.CS_LOGIN_REQ or id == ProtoMsgId.CS_RECONNECT_REQ then
  34. self:SendMsgWithAES(msgBody_, id,false,self:IsConfirmMsg(id))
  35. else
  36. self:SendMsgWithAES(msgBody_, id,false,self:IsConfirmMsg(id))
  37. end
  38. -- self:SendMsg(msgBody_, id,false,self:IsConfirmMsg(id))
  39. end
  40. function NetManager:IsConfirmMsg(id)
  41. for k,v in pairs(ProtoConfirmMsgId) do
  42. if v == id then
  43. return true
  44. end
  45. end
  46. return false
  47. end
  48. --接收服务器发送过来的消息...
  49. function NetManager:ReceiveMessage(msgBody_, msgId_)
  50. LogWarning("!!!!!!!!!!!!!!!!!Receive msgId : " .. msgId_ );
  51. --是否关闭屏蔽框..
  52. --CloseNetSendMaskWnd(msgId_);
  53. local data = ManagerContainer.PbManager:DecodePb(msgId_, msgBody_)
  54. if msgId_ == ProtoMsgId.SC_HAND_SHAKE_NTF then
  55. self:SendConnectReq(data.crypt_pass)
  56. return
  57. end
  58. if msgId_ == ProtoMsgId.SC_LOGIN_ACK or msgId_ == ProtoMsgId.SC_RECONNECT_ACK then
  59. --LogError("===============登录成功状态=================")
  60. self.IsLogin = true --登录成功状态
  61. self:CloseWaitingCo()
  62. ManagerContainer.LuaUIMgr:CloseNetWaiting()
  63. end
  64. if data ~=nil and data.error ~= nil and data.error ~= Enum.NetErrorCode.ERROR_OK then
  65. if data.error ~= Enum.NetErrorCode.ERROR_ROLE_NOT_FOUND and data.error ~= Enum.NetErrorCode.ERROR_ROLE_NOT_FOUND_NEED_ACTIVE_CODE then
  66. local message = I18N.T(data.error)
  67. if tonumber(message) ~= data.error then
  68. ManagerContainer.LuaUIMgr:ErrorNoticeDisplay(data.error)
  69. end
  70. end
  71. end
  72. local ls = registerPbCallback[msgId_]
  73. if ls ~= nil then
  74. if ls.callback then
  75. if ls.obj then
  76. ls.callback(ls.obj, data)
  77. else
  78. ls.callback(data)
  79. end
  80. else
  81. for i = 1, #ls, 1 do
  82. local item = ls[i]
  83. if item and item.callback then
  84. if item.obj then
  85. item.callback(item.obj, data)
  86. else
  87. item.callback(data)
  88. end
  89. end
  90. end
  91. end
  92. end
  93. end
  94. function NetManager:IsErrorData(data)
  95. if data == nil then
  96. return true
  97. end
  98. if data ~=nil and data.error ~= nil and data.error ~= Enum.NetErrorCode.ERROR_OK then
  99. return true
  100. end
  101. return false
  102. end
  103. function NetManager:NetRegister(id, func, obj)
  104. local ls = registerPbCallback[id]
  105. if not ls then
  106. ls = {callback = func, obj = obj}
  107. registerPbCallback[id] = ls
  108. return
  109. end
  110. if ls.callback then
  111. if ls.callback == func then
  112. if ls.obj ~= obj then
  113. ls.obj = obj
  114. LogError("Register NTF Protocol Callback Fail : " .. id .. " func's owner is not same old : " .. tostring(ls.obj) .. " new :" .. tostring(obj))
  115. end
  116. return
  117. end
  118. -- 转数组
  119. ls = {ls}
  120. registerPbCallback[id] = ls
  121. end
  122. for _, value in pairs(ls) do
  123. if value.callback == func then
  124. if value.obj ~= obj then
  125. value.obj = obj
  126. LogError("Register NTF Protocol Callback Fail : " .. id .. " func's owner is not same old : " .. tostring(value.obj) .. " new :" .. tostring(obj))
  127. end
  128. return
  129. end
  130. end
  131. ls[#ls+1] = {callback = func, obj = obj}
  132. end
  133. function NetManager:UnRegisterPbIdCallback(msgId, func, obj)
  134. local ls = registerPbCallback[msgId]
  135. if ls then
  136. if func then
  137. if ls.callback then
  138. if ls.callback == func then
  139. if obj and ls.obj ~= obj then
  140. LogError("UnRegister NTF Protocol Callback Fail : " .. msgId .. " func's owner is not same old : " .. tostring(ls.callback) .. " new :" .. tostring(obj))
  141. end
  142. registerPbCallback[msgId] = nil
  143. end
  144. else
  145. for i = #ls, 1, -1 do
  146. local item = ls[i]
  147. if item and item.callback == func then
  148. if obj and item.obj ~= obj then
  149. LogError("UnRegister NTF Protocol Callback Fail : " .. msgId .. " func's owner is not same old : " .. tostring(item.callback) .. " new :" .. tostring(obj))
  150. end
  151. table.remove(ls, i)
  152. break
  153. end
  154. if #ls <= 0 then
  155. registerPbCallback[msgId] = nil
  156. end
  157. end
  158. end
  159. else
  160. registerPbCallback[msgId] = nil
  161. end
  162. else
  163. if(msgId) then
  164. LogWarning("UnRegister NTF Protocol Callback Fail : " .. msgId);
  165. else
  166. LogWarning("UnRegister NTF Protocol Callback Fail : msgId is nil");
  167. end
  168. end
  169. end
  170. --释放掉所以的注册协议函数
  171. function NetManager:UnRegisterMsgReqAll()
  172. for _,v in pairs(registerPbCallback) do
  173. v = nil
  174. end
  175. end
  176. --连接gate网关服务器
  177. function NetManager:ConnectGate(address_, port_)
  178. self.isConnectingGate = true
  179. self:ConnectServer(address_, port_)
  180. end
  181. function NetManager:GetProtoVersion()
  182. return ManagerContainer.PbManager:GetEnumValue('GameVersion', 'GameVersion_Main')
  183. end
  184. function NetManager:SendConnectReq(crypt_pass)
  185. LogWarning("----------SendConnectReq-------------"..crypt_pass)
  186. self:SetAesKey(crypt_pass)
  187. if self.isConnectingGate then
  188. ManagerContainer.LuaGameMgr:LoadCommonUIAssets()
  189. -- LogError("----------LoadCommonUIAssets-------------")
  190. local openid = ManagerContainer.LuaGameMgr.openId
  191. -- LogError("----------LoadCommonUIAssets open_id-------------"..open_id)
  192. local token = ManagerContainer.LuaGameMgr.token
  193. -- LogError("----------LoadCommonUIAssets token-------------"..token)
  194. local pf = ManagerContainer.LuaGameMgr.platform
  195. -- LogError("----------LoadCommonUIAssets pf-------------"..pf)
  196. local gameVer = self:GetProtoVersion()
  197. -- LogError("----------LoadCommonUIAssets gameVer-------------"..gameVer)
  198. local serverData = ManagerContainer.LuaGameMgr.serverData
  199. -- LogError("----------LoadCommonUIAssets serverData-------------"..serverData)
  200. local serverId = serverData and serverData.id or nil
  201. -- LogError("----------LoadCommonUIAssets serverId-------------"..serverId)
  202. local subplatform = ManagerContainer.LuaGameMgr.channelName
  203. -- LogError("----------LoadCommonUIAssets subplatform-------------"..subplatform)
  204. local timesp = ManagerContainer.LuaGameMgr.SDKTimeSP or ""
  205. local loginData = {open_id = openid, platform_token = token, platform = pf, sub_platform = subplatform, crypt_pass = timesp ,game_version = gameVer, select_zone = serverId}
  206. ManagerContainer.NetManager:SendMessage(ProtoMsgId.CS_LOGIN_REQ, loginData)
  207. self.isConnectingGate = false;
  208. else
  209. local openid = ManagerContainer.LuaGameMgr.openId
  210. local uid = tostring(ManagerContainer.DataMgr.UserData:GetUserId())
  211. local token = ManagerContainer.LuaGameMgr.token
  212. local pf = ManagerContainer.LuaGameMgr.platform
  213. --local timesp = ManagerContainer.LuaGameMgr.SDKTimeSP or ""
  214. -- 发送重连消息
  215. ManagerContainer.NetManager:SendMessage(ProtoMsgId.CS_RECONNECT_REQ, {open_id = openid, platform_token = token, platform = pf, uid = uid})
  216. end
  217. end
  218. function NetManager:OnConnectGateCallback(result, reconnect)
  219. self.connectGated = (result and Enum.ParamState.Success or Enum.ParamState.Fail)
  220. if result then
  221. connectGateSuccess = true;
  222. else
  223. if reconnect then
  224. self:CloseWaitingCo()
  225. local cb = function()
  226. waitingCo = nil
  227. ManagerContainer.LoginMgr:ReconnectFail()
  228. end
  229. waitingCo = RegisterDelayStep(cb)
  230. else
  231. if not waitingCo then
  232. local cb = function()
  233. waitingCo = nil
  234. ManagerContainer.LuaUIMgr:ShowNetWaiting()
  235. end
  236. waitingCo = RegisterDelayStep(cb)
  237. end
  238. end
  239. end
  240. end
  241. function NetManager:CloseWaitingCo()
  242. if waitingCo then
  243. coroutine.stop(waitingCo)
  244. waitingCo = nil
  245. end
  246. end
  247. --是否连接gate服务器成功
  248. function NetManager:IsConnectGateSuccess()
  249. return connectGateSuccess;
  250. end
  251. -- 重连后回到界面
  252. function OnReConnected()
  253. end
  254. -- LuaNetHandler过来的通知lua的事件广播
  255. function NetManager:LuaNoitce(type_, value_)
  256. -- LuaEventMgr:DispatchEvent(type_, value_);
  257. end
  258. --打开重连窗口
  259. function NetManager:OpenReconnectionWnd(value_)
  260. end
  261. function NetManager:ResetNetMgr()
  262. -- LogError("ResetNetMgr")
  263. connectGateSuccess = false;
  264. self.connectGated = Enum.ParamState.None
  265. self:CloseGameServerConnect()
  266. self:CloseWaitingCo()
  267. end
  268. function NetManager:DisconnectSrv()
  269. self:Disconnect()
  270. end
  271. function NetManager:Destroy()
  272. --LogError("Destroy")
  273. self.ReceiveMessageFunc = nil
  274. self.OpenReconnectionWndFunc = nil
  275. self.LuaNoitceFunc = nil
  276. self:UnRegisterMsgReqAll()
  277. registerPbCallback = nil
  278. connectGateSuccess = false;
  279. self.connectGated = nil
  280. self:CloseGameServerConnect()
  281. self:CloseWaitingCo()
  282. if tolua.getpeer(self) ~= nil then
  283. tolua.setpeer(self, nil)
  284. end
  285. end
  286. return NetManager