_client.lua 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. -- Warning: do not edit this file.
  2. -- 警告: 不要编辑此文件
  3. ---------------------------------
  4. --! @file
  5. --! @brief a Doxygen::Lua NetClient.lua
  6. ---------------------------------
  7. require 'Protocol/generated/_codec'
  8. Protocol.Serializer.Config('Protocol/generated')
  9. local InputStream = require 'Protocol/generated/_istream'
  10. local OutputStream = require 'Protocol/generated/_ostream'
  11. local RequestHandler = {}
  12. RequestHandler.__index = function(self, key)
  13. print('RequestHandler', key)
  14. -- request
  15. Protocol.InitShortMap()
  16. local tempalteObj = Protocol.ShortStringDefined[key]
  17. if not tempalteObj then
  18. error(key .. ' not exists')
  19. end
  20. return function(msg, cb, errcb, option)
  21. if type(msg) == 'function' and not cb then
  22. msg = {}
  23. cb = msg
  24. end
  25. Protocol.Request(msg, tempalteObj, cb, errcb, option)
  26. end
  27. end
  28. local PushHandler = {}
  29. PushHandler.__index = function(self, key)
  30. -- request
  31. Protocol.InitShortMap()
  32. local tempalteObj = Protocol.ShortStringDefined[key]
  33. if not tempalteObj then
  34. error(key .. ' not exists')
  35. end
  36. return function(cb)
  37. Protocol.Listen(tempalteObj, cb)
  38. end
  39. end
  40. local NotifyHandler = {}
  41. NotifyHandler.__index = function(self, key)
  42. -- request
  43. Protocol.InitShortMap()
  44. local tempalteObj = Protocol.ShortStringDefined[key]
  45. if not tempalteObj then
  46. error(key .. ' not exists')
  47. end
  48. return function(msg)
  49. Protocol.Notify(msg, tempalteObj)
  50. end
  51. end
  52. Protocol = Protocol or {}
  53. Protocol.RequestHandler = setmetatable({}, RequestHandler)
  54. Protocol.PushHandler = setmetatable({}, PushHandler)
  55. Protocol.NotifyHandler = setmetatable({}, NotifyHandler)
  56. function Protocol.InitShortMap()
  57. if Protocol.ShortStringDefined then
  58. return
  59. end
  60. Protocol.ShortStringDefined = {}
  61. for k, v in pairs(Protocol.Serializer.StringDefined) do
  62. local fields = {}
  63. k:gsub(
  64. '([^%.]+)',
  65. function(c)
  66. fields[#fields + 1] = c
  67. end
  68. )
  69. local shortKey = fields[#fields]
  70. -- print('shortkey', shortKey)
  71. if Protocol.ShortStringDefined[shortKey] then
  72. error(key .. ' has exists')
  73. end
  74. Protocol.ShortStringDefined[shortKey] = v
  75. end
  76. end
  77. function Protocol.Request(msg, tempalteObj, cb, errcb, option)
  78. local output = OutputStream.Create()
  79. output:PutRouteOBJ(msg, tempalteObj)
  80. local rq = BinaryMessage.FromBuffer(tempalteObj.MessageID, output:GetBuffer())
  81. output:Dispose()
  82. if type(cb) == 'userdata' then
  83. option = errcb
  84. errcb = nil
  85. cb = nil
  86. end
  87. if type(errcb) == 'userdata' then
  88. option = errcb
  89. errcb = nil
  90. end
  91. GameSceneMgr.Instance:OnRequestStartEvent(tempalteObj.Name, option)
  92. TLNetManage.Instance:RequestBinary(
  93. rq,
  94. function(ex, rp)
  95. local msg_rp
  96. if not ex then
  97. --decode
  98. --print('rp.buffer', rp.Buffer)
  99. local input = InputStream.Create(rp:ToArray())
  100. msg_rp, rpTemplate = input:GetRouteOBJ(rp.Route)
  101. input:Dispose()
  102. GameSceneMgr.Instance:OnRequestEndEvent(tempalteObj.Name, msg_rp.s2c_code, msg_rp.s2c_msg, ex, option)
  103. if not msg_rp:IsSuccess() then
  104. -- TODO
  105. print('Got error code' .. msg_rp.s2c_code .. (msg_rp.s2c_msg or ''))
  106. if errcb then
  107. errcb(msg_rp)
  108. end
  109. elseif cb then
  110. cb(msg_rp)
  111. end
  112. print('Recived ', rpTemplate.Name, msg_rp.s2c_code)
  113. else
  114. -- TODO
  115. GameSceneMgr.Instance:OnRequestEndEvent(tostring(rp.Route), -1, '', ex, option)
  116. print('Got exception' .. tostring(ex))
  117. end
  118. end
  119. )
  120. end
  121. function Protocol.Listen(tempalteObj, cb)
  122. TLNetManage.Instance:ListenBinary(
  123. tempalteObj.MessageID,
  124. function(rp)
  125. --print('rp.buffer', rp.Buffer)
  126. local input = InputStream.Create(rp:ToArray())
  127. local msg_rp = input:GetRouteOBJ(rp.Route)
  128. input:Dispose()
  129. cb(msg_rp)
  130. end
  131. )
  132. end
  133. function Protocol.Notify(msg, tempalteObj)
  134. local output = OutputStream.Create()
  135. output:PutRouteOBJ(msg, tempalteObj)
  136. local rq = BinaryMessage.FromBuffer(tempalteObj.MessageID, output:GetBuffer())
  137. output:Dispose()
  138. TLNetManage.Instance:NotifyBinary(rq)
  139. end