watchdog returns addr,port

This commit is contained in:
Cloud Wu
2022-09-04 14:22:13 +08:00
parent 01314de8e1
commit b61daaccf1
5 changed files with 33 additions and 6 deletions

View File

@@ -12,11 +12,11 @@ skynet.start(function()
skynet.newservice("debug_console",8000) skynet.newservice("debug_console",8000)
skynet.newservice("simpledb") skynet.newservice("simpledb")
local watchdog = skynet.newservice("watchdog") local watchdog = skynet.newservice("watchdog")
skynet.call(watchdog, "lua", "start", { local addr,port = skynet.call(watchdog, "lua", "start", {
port = 8888, port = 8888,
maxclient = max_client, maxclient = max_client,
nodelay = true, nodelay = true,
}) })
skynet.error("Watchdog listen on", 8888) skynet.error("Watchdog listen on " .. addr .. ":" .. port)
skynet.exit() skynet.exit()
end) end)

View File

@@ -40,7 +40,7 @@ function SOCKET.data(fd, msg)
end end
function CMD.start(conf) function CMD.start(conf)
skynet.call(gate, "lua", "open" , conf) return skynet.call(gate, "lua", "open" , conf)
end end
function CMD.close(fd) function CMD.close(fd)

View File

@@ -22,6 +22,7 @@
#define TYPE_OPEN 4 #define TYPE_OPEN 4
#define TYPE_CLOSE 5 #define TYPE_CLOSE 5
#define TYPE_WARNING 6 #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 . 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 assert(size == -1); // never padding string
return filter_data(L, message->id, (uint8_t *)buffer, message->ud); return filter_data(L, message->id, (uint8_t *)buffer, message->ud);
case SKYNET_SOCKET_TYPE_CONNECT: case SKYNET_SOCKET_TYPE_CONNECT:
// ignore listen fd connect lua_pushvalue(L, lua_upvalueindex(TYPE_INIT));
return 1; lua_pushinteger(L, message->id);
lua_pushlstring(L, buffer, size);
lua_pushinteger(L, message->ud);
return 5;
case SKYNET_SOCKET_TYPE_CLOSE: case SKYNET_SOCKET_TYPE_CLOSE:
// no more data in fd (message->id) // no more data in fd (message->id)
close_uncomplete(L, message->id); close_uncomplete(L, message->id);
@@ -483,8 +487,9 @@ luaopen_skynet_netpack(lua_State *L) {
lua_pushliteral(L, "open"); lua_pushliteral(L, "open");
lua_pushliteral(L, "close"); lua_pushliteral(L, "close");
lua_pushliteral(L, "warning"); lua_pushliteral(L, "warning");
lua_pushliteral(L, "init");
lua_pushcclosure(L, lfilter, 6); lua_pushcclosure(L, lfilter, 7);
lua_setfield(L, -2, "filter"); lua_setfield(L, -2, "filter");
return 1; return 1;

View File

@@ -34,6 +34,8 @@ function gateserver.start(handler)
assert(handler.message) assert(handler.message)
assert(handler.connect) assert(handler.connect)
local listen_context = {}
function CMD.open( source, conf ) function CMD.open( source, conf )
assert(not socket) assert(not socket)
local address = conf.address or "0.0.0.0" local address = conf.address or "0.0.0.0"
@@ -42,6 +44,12 @@ function gateserver.start(handler)
nodelay = conf.nodelay nodelay = conf.nodelay
skynet.error(string.format("Listen on %s:%d", address, port)) skynet.error(string.format("Listen on %s:%d", address, port))
socket = socketdriver.listen(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) socketdriver.start(socket)
if handler.open then if handler.open then
return handler.open(source, conf) return handler.open(source, conf)
@@ -125,6 +133,19 @@ function gateserver.start(handler)
end end
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 { skynet.register_protocol {
name = "socket", name = "socket",
id = skynet.PTYPE_SOCKET, -- PTYPE_SOCKET = 6 id = skynet.PTYPE_SOCKET, -- PTYPE_SOCKET = 6

View File

@@ -13,6 +13,7 @@ local handler = {}
function handler.open(source, conf) function handler.open(source, conf)
watchdog = conf.watchdog or source watchdog = conf.watchdog or source
return conf.address, conf.port
end end
function handler.message(fd, msg, sz) function handler.message(fd, msg, sz)