InnerMsg.lua 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. --[[
  2. 内部通信协议模板规则如下( world <-->logic通信)
  3. 1. 在每个模块的Proto.lua文件中定义[ WL(world->logic)或 LW(logic->world) ]开头的协议
  4. 2. LW协议在每个模块的HandlerW协议中处理
  5. 3. {"v1", "int"},
  6. {"v2", "string"},
  7. {"v3", "table"},
  8. --]]
  9. proto_id = proto_id or {}
  10. proto_handler = proto_handler or {}
  11. proto_name = proto_name or {}
  12. proto_template = proto_template or {}
  13. wl = wl or {} --world -> logic
  14. lw = lw or {} --logic -> world
  15. local msg_parse = _G.msg_inner_parse
  16. local Config = require("Config")
  17. local Log = require("common.Log")
  18. function register(name, id, template, handler)
  19. if not proto_name[id] then
  20. if string.sub(name, 1, 2) == "WL" then
  21. if _G.is_middle then
  22. wl[name] = {[1] = id}
  23. else
  24. wl[id] = {[1] = id}
  25. end
  26. elseif string.sub(name, 1, 2) == "LW" then
  27. if _G.is_middle then
  28. lw[id] = {[1] = id}
  29. else
  30. lw[name] = {[1] = id}
  31. end
  32. end
  33. end
  34. proto_handler[id] = handler
  35. proto_name[id] = name
  36. proto_template[id] = template
  37. for i = 1, #template do
  38. if template[i][2] == "int" then
  39. template[i][3] = 0
  40. elseif template[i][2] == "string" then
  41. template[i][3] = 1
  42. elseif template[i][2] == "table" then
  43. template[i][3] = 2
  44. elseif template[i][2] == "chunk" then--发送时压缩,读取时不解压
  45. template[i][3] = 3
  46. elseif template[i][2] == "binary" then
  47. template[i][3] = 4
  48. elseif template[i][2] == "zip" then--发送时压缩,读取时解压
  49. template[i][3] = 5
  50. end
  51. end
  52. end
  53. function readMsg(id, msg)
  54. if not msg then
  55. -- Log.write(Log.LOGID_DEBUG, "[readMsg] msg参数为nil, id="..(id or "nil"))
  56. return
  57. end
  58. local template = proto_template[id]
  59. if not template then
  60. assert(nil, "readMsg no template: id:"..id)
  61. end
  62. local ret
  63. for i = 1, #template do
  64. if template[i][3] == 0 then
  65. ret, msg[template[i][1] ] = msg_parse.readint()
  66. if not ret then
  67. assert(nil, "inner msg:"..proto_name[id].." "..template[i][1])
  68. end
  69. elseif template[i][3] == 1 then
  70. ret, msg[template[i][1] ] = msg_parse.readstring()
  71. if not ret then
  72. assert(nil, "inner msg:"..proto_name[id].." "..template[i][1])
  73. end
  74. elseif template[i][3] == 2 then
  75. msg[template[i][1] ] = {}
  76. ret = msg_parse.readtable(msg[template[i][1]])
  77. if not ret then
  78. assert(nil, "inner msg:"..proto_name[id].." "..template[i][1])
  79. end
  80. elseif template[i][3] == 3 then
  81. ret, msg[template[i][1]] = msg_parse.readchunk()
  82. if not ret then
  83. assert(nil, "inner msg:"..proto_name[id].." "..template[i][1])
  84. end
  85. elseif template[i][3] == 4 then
  86. msg[template[i][1]] = {}
  87. ret = msg_parse.readbinary( msg[template[i][1]])
  88. if not ret then
  89. assert(nil, "inner msg:"..proto_name[id].." "..template[i][1])
  90. end
  91. elseif template[i][3] == 5 then
  92. msg[template[i][1]] = {}
  93. ret = msg_parse.readzip( msg[template[i][1]])
  94. if not ret then
  95. assert(nil, "inner msg:"..proto_name[id].." "..template[i][1])
  96. end
  97. end
  98. end
  99. end
  100. function sendMsg(fd, msg)
  101. local id = msg[1]
  102. local template = proto_template[id]
  103. if not template then
  104. assert(nil, "sendMsg no template: id:"..id)
  105. end
  106. --Log.write(Log.LOGID_INNER_CLOSE, "inner socket close...,drop inner msg id:", id)
  107. if not _G.is_middle and not msg_parse.check_connect() then --链接断了,写日志
  108. print("inner socket close...,drop inner msg id--"..id)
  109. return
  110. end
  111. msg_parse.writebegin(fd, id)
  112. for i = 1, #template do
  113. if template[i][3] == 0 then
  114. msg_parse.writeint(msg[template[i][1]])
  115. elseif template[i][3] == 1 then
  116. msg_parse.writestring(msg[template[i][1]])
  117. elseif template[i][3] == 2 then
  118. msg_parse.writetable(msg[template[i][1]])
  119. elseif template[i][3] == 3 then
  120. msg_parse.writechunk(msg[template[i][1]])
  121. elseif template[i][3] == 4 then
  122. msg_parse.writebinary(msg[template[i][1]], string.len(msg[template[i][1]]))
  123. elseif template[i][3] == 5 then
  124. msg_parse.writezip(msg[template[i][1]])
  125. end
  126. end
  127. msg_parse.writeend()
  128. end
  129. function unbinary(binary, len , out)
  130. return msg_parse.unbinary(binary, len , out)
  131. end