protoUtil.lua 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. -- 协议解析 辅助
  2. local core = require("sproto.core")
  3. local sprotoloader = require "sprotoloader"
  4. local root = {}
  5. local request = nil
  6. local response = nil
  7. local HOST = nil
  8. local attach = nil
  9. -- 更新
  10. function root:update()
  11. request = nil
  12. response = nil
  13. HOST = nil
  14. attach = nil
  15. end
  16. -- 获取请求协议
  17. function root:get_c2s()
  18. if request == nil then
  19. request = sprotoloader.load(1)
  20. end
  21. return request
  22. end
  23. -- 获取回应协议
  24. function root:get_s2c()
  25. if response == nil then
  26. response = sprotoloader.load(2)
  27. end
  28. return response
  29. end
  30. function root:get_host()
  31. HOST = HOST or self:get_c2s():host "package"
  32. return HOST
  33. end
  34. function root:host_request()
  35. local protoS2C = self:get_s2c()
  36. attach = attach or self:get_host():attach(protoS2C)
  37. return attach
  38. end
  39. -- 解码请求包 客户端->服务端
  40. function root:decode_c2s_req(msg, sz)
  41. local host = self:get_host()
  42. local ok, ptype, name, args, rsp = pcall(host.dispatch, host, msg, sz)
  43. if not ok then
  44. log.error("decode_c2s_req error type[%s]", ptype)
  45. return
  46. end
  47. return ok, ptype, name, args, rsp
  48. end
  49. -- 编码回应包 客户端->服务端
  50. function root:encode_c2s_rsp(rsp, res, name, args)
  51. local ok, rs = pcall(rsp, res)
  52. if not ok then
  53. log.error("sproto response error: %s", rs)
  54. return
  55. end
  56. return rs
  57. end
  58. function root:encode_c2s_rsp_ex(name, res, session, ud)
  59. local clientHost = self:get_host()
  60. local headerTmp = {type = nil, session = session, ud = ud}
  61. local header = core.encode(clientHost.__package, headerTmp)
  62. local c2s = self:get_c2s()
  63. local ok, content = pcall(c2s.response_encode, c2s, name, res)
  64. if not ok then
  65. error(string.format("打包协议%s出错 %s", name, content))
  66. return
  67. end
  68. return core.pack(header .. content)
  69. end
  70. -- 编码推送包 服务端->客户端
  71. function root:encode_s2c_req(name, args)
  72. local ok, data = pcall(self:host_request(), name, args)
  73. if not ok then
  74. log.error("s2c sproto name[%s] args[%s], error", name, tostring(args))
  75. return
  76. end
  77. return data
  78. end
  79. local function queryproto(self, pname)
  80. local v = self.__pcache[pname]
  81. if not v then
  82. local tag, req, resp = core.protocol(self.__cobj, pname)
  83. assert(tag, pname .. " not found")
  84. if tonumber(pname) then
  85. pname, tag = tag, pname
  86. end
  87. v = {
  88. request = req,
  89. response = resp,
  90. name = pname,
  91. tag = tag
  92. }
  93. self.__pcache[pname] = v
  94. self.__pcache[tag] = v
  95. end
  96. return v
  97. end
  98. -- 解码客户端请求协议
  99. function root:decode_c2s_req_ex(...)
  100. local bin = core.unpack(...)
  101. local header_tmp = {}
  102. header_tmp.type = nil
  103. header_tmp.session = nil
  104. header_tmp.ud = nil
  105. local clientHost = self:get_host()
  106. local header, size = core.decode(clientHost.__package, bin, header_tmp)
  107. local content = bin:sub(size + 1)
  108. if header.type then
  109. -- request
  110. local proto = queryproto(clientHost.__proto, header.type)
  111. local result
  112. if proto.request then
  113. result = core.decode(proto.request, content)
  114. end
  115. if header_tmp.session then
  116. return "REQUEST", proto.name, result, header.session, header.ud
  117. else
  118. return "REQUEST", proto.name, result, header.session, header.ud
  119. end
  120. else
  121. -- response
  122. local session = assert(header_tmp.session, "session not found")
  123. local rsp = assert(clientHost.__session[session], "Unknown session")
  124. clientHost.__session[session] = nil
  125. if rsp == true then
  126. return "RESPONSE", session, nil, header.ud
  127. else
  128. local result = core.decode(rsp, content)
  129. return "RESPONSE", session, result, header.ud
  130. end
  131. end
  132. end
  133. return root