From c26c2765851543a8b23a148868610747d4a84342 Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 25 Jun 2025 23:17:43 +0800 Subject: improve ping #6579 --- xmake/modules/net/ping.lua | 47 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/xmake/modules/net/ping.lua b/xmake/modules/net/ping.lua index 689bf4d94..db66a42b0 100644 --- a/xmake/modules/net/ping.lua +++ b/xmake/modules/net/ping.lua @@ -23,8 +23,8 @@ import("core.cache.detectcache") import("lib.detect.find_tool") import("async.runjobs") --- ping host -function _ping(ping, host) +-- using ping to ping host +function _ping_via_ping(ping, host) local data = nil if is_host("windows") then data = try { function () return os.iorun("%s -n 1 -w 1000 %s", ping.program, host) end } @@ -47,6 +47,45 @@ function _ping(ping, host) return timeval end +-- using curl to ping host +function _ping_via_curl(curl, host) + local data = try { function () return os.iorunv(curl.program, {"-o", os.nuldev(), "-s", "-w", "%{time_total}", "--max-time", "1", host}) end } + local timeval = 65535 + if data then + timeval = tonumber(data:trim()) * 1000 + end + return timeval +end + +-- using wget to ping host +function _ping_via_wget(wget, host) + local data = try { function () + local t = os.mclock() + os.runv(wget.program, {"-O", os.nuldev(), "--timeout=1", host}) + t = os.mclock() - t + return t + end } + local timeval = 65535 + if data then + timeval = data + end + return timeval +end + +-- ping host +-- @see https://github.com/xmake-io/xmake/issues/6579 +function _ping(ping, host) + local routers = { + ping = _ping_via_ping, + curl = _ping_via_curl, + wget = _ping_via_wget + } + local router = routers[ping.name] or _ping_via_ping + if router then + return router(ping, host) + end +end + -- send ping to hosts -- -- @param hosts the hosts @@ -55,9 +94,9 @@ end -- @return the time or -1 -- function main(hosts, opt) - opt = opt or {} - local ping = find_tool("ping", opt) + + local ping = find_tool("curl", opt) or find_tool("wget") or find_tool("ping", opt) if not ping then return {} end -- cgit v1.3.1 From cbbe73cf003569ffef57578e2cdfc2edb99b0ab1 Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 25 Jun 2025 23:22:25 +0800 Subject: add logs --- tests/actions/install/test.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/actions/install/test.lua b/tests/actions/install/test.lua index 4edc90275..1c84b9f4c 100644 --- a/tests/actions/install/test.lua +++ b/tests/actions/install/test.lua @@ -1,6 +1,6 @@ function main(t) if is_host("windows", "linux", "macosx") and os.arch():startswith("x") then - os.vrun("xmake -y") + os.vrun("xmake -y -vD") os.vrun("xmake run app") os.vrun("xmake install -o build/usr") if not is_host("linux") then -- TODO, change rpath has been not supported yet on linux. -- cgit v1.3.1 From 5120c8184c55cf80d6a90cbad51f59b30de34969 Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 25 Jun 2025 23:32:26 +0800 Subject: fix ping --- tests/actions/install/test.lua | 2 +- xmake/modules/net/ping.lua | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/tests/actions/install/test.lua b/tests/actions/install/test.lua index 1c84b9f4c..4edc90275 100644 --- a/tests/actions/install/test.lua +++ b/tests/actions/install/test.lua @@ -1,6 +1,6 @@ function main(t) if is_host("windows", "linux", "macosx") and os.arch():startswith("x") then - os.vrun("xmake -y -vD") + os.vrun("xmake -y") os.vrun("xmake run app") os.vrun("xmake install -o build/usr") if not is_host("linux") then -- TODO, change rpath has been not supported yet on linux. diff --git a/xmake/modules/net/ping.lua b/xmake/modules/net/ping.lua index db66a42b0..2f019cb66 100644 --- a/xmake/modules/net/ping.lua +++ b/xmake/modules/net/ping.lua @@ -49,10 +49,20 @@ end -- using curl to ping host function _ping_via_curl(curl, host) - local data = try { function () return os.iorunv(curl.program, {"-o", os.nuldev(), "-s", "-w", "%{time_total}", "--max-time", "1", host}) end } + local data, dt = try { function () + local t = os.mclock() + local outdata = os.iorunv(curl.program, {"-o", os.nuldev(), "-s", "-w", "%{time_total}", "--max-time", "1", host}) + t = os.mclock() - t + return outdata, t + end } local timeval = 65535 if data then - timeval = tonumber(data:trim()) * 1000 + local t = tonumber(data:trim()) + if t then + timeval = t * 1000 + else + timeval = dt + end end return timeval end -- cgit v1.3.1 From d3ae88f3054df26b2bc5cc7f308d59923405aa90 Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 25 Jun 2025 23:34:15 +0800 Subject: improve ping --- xmake/modules/net/ping.lua | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/xmake/modules/net/ping.lua b/xmake/modules/net/ping.lua index 2f019cb66..652e63782 100644 --- a/xmake/modules/net/ping.lua +++ b/xmake/modules/net/ping.lua @@ -51,8 +51,10 @@ end function _ping_via_curl(curl, host) local data, dt = try { function () local t = os.mclock() - local outdata = os.iorunv(curl.program, {"-o", os.nuldev(), "-s", "-w", "%{time_total}", "--max-time", "1", host}) + local tmpfile = os.tmpfile() + local outdata = os.iorunv(curl.program, {"-o", tmpfile, "-s", "-w", "%{time_total}", "--max-time", "1", host}) t = os.mclock() - t + os.tryrm(tmpfile) return outdata, t end } local timeval = 65535 -- cgit v1.3.1