clusterproxy.lua 930 B

12345678910111213141516171819202122232425262728293031323334
  1. local skynet = require "skynet"
  2. local cluster = require "skynet.cluster"
  3. require "skynet.manager" -- inject skynet.forward_type
  4. local node, address = ...
  5. skynet.register_protocol {
  6. name = "system",
  7. id = skynet.PTYPE_SYSTEM,
  8. unpack = function (...) return ... end,
  9. }
  10. local forward_map = {
  11. [skynet.PTYPE_SNAX] = skynet.PTYPE_SYSTEM,
  12. [skynet.PTYPE_LUA] = skynet.PTYPE_SYSTEM,
  13. [skynet.PTYPE_RESPONSE] = skynet.PTYPE_RESPONSE, -- don't free response message
  14. }
  15. skynet.forward_type( forward_map ,function()
  16. local clusterd = skynet.uniqueservice("clusterd")
  17. local n = tonumber(address)
  18. if n then
  19. address = n
  20. end
  21. local sender = skynet.call(clusterd, "lua", "sender", node)
  22. skynet.dispatch("system", function (session, source, msg, sz)
  23. if session == 0 then
  24. skynet.send(sender, "lua", "push", address, msg, sz)
  25. else
  26. skynet.ret(skynet.rawcall(sender, "lua", skynet.pack("req", address, msg, sz)))
  27. end
  28. end)
  29. end)