From ba22224a4ae2a474f7a158171b231057329f480a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=91=E9=A3=8E?= Date: Mon, 29 Jul 2013 21:05:03 +0800 Subject: [PATCH] add QUERY command to query local service address --- lualib/skynet.lua | 4 ++++ lualib/socket.lua | 9 +++++---- skynet-src/skynet_server.c | 9 +++++++++ 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 5593c7f8..62368228 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -158,6 +158,10 @@ function skynet.self() return self_handle end +function skynet.localname(name) + return string_to_handle(c.command("QUERY", name)) +end + function skynet.launch(...) local addr = c.command("LAUNCH", table.concat({...}," ")) if addr then diff --git a/lualib/socket.lua b/lualib/socket.lua index 8f415807..7965ac2b 100644 --- a/lualib/socket.lua +++ b/lualib/socket.lua @@ -14,6 +14,7 @@ local READTHREAD= {} -- fd:thread local CLOSED = {} -- fd:true local selfaddr = skynet.self() +local sockets = assert(skynet.localname ".socket") local function response(session) skynet.redirect(selfaddr , 0, "response", session, "") @@ -94,7 +95,7 @@ local socket = {} function socket.open(addr, port) local cmd = "open" .. " " .. (port and (addr..":"..port) or addr) - local r = skynet.call(".socket", "text", cmd) + local r = skynet.call(sockets, "text", cmd) if r == "" then return nil, cmd .. " failed" end @@ -105,7 +106,7 @@ function socket.open(addr, port) end function socket.stdin() - local r = skynet.call(".socket", "text", "bind 1") + local r = skynet.call(sockets, "text", "bind 1") if r == "" then error("stdin bind failed") end @@ -117,7 +118,7 @@ end function socket.close(fd) socket.lock(fd) - skynet.call(".socket", "text", "close", fd) + skynet.call(sockets, "text", "close", fd) READBUF[fd] = nil READLOCK[fd] = nil CLOSED[fd] = nil @@ -221,7 +222,7 @@ function socket.write(fd, msg, sz) if CLOSED[fd] or not READBUF[fd] then return end - skynet.send(".socket", "client", fd, msg, sz) + skynet.send(sockets, "client", fd, msg, sz) return true end diff --git a/skynet-src/skynet_server.c b/skynet-src/skynet_server.c index 3a725bec..10e9c945 100644 --- a/skynet-src/skynet_server.c +++ b/skynet-src/skynet_server.c @@ -384,6 +384,15 @@ skynet_command(struct skynet_context * context, const char * cmd , const char * } } + if (strcmp(cmd,"QUERY") == 0) { + if (param[0] == '.') { + uint32_t handle = skynet_handle_findname(param+1); + sprintf(context->result, ":%x", handle); + return context->result; + } + return NULL; + } + if (strcmp(cmd,"NAME") == 0) { int size = strlen(param); char name[size+1];