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.

209 lines
5.6 KiB

  1. require("luaScript.Protocol.ProtoDef")
  2. local Protocol = class("Protocol" , require("luaScript.Protocol.BindableArray"))
  3. function Protocol:ctor(net)
  4. -- 协议对应的网络链接
  5. self.net = net;
  6. -- 保存模块ID
  7. self.ModuleId = 99;
  8. -- 已经注册的服务器消息列表
  9. self.PushMsgList = {}
  10. cc.GameObject.extend(self)
  11. self:addComponent(require("luaScript.cc.components.behavior.EventProtocol"):new()):exportMethods()
  12. end
  13. -- 记录所有发送消息等待回调的函数列表,当断线重连时
  14. g_regMsgCallBackFuncs = {}
  15. --[[
  16. 发送消息,并接收回调
  17. args = {
  18. cmd , -- 命令ID
  19. sender , -- 可选参数,发送的结构体实例(通过defClass定义的类的new创建出来的实例)
  20. reader , -- 可选参数,接收的结构体类型(通过defClass定义)
  21. func , -- 可选参数,回调函数,类型是func(statusCode , response),其中response时read结构体读出来的数据
  22. }
  23. --]]
  24. function Protocol:sendResponse(args)
  25. assert(args.cmd ~= nil);
  26. -- 检查一下网络是否正常
  27. if not self.net or not self.net:isConnected() then
  28. if isWin32Platform() then
  29. assert(false);
  30. end
  31. return
  32. end
  33. -- 如果定义了需要发送的数据,就发送数据,否则只发命令
  34. if args.sender then
  35. args.sender:send(self.ModuleId , args.cmd, self.net);
  36. else
  37. self.net:sendCmd(self.ModuleId , args.cmd);
  38. end
  39. local unregFunc = nil
  40. local function onMsg(statusCode , stream)
  41. -- 清空注册以及清空记录
  42. g_regMsgCallBackFuncs[unregFunc]()
  43. g_regMsgCallBackFuncs[unregFunc] = nil
  44. if statusCode ~= 0 then
  45. args.func(statusCode);
  46. print("onMsg时返回了非0 statusCode:" .. tostring(statusCode));
  47. return
  48. end
  49. if args.reader then
  50. local response = args.reader:tryRead(stream);
  51. if response then
  52. args.func(statusCode , response);
  53. else
  54. args.func(2);
  55. end
  56. else
  57. args.func(statusCode);
  58. end
  59. end
  60. -- 如果定义了回调函数,则接收数据
  61. if args.func and args.resCmd then
  62. assert(type(args.func) == "function")
  63. self.net:regMsg(self.ModuleId, args.resCmd, onMsg);
  64. unregFunc = function()self.net:unregMsg(onMsg)end
  65. g_regMsgCallBackFuncs[unregFunc] = unregFunc
  66. end
  67. end
  68. --[[
  69. 定义网络消息函数,并把这个函数导出到当前Protocol类中对外公开
  70. 对外公开的函数定义是name(sendArgs , responseFunc)或name(responseFunc)
  71. args = {
  72. name , -- 需要导出的函数名称
  73. cmd , -- 命令ID
  74. sender , -- 可选参数,发送的结构体类型(通过defClass定义)
  75. reader , -- 可选参数,接收的结构体类型(通过defClass定义)
  76. func , -- 可选参数,回调函数,类型是func(statusCode , response)或func(statusCode),其中response时read结构体读出来的数据
  77. }
  78. --]]
  79. function Protocol:defMsgFunc(args)
  80. --[[
  81. {
  82. name = "",
  83. cmd = number,
  84. resCmd = number,
  85. sender = netClass,
  86. reader = netClass,
  87. func = function,
  88. }
  89. ]]
  90. assert(args.name ~= nil and type(args.cmd) == "number");
  91. if args.sender then
  92. -- sendArgs是一个表,会把这个表的数据拷贝到sender中
  93. local function sendData(self , sendArgs , responseFunc)
  94. local args = clone(args);
  95. local sender = args.sender:new();
  96. for i , v in pairs(sendArgs) do
  97. sender[i] = v;
  98. end
  99. args.sender = sender;
  100. local argFunc = args.func;
  101. if argFunc then
  102. args.func = function(statusCode , response)
  103. argFunc(statusCode , response);
  104. if responseFunc then
  105. responseFunc(statusCode , response)
  106. end
  107. end;
  108. elseif responseFunc then
  109. args.func = responseFunc;
  110. end
  111. self:sendResponse(args);
  112. end
  113. self[args.name] = sendData;
  114. else
  115. local function sendData(self , responseFunc)
  116. local args = clone(args);
  117. local argFunc = args.func;
  118. if argFunc then
  119. args.func = function(statusCode , response)
  120. argFunc(statusCode , response);
  121. if responseFunc then
  122. responseFunc(statusCode , response)
  123. end
  124. end;
  125. elseif responseFunc then
  126. args.func = responseFunc;
  127. end
  128. self:sendResponse(args);
  129. end
  130. self[args.name] = sendData;
  131. end
  132. end
  133. --[[
  134. 定义一个推送消息,会持续地接收这个消息,并通过回调函数返回数据
  135. args = {
  136. cmd , -- 命令ID
  137. reader , -- 可选参数,接收的结构体类型(通过defClass定义)
  138. func , -- 可选参数,回调函数,类型是func(statusCode , response),其中response时read结构体读出来的数据
  139. }
  140. --]]
  141. function Protocol:defPushMsg(args)
  142. assert(args.cmd ~= nil);
  143. local function onMsg(statusCode , stream)
  144. if statusCode ~= 0 then
  145. args.func(statusCode);
  146. print("onMsg时返回了非0 statusCode:" .. tostring(statusCode));
  147. return
  148. end
  149. if args.reader then
  150. local response = args.reader:tryRead(stream);
  151. if response then
  152. args.func(statusCode , response);
  153. else
  154. args.func(2);
  155. end
  156. else
  157. args.func(statusCode);
  158. end
  159. end
  160. -- 如果定义了回调函数,则接收数据
  161. if args.func then
  162. --table.insert(args.cmd, args.func);
  163. self.PushMsgList[args.cmd] = onMsg--args.func
  164. self.net:regMsg(self.ModuleId, args.cmd, onMsg);
  165. end
  166. end
  167. function Protocol:defPullMsg(args)
  168. if args.sender then
  169. -- sendArgs是一个表,会把这个表的数据拷贝到sender中
  170. local function sendData(responseFunc)
  171. local args = clone(args);
  172. local sender = args.sender:new();
  173. args.sender = sender;
  174. args.func = responseFunc;
  175. self:sendResponse(args);
  176. end
  177. else
  178. local function sendData(responseFunc)
  179. local args = clone(args);
  180. args.func = responseFunc;
  181. self:sendResponse(args);
  182. end
  183. end
  184. self:defMsgFunc(args);
  185. end
  186. function Protocol:unRegAll()
  187. for cmdId,func in pairs(self.PushMsgList) do
  188. self.net:unregMsg(func);
  189. end
  190. end
  191. return Protocol;