NetworkManager.lua.txt 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. local NetworkManager = {}
  2. local function ConnectServer(serverIP, serverPort)
  3. local connectState = CS.ProjectNine.STCPConnecter.Connect(serverIP, serverPort)
  4. NetworkManager.connectState = connectState
  5. return connectState
  6. end
  7. local function ConnectLogicCoroutine()
  8. local serverIP = "127.0.0.1"
  9. local serverPort = 25500
  10. local eventManager = require("Base/SEventDispatchCenter.lua")
  11. require("Base/CoroutineHelper.lua"):WaitForEndOfFrame()
  12. local hasFailed = false
  13. local condition = false
  14. while condition == false do
  15. local connectState = SafeCall(ConnectServer, serverIP, serverPort)
  16. if not connectState then
  17. print(string.format(
  18. "NetworkManager.ConnectLogicCoroutine ConnectServer(%s:%s) Failed. Retry after 5s:",
  19. serverIP, serverPort, connectState
  20. ))
  21. eventManager:DispatchEvent(eventManager.EventType.NET_CONNECT_STATE, false)
  22. hasFailed = true
  23. require("Base/CoroutineHelper.lua"):Wait(3)
  24. end
  25. condition = connectState == true
  26. end
  27. print(string.format( "NetworkManager.ConnectLogicCoroutine ConnectServer(%s:%s) Success!", serverIP, serverPort))
  28. if hasFailed == true then
  29. local languageConfig = require("Config/SConfigManager.lua"):GetConfig("languageConfig")
  30. ShowDialogOK(languageConfig["login"]["connect_success"])
  31. eventManager:DispatchEvent(eventManager.EventType.NET_CONNECT_STATE, true)
  32. end
  33. end
  34. local function RecvlogicCoroutine()
  35. while true do
  36. require("Base/CoroutineHelper.lua"):Wait(0.1)
  37. local serverMessage = CS.ProjectNine.STCPConnecter.RecvMessage()
  38. if serverMessage ~= "" then
  39. local serverProtocol = NetworkManager._jsonConvert:decode(serverMessage)
  40. local processorName = serverProtocol.fnProcessor
  41. if not processorName then print("Get processorName Failed for: ", serverMessage); end
  42. local fnProcessor = NetworkManager._s2c[serverProtocol.fnProcessor]
  43. if not fnProcessor then print("Get fnProcessor failed for : " , serverMessage) end
  44. fnProcessor(NetworkManager._s2c, table.unpack(serverProtocol.tParam))
  45. end
  46. end
  47. end
  48. function NetworkManager:Init()
  49. print("NetworkManager.Init~")
  50. self._c2s = require("Network/C2S.lua")
  51. self._s2c = require("Network/S2C.lua")
  52. self._connectLogicCoroutine = require("Base/CoroutineHelper.lua"):Start(ConnectLogicCoroutine)
  53. self._recvlogicCoroutine = require("Base/CoroutineHelper.lua"):Start(RecvlogicCoroutine)
  54. self._jsonConvert = require("Base/json.lua")
  55. end
  56. function NetworkManager:UnInit()
  57. require("Base/CoroutineHelper.lua"):Stop(NetworkManager._connectLogicCoroutine)
  58. require("Base/CoroutineHelper.lua"):Stop(NetworkManager._recvlogicCoroutine)
  59. print("NetworkManager.UnInit ~")
  60. end
  61. function NetworkManager:RemoteCall(funcName, ...)
  62. print("NetworkManager:RemoteCall funcName:", funcName, " param:", ...)
  63. self._c2s:RemoteCall(funcName, ...)
  64. end
  65. function NetworkManager:ApplyTestBattle()
  66. self._c2s:ApplyTestBattle()
  67. end
  68. function NetworkManager:Login(account, password)
  69. if not self.connectState then
  70. print("Network is not ready.");
  71. return
  72. end
  73. print("NetworkManager.Login ->", account, password)
  74. self._c2s:Login(account, password)
  75. end
  76. function NetworkManager:ModifyPlayerName(newName)
  77. if not self.connectState then
  78. print("Network is not ready.")
  79. return
  80. end
  81. print("NetworkManager.ModifyPlayerName ->", newName)
  82. self._c2s:ModifyPlayerName(newName)
  83. end
  84. return NetworkManager