From b61daaccf1c98d3ca878ca94e9a441d2140e045d Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sun, 4 Sep 2022 14:22:13 +0800 Subject: [PATCH] watchdog returns addr,port --- examples/main.lua | 4 ++-- examples/watchdog.lua | 2 +- lualib-src/lua-netpack.c | 11 ++++++++--- lualib/snax/gateserver.lua | 21 +++++++++++++++++++++ service/gate.lua | 1 + 5 files changed, 33 insertions(+), 6 deletions(-) diff --git a/examples/main.lua b/examples/main.lua index 5a2150db..4d2bac4a 100644 --- a/examples/main.lua +++ b/examples/main.lua @@ -12,11 +12,11 @@ skynet.start(function() skynet.newservice("debug_console",8000) skynet.newservice("simpledb") local watchdog = skynet.newservice("watchdog") - skynet.call(watchdog, "lua", "start", { + local addr,port = skynet.call(watchdog, "lua", "start", { port = 8888, maxclient = max_client, nodelay = true, }) - skynet.error("Watchdog listen on", 8888) + skynet.error("Watchdog listen on " .. addr .. ":" .. port) skynet.exit() end) diff --git a/examples/watchdog.lua b/examples/watchdog.lua index dabbd097..9b04933a 100644 --- a/examples/watchdog.lua +++ b/examples/watchdog.lua @@ -40,7 +40,7 @@ function SOCKET.data(fd, msg) end function CMD.start(conf) - skynet.call(gate, "lua", "open" , conf) + return skynet.call(gate, "lua", "open" , conf) end function CMD.close(fd) diff --git a/lualib-src/lua-netpack.c b/lualib-src/lua-netpack.c index ac84972d..70016f22 100644 --- a/lualib-src/lua-netpack.c +++ b/lualib-src/lua-netpack.c @@ -22,6 +22,7 @@ #define TYPE_OPEN 4 #define TYPE_CLOSE 5 #define TYPE_WARNING 6 +#define TYPE_INIT 7 /* Each package is uint16 + data , uint16 (serialized in big-endian) is the number of bytes comprising the data . @@ -354,8 +355,11 @@ lfilter(lua_State *L) { assert(size == -1); // never padding string return filter_data(L, message->id, (uint8_t *)buffer, message->ud); case SKYNET_SOCKET_TYPE_CONNECT: - // ignore listen fd connect - return 1; + lua_pushvalue(L, lua_upvalueindex(TYPE_INIT)); + lua_pushinteger(L, message->id); + lua_pushlstring(L, buffer, size); + lua_pushinteger(L, message->ud); + return 5; case SKYNET_SOCKET_TYPE_CLOSE: // no more data in fd (message->id) close_uncomplete(L, message->id); @@ -483,8 +487,9 @@ luaopen_skynet_netpack(lua_State *L) { lua_pushliteral(L, "open"); lua_pushliteral(L, "close"); lua_pushliteral(L, "warning"); + lua_pushliteral(L, "init"); - lua_pushcclosure(L, lfilter, 6); + lua_pushcclosure(L, lfilter, 7); lua_setfield(L, -2, "filter"); return 1; diff --git a/lualib/snax/gateserver.lua b/lualib/snax/gateserver.lua index 3737f025..75cd6e54 100644 --- a/lualib/snax/gateserver.lua +++ b/lualib/snax/gateserver.lua @@ -34,6 +34,8 @@ function gateserver.start(handler) assert(handler.message) assert(handler.connect) + local listen_context = {} + function CMD.open( source, conf ) assert(not socket) local address = conf.address or "0.0.0.0" @@ -42,6 +44,12 @@ function gateserver.start(handler) nodelay = conf.nodelay skynet.error(string.format("Listen on %s:%d", address, port)) socket = socketdriver.listen(address, port) + listen_context.co = coroutine.running() + listen_context.fd = socket + skynet.wait(listen_context.co) + conf.address = listen_context.addr + conf.port = listen_context.port + listen_context = nil socketdriver.start(socket) if handler.open then return handler.open(source, conf) @@ -125,6 +133,19 @@ function gateserver.start(handler) end end + function MSG.init(id, addr, port) + if listen_context then + local co = listen_context.co + if co then + assert(id == listen_context.fd) + listen_context.addr = addr + listen_context.port = port + skynet.wakeup(co) + listen_context.co = nil + end + end + end + skynet.register_protocol { name = "socket", id = skynet.PTYPE_SOCKET, -- PTYPE_SOCKET = 6 diff --git a/service/gate.lua b/service/gate.lua index d78cdafa..1fe35afe 100644 --- a/service/gate.lua +++ b/service/gate.lua @@ -13,6 +13,7 @@ local handler = {} function handler.open(source, conf) watchdog = conf.watchdog or source + return conf.address, conf.port end function handler.message(fd, msg, sz)