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.

312 lines
7.6 KiB

  1. require("luaScript.Tools.NetBase")
  2. local NetGameServer = class("NetGameServer", NetBase)
  3. local IpStep =
  4. {
  5. fromConfig = 1,
  6. fromFile = 2,
  7. fromUrl = 3,
  8. }
  9. -- 构造函数
  10. function NetGameServer:ctor()
  11. NetGameServer.super.ctor(self);
  12. -- 所有IP
  13. self.ipList = {}
  14. -- 当前IP的索引
  15. self.ipIndex = 0;
  16. -- 当前IP使用的次数
  17. self.curReconnectTimes = 0;
  18. -- 每个IP最多重连3次
  19. self.maxReconnectTimes = 2;
  20. self.StringKey = "ServerInfo2"
  21. -- ip 管理器
  22. -- require("luaScript.Tools.IpManager");
  23. -- self.ipManager = IpManager:new();
  24. -- 初始化默认使用的ip和端口
  25. self:resetIpListFromConfig(); -- todo lwq 先使用服务器下发的地址,然后再使用本地的地址
  26. self:resetIpListFromFile();
  27. -- 优先从 url 获取Ip
  28. self.stepIdx = 1;
  29. --self.stepList = {IpStep.fromConfig, IpStep.fromUrl}
  30. self.stepList = {IpStep.fromConfig}
  31. end
  32. -- 重写:连接时的回调
  33. function NetGameServer:OnConnecttingCallback()
  34. logD("moduleID[99] - NetGameServer:OnConnecttingCallback()")
  35. self.connectSuccess = false
  36. app:dispatchEvent({name = "onGameServerConnecting" , net = self});
  37. end
  38. -- 重写:断开连接时的回调
  39. function NetGameServer:OnDisconnectCallback()
  40. logD("moduleID[99] - NetGameServer:OnDisconnectCallback()")
  41. self.connectSuccess = false
  42. app:dispatchEvent({name = "onGameServerDisConnect" , net = self});
  43. -- self:close()
  44. if not app.user.kickOut then
  45. app.user.kickOut = false
  46. self:reConnect();
  47. end
  48. end
  49. -- 重写:连接成功之后的回调
  50. function NetGameServer:OnConnectedCallback()
  51. logD("moduleID[99] - NetGameServer:OnConnectedCallback()")
  52. app:dispatchEvent({name = "onGameServerConnected" , net = self});
  53. self:onConnectIpSave()
  54. end
  55. -- 将连接成功的ipList写入到本地文件
  56. function NetGameServer:saveIpToFile()
  57. local str1 = self.ip..":"..self.port;
  58. saveUserInfo(self.StringKey, str1);
  59. end
  60. -- 重置 ipList 为本地文件中的值
  61. function NetGameServer:resetIpListFromFile()
  62. log("200000 moduleID[99] NetGameServer:resetIpListFromFile()")
  63. self.ipIndex = 0;
  64. -- self.ipList = {};
  65. local str1 = loadUserInfo(self.StringKey)
  66. if str1 and str1 ~= "" then
  67. local arr = string.split(str1, ":")
  68. if table.nums(arr) == 2 then
  69. if tonumber(arr[2]) > 0 then
  70. table.insert(self.ipList, {ip = arr[1], port = arr[2]})
  71. return
  72. end
  73. end
  74. end
  75. end
  76. -- 重置 ipList 为配置的默认值
  77. function NetGameServer:resetIpListFromConfig()
  78. log("200000 moduleID[99] NetGameServer:resetIpListFromConfig()")
  79. local strGameServer = ""
  80. if PhpSetting and PhpSetting.gameServer then
  81. strGameServer = PhpSetting.gameServer;
  82. end
  83. self.ipList = {};
  84. local arr1 = string.split(strGameServer, "|")
  85. for k,v in pairs(arr1) do
  86. local arr2 = string.split(v, ":")
  87. table.insert(self.ipList, {ip=arr2[1], port=arr2[2]});
  88. end
  89. end
  90. -- 重置获取ip的顺序
  91. function NetGameServer:resetIpStep()
  92. -- 优先从 url 获取Ip
  93. self.stepIdx = 1;
  94. --self.stepList = {IpStep.fromUrl, IpStep.fromConfig}
  95. self.stepList = {IpStep.fromConfig}
  96. end
  97. -- 更新可用的ipList
  98. -- callback = function()
  99. --[[function NetGameServer:upateIpList(callback)
  100. log("200000 NetGameServer:upateIpList()")
  101. local function doCallback(ret)
  102. if callback then
  103. callback(ret);
  104. end
  105. end
  106. self.ipList = {}
  107. self.ipIndex = 0;
  108. local curStep = self.stepList[self.stepIdx];
  109. if not curStep then
  110. doCallback(false);
  111. return
  112. end
  113. log("200000 NetGameServer:upateIpList() curStep = ", curStep)
  114. if curStep == IpStep.fromUrl then
  115. if self.ipManager then
  116. local function onUpdateIpListResponse(ipList)
  117. log("200000 NetGameServer:resetIpListFromFile() updateIpList --> ipList = ", table.tostring(ipList))
  118. if ipList and type(ipList) == "table" then
  119. for k,v in pairs(ipList) do
  120. table.insert(self.ipList, {ip = v.ip, port = v.port});
  121. end
  122. else
  123. self.stepIdx = self.stepIdx + 1
  124. end
  125. doCallback(true);
  126. end
  127. self.ipManager:updateIpList(onUpdateIpListResponse);
  128. end
  129. elseif curStep == IpStep.fromConfig then
  130. self.stepIdx = self.stepIdx + 1
  131. self:resetIpListFromConfig();
  132. doCallback(true);
  133. elseif curStep == IpStep.fromFile then
  134. self.stepIdx = self.stepIdx + 1
  135. self:resetIpListFromFile();
  136. doCallback(true);
  137. else
  138. doCallback(true);
  139. end
  140. end--]]
  141. -- 获取一个可用使用的ip和port
  142. -- callback = function(ip, port)
  143. --[[function NetGameServer:getIp(callback)
  144. log("200000 NetGameServer:getIp() ")
  145. local function doCallback(ip, port)
  146. if callback then
  147. callback(ip, port);
  148. end
  149. end
  150. local function getFromList()
  151. log("200000 NetGameServer:getIp() getFromList()")
  152. self.ipIndex = self.ipIndex + 1
  153. if self.ipIndex <= #self.ipList then
  154. doCallback(self.ipList[self.ipIndex].ip, self.ipList[self.ipIndex].port);
  155. else
  156. doCallback(nil, nil)
  157. end
  158. end
  159. local function getWithUpdate()
  160. log("200000 NetGameServer:getIp() getWithUpdate()")
  161. local function onUpdateEnd(ret)
  162. if #self.ipList > 0 then
  163. getFromList();
  164. else
  165. if ret then
  166. self:upateIpList(onUpdateEnd)
  167. else
  168. doCallback(nil, nil)
  169. end
  170. end
  171. end
  172. self:upateIpList(onUpdateEnd)
  173. end
  174. log("200000 NetGameServer:getIp() ", self.ip, self.port, self.curReconnectTimes, self.maxReconnectTimes)
  175. if self.curReconnectTimes < self.maxReconnectTimes and self.ip and self.port then
  176. log("200000 NetGameServer:getIp() use old")
  177. doCallback(self.ip, self.port)
  178. else
  179. self.curReconnectTimes = 0
  180. if self.ipList and self.ipIndex < #self.ipList then
  181. getFromList();
  182. else
  183. getWithUpdate();
  184. end
  185. end
  186. end--]]
  187. function NetGameServer:getIp(callback)
  188. log("200000 moduleID[99] NetGameServer:getIp() ")
  189. log("200000 moduleID[99] NetGameServer:getIp() ipList = ", table.tostring(self.ipList))
  190. local function doCallback(ip, port)
  191. if callback then
  192. callback(ip, port);
  193. end
  194. end
  195. self.ipIndex = self.ipIndex + 1
  196. if self.ipIndex <= #self.ipList then
  197. log("200000 moduleID[99] NetGameServer:getIp() "..self.ipIndex)
  198. if not self.ipList[self.ipIndex] then
  199. doCallback(nil ,nil)
  200. return
  201. end
  202. local tip = self.ipList[self.ipIndex].ip
  203. local tport = self.ipList[self.ipIndex].port
  204. doCallback(tip,tport)
  205. else
  206. doCallback(nil ,nil)
  207. end
  208. end
  209. -- 开始连接
  210. function NetGameServer:startNet()
  211. logD("moduleID[99] - NetGameServer:startNet()")
  212. self.isNeedReconnect = true
  213. self:getIp(function(ip, port)
  214. if ip and port then
  215. self.curReconnectTimes = self.curReconnectTimes + 1
  216. self:connect(ip, port)
  217. else
  218. -- ip 不可用,提示玩家重启游戏
  219. local function onOk()
  220. cc.Application:getInstance():restart();
  221. end
  222. app.waitDialogManager:closeAllNetWait();
  223. showConfirmDialog("网络连接失败,点击确定重启游戏!", onOk);
  224. end
  225. end )
  226. end
  227. -- 重新连接服务器
  228. function NetGameServer:reConnect()
  229. logD("moduleID[99] - NetGameServer:reConnect()")
  230. app.waitDialogManager:closeAllNetWait();
  231. app.waitDialogManager:showWaitNetworkDialog("重连中....")
  232. if not self.isNeedReconnect then
  233. return;
  234. end
  235. self:startNet();
  236. end
  237. --连上IP 存储ip
  238. function NetGameServer:onConnectIpSave()--这些操作是之前每次收到服务器回复都会执行,现在提出来,只在连上的时候执行一次
  239. logD("moduleID[99] NetGameServer:onConnectIpSave()")
  240. self.ipIndex = 0
  241. -- 标记为成功链接
  242. self.connectSuccess = true
  243. -- 能收到服务器下发的消息,才说明是真的连接上了
  244. self.curReconnectTimes = 0;
  245. -- 重置 ipManager 的索引
  246. if self.ipManager then
  247. self.ipManager.urlIndex = 0;
  248. end
  249. -- 将连接成功的ip写入到本地文件
  250. self:saveIpToFile();
  251. -- 重置执行队列
  252. self:resetIpStep()
  253. end
  254. function NetGameServer:onRecvServerMsg(moduleID , cmdID , statusCode , stream)
  255. -- 发送事件
  256. app:dispatchEvent({name = "onRecvMsgFromGameServer" , net = self});
  257. end
  258. return NetGameServer;