| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- local NetManager = class("NetManager", function ()
- return NetworkMgr.Instance
- end)
- local connectGateSuccess = false; --连接gate是否成功,如果成功就不需要重新断开再此重连。除非强制断开重连
- local registerPbCallback = {}
- local waitingCo = nil
- function NetManager:ctor()
- self.connectGated = Enum.ParamState.None
- self.isConnectingGate = false
- self.ReceiveMessageFunc = self.ReceiveMessage
- self.OpenReconnectionWndFunc = self.OpenReconnectionWnd
- self.LuaNoitceFunc = self.LuaNoitce
- self:SetGameServerConnectedLuaFunc(self.OnConnectGateCallback)
- end
- function NetManager:OnInit()
- Log("LuaNetwork...OnInit...OK!!!");
- end
- --向服务器发送消息...
- function NetManager:SendMessage(id, data)
- LogWarning("Send msgId : " .. id);
- Log("Send msgId : " .. id);
- local connectStatus = self:GetConnectStatus()
- if not connectStatus then
- LogError("net is disconnected ")
- ManagerContainer.LuaUIMgr:ShowNetWaiting()
- return
- end
- if self.IsLogin == false and (id ~= ProtoMsgId.CS_LOGIN_REQ and id ~= ProtoMsgId.CS_RECONNECT_REQ) then
- LogError("未登录状态,不能发送消息: " .. id)
- return
- end
- local msgBody_ = ManagerContainer.PbManager:EncodePb(id, data)
- if id == ProtoMsgId.CS_LOGIN_REQ or id == ProtoMsgId.CS_RECONNECT_REQ then
- self:SendMsgWithAES(msgBody_, id,false,self:IsConfirmMsg(id))
- else
- self:SendMsgWithAES(msgBody_, id,false,self:IsConfirmMsg(id))
- end
- -- self:SendMsg(msgBody_, id,false,self:IsConfirmMsg(id))
- end
- function NetManager:IsConfirmMsg(id)
- for k,v in pairs(ProtoConfirmMsgId) do
- if v == id then
- return true
- end
- end
- return false
- end
- --接收服务器发送过来的消息...
- function NetManager:ReceiveMessage(msgBody_, msgId_)
- LogWarning("!!!!!!!!!!!!!!!!!Receive msgId : " .. msgId_ );
- --是否关闭屏蔽框..
- --CloseNetSendMaskWnd(msgId_);
- local data = ManagerContainer.PbManager:DecodePb(msgId_, msgBody_)
- if msgId_ == ProtoMsgId.SC_HAND_SHAKE_NTF then
- self:SendConnectReq(data.crypt_pass)
- return
- end
- if msgId_ == ProtoMsgId.SC_LOGIN_ACK or msgId_ == ProtoMsgId.SC_RECONNECT_ACK then
- --LogError("===============登录成功状态=================")
- self.IsLogin = true --登录成功状态
- self:CloseWaitingCo()
- ManagerContainer.LuaUIMgr:CloseNetWaiting()
- end
- if data ~=nil and data.error ~= nil and data.error ~= Enum.NetErrorCode.ERROR_OK then
- if data.error ~= Enum.NetErrorCode.ERROR_ROLE_NOT_FOUND and data.error ~= Enum.NetErrorCode.ERROR_ROLE_NOT_FOUND_NEED_ACTIVE_CODE then
- local message = I18N.T(data.error)
- if tonumber(message) ~= data.error then
- ManagerContainer.LuaUIMgr:ErrorNoticeDisplay(data.error)
- end
- end
- end
- local ls = registerPbCallback[msgId_]
- if ls ~= nil then
- if ls.callback then
- if ls.obj then
- ls.callback(ls.obj, data)
- else
- ls.callback(data)
- end
- else
- for i = 1, #ls, 1 do
- local item = ls[i]
- if item and item.callback then
- if item.obj then
- item.callback(item.obj, data)
- else
- item.callback(data)
- end
- end
- end
- end
- end
- end
- function NetManager:IsErrorData(data)
- if data == nil then
- return true
- end
- if data ~=nil and data.error ~= nil and data.error ~= Enum.NetErrorCode.ERROR_OK then
- return true
- end
- return false
- end
- function NetManager:NetRegister(id, func, obj)
- local ls = registerPbCallback[id]
- if not ls then
- ls = {callback = func, obj = obj}
- registerPbCallback[id] = ls
- return
- end
- if ls.callback then
- if ls.callback == func then
- if ls.obj ~= obj then
- ls.obj = obj
- LogError("Register NTF Protocol Callback Fail : " .. id .. " func's owner is not same old : " .. tostring(ls.obj) .. " new :" .. tostring(obj))
- end
- return
- end
- -- 转数组
- ls = {ls}
- registerPbCallback[id] = ls
- end
- for _, value in pairs(ls) do
- if value.callback == func then
- if value.obj ~= obj then
- value.obj = obj
- LogError("Register NTF Protocol Callback Fail : " .. id .. " func's owner is not same old : " .. tostring(value.obj) .. " new :" .. tostring(obj))
- end
- return
- end
- end
- ls[#ls+1] = {callback = func, obj = obj}
- end
- function NetManager:UnRegisterPbIdCallback(msgId, func, obj)
- local ls = registerPbCallback[msgId]
- if ls then
- if func then
- if ls.callback then
- if ls.callback == func then
- if obj and ls.obj ~= obj then
- LogError("UnRegister NTF Protocol Callback Fail : " .. msgId .. " func's owner is not same old : " .. tostring(ls.callback) .. " new :" .. tostring(obj))
- end
- registerPbCallback[msgId] = nil
- end
- else
- for i = #ls, 1, -1 do
- local item = ls[i]
- if item and item.callback == func then
- if obj and item.obj ~= obj then
- LogError("UnRegister NTF Protocol Callback Fail : " .. msgId .. " func's owner is not same old : " .. tostring(item.callback) .. " new :" .. tostring(obj))
- end
- table.remove(ls, i)
- break
- end
- if #ls <= 0 then
- registerPbCallback[msgId] = nil
- end
- end
- end
- else
- registerPbCallback[msgId] = nil
- end
- else
- if(msgId) then
- LogWarning("UnRegister NTF Protocol Callback Fail : " .. msgId);
- else
- LogWarning("UnRegister NTF Protocol Callback Fail : msgId is nil");
- end
- end
- end
- --释放掉所以的注册协议函数
- function NetManager:UnRegisterMsgReqAll()
- for _,v in pairs(registerPbCallback) do
- v = nil
- end
- end
- --连接gate网关服务器
- function NetManager:ConnectGate(address_, port_)
- self.isConnectingGate = true
- self:ConnectServer(address_, port_)
- end
- function NetManager:GetProtoVersion()
- return ManagerContainer.PbManager:GetEnumValue('GameVersion', 'GameVersion_Main')
- end
- function NetManager:SendConnectReq(crypt_pass)
- LogWarning("----------SendConnectReq-------------"..crypt_pass)
- self:SetAesKey(crypt_pass)
- if self.isConnectingGate then
- ManagerContainer.LuaGameMgr:LoadCommonUIAssets()
- -- LogError("----------LoadCommonUIAssets-------------")
- local openid = ManagerContainer.LuaGameMgr.openId
- -- LogError("----------LoadCommonUIAssets open_id-------------"..open_id)
- local token = ManagerContainer.LuaGameMgr.token
- -- LogError("----------LoadCommonUIAssets token-------------"..token)
- local pf = ManagerContainer.LuaGameMgr.platform
- -- LogError("----------LoadCommonUIAssets pf-------------"..pf)
- local gameVer = self:GetProtoVersion()
- -- LogError("----------LoadCommonUIAssets gameVer-------------"..gameVer)
- local serverData = ManagerContainer.LuaGameMgr.serverData
- -- LogError("----------LoadCommonUIAssets serverData-------------"..serverData)
- local serverId = serverData and serverData.id or nil
- -- LogError("----------LoadCommonUIAssets serverId-------------"..serverId)
- local subplatform = ManagerContainer.LuaGameMgr.channelName
- -- LogError("----------LoadCommonUIAssets subplatform-------------"..subplatform)
- local timesp = ManagerContainer.LuaGameMgr.SDKTimeSP or ""
- local loginData = {open_id = openid, platform_token = token, platform = pf, sub_platform = subplatform, crypt_pass = timesp ,game_version = gameVer, select_zone = serverId}
- ManagerContainer.NetManager:SendMessage(ProtoMsgId.CS_LOGIN_REQ, loginData)
- self.isConnectingGate = false;
- else
- local openid = ManagerContainer.LuaGameMgr.openId
- local uid = tostring(ManagerContainer.DataMgr.UserData:GetUserId())
- local token = ManagerContainer.LuaGameMgr.token
- local pf = ManagerContainer.LuaGameMgr.platform
- --local timesp = ManagerContainer.LuaGameMgr.SDKTimeSP or ""
- -- 发送重连消息
- ManagerContainer.NetManager:SendMessage(ProtoMsgId.CS_RECONNECT_REQ, {open_id = openid, platform_token = token, platform = pf, uid = uid})
- end
- end
- function NetManager:OnConnectGateCallback(result, reconnect)
- self.connectGated = (result and Enum.ParamState.Success or Enum.ParamState.Fail)
- if result then
- connectGateSuccess = true;
- else
- if reconnect then
- self:CloseWaitingCo()
- local cb = function()
- waitingCo = nil
- ManagerContainer.LoginMgr:ReconnectFail()
- end
- waitingCo = RegisterDelayStep(cb)
- else
- if not waitingCo then
- local cb = function()
- waitingCo = nil
- ManagerContainer.LuaUIMgr:ShowNetWaiting()
- end
- waitingCo = RegisterDelayStep(cb)
- end
- end
- end
- end
- function NetManager:CloseWaitingCo()
- if waitingCo then
- coroutine.stop(waitingCo)
- waitingCo = nil
- end
- end
- --是否连接gate服务器成功
- function NetManager:IsConnectGateSuccess()
- return connectGateSuccess;
- end
- -- 重连后回到界面
- function OnReConnected()
- end
- -- LuaNetHandler过来的通知lua的事件广播
- function NetManager:LuaNoitce(type_, value_)
- -- LuaEventMgr:DispatchEvent(type_, value_);
- end
- --打开重连窗口
- function NetManager:OpenReconnectionWnd(value_)
- end
- function NetManager:ResetNetMgr()
- -- LogError("ResetNetMgr")
- connectGateSuccess = false;
- self.connectGated = Enum.ParamState.None
- self:CloseGameServerConnect()
- self:CloseWaitingCo()
- end
- function NetManager:DisconnectSrv()
- self:Disconnect()
- end
- function NetManager:Destroy()
- --LogError("Destroy")
- self.ReceiveMessageFunc = nil
- self.OpenReconnectionWndFunc = nil
- self.LuaNoitceFunc = nil
- self:UnRegisterMsgReqAll()
- registerPbCallback = nil
- connectGateSuccess = false;
- self.connectGated = nil
- self:CloseGameServerConnect()
- self:CloseWaitingCo()
-
- if tolua.getpeer(self) ~= nil then
- tolua.setpeer(self, nil)
- end
- end
- return NetManager
|