InnerMsg.lua 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. local template = proto_template[id]
  55. if not template then
  56. assert(nil, "readMsg no template: id:"..id)
  57. end
  58. local ret
  59. for i = 1, #template do
  60. if template[i][3] == 0 then
  61. ret, msg[template[i][1] ] = msg_parse.readint()
  62. if not ret then
  63. assert(nil, "inner msg:"..proto_name[id]..template[i][1])
  64. end
  65. elseif template[i][3] == 1 then
  66. ret, msg[template[i][1] ] = msg_parse.readstring()
  67. if not ret then
  68. assert(nil, "inner msg:"..proto_name[id]..template[i][1])
  69. end
  70. elseif template[i][3] == 2 then
  71. msg[template[i][1] ] = {}
  72. ret = msg_parse.readtable(msg[template[i][1]])
  73. if not ret then
  74. assert(nil, "inner msg:"..proto_name[id]..template[i][1])
  75. end
  76. elseif template[i][3] == 3 then
  77. ret, msg[template[i][1]] = msg_parse.readchunk()
  78. if not ret then
  79. assert(nil, "inner msg:"..proto_name[id]..template[i][1])
  80. end
  81. elseif template[i][3] == 4 then
  82. msg[template[i][1]] = {}
  83. ret = msg_parse.readbinary( msg[template[i][1]])
  84. if not ret then
  85. assert(nil, "inner msg:"..proto_name[id]..template[i][1])
  86. end
  87. elseif template[i][3] == 5 then
  88. msg[template[i][1]] = {}
  89. ret = msg_parse.readzip( msg[template[i][1]])
  90. if not ret then
  91. assert(nil, "inner msg:"..proto_name[id]..template[i][1])
  92. end
  93. end
  94. end
  95. end
  96. function sendMsg(fd, msg)
  97. local id = msg[1]
  98. local template = proto_template[id]
  99. if not template then
  100. assert(nil, "sendMsg no template: id:"..id)
  101. end
  102. --Log.write(Log.LOGID_INNER_CLOSE, "inner socket close...,drop inner msg id:", id)
  103. if not _G.is_middle and not msg_parse.check_connect() then --链接断了,写日志
  104. print("inner socket close...,drop inner msg id--"..id)
  105. return
  106. end
  107. msg_parse.writebegin(fd, id)
  108. for i = 1, #template do
  109. if template[i][3] == 0 then
  110. msg_parse.writeint(msg[template[i][1]])
  111. elseif template[i][3] == 1 then
  112. msg_parse.writestring(msg[template[i][1]])
  113. elseif template[i][3] == 2 then
  114. msg_parse.writetable(msg[template[i][1]])
  115. elseif template[i][3] == 3 then
  116. msg_parse.writechunk(msg[template[i][1]])
  117. elseif template[i][3] == 4 then
  118. msg_parse.writebinary(msg[template[i][1]], string.len(msg[template[i][1]]))
  119. elseif template[i][3] == 5 then
  120. msg_parse.writezip(msg[template[i][1]])
  121. end
  122. end
  123. msg_parse.writeend()
  124. end
  125. function unbinary(binary, len , out)
  126. return msg_parse.unbinary(binary, len , out)
  127. end